blob: 7e90f178763b8cc1b44170f4578f82604118cfcd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Gala6d7bfa82008-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 Gala6d7bfa82008-02-27 21:51:47 -06007 */
8
Sughosh Ganu291bf9c2024-08-26 17:29:18 +05309#include <alist.h>
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +010010#include <efi_loader.h>
Sughosh Ganua3af5ba2024-10-15 21:07:07 +053011#include <event.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060012#include <image.h>
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +010013#include <mapmem.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060014#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <malloc.h>
Sughosh Ganu6518b412024-08-26 17:29:24 +053017#include <spl.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060018
Marek Vasuta2eec022021-09-10 22:47:09 +020019#include <asm/global_data.h>
Marek Vasut0fcae7f2021-11-13 18:34:37 +010020#include <asm/sections.h>
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053021#include <linux/kernel.h>
Sughosh Ganue5348c72024-08-26 17:29:30 +053022#include <linux/sizes.h>
Marek Vasuta2eec022021-09-10 22:47:09 +020023
24DECLARE_GLOBAL_DATA_PTR;
25
Sughosh Ganua3af5ba2024-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 Gala6d7bfa82008-02-27 21:51:47 -060030#define LMB_ALLOC_ANYWHERE 0
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053031#define LMB_ALIST_INITIAL_SIZE 4
Kumar Gala6d7bfa82008-02-27 21:51:47 -060032
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053033static struct lmb lmb;
34
Sughosh Ganua3af5ba2024-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,
43 u8 op)
44{
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
54 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;
67 } else {
68 return 0;
69 }
70}
71
Sughosh Ganuf04416f2024-08-26 17:29:40 +053072static void lmb_print_region_flags(enum lmb_flags flags)
73{
74 u64 bitpos;
Sughosh Ganu7ebbdd72024-10-15 21:07:04 +053075 const char *flag_str[] = { "none", "no-map", "no-overwrite", "no-notify" };
Sughosh Ganuf04416f2024-08-26 17:29:40 +053076
77 do {
78 bitpos = flags ? fls(flags) - 1 : 0;
79 printf("%s", flag_str[bitpos]);
80 flags &= ~(1ull << bitpos);
81 puts(flags ? ", " : "\n");
82 } while (flags);
83}
84
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053085static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060086{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053087 struct lmb_region *rgn = lmb_rgn_lst->data;
Patrick Delaunay48120022021-05-07 14:50:31 +020088 unsigned long long base, size, end;
89 enum lmb_flags flags;
90 int i;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060091
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053092 printf(" %s.count = 0x%x\n", name, lmb_rgn_lst->count);
Patrick Delaunay48120022021-05-07 14:50:31 +020093
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053094 for (i = 0; i < lmb_rgn_lst->count; i++) {
95 base = rgn[i].base;
96 size = rgn[i].size;
Patrick Delaunay48120022021-05-07 14:50:31 +020097 end = base + size - 1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053098 flags = rgn[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060099
Sughosh Ganuf04416f2024-08-26 17:29:40 +0530100 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: ",
101 name, i, base, end, size);
102 lmb_print_region_flags(flags);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600103 }
Tero Kristo97c418b2020-07-20 11:10:45 +0300104}
105
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530106void lmb_dump_all_force(void)
Patrick Delaunay48120022021-05-07 14:50:31 +0200107{
108 printf("lmb_dump_all:\n");
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530109 lmb_dump_region(&lmb.free_mem, "memory");
110 lmb_dump_region(&lmb.used_mem, "reserved");
Patrick Delaunay48120022021-05-07 14:50:31 +0200111}
112
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530113void lmb_dump_all(void)
Tero Kristo97c418b2020-07-20 11:10:45 +0300114{
115#ifdef DEBUG
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530116 lmb_dump_all_force();
Tero Kristo97c418b2020-07-20 11:10:45 +0300117#endif
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600118}
119
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100120static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
121 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600122{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100123 const phys_addr_t base1_end = base1 + size1 - 1;
124 const phys_addr_t base2_end = base2 + size2 - 1;
125
126 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600127}
128
Becky Bruced26d67c2008-06-09 20:37:18 -0500129static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100130 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600131{
132 if (base2 == base1 + size1)
133 return 1;
134 else if (base1 == base2 + size2)
135 return -1;
136
137 return 0;
138}
139
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530140static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1,
Udit Kumar4e8e6632023-09-26 16:54:42 +0530141 unsigned long r2)
142{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530143 struct lmb_region *rgn = lmb_rgn_lst->data;
144
145 phys_addr_t base1 = rgn[r1].base;
146 phys_size_t size1 = rgn[r1].size;
147 phys_addr_t base2 = rgn[r2].base;
148 phys_size_t size2 = rgn[r2].size;
Udit Kumar4e8e6632023-09-26 16:54:42 +0530149
150 return lmb_addrs_overlap(base1, size1, base2, size2);
151}
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530152
153static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100154 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600155{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530156 struct lmb_region *rgn = lmb_rgn_lst->data;
157
158 phys_addr_t base1 = rgn[r1].base;
159 phys_size_t size1 = rgn[r1].size;
160 phys_addr_t base2 = rgn[r2].base;
161 phys_size_t size2 = rgn[r2].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600162 return lmb_addrs_adjacent(base1, size1, base2, size2);
163}
164
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530165static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600166{
167 unsigned long i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530168 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600169
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530170 for (i = r; i < lmb_rgn_lst->count - 1; i++) {
171 rgn[i].base = rgn[i + 1].base;
172 rgn[i].size = rgn[i + 1].size;
173 rgn[i].flags = rgn[i + 1].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600174 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530175 lmb_rgn_lst->count--;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600176}
177
178/* Assumption: base addr of region 1 < base addr of region 2 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530179static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100180 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600181{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530182 struct lmb_region *rgn = lmb_rgn_lst->data;
183
184 rgn[r1].size += rgn[r2].size;
185 lmb_remove_region(lmb_rgn_lst, r2);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600186}
187
Udit Kumar4e8e6632023-09-26 16:54:42 +0530188/*Assumption : base addr of region 1 < base addr of region 2*/
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530189static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst,
190 unsigned long r1, unsigned long r2)
Udit Kumar4e8e6632023-09-26 16:54:42 +0530191{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530192 struct lmb_region *rgn = lmb_rgn_lst->data;
193
194 phys_addr_t base1 = rgn[r1].base;
195 phys_size_t size1 = rgn[r1].size;
196 phys_addr_t base2 = rgn[r2].base;
197 phys_size_t size2 = rgn[r2].size;
Udit Kumar4e8e6632023-09-26 16:54:42 +0530198
199 if (base1 + size1 > base2 + size2) {
200 printf("This will not be a case any time\n");
201 return;
202 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530203 rgn[r1].size = base2 + size2 - base1;
204 lmb_remove_region(lmb_rgn_lst, r2);
Udit Kumar4e8e6632023-09-26 16:54:42 +0530205}
206
Sughosh Ganue5348c72024-08-26 17:29:30 +0530207static void lmb_reserve_uboot_region(void)
208{
209 int bank;
210 ulong end, bank_end;
211 phys_addr_t rsv_start;
212
213 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
214 end = gd->ram_top;
215
216 /*
217 * Reserve memory from aligned address below the bottom of U-Boot stack
218 * until end of RAM area to prevent LMB from overwriting that memory.
219 */
220 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
221
222 /* adjust sp by 16K to be safe */
223 rsv_start -= SZ_16K;
224 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
225 if (!gd->bd->bi_dram[bank].size ||
226 rsv_start < gd->bd->bi_dram[bank].start)
227 continue;
228 /* Watch out for RAM at end of address space! */
229 bank_end = gd->bd->bi_dram[bank].start +
230 gd->bd->bi_dram[bank].size - 1;
231 if (rsv_start > bank_end)
232 continue;
233 if (bank_end > end)
234 bank_end = end - 1;
235
236 lmb_reserve_flags(rsv_start, bank_end - rsv_start + 1,
237 LMB_NOOVERWRITE);
238
239 if (gd->flags & GD_FLG_SKIP_RELOC)
240 lmb_reserve_flags((phys_addr_t)(uintptr_t)_start,
241 gd->mon_len, LMB_NOOVERWRITE);
242
243 break;
244 }
245}
246
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530247static void lmb_reserve_common(void *fdt_blob)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100248{
Sughosh Ganue5348c72024-08-26 17:29:30 +0530249 lmb_reserve_uboot_region();
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100250
Simon Glass85c057e2021-09-25 19:43:21 -0600251 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530252 boot_fdt_add_mem_rsv_regions(fdt_blob);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100253}
254
Sughosh Ganu6518b412024-08-26 17:29:24 +0530255static __maybe_unused void lmb_reserve_common_spl(void)
256{
257 phys_addr_t rsv_start;
258 phys_size_t rsv_size;
259
260 /*
261 * Assume a SPL stack of 16KB. This must be
262 * more than enough for the SPL stage.
263 */
264 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
265 rsv_start = gd->start_addr_sp - 16384;
266 rsv_size = 16384;
267 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
268 }
269
270 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
271 /* Reserve the bss region */
272 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
273 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
274 (phys_addr_t)(uintptr_t)__bss_start;
275 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
276 }
277}
278
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530279/**
280 * lmb_add_memory() - Add memory range for LMB allocations
281 *
282 * Add the entire available memory range to the pool of memory that
283 * can be used by the LMB module for allocations.
284 *
285 * Return: None
286 */
287void lmb_add_memory(void)
288{
289 int i;
290 phys_size_t size;
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530291 u64 ram_top = gd->ram_top;
292 struct bd_info *bd = gd->bd;
293
Sughosh Ganu1a36d442024-10-15 21:07:11 +0530294 if (CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP))
295 return lmb_arch_add_memory();
296
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530297 /* Assume a 4GB ram_top if not defined */
298 if (!ram_top)
299 ram_top = 0x100000000ULL;
300
301 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
302 size = bd->bi_dram[i].size;
303 if (size) {
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530304 lmb_add(bd->bi_dram[i].start, size);
Sughosh Ganubafdac02024-10-15 21:07:05 +0530305
306 /*
307 * Reserve memory above ram_top as
308 * no-overwrite so that it cannot be
309 * allocated
310 */
311 if (bd->bi_dram[i].start >= ram_top)
312 lmb_reserve_flags(bd->bi_dram[i].start, size,
313 LMB_NOOVERWRITE);
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530314 }
315 }
316}
317
Sughosh Ganu50e27272024-08-26 17:29:19 +0530318static long lmb_resize_regions(struct alist *lmb_rgn_lst,
319 unsigned long idx_start,
320 phys_addr_t base, phys_size_t size)
321{
322 phys_size_t rgnsize;
323 unsigned long rgn_cnt, idx, idx_end;
324 phys_addr_t rgnbase, rgnend;
325 phys_addr_t mergebase, mergeend;
326 struct lmb_region *rgn = lmb_rgn_lst->data;
327
328 rgn_cnt = 0;
329 idx = idx_start;
330 idx_end = idx_start;
331
332 /*
333 * First thing to do is to identify how many regions
334 * the requested region overlaps.
335 * If the flags match, combine all these overlapping
336 * regions into a single region, and remove the merged
337 * regions.
338 */
339 while (idx <= lmb_rgn_lst->count - 1) {
340 rgnbase = rgn[idx].base;
341 rgnsize = rgn[idx].size;
342
343 if (lmb_addrs_overlap(base, size, rgnbase,
344 rgnsize)) {
345 if (rgn[idx].flags != LMB_NONE)
346 return -1;
347 rgn_cnt++;
348 idx_end = idx;
349 }
350 idx++;
351 }
352
353 /* The merged region's base and size */
354 rgnbase = rgn[idx_start].base;
355 mergebase = min(base, rgnbase);
356 rgnend = rgn[idx_end].base + rgn[idx_end].size;
357 mergeend = max(rgnend, (base + size));
358
359 rgn[idx_start].base = mergebase;
360 rgn[idx_start].size = mergeend - mergebase;
361
362 /* Now remove the merged regions */
363 while (--rgn_cnt)
364 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
365
366 return 0;
367}
368
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530369/**
370 * lmb_add_region_flags() - Add an lmb region to the given list
371 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
372 * @base: Start address of the region
373 * @size: Size of the region to be added
374 * @flags: Attributes of the LMB region
375 *
376 * Add a region of memory to the list. If the region does not exist, add
377 * it to the list. Depending on the attributes of the region to be added,
378 * the function might resize an already existing region or coalesce two
379 * adjacent regions.
380 *
381 *
382 * Returns: 0 if the region addition successful, -1 on failure
383 */
384static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200385 phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600386{
387 unsigned long coalesced = 0;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530388 long ret, i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530389 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600390
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530391 if (alist_err(lmb_rgn_lst))
392 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600393
394 /* First try and coalesce this LMB with another. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530395 for (i = 0; i < lmb_rgn_lst->count; i++) {
396 phys_addr_t rgnbase = rgn[i].base;
397 phys_size_t rgnsize = rgn[i].size;
398 phys_size_t rgnflags = rgn[i].flags;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100399 phys_addr_t end = base + size - 1;
400 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100401 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200402 if (flags == rgnflags)
403 /* Already have this region, so we're done */
404 return 0;
405 else
406 return -1; /* regions with new flags */
407 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600408
Sughosh Ganu50e27272024-08-26 17:29:19 +0530409 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
410 if (ret > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200411 if (flags != rgnflags)
412 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530413 rgn[i].base -= size;
414 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600415 coalesced++;
416 break;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530417 } else if (ret < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200418 if (flags != rgnflags)
419 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530420 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600421 coalesced++;
422 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100423 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sughosh Ganu50e27272024-08-26 17:29:19 +0530424 if (flags == LMB_NONE) {
425 ret = lmb_resize_regions(lmb_rgn_lst, i, base,
426 size);
427 if (ret < 0)
428 return -1;
429
430 coalesced++;
431 break;
432 } else {
433 return -1;
434 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600435 }
436 }
437
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530438 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
439 rgn = lmb_rgn_lst->data;
440 if (rgn[i].flags == rgn[i + 1].flags) {
441 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
442 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
443 coalesced++;
444 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
445 /* fix overlapping area */
446 lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
447 coalesced++;
448 }
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200449 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600450 }
451
452 if (coalesced)
453 return coalesced;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530454
455 if (alist_full(lmb_rgn_lst) &&
456 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600457 return -1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530458 rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600459
460 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530461 for (i = lmb_rgn_lst->count; i >= 0; i--) {
462 if (i && base < rgn[i - 1].base) {
463 rgn[i] = rgn[i - 1];
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600464 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530465 rgn[i].base = base;
466 rgn[i].size = size;
467 rgn[i].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600468 break;
469 }
470 }
471
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530472 lmb_rgn_lst->count++;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600473
474 return 0;
475}
476
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530477static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200478 phys_size_t size)
479{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530480 return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200481}
482
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600483/* This routine may be called with relocation disabled. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530484long lmb_add(phys_addr_t base, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600485{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530486 long ret;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530487 struct alist *lmb_rgn_lst = &lmb.free_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600488
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530489 ret = lmb_add_region(lmb_rgn_lst, base, size);
Caleb Connollya2025672024-10-18 15:51:07 +0200490 if (ret < 0)
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530491 return ret;
492
493 if (lmb_should_notify(LMB_NONE))
494 return lmb_map_update_notify(base, size, MAP_OP_ADD);
495
496 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600497}
498
Sughosh Ganu99b59662024-10-15 21:07:17 +0530499static long _lmb_free(phys_addr_t base, phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500500{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530501 struct lmb_region *rgn;
502 struct alist *lmb_rgn_lst = &lmb.used_mem;
Andy Fleming09d2a712008-07-07 14:24:39 -0500503 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100504 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500505 int i;
506
507 rgnbegin = rgnend = 0; /* supress gcc warnings */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530508 rgn = lmb_rgn_lst->data;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500509 /* Find the region where (base, size) belongs to */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530510 for (i = 0; i < lmb_rgn_lst->count; i++) {
511 rgnbegin = rgn[i].base;
512 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500513
514 if ((rgnbegin <= base) && (end <= rgnend))
515 break;
516 }
517
518 /* Didn't find the region */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530519 if (i == lmb_rgn_lst->count)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500520 return -1;
521
522 /* Check to see if we are removing entire region */
523 if ((rgnbegin == base) && (rgnend == end)) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530524 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500525 return 0;
526 }
527
528 /* Check to see if region is matching at the front */
529 if (rgnbegin == base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530530 rgn[i].base = end + 1;
531 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500532 return 0;
533 }
534
535 /* Check to see if the region is matching at the end */
536 if (rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530537 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500538 return 0;
539 }
540
541 /*
542 * We need to split the entry - adjust the current one to the
543 * beginging of the hole and add the region after hole.
544 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530545 rgn[i].size = base - rgn[i].base;
546 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
547 rgn[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500548}
549
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530550/**
551 * lmb_free_flags() - Free up a region of memory
552 * @base: Base Address of region to be freed
553 * @size: Size of the region to be freed
554 * @flags: Memory region attributes
555 *
556 * Free up a region of memory.
557 *
558 * Return: 0 if successful, -1 on failure
559 */
560long lmb_free_flags(phys_addr_t base, phys_size_t size,
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530561 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530562{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530563 long ret;
564
Sughosh Ganu99b59662024-10-15 21:07:17 +0530565 ret = _lmb_free(base, size);
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530566 if (ret < 0)
567 return ret;
568
569 if (lmb_should_notify(flags))
570 return lmb_map_update_notify(base, size, MAP_OP_FREE);
571
572 return ret;
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530573}
574
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530575long lmb_free(phys_addr_t base, phys_size_t size)
576{
577 return lmb_free_flags(base, size, LMB_NONE);
578}
579
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530580long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600581{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530582 long ret = 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530583 struct alist *lmb_rgn_lst = &lmb.used_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600584
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530585 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
586 if (ret < 0)
587 return -1;
588
589 if (lmb_should_notify(flags))
590 return lmb_map_update_notify(base, size, MAP_OP_RESERVE);
591
592 return ret;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200593}
594
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530595long lmb_reserve(phys_addr_t base, phys_size_t size)
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200596{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530597 return lmb_reserve_flags(base, size, LMB_NONE);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600598}
599
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530600static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Becky Bruced26d67c2008-06-09 20:37:18 -0500601 phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600602{
603 unsigned long i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530604 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600605
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530606 for (i = 0; i < lmb_rgn_lst->count; i++) {
607 phys_addr_t rgnbase = rgn[i].base;
608 phys_size_t rgnsize = rgn[i].size;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100609 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600610 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600611 }
612
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530613 return (i < lmb_rgn_lst->count) ? i : -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600614}
615
Becky Bruced26d67c2008-06-09 20:37:18 -0500616static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600617{
618 return addr & ~(size - 1);
619}
620
Sughosh Ganu99b59662024-10-15 21:07:17 +0530621static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align,
Sughosh Ganu50e27272024-08-26 17:29:19 +0530622 phys_addr_t max_addr, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600623{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530624 u8 op;
625 int ret;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100626 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500627 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500628 phys_addr_t res_base;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530629 struct lmb_region *lmb_used = lmb.used_mem.data;
630 struct lmb_region *lmb_memory = lmb.free_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600631
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530632 for (i = lmb.free_mem.count - 1; i >= 0; i--) {
633 phys_addr_t lmbbase = lmb_memory[i].base;
634 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600635
Andy Fleming78bd5a72008-06-16 13:58:55 -0500636 if (lmbsize < size)
637 continue;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600638 if (max_addr == LMB_ALLOC_ANYWHERE)
639 base = lmb_align_down(lmbbase + lmbsize - size, align);
640 else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600641 base = lmbbase + lmbsize;
642 if (base < lmbbase)
643 base = -1;
644 base = min(base, max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600645 base = lmb_align_down(base - size, align);
646 } else
647 continue;
648
Andy Fleming78bd5a72008-06-16 13:58:55 -0500649 while (base && lmbbase <= base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530650 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100651 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500652 /* This area isn't reserved, take it */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530653 if (lmb_add_region_flags(&lmb.used_mem, base,
654 size, flags) < 0)
Andy Fleming78bd5a72008-06-16 13:58:55 -0500655 return 0;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530656
657 if (lmb_should_notify(flags)) {
658 op = MAP_OP_RESERVE;
659 ret = lmb_map_update_notify(base, size,
660 op);
661 if (ret)
662 return ret;
663 }
664
Andy Fleming78bd5a72008-06-16 13:58:55 -0500665 return base;
666 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530667
668 res_base = lmb_used[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500669 if (res_base < size)
670 break;
671 base = lmb_align_down(res_base - size, align);
672 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600673 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500674 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600675}
676
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530677phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530678{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530679 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530680}
681
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530682/**
683 * lmb_alloc_flags() - Allocate memory region with specified attributes
684 * @size: Size of the region requested
685 * @align: Alignment of the memory region requested
686 * @flags: Memory region attributes to be set
687 *
688 * Allocate a region of memory with the attributes specified through the
689 * parameter.
690 *
691 * Return: base address on success, 0 on error
692 */
693phys_addr_t lmb_alloc_flags(phys_size_t size, ulong align, uint flags)
694{
Sughosh Ganu99b59662024-10-15 21:07:17 +0530695 return _lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE,
696 flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530697}
698
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530699phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530700{
701 phys_addr_t alloc;
702
Sughosh Ganu99b59662024-10-15 21:07:17 +0530703 alloc = _lmb_alloc_base(size, align, max_addr, LMB_NONE);
Sughosh Ganu78435de2024-08-26 17:29:16 +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 Ganu7f15d322024-10-15 21:07:03 +0530712/**
713 * lmb_alloc_base_flags() - Allocate specified memory region with specified attributes
714 * @size: Size of the region requested
715 * @align: Alignment of the memory region requested
716 * @max_addr: Maximum address of the requested region
717 * @flags: Memory region attributes to be set
718 *
719 * Allocate a region of memory with the attributes specified through the
720 * parameter. The max_addr parameter is used to specify the maximum address
721 * below which the requested region should be allocated.
722 *
723 * Return: base address on success, 0 on error
724 */
725phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
726 phys_addr_t max_addr, uint flags)
727{
728 phys_addr_t alloc;
729
Sughosh Ganu99b59662024-10-15 21:07:17 +0530730 alloc = _lmb_alloc_base(size, align, max_addr, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530731
732 if (alloc == 0)
733 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
734 (ulong)size, (ulong)max_addr);
735
736 return alloc;
737}
738
Sughosh Ganu99b59662024-10-15 21:07:17 +0530739static phys_addr_t _lmb_alloc_addr(phys_addr_t base, phys_size_t size,
Sughosh Ganu50e27272024-08-26 17:29:19 +0530740 enum lmb_flags flags)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100741{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100742 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530743 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100744
745 /* Check if the requested address is in one of the memory regions */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530746 rgn = lmb_overlaps_region(&lmb.free_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100747 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100748 /*
749 * Check if the requested end address is in the same memory
750 * region we found.
751 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530752 if (lmb_addrs_overlap(lmb_memory[rgn].base,
753 lmb_memory[rgn].size,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100754 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100755 /* ok, reserve the memory */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530756 if (lmb_reserve_flags(base, size, flags) >= 0)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100757 return base;
758 }
759 }
Sughosh Ganu50e27272024-08-26 17:29:19 +0530760
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100761 return 0;
762}
763
Sughosh Ganu50e27272024-08-26 17:29:19 +0530764/*
765 * Try to allocate a specific address range: must be in defined memory but not
766 * reserved
767 */
768phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
769{
Sughosh Ganu99b59662024-10-15 21:07:17 +0530770 return _lmb_alloc_addr(base, size, LMB_NONE);
Sughosh Ganu50e27272024-08-26 17:29:19 +0530771}
772
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530773/**
774 * lmb_alloc_addr_flags() - Allocate specified memory address with specified attributes
775 * @base: Base Address requested
776 * @size: Size of the region requested
777 * @flags: Memory region attributes to be set
778 *
779 * Allocate a region of memory with the attributes specified through the
780 * parameter. The base parameter is used to specify the base address
781 * of the requested region.
782 *
783 * Return: base address on success, 0 on error
784 */
785phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
786 uint flags)
787{
Sughosh Ganu99b59662024-10-15 21:07:17 +0530788 return _lmb_alloc_addr(base, size, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530789}
790
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100791/* Return number of bytes from a given address that are free */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530792phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100793{
794 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100795 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530796 struct lmb_region *lmb_used = lmb.used_mem.data;
797 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100798
799 /* check if the requested address is in the memory regions */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530800 rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100801 if (rgn >= 0) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530802 for (i = 0; i < lmb.used_mem.count; i++) {
803 if (addr < lmb_used[i].base) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100804 /* first reserved range > requested address */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530805 return lmb_used[i].base - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100806 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530807 if (lmb_used[i].base +
808 lmb_used[i].size > addr) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100809 /* requested addr is in this reserved range */
810 return 0;
811 }
812 }
813 /* if we come here: no reserved ranges above requested addr */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530814 return lmb_memory[lmb.free_mem.count - 1].base +
815 lmb_memory[lmb.free_mem.count - 1].size - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100816 }
817 return 0;
818}
819
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530820int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600821{
822 int i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530823 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600824
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530825 for (i = 0; i < lmb.used_mem.count; i++) {
826 phys_addr_t upper = lmb_used[i].base +
827 lmb_used[i].size - 1;
828 if (addr >= lmb_used[i].base && addr <= upper)
829 return (lmb_used[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600830 }
831 return 0;
832}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500833
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530834static int lmb_setup(bool test)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530835{
836 bool ret;
837
838 ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region),
839 (uint)LMB_ALIST_INITIAL_SIZE);
840 if (!ret) {
841 log_debug("Unable to initialise the list for LMB free memory\n");
842 return -ENOMEM;
843 }
844
845 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
846 (uint)LMB_ALIST_INITIAL_SIZE);
847 if (!ret) {
848 log_debug("Unable to initialise the list for LMB used memory\n");
849 return -ENOMEM;
850 }
851
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530852 lmb.test = test;
853
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530854 return 0;
855}
856
857/**
858 * lmb_init() - Initialise the LMB module
859 *
860 * Initialise the LMB lists needed for keeping the memory map. There
861 * are two lists, in form of alloced list data structure. One for the
862 * available memory, and one for the used memory. Initialise the two
863 * lists as part of board init. Add memory to the available memory
864 * list and reserve common areas by adding them to the used memory
865 * list.
866 *
867 * Return: 0 on success, -ve on error
868 */
869int lmb_init(void)
870{
871 int ret;
872
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530873 ret = lmb_setup(false);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530874 if (ret) {
875 log_info("Unable to init LMB\n");
876 return ret;
877 }
878
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530879 lmb_add_memory();
880
Sughosh Ganu6518b412024-08-26 17:29:24 +0530881 /* Reserve the U-Boot image region once U-Boot has relocated */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600882 if (xpl_phase() == PHASE_SPL)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530883 lmb_reserve_common_spl();
Simon Glassd4dce4a2024-09-29 19:49:36 -0600884 else if (xpl_phase() == PHASE_BOARD_R)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530885 lmb_reserve_common((void *)gd->fdt_blob);
886
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530887 return 0;
888}
889
890#if CONFIG_IS_ENABLED(UNIT_TEST)
891struct lmb *lmb_get(void)
892{
893 return &lmb;
894}
895
896int lmb_push(struct lmb *store)
897{
898 int ret;
899
900 *store = lmb;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530901 ret = lmb_setup(true);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530902 if (ret)
903 return ret;
904
905 return 0;
906}
907
908void lmb_pop(struct lmb *store)
909{
910 alist_uninit(&lmb.free_mem);
911 alist_uninit(&lmb.used_mem);
912 lmb = *store;
913}
914#endif /* UNIT_TEST */