blob: 96a055f951e657bf3acc05c9fa804698ce90c312 [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
Sughosh Ganued17a332024-08-26 17:29:18 +053030static struct lmb lmb;
31
Sughosh Ganu2f619152024-10-15 21:07:07 +053032static bool lmb_should_notify(enum lmb_flags flags)
33{
34 return !lmb.test && !(flags & LMB_NONOTIFY) &&
35 CONFIG_IS_ENABLED(EFI_LOADER);
36}
37
38static int __maybe_unused lmb_map_update_notify(phys_addr_t addr,
39 phys_size_t size,
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +030040 u8 op, enum lmb_flags flags)
Sughosh Ganu2f619152024-10-15 21:07:07 +053041{
42 u64 efi_addr;
43 u64 pages;
44 efi_status_t status;
45
46 if (op != MAP_OP_RESERVE && op != MAP_OP_FREE && op != MAP_OP_ADD) {
47 log_err("Invalid map update op received (%d)\n", op);
48 return -1;
49 }
50
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +030051 if (!lmb_should_notify(flags))
52 return 0;
53
Sughosh Ganu2f619152024-10-15 21:07:07 +053054 efi_addr = (uintptr_t)map_sysmem(addr, 0);
55 pages = efi_size_in_pages(size + (efi_addr & EFI_PAGE_MASK));
56 efi_addr &= ~EFI_PAGE_MASK;
57
58 status = efi_add_memory_map_pg(efi_addr, pages,
59 op == MAP_OP_RESERVE ?
60 EFI_BOOT_SERVICES_DATA :
61 EFI_CONVENTIONAL_MEMORY,
62 false);
63 if (status != EFI_SUCCESS) {
64 log_err("%s: LMB Map notify failure %lu\n", __func__,
65 status & ~EFI_ERROR_MASK);
66 return -1;
Sughosh Ganu2f619152024-10-15 21:07:07 +053067 }
Ilias Apalodimase26d2ca2024-10-24 13:46:25 +030068 unmap_sysmem((void *)(uintptr_t)efi_addr);
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +030069
70 return 0;
Sughosh Ganu2f619152024-10-15 21:07:07 +053071}
72
Sughosh Ganuf8ffc6f2024-08-26 17:29:40 +053073static void lmb_print_region_flags(enum lmb_flags flags)
74{
75 u64 bitpos;
Sughosh Ganu3c6896a2024-10-15 21:07:04 +053076 const char *flag_str[] = { "none", "no-map", "no-overwrite", "no-notify" };
Sughosh Ganuf8ffc6f2024-08-26 17:29:40 +053077
78 do {
79 bitpos = flags ? fls(flags) - 1 : 0;
Sughosh Ganuc3cf0dc2024-10-21 22:48:20 +053080 assert_noisy(bitpos < ARRAY_SIZE(flag_str));
Sughosh Ganuf8ffc6f2024-08-26 17:29:40 +053081 printf("%s", flag_str[bitpos]);
82 flags &= ~(1ull << bitpos);
83 puts(flags ? ", " : "\n");
84 } while (flags);
85}
86
Sughosh Ganued17a332024-08-26 17:29:18 +053087static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
Patrick Delaunay358c7782021-05-07 14:50:31 +020088{
Sughosh Ganued17a332024-08-26 17:29:18 +053089 struct lmb_region *rgn = lmb_rgn_lst->data;
Patrick Delaunay358c7782021-05-07 14:50:31 +020090 unsigned long long base, size, end;
91 enum lmb_flags flags;
92 int i;
93
Sughosh Ganued17a332024-08-26 17:29:18 +053094 printf(" %s.count = 0x%x\n", name, lmb_rgn_lst->count);
Patrick Delaunay358c7782021-05-07 14:50:31 +020095
Sughosh Ganued17a332024-08-26 17:29:18 +053096 for (i = 0; i < lmb_rgn_lst->count; i++) {
97 base = rgn[i].base;
98 size = rgn[i].size;
Patrick Delaunay358c7782021-05-07 14:50:31 +020099 end = base + size - 1;
Sughosh Ganued17a332024-08-26 17:29:18 +0530100 flags = rgn[i].flags;
Patrick Delaunay358c7782021-05-07 14:50:31 +0200101
Sughosh Ganuf8ffc6f2024-08-26 17:29:40 +0530102 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: ",
103 name, i, base, end, size);
104 lmb_print_region_flags(flags);
Patrick Delaunay358c7782021-05-07 14:50:31 +0200105 }
106}
107
Sughosh Ganued17a332024-08-26 17:29:18 +0530108void lmb_dump_all_force(void)
Tero Kristo9996cea2020-07-20 11:10:45 +0300109{
Tero Kristo9996cea2020-07-20 11:10:45 +0300110 printf("lmb_dump_all:\n");
Sughosh Ganued17a332024-08-26 17:29:18 +0530111 lmb_dump_region(&lmb.free_mem, "memory");
112 lmb_dump_region(&lmb.used_mem, "reserved");
Tero Kristo9996cea2020-07-20 11:10:45 +0300113}
114
Sughosh Ganued17a332024-08-26 17:29:18 +0530115void lmb_dump_all(void)
Kumar Gala4ed65522008-02-27 21:51:47 -0600116{
117#ifdef DEBUG
Sughosh Ganued17a332024-08-26 17:29:18 +0530118 lmb_dump_all_force();
Tero Kristo9996cea2020-07-20 11:10:45 +0300119#endif
Kumar Gala4ed65522008-02-27 21:51:47 -0600120}
121
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100122static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
123 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600124{
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100125 const phys_addr_t base1_end = base1 + size1 - 1;
126 const phys_addr_t base2_end = base2 + size2 - 1;
127
128 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala4ed65522008-02-27 21:51:47 -0600129}
130
Becky Bruce391fd932008-06-09 20:37:18 -0500131static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100132 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600133{
134 if (base2 == base1 + size1)
135 return 1;
136 else if (base1 == base2 + size2)
137 return -1;
138
139 return 0;
140}
141
Sughosh Ganued17a332024-08-26 17:29:18 +0530142static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1,
Udit Kumaredb58242023-09-26 16:54:42 +0530143 unsigned long r2)
144{
Sughosh Ganued17a332024-08-26 17:29:18 +0530145 struct lmb_region *rgn = lmb_rgn_lst->data;
146
147 phys_addr_t base1 = rgn[r1].base;
148 phys_size_t size1 = rgn[r1].size;
149 phys_addr_t base2 = rgn[r2].base;
150 phys_size_t size2 = rgn[r2].size;
Udit Kumaredb58242023-09-26 16:54:42 +0530151
152 return lmb_addrs_overlap(base1, size1, base2, size2);
153}
Sughosh Ganued17a332024-08-26 17:29:18 +0530154
155static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100156 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600157{
Sughosh Ganued17a332024-08-26 17:29:18 +0530158 struct lmb_region *rgn = lmb_rgn_lst->data;
159
160 phys_addr_t base1 = rgn[r1].base;
161 phys_size_t size1 = rgn[r1].size;
162 phys_addr_t base2 = rgn[r2].base;
163 phys_size_t size2 = rgn[r2].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600164 return lmb_addrs_adjacent(base1, size1, base2, size2);
165}
166
Sughosh Ganued17a332024-08-26 17:29:18 +0530167static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
Kumar Gala4ed65522008-02-27 21:51:47 -0600168{
169 unsigned long i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530170 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600171
Sughosh Ganued17a332024-08-26 17:29:18 +0530172 for (i = r; i < lmb_rgn_lst->count - 1; i++) {
173 rgn[i].base = rgn[i + 1].base;
174 rgn[i].size = rgn[i + 1].size;
175 rgn[i].flags = rgn[i + 1].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600176 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530177 lmb_rgn_lst->count--;
Kumar Gala4ed65522008-02-27 21:51:47 -0600178}
179
180/* Assumption: base addr of region 1 < base addr of region 2 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530181static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100182 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600183{
Sughosh Ganued17a332024-08-26 17:29:18 +0530184 struct lmb_region *rgn = lmb_rgn_lst->data;
185
186 rgn[r1].size += rgn[r2].size;
187 lmb_remove_region(lmb_rgn_lst, r2);
Kumar Gala4ed65522008-02-27 21:51:47 -0600188}
189
Udit Kumaredb58242023-09-26 16:54:42 +0530190/*Assumption : base addr of region 1 < base addr of region 2*/
Sughosh Ganued17a332024-08-26 17:29:18 +0530191static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst,
192 unsigned long r1, unsigned long r2)
Udit Kumaredb58242023-09-26 16:54:42 +0530193{
Sughosh Ganued17a332024-08-26 17:29:18 +0530194 struct lmb_region *rgn = lmb_rgn_lst->data;
195
196 phys_addr_t base1 = rgn[r1].base;
197 phys_size_t size1 = rgn[r1].size;
198 phys_addr_t base2 = rgn[r2].base;
199 phys_size_t size2 = rgn[r2].size;
Udit Kumaredb58242023-09-26 16:54:42 +0530200
201 if (base1 + size1 > base2 + size2) {
202 printf("This will not be a case any time\n");
203 return;
204 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530205 rgn[r1].size = base2 + size2 - base1;
206 lmb_remove_region(lmb_rgn_lst, r2);
Udit Kumaredb58242023-09-26 16:54:42 +0530207}
208
Sughosh Ganu6534d262024-08-26 17:29:30 +0530209static void lmb_reserve_uboot_region(void)
210{
211 int bank;
212 ulong end, bank_end;
213 phys_addr_t rsv_start;
214
215 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
216 end = gd->ram_top;
217
218 /*
219 * Reserve memory from aligned address below the bottom of U-Boot stack
220 * until end of RAM area to prevent LMB from overwriting that memory.
221 */
222 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
223
224 /* adjust sp by 16K to be safe */
225 rsv_start -= SZ_16K;
226 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
227 if (!gd->bd->bi_dram[bank].size ||
228 rsv_start < gd->bd->bi_dram[bank].start)
229 continue;
230 /* Watch out for RAM at end of address space! */
231 bank_end = gd->bd->bi_dram[bank].start +
232 gd->bd->bi_dram[bank].size - 1;
233 if (rsv_start > bank_end)
234 continue;
235 if (bank_end > end)
236 bank_end = end - 1;
237
238 lmb_reserve_flags(rsv_start, bank_end - rsv_start + 1,
239 LMB_NOOVERWRITE);
240
241 if (gd->flags & GD_FLG_SKIP_RELOC)
242 lmb_reserve_flags((phys_addr_t)(uintptr_t)_start,
243 gd->mon_len, LMB_NOOVERWRITE);
244
245 break;
246 }
247}
248
Sughosh Ganued17a332024-08-26 17:29:18 +0530249static void lmb_reserve_common(void *fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100250{
Sughosh Ganu6534d262024-08-26 17:29:30 +0530251 lmb_reserve_uboot_region();
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100252
Simon Glass0c303f92021-09-25 19:43:21 -0600253 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Sughosh Ganued17a332024-08-26 17:29:18 +0530254 boot_fdt_add_mem_rsv_regions(fdt_blob);
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100255}
256
Sughosh Ganuf4fb1542024-08-26 17:29:24 +0530257static __maybe_unused void lmb_reserve_common_spl(void)
258{
259 phys_addr_t rsv_start;
260 phys_size_t rsv_size;
261
262 /*
263 * Assume a SPL stack of 16KB. This must be
264 * more than enough for the SPL stage.
265 */
266 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
267 rsv_start = gd->start_addr_sp - 16384;
268 rsv_size = 16384;
269 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
270 }
271
272 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
273 /* Reserve the bss region */
274 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
275 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
276 (phys_addr_t)(uintptr_t)__bss_start;
277 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
278 }
279}
280
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530281/**
282 * lmb_add_memory() - Add memory range for LMB allocations
283 *
284 * Add the entire available memory range to the pool of memory that
285 * can be used by the LMB module for allocations.
286 *
287 * Return: None
288 */
289void lmb_add_memory(void)
290{
291 int i;
292 phys_size_t size;
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530293 u64 ram_top = gd->ram_top;
294 struct bd_info *bd = gd->bd;
295
Sughosh Ganu497da0c2024-10-15 21:07:11 +0530296 if (CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP))
297 return lmb_arch_add_memory();
298
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530299 /* Assume a 4GB ram_top if not defined */
300 if (!ram_top)
301 ram_top = 0x100000000ULL;
302
303 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
304 size = bd->bi_dram[i].size;
305 if (size) {
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530306 lmb_add(bd->bi_dram[i].start, size);
Sughosh Ganueb052cb2024-10-15 21:07:05 +0530307
308 /*
309 * Reserve memory above ram_top as
310 * no-overwrite so that it cannot be
311 * allocated
312 */
313 if (bd->bi_dram[i].start >= ram_top)
314 lmb_reserve_flags(bd->bi_dram[i].start, size,
315 LMB_NOOVERWRITE);
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530316 }
317 }
318}
319
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530320static long lmb_resize_regions(struct alist *lmb_rgn_lst,
321 unsigned long idx_start,
322 phys_addr_t base, phys_size_t size)
323{
324 phys_size_t rgnsize;
325 unsigned long rgn_cnt, idx, idx_end;
326 phys_addr_t rgnbase, rgnend;
327 phys_addr_t mergebase, mergeend;
328 struct lmb_region *rgn = lmb_rgn_lst->data;
329
330 rgn_cnt = 0;
331 idx = idx_start;
332 idx_end = idx_start;
333
334 /*
335 * First thing to do is to identify how many regions
336 * the requested region overlaps.
337 * If the flags match, combine all these overlapping
338 * regions into a single region, and remove the merged
339 * regions.
340 */
341 while (idx <= lmb_rgn_lst->count - 1) {
342 rgnbase = rgn[idx].base;
343 rgnsize = rgn[idx].size;
344
345 if (lmb_addrs_overlap(base, size, rgnbase,
346 rgnsize)) {
347 if (rgn[idx].flags != LMB_NONE)
348 return -1;
349 rgn_cnt++;
350 idx_end = idx;
351 }
352 idx++;
353 }
354
355 /* The merged region's base and size */
356 rgnbase = rgn[idx_start].base;
357 mergebase = min(base, rgnbase);
358 rgnend = rgn[idx_end].base + rgn[idx_end].size;
359 mergeend = max(rgnend, (base + size));
360
361 rgn[idx_start].base = mergebase;
362 rgn[idx_start].size = mergeend - mergebase;
363
364 /* Now remove the merged regions */
365 while (--rgn_cnt)
366 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
367
368 return 0;
369}
370
Sughosh Ganued17a332024-08-26 17:29:18 +0530371/**
372 * lmb_add_region_flags() - Add an lmb region to the given list
373 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
374 * @base: Start address of the region
375 * @size: Size of the region to be added
376 * @flags: Attributes of the LMB region
377 *
378 * Add a region of memory to the list. If the region does not exist, add
379 * it to the list. Depending on the attributes of the region to be added,
380 * the function might resize an already existing region or coalesce two
381 * adjacent regions.
382 *
383 *
384 * Returns: 0 if the region addition successful, -1 on failure
385 */
386static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200387 phys_size_t size, enum lmb_flags flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600388{
389 unsigned long coalesced = 0;
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530390 long ret, i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530391 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600392
Sughosh Ganued17a332024-08-26 17:29:18 +0530393 if (alist_err(lmb_rgn_lst))
394 return -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600395
396 /* First try and coalesce this LMB with another. */
Sughosh Ganued17a332024-08-26 17:29:18 +0530397 for (i = 0; i < lmb_rgn_lst->count; i++) {
398 phys_addr_t rgnbase = rgn[i].base;
399 phys_size_t rgnsize = rgn[i].size;
400 phys_size_t rgnflags = rgn[i].flags;
Sjoerd Simons0d91c882023-02-12 16:07:05 +0100401 phys_addr_t end = base + size - 1;
402 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Sjoerd Simons0d91c882023-02-12 16:07:05 +0100403 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200404 if (flags == rgnflags)
405 /* Already have this region, so we're done */
406 return 0;
407 else
408 return -1; /* regions with new flags */
409 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600410
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530411 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
412 if (ret > 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200413 if (flags != rgnflags)
414 break;
Sughosh Ganued17a332024-08-26 17:29:18 +0530415 rgn[i].base -= size;
416 rgn[i].size += size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600417 coalesced++;
418 break;
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530419 } else if (ret < 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200420 if (flags != rgnflags)
421 break;
Sughosh Ganued17a332024-08-26 17:29:18 +0530422 rgn[i].size += size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600423 coalesced++;
424 break;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100425 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530426 if (flags == LMB_NONE) {
427 ret = lmb_resize_regions(lmb_rgn_lst, i, base,
428 size);
429 if (ret < 0)
430 return -1;
431
432 coalesced++;
433 break;
434 } else {
435 return -1;
436 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600437 }
438 }
439
Sughosh Ganued17a332024-08-26 17:29:18 +0530440 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
441 rgn = lmb_rgn_lst->data;
442 if (rgn[i].flags == rgn[i + 1].flags) {
443 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
444 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
445 coalesced++;
446 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
447 /* fix overlapping area */
448 lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
449 coalesced++;
450 }
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200451 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600452 }
453
454 if (coalesced)
Ilias Apalodimas0f57b002024-10-23 18:22:00 +0300455 return 0;
Sughosh Ganued17a332024-08-26 17:29:18 +0530456
457 if (alist_full(lmb_rgn_lst) &&
458 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala4ed65522008-02-27 21:51:47 -0600459 return -1;
Sughosh Ganued17a332024-08-26 17:29:18 +0530460 rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600461
462 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganued17a332024-08-26 17:29:18 +0530463 for (i = lmb_rgn_lst->count; i >= 0; i--) {
464 if (i && base < rgn[i - 1].base) {
465 rgn[i] = rgn[i - 1];
Kumar Gala4ed65522008-02-27 21:51:47 -0600466 } else {
Sughosh Ganued17a332024-08-26 17:29:18 +0530467 rgn[i].base = base;
468 rgn[i].size = size;
469 rgn[i].flags = flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600470 break;
471 }
472 }
473
Sughosh Ganued17a332024-08-26 17:29:18 +0530474 lmb_rgn_lst->count++;
Kumar Gala4ed65522008-02-27 21:51:47 -0600475
476 return 0;
477}
478
Sughosh Ganued17a332024-08-26 17:29:18 +0530479static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200480 phys_size_t size)
481{
Sughosh Ganued17a332024-08-26 17:29:18 +0530482 return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200483}
484
Kumar Gala4ed65522008-02-27 21:51:47 -0600485/* This routine may be called with relocation disabled. */
Sughosh Ganued17a332024-08-26 17:29:18 +0530486long lmb_add(phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600487{
Sughosh Ganu2f619152024-10-15 21:07:07 +0530488 long ret;
Sughosh Ganued17a332024-08-26 17:29:18 +0530489 struct alist *lmb_rgn_lst = &lmb.free_mem;
Kumar Gala4ed65522008-02-27 21:51:47 -0600490
Sughosh Ganu2f619152024-10-15 21:07:07 +0530491 ret = lmb_add_region(lmb_rgn_lst, base, size);
Ilias Apalodimas0f57b002024-10-23 18:22:00 +0300492 if (ret)
Sughosh Ganu2f619152024-10-15 21:07:07 +0530493 return ret;
494
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +0300495 return lmb_map_update_notify(base, size, MAP_OP_ADD, LMB_NONE);
Kumar Gala4ed65522008-02-27 21:51:47 -0600496}
497
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530498static long _lmb_free(phys_addr_t base, phys_size_t size)
Andy Fleming63796c42008-06-16 13:58:54 -0500499{
Sughosh Ganued17a332024-08-26 17:29:18 +0530500 struct lmb_region *rgn;
501 struct alist *lmb_rgn_lst = &lmb.used_mem;
Andy Fleming98874ff2008-07-07 14:24:39 -0500502 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100503 phys_addr_t end = base + size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500504 int i;
505
506 rgnbegin = rgnend = 0; /* supress gcc warnings */
Sughosh Ganued17a332024-08-26 17:29:18 +0530507 rgn = lmb_rgn_lst->data;
Andy Fleming63796c42008-06-16 13:58:54 -0500508 /* Find the region where (base, size) belongs to */
Sughosh Ganued17a332024-08-26 17:29:18 +0530509 for (i = 0; i < lmb_rgn_lst->count; i++) {
510 rgnbegin = rgn[i].base;
511 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500512
513 if ((rgnbegin <= base) && (end <= rgnend))
514 break;
515 }
516
517 /* Didn't find the region */
Sughosh Ganued17a332024-08-26 17:29:18 +0530518 if (i == lmb_rgn_lst->count)
Andy Fleming63796c42008-06-16 13:58:54 -0500519 return -1;
520
521 /* Check to see if we are removing entire region */
522 if ((rgnbegin == base) && (rgnend == end)) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530523 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming63796c42008-06-16 13:58:54 -0500524 return 0;
525 }
526
527 /* Check to see if region is matching at the front */
528 if (rgnbegin == base) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530529 rgn[i].base = end + 1;
530 rgn[i].size -= size;
Andy Fleming63796c42008-06-16 13:58:54 -0500531 return 0;
532 }
533
534 /* Check to see if the region is matching at the end */
535 if (rgnend == end) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530536 rgn[i].size -= size;
Andy Fleming63796c42008-06-16 13:58:54 -0500537 return 0;
538 }
539
540 /*
541 * We need to split the entry - adjust the current one to the
542 * beginging of the hole and add the region after hole.
543 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530544 rgn[i].size = base - rgn[i].base;
545 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
546 rgn[i].flags);
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200547}
548
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530549/**
550 * lmb_free_flags() - Free up a region of memory
551 * @base: Base Address of region to be freed
552 * @size: Size of the region to be freed
553 * @flags: Memory region attributes
554 *
555 * Free up a region of memory.
556 *
557 * Return: 0 if successful, -1 on failure
558 */
559long lmb_free_flags(phys_addr_t base, phys_size_t size,
Sughosh Ganu2f619152024-10-15 21:07:07 +0530560 uint flags)
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530561{
Sughosh Ganu2f619152024-10-15 21:07:07 +0530562 long ret;
563
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530564 ret = _lmb_free(base, size);
Sughosh Ganu2f619152024-10-15 21:07:07 +0530565 if (ret < 0)
566 return ret;
567
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +0300568 return lmb_map_update_notify(base, size, MAP_OP_FREE, flags);
Sughosh Ganu2f619152024-10-15 21:07:07 +0530569}
570
571long lmb_free(phys_addr_t base, phys_size_t size)
572{
573 return lmb_free_flags(base, size, LMB_NONE);
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530574}
575
Sughosh Ganued17a332024-08-26 17:29:18 +0530576long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags)
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200577{
Sughosh Ganu2f619152024-10-15 21:07:07 +0530578 long ret = 0;
Sughosh Ganued17a332024-08-26 17:29:18 +0530579 struct alist *lmb_rgn_lst = &lmb.used_mem;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200580
Sughosh Ganu2f619152024-10-15 21:07:07 +0530581 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Ilias Apalodimas0f57b002024-10-23 18:22:00 +0300582 if (ret)
583 return ret;
Sughosh Ganu2f619152024-10-15 21:07:07 +0530584
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +0300585 return lmb_map_update_notify(base, size, MAP_OP_RESERVE, flags);
Andy Fleming63796c42008-06-16 13:58:54 -0500586}
587
Sughosh Ganued17a332024-08-26 17:29:18 +0530588long lmb_reserve(phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600589{
Sughosh Ganued17a332024-08-26 17:29:18 +0530590 return lmb_reserve_flags(base, size, LMB_NONE);
Kumar Gala4ed65522008-02-27 21:51:47 -0600591}
592
Sughosh Ganued17a332024-08-26 17:29:18 +0530593static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Becky Bruce391fd932008-06-09 20:37:18 -0500594 phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600595{
596 unsigned long i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530597 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600598
Sughosh Ganued17a332024-08-26 17:29:18 +0530599 for (i = 0; i < lmb_rgn_lst->count; i++) {
600 phys_addr_t rgnbase = rgn[i].base;
601 phys_size_t rgnsize = rgn[i].size;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100602 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala4ed65522008-02-27 21:51:47 -0600603 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600604 }
605
Sughosh Ganued17a332024-08-26 17:29:18 +0530606 return (i < lmb_rgn_lst->count) ? i : -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600607}
608
Becky Bruce391fd932008-06-09 20:37:18 -0500609static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600610{
611 return addr & ~(size - 1);
612}
613
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530614static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align,
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530615 phys_addr_t max_addr, enum lmb_flags flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600616{
Sughosh Ganu2f619152024-10-15 21:07:07 +0530617 int ret;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100618 long i, rgn;
Becky Bruce391fd932008-06-09 20:37:18 -0500619 phys_addr_t base = 0;
Andy Fleming7570a992008-06-16 13:58:55 -0500620 phys_addr_t res_base;
Sughosh Ganued17a332024-08-26 17:29:18 +0530621 struct lmb_region *lmb_used = lmb.used_mem.data;
622 struct lmb_region *lmb_memory = lmb.free_mem.data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600623
Sughosh Ganued17a332024-08-26 17:29:18 +0530624 for (i = lmb.free_mem.count - 1; i >= 0; i--) {
625 phys_addr_t lmbbase = lmb_memory[i].base;
626 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600627
Andy Fleming7570a992008-06-16 13:58:55 -0500628 if (lmbsize < size)
629 continue;
Kumar Gala4ed65522008-02-27 21:51:47 -0600630 if (max_addr == LMB_ALLOC_ANYWHERE)
631 base = lmb_align_down(lmbbase + lmbsize - size, align);
632 else if (lmbbase < max_addr) {
Stephen Warrenad3fda52014-07-31 13:40:07 -0600633 base = lmbbase + lmbsize;
634 if (base < lmbbase)
635 base = -1;
636 base = min(base, max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600637 base = lmb_align_down(base - size, align);
638 } else
639 continue;
640
Andy Fleming7570a992008-06-16 13:58:55 -0500641 while (base && lmbbase <= base) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530642 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100643 if (rgn < 0) {
Andy Fleming7570a992008-06-16 13:58:55 -0500644 /* This area isn't reserved, take it */
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530645 if (lmb_add_region_flags(&lmb.used_mem, base,
Ilias Apalodimas0f57b002024-10-23 18:22:00 +0300646 size, flags))
Andy Fleming7570a992008-06-16 13:58:55 -0500647 return 0;
Sughosh Ganu2f619152024-10-15 21:07:07 +0530648
Ilias Apalodimasf6fb6a82024-10-23 18:22:01 +0300649 ret = lmb_map_update_notify(base, size,
650 MAP_OP_RESERVE,
651 flags);
652 if (ret)
653 return ret;
Sughosh Ganu2f619152024-10-15 21:07:07 +0530654
Andy Fleming7570a992008-06-16 13:58:55 -0500655 return base;
656 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530657
658 res_base = lmb_used[rgn].base;
Andy Fleming7570a992008-06-16 13:58:55 -0500659 if (res_base < size)
660 break;
661 base = lmb_align_down(res_base - size, align);
662 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600663 }
Andy Fleming7570a992008-06-16 13:58:55 -0500664 return 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600665}
666
Sughosh Ganued17a332024-08-26 17:29:18 +0530667phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530668{
Sughosh Ganued17a332024-08-26 17:29:18 +0530669 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530670}
671
Sughosh Ganued17a332024-08-26 17:29:18 +0530672phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530673{
674 phys_addr_t alloc;
675
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530676 alloc = _lmb_alloc_base(size, align, max_addr, LMB_NONE);
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530677
678 if (alloc == 0)
679 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
680 (ulong)size, (ulong)max_addr);
681
682 return alloc;
683}
684
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530685/**
686 * lmb_alloc_base_flags() - Allocate specified memory region with specified attributes
687 * @size: Size of the region requested
688 * @align: Alignment of the memory region requested
689 * @max_addr: Maximum address of the requested region
690 * @flags: Memory region attributes to be set
691 *
692 * Allocate a region of memory with the attributes specified through the
693 * parameter. The max_addr parameter is used to specify the maximum address
694 * below which the requested region should be allocated.
695 *
696 * Return: base address on success, 0 on error
697 */
698phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
699 phys_addr_t max_addr, uint flags)
700{
701 phys_addr_t alloc;
702
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530703 alloc = _lmb_alloc_base(size, align, max_addr, flags);
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530704
705 if (alloc == 0)
706 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
707 (ulong)size, (ulong)max_addr);
708
709 return alloc;
710}
711
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530712static phys_addr_t _lmb_alloc_addr(phys_addr_t base, phys_size_t size,
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530713 enum lmb_flags flags)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100714{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100715 long rgn;
Sughosh Ganued17a332024-08-26 17:29:18 +0530716 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100717
718 /* Check if the requested address is in one of the memory regions */
Sughosh Ganued17a332024-08-26 17:29:18 +0530719 rgn = lmb_overlaps_region(&lmb.free_mem, base, size);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100720 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100721 /*
722 * Check if the requested end address is in the same memory
723 * region we found.
724 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530725 if (lmb_addrs_overlap(lmb_memory[rgn].base,
726 lmb_memory[rgn].size,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100727 base + size - 1, 1)) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100728 /* ok, reserve the memory */
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530729 if (lmb_reserve_flags(base, size, flags) >= 0)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100730 return base;
731 }
732 }
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530733
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100734 return 0;
735}
736
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530737/*
738 * Try to allocate a specific address range: must be in defined memory but not
739 * reserved
740 */
741phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
742{
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530743 return _lmb_alloc_addr(base, size, LMB_NONE);
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530744}
745
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530746/**
747 * lmb_alloc_addr_flags() - Allocate specified memory address with specified attributes
748 * @base: Base Address requested
749 * @size: Size of the region requested
750 * @flags: Memory region attributes to be set
751 *
752 * Allocate a region of memory with the attributes specified through the
753 * parameter. The base parameter is used to specify the base address
754 * of the requested region.
755 *
756 * Return: base address on success, 0 on error
757 */
758phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
759 uint flags)
760{
Sughosh Ganu8d0df5f2024-10-15 21:07:17 +0530761 return _lmb_alloc_addr(base, size, flags);
Sughosh Ganuc8a8f012024-10-15 21:07:03 +0530762}
763
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100764/* Return number of bytes from a given address that are free */
Sughosh Ganued17a332024-08-26 17:29:18 +0530765phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100766{
767 int i;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100768 long rgn;
Sughosh Ganued17a332024-08-26 17:29:18 +0530769 struct lmb_region *lmb_used = lmb.used_mem.data;
770 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100771
772 /* check if the requested address is in the memory regions */
Sughosh Ganued17a332024-08-26 17:29:18 +0530773 rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100774 if (rgn >= 0) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530775 for (i = 0; i < lmb.used_mem.count; i++) {
776 if (addr < lmb_used[i].base) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100777 /* first reserved range > requested address */
Sughosh Ganued17a332024-08-26 17:29:18 +0530778 return lmb_used[i].base - addr;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100779 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530780 if (lmb_used[i].base +
781 lmb_used[i].size > addr) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100782 /* requested addr is in this reserved range */
783 return 0;
784 }
785 }
786 /* if we come here: no reserved ranges above requested addr */
Sughosh Ganued17a332024-08-26 17:29:18 +0530787 return lmb_memory[lmb.free_mem.count - 1].base +
788 lmb_memory[lmb.free_mem.count - 1].size - addr;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100789 }
790 return 0;
791}
792
Sughosh Ganued17a332024-08-26 17:29:18 +0530793int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600794{
795 int i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530796 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600797
Sughosh Ganued17a332024-08-26 17:29:18 +0530798 for (i = 0; i < lmb.used_mem.count; i++) {
799 phys_addr_t upper = lmb_used[i].base +
800 lmb_used[i].size - 1;
801 if (addr >= lmb_used[i].base && addr <= upper)
802 return (lmb_used[i].flags & flags) == flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600803 }
804 return 0;
805}
Mike Frysingera16028d2009-11-03 11:35:59 -0500806
Sughosh Ganu2f619152024-10-15 21:07:07 +0530807static int lmb_setup(bool test)
Sughosh Ganued17a332024-08-26 17:29:18 +0530808{
809 bool ret;
810
811 ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region),
812 (uint)LMB_ALIST_INITIAL_SIZE);
813 if (!ret) {
814 log_debug("Unable to initialise the list for LMB free memory\n");
815 return -ENOMEM;
816 }
817
818 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
819 (uint)LMB_ALIST_INITIAL_SIZE);
820 if (!ret) {
821 log_debug("Unable to initialise the list for LMB used memory\n");
822 return -ENOMEM;
823 }
824
Sughosh Ganu2f619152024-10-15 21:07:07 +0530825 lmb.test = test;
826
Sughosh Ganued17a332024-08-26 17:29:18 +0530827 return 0;
828}
829
830/**
831 * lmb_init() - Initialise the LMB module
832 *
833 * Initialise the LMB lists needed for keeping the memory map. There
834 * are two lists, in form of alloced list data structure. One for the
835 * available memory, and one for the used memory. Initialise the two
836 * lists as part of board init. Add memory to the available memory
837 * list and reserve common areas by adding them to the used memory
838 * list.
839 *
840 * Return: 0 on success, -ve on error
841 */
842int lmb_init(void)
843{
844 int ret;
845
Sughosh Ganu2f619152024-10-15 21:07:07 +0530846 ret = lmb_setup(false);
Sughosh Ganued17a332024-08-26 17:29:18 +0530847 if (ret) {
848 log_info("Unable to init LMB\n");
849 return ret;
850 }
851
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530852 lmb_add_memory();
853
Sughosh Ganuf4fb1542024-08-26 17:29:24 +0530854 /* Reserve the U-Boot image region once U-Boot has relocated */
Simon Glass456bdb72024-09-29 19:49:36 -0600855 if (xpl_phase() == PHASE_SPL)
Sughosh Ganuf4fb1542024-08-26 17:29:24 +0530856 lmb_reserve_common_spl();
Simon Glass456bdb72024-09-29 19:49:36 -0600857 else if (xpl_phase() == PHASE_BOARD_R)
Sughosh Ganuf4fb1542024-08-26 17:29:24 +0530858 lmb_reserve_common((void *)gd->fdt_blob);
859
Sughosh Ganued17a332024-08-26 17:29:18 +0530860 return 0;
861}
862
Sughosh Ganued17a332024-08-26 17:29:18 +0530863struct lmb *lmb_get(void)
864{
865 return &lmb;
866}
867
Simon Glass1c30f7a2024-10-21 10:19:31 +0200868#if CONFIG_IS_ENABLED(UNIT_TEST)
Sughosh Ganued17a332024-08-26 17:29:18 +0530869int lmb_push(struct lmb *store)
870{
871 int ret;
872
873 *store = lmb;
Sughosh Ganu2f619152024-10-15 21:07:07 +0530874 ret = lmb_setup(true);
Sughosh Ganued17a332024-08-26 17:29:18 +0530875 if (ret)
876 return ret;
877
878 return 0;
879}
880
881void lmb_pop(struct lmb *store)
882{
883 alist_uninit(&lmb.free_mem);
884 alist_uninit(&lmb.used_mem);
885 lmb = *store;
886}
887#endif /* UNIT_TEST */