blob: c6c0d0f9a7c7dc41fe347f8ad0c714105f72767f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Gala4ed65522008-02-27 21:51:47 -06002/*
3 * Procedures for maintaining information about logical memory blocks.
4 *
5 * Peter Bergner, IBM Corp. June 2001.
6 * Copyright (C) 2001 Peter Bergner.
Kumar Gala4ed65522008-02-27 21:51:47 -06007 */
8
Sughosh Ganued17a332024-08-26 17:29:18 +05309#include <alist.h>
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +010010#include <efi_loader.h>
Sughosh Ganu2f619152024-10-15 21:07:07 +053011#include <event.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060012#include <image.h>
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +010013#include <mapmem.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060014#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <malloc.h>
Sughosh Ganuf4fb1542024-08-26 17:29:24 +053017#include <spl.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060018
Marek Vasut12746982021-09-10 22:47:09 +020019#include <asm/global_data.h>
Marek Vasutbd994c02021-11-13 18:34:37 +010020#include <asm/sections.h>
Sughosh Ganued17a332024-08-26 17:29:18 +053021#include <linux/kernel.h>
Sughosh Ganu6534d262024-08-26 17:29:30 +053022#include <linux/sizes.h>
Marek Vasut12746982021-09-10 22:47:09 +020023
24DECLARE_GLOBAL_DATA_PTR;
25
Sughosh Ganu2f619152024-10-15 21:07:07 +053026#define MAP_OP_RESERVE (u8)0x1
27#define MAP_OP_FREE (u8)0x2
28#define MAP_OP_ADD (u8)0x3
29
Kumar Gala4ed65522008-02-27 21:51:47 -060030#define LMB_ALLOC_ANYWHERE 0
Sughosh Ganued17a332024-08-26 17:29:18 +053031#define LMB_ALIST_INITIAL_SIZE 4
Kumar Gala4ed65522008-02-27 21:51:47 -060032
Sughosh Ganued17a332024-08-26 17:29:18 +053033static struct lmb lmb;
34
Sughosh Ganu2f619152024-10-15 21:07:07 +053035static bool lmb_should_notify(enum lmb_flags flags)
36{
37 return !lmb.test && !(flags & LMB_NONOTIFY) &&
38 CONFIG_IS_ENABLED(EFI_LOADER);
39}
40
41static int __maybe_unused lmb_map_update_notify(phys_addr_t addr,
42 phys_size_t size,
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +030043 u8 op, enum lmb_flags flags)
Sughosh Ganu2f619152024-10-15 21:07:07 +053044{
45 u64 efi_addr;
46 u64 pages;
47 efi_status_t status;
48
49 if (op != MAP_OP_RESERVE && op != MAP_OP_FREE && op != MAP_OP_ADD) {
50 log_err("Invalid map update op received (%d)\n", op);
51 return -1;
52 }
53
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +030054 if (!lmb_should_notify(flags))
55 return 0;
56
Sughosh Ganu2f619152024-10-15 21:07:07 +053057 efi_addr = (uintptr_t)map_sysmem(addr, 0);
58 pages = efi_size_in_pages(size + (efi_addr & EFI_PAGE_MASK));
59 efi_addr &= ~EFI_PAGE_MASK;
60
61 status = efi_add_memory_map_pg(efi_addr, pages,
62 op == MAP_OP_RESERVE ?
63 EFI_BOOT_SERVICES_DATA :
64 EFI_CONVENTIONAL_MEMORY,
65 false);
66 if (status != EFI_SUCCESS) {
67 log_err("%s: LMB Map notify failure %lu\n", __func__,
68 status & ~EFI_ERROR_MASK);
69 return -1;
Sughosh Ganu2f619152024-10-15 21:07:07 +053070 }
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +030071
72 return 0;
Sughosh Ganu2f619152024-10-15 21:07:07 +053073}
74
Sughosh Ganuf8ffc6f2024-08-26 17:29:40 +053075static void lmb_print_region_flags(enum lmb_flags flags)
76{
77 u64 bitpos;
Sughosh Ganu3c6896a2024-10-15 21:07:04 +053078 const char *flag_str[] = { "none", "no-map", "no-overwrite", "no-notify" };
Sughosh Ganuf8ffc6f2024-08-26 17:29:40 +053079
80 do {
81 bitpos = flags ? fls(flags) - 1 : 0;
Sughosh Ganuc3cf0dc2024-10-21 22:48:20 +053082 assert_noisy(bitpos < ARRAY_SIZE(flag_str));
Sughosh Ganuf8ffc6f2024-08-26 17:29:40 +053083 printf("%s", flag_str[bitpos]);
84 flags &= ~(1ull << bitpos);
85 puts(flags ? ", " : "\n");
86 } while (flags);
87}
88
Sughosh Ganued17a332024-08-26 17:29:18 +053089static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
Patrick Delaunay358c7782021-05-07 14:50:31 +020090{
Sughosh Ganued17a332024-08-26 17:29:18 +053091 struct lmb_region *rgn = lmb_rgn_lst->data;
Patrick Delaunay358c7782021-05-07 14:50:31 +020092 unsigned long long base, size, end;
93 enum lmb_flags flags;
94 int i;
95
Sughosh Ganued17a332024-08-26 17:29:18 +053096 printf(" %s.count = 0x%x\n", name, lmb_rgn_lst->count);
Patrick Delaunay358c7782021-05-07 14:50:31 +020097
Sughosh Ganued17a332024-08-26 17:29:18 +053098 for (i = 0; i < lmb_rgn_lst->count; i++) {
99 base = rgn[i].base;
100 size = rgn[i].size;
Patrick Delaunay358c7782021-05-07 14:50:31 +0200101 end = base + size - 1;
Sughosh Ganued17a332024-08-26 17:29:18 +0530102 flags = rgn[i].flags;
Patrick Delaunay358c7782021-05-07 14:50:31 +0200103
Sughosh Ganuf8ffc6f2024-08-26 17:29:40 +0530104 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: ",
105 name, i, base, end, size);
106 lmb_print_region_flags(flags);
Patrick Delaunay358c7782021-05-07 14:50:31 +0200107 }
108}
109
Sughosh Ganued17a332024-08-26 17:29:18 +0530110void lmb_dump_all_force(void)
Tero Kristo9996cea2020-07-20 11:10:45 +0300111{
Tero Kristo9996cea2020-07-20 11:10:45 +0300112 printf("lmb_dump_all:\n");
Sughosh Ganued17a332024-08-26 17:29:18 +0530113 lmb_dump_region(&lmb.free_mem, "memory");
114 lmb_dump_region(&lmb.used_mem, "reserved");
Tero Kristo9996cea2020-07-20 11:10:45 +0300115}
116
Sughosh Ganued17a332024-08-26 17:29:18 +0530117void lmb_dump_all(void)
Kumar Gala4ed65522008-02-27 21:51:47 -0600118{
119#ifdef DEBUG
Sughosh Ganued17a332024-08-26 17:29:18 +0530120 lmb_dump_all_force();
Tero Kristo9996cea2020-07-20 11:10:45 +0300121#endif
Kumar Gala4ed65522008-02-27 21:51:47 -0600122}
123
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100124static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
125 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600126{
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100127 const phys_addr_t base1_end = base1 + size1 - 1;
128 const phys_addr_t base2_end = base2 + size2 - 1;
129
130 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala4ed65522008-02-27 21:51:47 -0600131}
132
Becky Bruce391fd932008-06-09 20:37:18 -0500133static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100134 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600135{
136 if (base2 == base1 + size1)
137 return 1;
138 else if (base1 == base2 + size2)
139 return -1;
140
141 return 0;
142}
143
Sughosh Ganued17a332024-08-26 17:29:18 +0530144static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1,
Udit Kumaredb58242023-09-26 16:54:42 +0530145 unsigned long r2)
146{
Sughosh Ganued17a332024-08-26 17:29:18 +0530147 struct lmb_region *rgn = lmb_rgn_lst->data;
148
149 phys_addr_t base1 = rgn[r1].base;
150 phys_size_t size1 = rgn[r1].size;
151 phys_addr_t base2 = rgn[r2].base;
152 phys_size_t size2 = rgn[r2].size;
Udit Kumaredb58242023-09-26 16:54:42 +0530153
154 return lmb_addrs_overlap(base1, size1, base2, size2);
155}
Sughosh Ganued17a332024-08-26 17:29:18 +0530156
157static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100158 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600159{
Sughosh Ganued17a332024-08-26 17:29:18 +0530160 struct lmb_region *rgn = lmb_rgn_lst->data;
161
162 phys_addr_t base1 = rgn[r1].base;
163 phys_size_t size1 = rgn[r1].size;
164 phys_addr_t base2 = rgn[r2].base;
165 phys_size_t size2 = rgn[r2].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600166 return lmb_addrs_adjacent(base1, size1, base2, size2);
167}
168
Sughosh Ganued17a332024-08-26 17:29:18 +0530169static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
Kumar Gala4ed65522008-02-27 21:51:47 -0600170{
171 unsigned long i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530172 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600173
Sughosh Ganued17a332024-08-26 17:29:18 +0530174 for (i = r; i < lmb_rgn_lst->count - 1; i++) {
175 rgn[i].base = rgn[i + 1].base;
176 rgn[i].size = rgn[i + 1].size;
177 rgn[i].flags = rgn[i + 1].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600178 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530179 lmb_rgn_lst->count--;
Kumar Gala4ed65522008-02-27 21:51:47 -0600180}
181
182/* Assumption: base addr of region 1 < base addr of region 2 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530183static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100184 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600185{
Sughosh Ganued17a332024-08-26 17:29:18 +0530186 struct lmb_region *rgn = lmb_rgn_lst->data;
187
188 rgn[r1].size += rgn[r2].size;
189 lmb_remove_region(lmb_rgn_lst, r2);
Kumar Gala4ed65522008-02-27 21:51:47 -0600190}
191
Udit Kumaredb58242023-09-26 16:54:42 +0530192/*Assumption : base addr of region 1 < base addr of region 2*/
Sughosh Ganued17a332024-08-26 17:29:18 +0530193static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst,
194 unsigned long r1, unsigned long r2)
Udit Kumaredb58242023-09-26 16:54:42 +0530195{
Sughosh Ganued17a332024-08-26 17:29:18 +0530196 struct lmb_region *rgn = lmb_rgn_lst->data;
197
198 phys_addr_t base1 = rgn[r1].base;
199 phys_size_t size1 = rgn[r1].size;
200 phys_addr_t base2 = rgn[r2].base;
201 phys_size_t size2 = rgn[r2].size;
Udit Kumaredb58242023-09-26 16:54:42 +0530202
203 if (base1 + size1 > base2 + size2) {
204 printf("This will not be a case any time\n");
205 return;
206 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530207 rgn[r1].size = base2 + size2 - base1;
208 lmb_remove_region(lmb_rgn_lst, r2);
Udit Kumaredb58242023-09-26 16:54:42 +0530209}
210
Sughosh Ganu6534d262024-08-26 17:29:30 +0530211static void lmb_reserve_uboot_region(void)
212{
213 int bank;
214 ulong end, bank_end;
215 phys_addr_t rsv_start;
216
217 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
218 end = gd->ram_top;
219
220 /*
221 * Reserve memory from aligned address below the bottom of U-Boot stack
222 * until end of RAM area to prevent LMB from overwriting that memory.
223 */
224 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
225
226 /* adjust sp by 16K to be safe */
227 rsv_start -= SZ_16K;
228 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
229 if (!gd->bd->bi_dram[bank].size ||
230 rsv_start < gd->bd->bi_dram[bank].start)
231 continue;
232 /* Watch out for RAM at end of address space! */
233 bank_end = gd->bd->bi_dram[bank].start +
234 gd->bd->bi_dram[bank].size - 1;
235 if (rsv_start > bank_end)
236 continue;
237 if (bank_end > end)
238 bank_end = end - 1;
239
240 lmb_reserve_flags(rsv_start, bank_end - rsv_start + 1,
241 LMB_NOOVERWRITE);
242
243 if (gd->flags & GD_FLG_SKIP_RELOC)
244 lmb_reserve_flags((phys_addr_t)(uintptr_t)_start,
245 gd->mon_len, LMB_NOOVERWRITE);
246
247 break;
248 }
249}
250
Sughosh Ganued17a332024-08-26 17:29:18 +0530251static void lmb_reserve_common(void *fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100252{
Sughosh Ganu6534d262024-08-26 17:29:30 +0530253 lmb_reserve_uboot_region();
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100254
Simon Glass0c303f92021-09-25 19:43:21 -0600255 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Sughosh Ganued17a332024-08-26 17:29:18 +0530256 boot_fdt_add_mem_rsv_regions(fdt_blob);
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100257}
258
Sughosh Ganuf4fb1542024-08-26 17:29:24 +0530259static __maybe_unused void lmb_reserve_common_spl(void)
260{
261 phys_addr_t rsv_start;
262 phys_size_t rsv_size;
263
264 /*
265 * Assume a SPL stack of 16KB. This must be
266 * more than enough for the SPL stage.
267 */
268 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
269 rsv_start = gd->start_addr_sp - 16384;
270 rsv_size = 16384;
271 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
272 }
273
274 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
275 /* Reserve the bss region */
276 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
277 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
278 (phys_addr_t)(uintptr_t)__bss_start;
279 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
280 }
281}
282
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530283/**
284 * lmb_add_memory() - Add memory range for LMB allocations
285 *
286 * Add the entire available memory range to the pool of memory that
287 * can be used by the LMB module for allocations.
288 *
289 * Return: None
290 */
291void lmb_add_memory(void)
292{
293 int i;
294 phys_size_t size;
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530295 u64 ram_top = gd->ram_top;
296 struct bd_info *bd = gd->bd;
297
Sughosh Ganu497da0c2024-10-15 21:07:11 +0530298 if (CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP))
299 return lmb_arch_add_memory();
300
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530301 /* Assume a 4GB ram_top if not defined */
302 if (!ram_top)
303 ram_top = 0x100000000ULL;
304
305 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
306 size = bd->bi_dram[i].size;
307 if (size) {
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530308 lmb_add(bd->bi_dram[i].start, size);
Sughosh Ganueb052cb2024-10-15 21:07:05 +0530309
310 /*
311 * Reserve memory above ram_top as
312 * no-overwrite so that it cannot be
313 * allocated
314 */
315 if (bd->bi_dram[i].start >= ram_top)
316 lmb_reserve_flags(bd->bi_dram[i].start, size,
317 LMB_NOOVERWRITE);
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530318 }
319 }
320}
321
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530322static long lmb_resize_regions(struct alist *lmb_rgn_lst,
323 unsigned long idx_start,
324 phys_addr_t base, phys_size_t size)
325{
326 phys_size_t rgnsize;
327 unsigned long rgn_cnt, idx, idx_end;
328 phys_addr_t rgnbase, rgnend;
329 phys_addr_t mergebase, mergeend;
330 struct lmb_region *rgn = lmb_rgn_lst->data;
331
332 rgn_cnt = 0;
333 idx = idx_start;
334 idx_end = idx_start;
335
336 /*
337 * First thing to do is to identify how many regions
338 * the requested region overlaps.
339 * If the flags match, combine all these overlapping
340 * regions into a single region, and remove the merged
341 * regions.
342 */
343 while (idx <= lmb_rgn_lst->count - 1) {
344 rgnbase = rgn[idx].base;
345 rgnsize = rgn[idx].size;
346
347 if (lmb_addrs_overlap(base, size, rgnbase,
348 rgnsize)) {
349 if (rgn[idx].flags != LMB_NONE)
350 return -1;
351 rgn_cnt++;
352 idx_end = idx;
353 }
354 idx++;
355 }
356
357 /* The merged region's base and size */
358 rgnbase = rgn[idx_start].base;
359 mergebase = min(base, rgnbase);
360 rgnend = rgn[idx_end].base + rgn[idx_end].size;
361 mergeend = max(rgnend, (base + size));
362
363 rgn[idx_start].base = mergebase;
364 rgn[idx_start].size = mergeend - mergebase;
365
366 /* Now remove the merged regions */
367 while (--rgn_cnt)
368 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
369
370 return 0;
371}
372
Sughosh Ganued17a332024-08-26 17:29:18 +0530373/**
374 * lmb_add_region_flags() - Add an lmb region to the given list
375 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
376 * @base: Start address of the region
377 * @size: Size of the region to be added
378 * @flags: Attributes of the LMB region
379 *
380 * Add a region of memory to the list. If the region does not exist, add
381 * it to the list. Depending on the attributes of the region to be added,
382 * the function might resize an already existing region or coalesce two
383 * adjacent regions.
384 *
385 *
386 * Returns: 0 if the region addition successful, -1 on failure
387 */
388static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200389 phys_size_t size, enum lmb_flags flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600390{
391 unsigned long coalesced = 0;
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530392 long ret, i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530393 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600394
Sughosh Ganued17a332024-08-26 17:29:18 +0530395 if (alist_err(lmb_rgn_lst))
396 return -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600397
398 /* First try and coalesce this LMB with another. */
Sughosh Ganued17a332024-08-26 17:29:18 +0530399 for (i = 0; i < lmb_rgn_lst->count; i++) {
400 phys_addr_t rgnbase = rgn[i].base;
401 phys_size_t rgnsize = rgn[i].size;
402 phys_size_t rgnflags = rgn[i].flags;
Sjoerd Simons0d91c882023-02-12 16:07:05 +0100403 phys_addr_t end = base + size - 1;
404 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Sjoerd Simons0d91c882023-02-12 16:07:05 +0100405 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200406 if (flags == rgnflags)
407 /* Already have this region, so we're done */
408 return 0;
409 else
410 return -1; /* regions with new flags */
411 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600412
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530413 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
414 if (ret > 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200415 if (flags != rgnflags)
416 break;
Sughosh Ganued17a332024-08-26 17:29:18 +0530417 rgn[i].base -= size;
418 rgn[i].size += size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600419 coalesced++;
420 break;
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530421 } else if (ret < 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200422 if (flags != rgnflags)
423 break;
Sughosh Ganued17a332024-08-26 17:29:18 +0530424 rgn[i].size += size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600425 coalesced++;
426 break;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100427 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530428 if (flags == LMB_NONE) {
429 ret = lmb_resize_regions(lmb_rgn_lst, i, base,
430 size);
431 if (ret < 0)
432 return -1;
433
434 coalesced++;
435 break;
436 } else {
437 return -1;
438 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600439 }
440 }
441
Sughosh Ganued17a332024-08-26 17:29:18 +0530442 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
443 rgn = lmb_rgn_lst->data;
444 if (rgn[i].flags == rgn[i + 1].flags) {
445 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
446 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
447 coalesced++;
448 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
449 /* fix overlapping area */
450 lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
451 coalesced++;
452 }
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200453 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600454 }
455
456 if (coalesced)
Ilias Apalodimas0f57b002024-10-23 18:22:00 +0300457 return 0;
Sughosh Ganued17a332024-08-26 17:29:18 +0530458
459 if (alist_full(lmb_rgn_lst) &&
460 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala4ed65522008-02-27 21:51:47 -0600461 return -1;
Sughosh Ganued17a332024-08-26 17:29:18 +0530462 rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600463
464 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganued17a332024-08-26 17:29:18 +0530465 for (i = lmb_rgn_lst->count; i >= 0; i--) {
466 if (i && base < rgn[i - 1].base) {
467 rgn[i] = rgn[i - 1];
Kumar Gala4ed65522008-02-27 21:51:47 -0600468 } else {
Sughosh Ganued17a332024-08-26 17:29:18 +0530469 rgn[i].base = base;
470 rgn[i].size = size;
471 rgn[i].flags = flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600472 break;
473 }
474 }
475
Sughosh Ganued17a332024-08-26 17:29:18 +0530476 lmb_rgn_lst->count++;
Kumar Gala4ed65522008-02-27 21:51:47 -0600477
478 return 0;
479}
480
Sughosh Ganued17a332024-08-26 17:29:18 +0530481static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200482 phys_size_t size)
483{
Sughosh Ganued17a332024-08-26 17:29:18 +0530484 return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200485}
486
Kumar Gala4ed65522008-02-27 21:51:47 -0600487/* This routine may be called with relocation disabled. */
Sughosh Ganued17a332024-08-26 17:29:18 +0530488long lmb_add(phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600489{
Sughosh Ganu2f619152024-10-15 21:07:07 +0530490 long ret;
Sughosh Ganued17a332024-08-26 17:29:18 +0530491 struct alist *lmb_rgn_lst = &lmb.free_mem;
Kumar Gala4ed65522008-02-27 21:51:47 -0600492
Sughosh Ganu2f619152024-10-15 21:07:07 +0530493 ret = lmb_add_region(lmb_rgn_lst, base, size);
Ilias Apalodimas0f57b002024-10-23 18:22:00 +0300494 if (ret)
Sughosh Ganu2f619152024-10-15 21:07:07 +0530495 return ret;
496
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +0300497 return lmb_map_update_notify(base, size, MAP_OP_ADD, LMB_NONE);
Kumar Gala4ed65522008-02-27 21:51:47 -0600498}
499
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530500static long _lmb_free(phys_addr_t base, phys_size_t size)
Andy Fleming63796c42008-06-16 13:58:54 -0500501{
Sughosh Ganued17a332024-08-26 17:29:18 +0530502 struct lmb_region *rgn;
503 struct alist *lmb_rgn_lst = &lmb.used_mem;
Andy Fleming98874ff2008-07-07 14:24:39 -0500504 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100505 phys_addr_t end = base + size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500506 int i;
507
508 rgnbegin = rgnend = 0; /* supress gcc warnings */
Sughosh Ganued17a332024-08-26 17:29:18 +0530509 rgn = lmb_rgn_lst->data;
Andy Fleming63796c42008-06-16 13:58:54 -0500510 /* Find the region where (base, size) belongs to */
Sughosh Ganued17a332024-08-26 17:29:18 +0530511 for (i = 0; i < lmb_rgn_lst->count; i++) {
512 rgnbegin = rgn[i].base;
513 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500514
515 if ((rgnbegin <= base) && (end <= rgnend))
516 break;
517 }
518
519 /* Didn't find the region */
Sughosh Ganued17a332024-08-26 17:29:18 +0530520 if (i == lmb_rgn_lst->count)
Andy Fleming63796c42008-06-16 13:58:54 -0500521 return -1;
522
523 /* Check to see if we are removing entire region */
524 if ((rgnbegin == base) && (rgnend == end)) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530525 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming63796c42008-06-16 13:58:54 -0500526 return 0;
527 }
528
529 /* Check to see if region is matching at the front */
530 if (rgnbegin == base) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530531 rgn[i].base = end + 1;
532 rgn[i].size -= size;
Andy Fleming63796c42008-06-16 13:58:54 -0500533 return 0;
534 }
535
536 /* Check to see if the region is matching at the end */
537 if (rgnend == end) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530538 rgn[i].size -= size;
Andy Fleming63796c42008-06-16 13:58:54 -0500539 return 0;
540 }
541
542 /*
543 * We need to split the entry - adjust the current one to the
544 * beginging of the hole and add the region after hole.
545 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530546 rgn[i].size = base - rgn[i].base;
547 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
548 rgn[i].flags);
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200549}
550
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530551/**
552 * lmb_free_flags() - Free up a region of memory
553 * @base: Base Address of region to be freed
554 * @size: Size of the region to be freed
555 * @flags: Memory region attributes
556 *
557 * Free up a region of memory.
558 *
559 * Return: 0 if successful, -1 on failure
560 */
561long lmb_free_flags(phys_addr_t base, phys_size_t size,
Sughosh Ganu2f619152024-10-15 21:07:07 +0530562 uint flags)
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530563{
Sughosh Ganu2f619152024-10-15 21:07:07 +0530564 long ret;
565
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530566 ret = _lmb_free(base, size);
Sughosh Ganu2f619152024-10-15 21:07:07 +0530567 if (ret < 0)
568 return ret;
569
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +0300570 return lmb_map_update_notify(base, size, MAP_OP_FREE, flags);
Sughosh Ganu2f619152024-10-15 21:07:07 +0530571}
572
573long lmb_free(phys_addr_t base, phys_size_t size)
574{
575 return lmb_free_flags(base, size, LMB_NONE);
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530576}
577
Sughosh Ganued17a332024-08-26 17:29:18 +0530578long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags)
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200579{
Sughosh Ganu2f619152024-10-15 21:07:07 +0530580 long ret = 0;
Sughosh Ganued17a332024-08-26 17:29:18 +0530581 struct alist *lmb_rgn_lst = &lmb.used_mem;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200582
Sughosh Ganu2f619152024-10-15 21:07:07 +0530583 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Ilias Apalodimas0f57b002024-10-23 18:22:00 +0300584 if (ret)
585 return ret;
Sughosh Ganu2f619152024-10-15 21:07:07 +0530586
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +0300587 return lmb_map_update_notify(base, size, MAP_OP_RESERVE, flags);
Andy Fleming63796c42008-06-16 13:58:54 -0500588}
589
Sughosh Ganued17a332024-08-26 17:29:18 +0530590long lmb_reserve(phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600591{
Sughosh Ganued17a332024-08-26 17:29:18 +0530592 return lmb_reserve_flags(base, size, LMB_NONE);
Kumar Gala4ed65522008-02-27 21:51:47 -0600593}
594
Sughosh Ganued17a332024-08-26 17:29:18 +0530595static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Becky Bruce391fd932008-06-09 20:37:18 -0500596 phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600597{
598 unsigned long i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530599 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600600
Sughosh Ganued17a332024-08-26 17:29:18 +0530601 for (i = 0; i < lmb_rgn_lst->count; i++) {
602 phys_addr_t rgnbase = rgn[i].base;
603 phys_size_t rgnsize = rgn[i].size;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100604 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala4ed65522008-02-27 21:51:47 -0600605 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600606 }
607
Sughosh Ganued17a332024-08-26 17:29:18 +0530608 return (i < lmb_rgn_lst->count) ? i : -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600609}
610
Becky Bruce391fd932008-06-09 20:37:18 -0500611static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600612{
613 return addr & ~(size - 1);
614}
615
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530616static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align,
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530617 phys_addr_t max_addr, enum lmb_flags flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600618{
Sughosh Ganu2f619152024-10-15 21:07:07 +0530619 int ret;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100620 long i, rgn;
Becky Bruce391fd932008-06-09 20:37:18 -0500621 phys_addr_t base = 0;
Andy Fleming7570a992008-06-16 13:58:55 -0500622 phys_addr_t res_base;
Sughosh Ganued17a332024-08-26 17:29:18 +0530623 struct lmb_region *lmb_used = lmb.used_mem.data;
624 struct lmb_region *lmb_memory = lmb.free_mem.data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600625
Sughosh Ganued17a332024-08-26 17:29:18 +0530626 for (i = lmb.free_mem.count - 1; i >= 0; i--) {
627 phys_addr_t lmbbase = lmb_memory[i].base;
628 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600629
Andy Fleming7570a992008-06-16 13:58:55 -0500630 if (lmbsize < size)
631 continue;
Kumar Gala4ed65522008-02-27 21:51:47 -0600632 if (max_addr == LMB_ALLOC_ANYWHERE)
633 base = lmb_align_down(lmbbase + lmbsize - size, align);
634 else if (lmbbase < max_addr) {
Stephen Warrenad3fda52014-07-31 13:40:07 -0600635 base = lmbbase + lmbsize;
636 if (base < lmbbase)
637 base = -1;
638 base = min(base, max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600639 base = lmb_align_down(base - size, align);
640 } else
641 continue;
642
Andy Fleming7570a992008-06-16 13:58:55 -0500643 while (base && lmbbase <= base) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530644 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100645 if (rgn < 0) {
Andy Fleming7570a992008-06-16 13:58:55 -0500646 /* This area isn't reserved, take it */
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530647 if (lmb_add_region_flags(&lmb.used_mem, base,
Ilias Apalodimas0f57b002024-10-23 18:22:00 +0300648 size, flags))
Andy Fleming7570a992008-06-16 13:58:55 -0500649 return 0;
Sughosh Ganu2f619152024-10-15 21:07:07 +0530650
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +0300651 ret = lmb_map_update_notify(base, size,
652 MAP_OP_RESERVE,
653 flags);
654 if (ret)
655 return ret;
Sughosh Ganu2f619152024-10-15 21:07:07 +0530656
Andy Fleming7570a992008-06-16 13:58:55 -0500657 return base;
658 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530659
660 res_base = lmb_used[rgn].base;
Andy Fleming7570a992008-06-16 13:58:55 -0500661 if (res_base < size)
662 break;
663 base = lmb_align_down(res_base - size, align);
664 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600665 }
Andy Fleming7570a992008-06-16 13:58:55 -0500666 return 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600667}
668
Sughosh Ganued17a332024-08-26 17:29:18 +0530669phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530670{
Sughosh Ganued17a332024-08-26 17:29:18 +0530671 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530672}
673
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530674/**
675 * lmb_alloc_flags() - Allocate memory region with specified attributes
676 * @size: Size of the region requested
677 * @align: Alignment of the memory region requested
678 * @flags: Memory region attributes to be set
679 *
680 * Allocate a region of memory with the attributes specified through the
681 * parameter.
682 *
683 * Return: base address on success, 0 on error
684 */
685phys_addr_t lmb_alloc_flags(phys_size_t size, ulong align, uint flags)
686{
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530687 return _lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE,
688 flags);
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530689}
690
Sughosh Ganued17a332024-08-26 17:29:18 +0530691phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530692{
693 phys_addr_t alloc;
694
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530695 alloc = _lmb_alloc_base(size, align, max_addr, LMB_NONE);
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530696
697 if (alloc == 0)
698 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
699 (ulong)size, (ulong)max_addr);
700
701 return alloc;
702}
703
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530704/**
705 * lmb_alloc_base_flags() - Allocate specified memory region with specified attributes
706 * @size: Size of the region requested
707 * @align: Alignment of the memory region requested
708 * @max_addr: Maximum address of the requested region
709 * @flags: Memory region attributes to be set
710 *
711 * Allocate a region of memory with the attributes specified through the
712 * parameter. The max_addr parameter is used to specify the maximum address
713 * below which the requested region should be allocated.
714 *
715 * Return: base address on success, 0 on error
716 */
717phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
718 phys_addr_t max_addr, uint flags)
719{
720 phys_addr_t alloc;
721
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530722 alloc = _lmb_alloc_base(size, align, max_addr, flags);
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530723
724 if (alloc == 0)
725 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
726 (ulong)size, (ulong)max_addr);
727
728 return alloc;
729}
730
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530731static phys_addr_t _lmb_alloc_addr(phys_addr_t base, phys_size_t size,
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530732 enum lmb_flags flags)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100733{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100734 long rgn;
Sughosh Ganued17a332024-08-26 17:29:18 +0530735 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100736
737 /* Check if the requested address is in one of the memory regions */
Sughosh Ganued17a332024-08-26 17:29:18 +0530738 rgn = lmb_overlaps_region(&lmb.free_mem, base, size);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100739 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100740 /*
741 * Check if the requested end address is in the same memory
742 * region we found.
743 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530744 if (lmb_addrs_overlap(lmb_memory[rgn].base,
745 lmb_memory[rgn].size,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100746 base + size - 1, 1)) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100747 /* ok, reserve the memory */
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530748 if (lmb_reserve_flags(base, size, flags) >= 0)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100749 return base;
750 }
751 }
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530752
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100753 return 0;
754}
755
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530756/*
757 * Try to allocate a specific address range: must be in defined memory but not
758 * reserved
759 */
760phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
761{
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530762 return _lmb_alloc_addr(base, size, LMB_NONE);
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530763}
764
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530765/**
766 * lmb_alloc_addr_flags() - Allocate specified memory address with specified attributes
767 * @base: Base Address requested
768 * @size: Size of the region requested
769 * @flags: Memory region attributes to be set
770 *
771 * Allocate a region of memory with the attributes specified through the
772 * parameter. The base parameter is used to specify the base address
773 * of the requested region.
774 *
775 * Return: base address on success, 0 on error
776 */
777phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
778 uint flags)
779{
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530780 return _lmb_alloc_addr(base, size, flags);
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530781}
782
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100783/* Return number of bytes from a given address that are free */
Sughosh Ganued17a332024-08-26 17:29:18 +0530784phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100785{
786 int i;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100787 long rgn;
Sughosh Ganued17a332024-08-26 17:29:18 +0530788 struct lmb_region *lmb_used = lmb.used_mem.data;
789 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100790
791 /* check if the requested address is in the memory regions */
Sughosh Ganued17a332024-08-26 17:29:18 +0530792 rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100793 if (rgn >= 0) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530794 for (i = 0; i < lmb.used_mem.count; i++) {
795 if (addr < lmb_used[i].base) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100796 /* first reserved range > requested address */
Sughosh Ganued17a332024-08-26 17:29:18 +0530797 return lmb_used[i].base - addr;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100798 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530799 if (lmb_used[i].base +
800 lmb_used[i].size > addr) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100801 /* requested addr is in this reserved range */
802 return 0;
803 }
804 }
805 /* if we come here: no reserved ranges above requested addr */
Sughosh Ganued17a332024-08-26 17:29:18 +0530806 return lmb_memory[lmb.free_mem.count - 1].base +
807 lmb_memory[lmb.free_mem.count - 1].size - addr;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100808 }
809 return 0;
810}
811
Sughosh Ganued17a332024-08-26 17:29:18 +0530812int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600813{
814 int i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530815 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600816
Sughosh Ganued17a332024-08-26 17:29:18 +0530817 for (i = 0; i < lmb.used_mem.count; i++) {
818 phys_addr_t upper = lmb_used[i].base +
819 lmb_used[i].size - 1;
820 if (addr >= lmb_used[i].base && addr <= upper)
821 return (lmb_used[i].flags & flags) == flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600822 }
823 return 0;
824}
Mike Frysingera16028d2009-11-03 11:35:59 -0500825
Sughosh Ganu2f619152024-10-15 21:07:07 +0530826static int lmb_setup(bool test)
Sughosh Ganued17a332024-08-26 17:29:18 +0530827{
828 bool ret;
829
830 ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region),
831 (uint)LMB_ALIST_INITIAL_SIZE);
832 if (!ret) {
833 log_debug("Unable to initialise the list for LMB free memory\n");
834 return -ENOMEM;
835 }
836
837 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
838 (uint)LMB_ALIST_INITIAL_SIZE);
839 if (!ret) {
840 log_debug("Unable to initialise the list for LMB used memory\n");
841 return -ENOMEM;
842 }
843
Sughosh Ganu2f619152024-10-15 21:07:07 +0530844 lmb.test = test;
845
Sughosh Ganued17a332024-08-26 17:29:18 +0530846 return 0;
847}
848
849/**
850 * lmb_init() - Initialise the LMB module
851 *
852 * Initialise the LMB lists needed for keeping the memory map. There
853 * are two lists, in form of alloced list data structure. One for the
854 * available memory, and one for the used memory. Initialise the two
855 * lists as part of board init. Add memory to the available memory
856 * list and reserve common areas by adding them to the used memory
857 * list.
858 *
859 * Return: 0 on success, -ve on error
860 */
861int lmb_init(void)
862{
863 int ret;
864
Sughosh Ganu2f619152024-10-15 21:07:07 +0530865 ret = lmb_setup(false);
Sughosh Ganued17a332024-08-26 17:29:18 +0530866 if (ret) {
867 log_info("Unable to init LMB\n");
868 return ret;
869 }
870
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530871 lmb_add_memory();
872
Sughosh Ganuf4fb1542024-08-26 17:29:24 +0530873 /* Reserve the U-Boot image region once U-Boot has relocated */
Simon Glass456bdb72024-09-29 19:49:36 -0600874 if (xpl_phase() == PHASE_SPL)
Sughosh Ganuf4fb1542024-08-26 17:29:24 +0530875 lmb_reserve_common_spl();
Simon Glass456bdb72024-09-29 19:49:36 -0600876 else if (xpl_phase() == PHASE_BOARD_R)
Sughosh Ganuf4fb1542024-08-26 17:29:24 +0530877 lmb_reserve_common((void *)gd->fdt_blob);
878
Sughosh Ganued17a332024-08-26 17:29:18 +0530879 return 0;
880}
881
Sughosh Ganued17a332024-08-26 17:29:18 +0530882struct lmb *lmb_get(void)
883{
884 return &lmb;
885}
886
Simon Glass1c30f7a2024-10-21 10:19:31 +0200887#if CONFIG_IS_ENABLED(UNIT_TEST)
Sughosh Ganued17a332024-08-26 17:29:18 +0530888int lmb_push(struct lmb *store)
889{
890 int ret;
891
892 *store = lmb;
Sughosh Ganu2f619152024-10-15 21:07:07 +0530893 ret = lmb_setup(true);
Sughosh Ganued17a332024-08-26 17:29:18 +0530894 if (ret)
895 return ret;
896
897 return 0;
898}
899
900void lmb_pop(struct lmb *store)
901{
902 alist_uninit(&lmb.free_mem);
903 alist_uninit(&lmb.used_mem);
904 lmb = *store;
905}
906#endif /* UNIT_TEST */