blob: bb6f232f6bc50791ac1249b2938d08d301891190 [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 Ganu86b63942025-03-03 19:02:30 +053026#define LMB_RGN_OVERLAP 1
27#define LMB_RGN_ADJACENT 2
28
Janne Grunau5631f442024-11-11 07:56:32 +010029/*
30 * The following low level LMB functions must not access the global LMB memory
31 * map since they are also used to manage IOVA memory maps in iommu drivers like
32 * apple_dart.
33 */
Kumar Gala6d7bfa82008-02-27 21:51:47 -060034
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010035static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
36 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060037{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010038 const phys_addr_t base1_end = base1 + size1 - 1;
39 const phys_addr_t base2_end = base2 + size2 - 1;
40
41 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala6d7bfa82008-02-27 21:51:47 -060042}
43
Becky Bruced26d67c2008-06-09 20:37:18 -050044static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010045 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060046{
47 if (base2 == base1 + size1)
48 return 1;
49 else if (base1 == base2 + size2)
50 return -1;
51
52 return 0;
53}
54
Sughosh Ganu86b63942025-03-03 19:02:30 +053055/**
56 * lmb_regions_check() - Check if the regions overlap, or are adjacent
57 * @lmb_rgn_lst: List of LMB regions
58 * @r1: First region to check
59 * @r2: Second region to check
60 *
61 * Check if the two regions with matching flags, r1 and r2 are
62 * adjacent to each other, or if they overlap.
63 *
64 * Return:
65 * * %LMB_RGN_OVERLAP - Regions overlap
66 * * %LMB_RGN_ADJACENT - Regions adjacent to each other
67 * * 0 - Neither of the above, or flags mismatch
68 */
69static long lmb_regions_check(struct alist *lmb_rgn_lst, unsigned long r1,
70 unsigned long r2)
Udit Kumar4e8e6632023-09-26 16:54:42 +053071{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053072 struct lmb_region *rgn = lmb_rgn_lst->data;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053073 phys_addr_t base1 = rgn[r1].base;
74 phys_size_t size1 = rgn[r1].size;
75 phys_addr_t base2 = rgn[r2].base;
76 phys_size_t size2 = rgn[r2].size;
Udit Kumar4e8e6632023-09-26 16:54:42 +053077
Sughosh Ganu86b63942025-03-03 19:02:30 +053078 if (rgn[r1].flags != rgn[r2].flags)
79 return 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053080
Sughosh Ganu86b63942025-03-03 19:02:30 +053081 if (lmb_addrs_overlap(base1, size1, base2, size2))
82 return LMB_RGN_OVERLAP;
83 else if (lmb_addrs_adjacent(base1, size1, base2, size2))
84 return LMB_RGN_ADJACENT;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -060085
Sughosh Ganu86b63942025-03-03 19:02:30 +053086 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060087}
88
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053089static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060090{
91 unsigned long i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053092 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060093
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053094 for (i = r; i < lmb_rgn_lst->count - 1; i++) {
95 rgn[i].base = rgn[i + 1].base;
96 rgn[i].size = rgn[i + 1].size;
97 rgn[i].flags = rgn[i + 1].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060098 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053099 lmb_rgn_lst->count--;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600100}
101
102/* Assumption: base addr of region 1 < base addr of region 2 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530103static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100104 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600105{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530106 struct lmb_region *rgn = lmb_rgn_lst->data;
107
108 rgn[r1].size += rgn[r2].size;
109 lmb_remove_region(lmb_rgn_lst, r2);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600110}
111
Sughosh Ganu50e27272024-08-26 17:29:19 +0530112static long lmb_resize_regions(struct alist *lmb_rgn_lst,
113 unsigned long idx_start,
114 phys_addr_t base, phys_size_t size)
115{
116 phys_size_t rgnsize;
117 unsigned long rgn_cnt, idx, idx_end;
118 phys_addr_t rgnbase, rgnend;
119 phys_addr_t mergebase, mergeend;
120 struct lmb_region *rgn = lmb_rgn_lst->data;
121
122 rgn_cnt = 0;
123 idx = idx_start;
124 idx_end = idx_start;
125
126 /*
127 * First thing to do is to identify how many regions
128 * the requested region overlaps.
129 * If the flags match, combine all these overlapping
130 * regions into a single region, and remove the merged
131 * regions.
132 */
133 while (idx <= lmb_rgn_lst->count - 1) {
134 rgnbase = rgn[idx].base;
135 rgnsize = rgn[idx].size;
136
137 if (lmb_addrs_overlap(base, size, rgnbase,
138 rgnsize)) {
139 if (rgn[idx].flags != LMB_NONE)
140 return -1;
141 rgn_cnt++;
142 idx_end = idx;
143 }
144 idx++;
145 }
146
147 /* The merged region's base and size */
148 rgnbase = rgn[idx_start].base;
149 mergebase = min(base, rgnbase);
150 rgnend = rgn[idx_end].base + rgn[idx_end].size;
151 mergeend = max(rgnend, (base + size));
152
153 rgn[idx_start].base = mergebase;
154 rgn[idx_start].size = mergeend - mergebase;
155
156 /* Now remove the merged regions */
157 while (--rgn_cnt)
158 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
159
160 return 0;
161}
162
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530163/**
164 * lmb_add_region_flags() - Add an lmb region to the given list
165 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
166 * @base: Start address of the region
167 * @size: Size of the region to be added
168 * @flags: Attributes of the LMB region
169 *
170 * Add a region of memory to the list. If the region does not exist, add
171 * it to the list. Depending on the attributes of the region to be added,
172 * the function might resize an already existing region or coalesce two
173 * adjacent regions.
174 *
Sam Protsenkob4f81102024-12-10 20:17:01 -0600175 * Return:
176 * * %0 - Added successfully, or it's already added (only if LMB_NONE)
177 * * %-EEXIST - The region is already added, and flags != LMB_NONE
178 * * %-1 - Failure
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530179 */
180static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200181 phys_size_t size, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600182{
183 unsigned long coalesced = 0;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530184 long ret, i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530185 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600186
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530187 if (alist_err(lmb_rgn_lst))
188 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600189
190 /* First try and coalesce this LMB with another. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530191 for (i = 0; i < lmb_rgn_lst->count; i++) {
192 phys_addr_t rgnbase = rgn[i].base;
193 phys_size_t rgnsize = rgn[i].size;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200194 u32 rgnflags = rgn[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600195
Sughosh Ganu50e27272024-08-26 17:29:19 +0530196 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
197 if (ret > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200198 if (flags != rgnflags)
199 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530200 rgn[i].base -= size;
201 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600202 coalesced++;
203 break;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530204 } else if (ret < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200205 if (flags != rgnflags)
Sughosh Ganu4ee65882025-03-03 19:02:28 +0530206 continue;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530207 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600208 coalesced++;
209 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100210 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sam Protsenkob4f81102024-12-10 20:17:01 -0600211 ret = lmb_resize_regions(lmb_rgn_lst, i, base, size);
212 if (ret < 0)
Sughosh Ganu50e27272024-08-26 17:29:19 +0530213 return -1;
Sam Protsenkob4f81102024-12-10 20:17:01 -0600214
215 coalesced++;
216 break;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600217
218 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600219 }
220 }
221
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530222 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
Sughosh Ganu86b63942025-03-03 19:02:30 +0530223 ret = lmb_regions_check(lmb_rgn_lst, i, i + 1);
224 if (ret == LMB_RGN_ADJACENT) {
225 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
226 coalesced++;
227 } else if (ret == LMB_RGN_OVERLAP) {
228 /* fix overlapping areas */
229 phys_addr_t rgnbase = rgn[i].base;
230 phys_size_t rgnsize = rgn[i].size;
Sughosh Ganua92c5bb2025-03-03 19:02:27 +0530231
Sughosh Ganu86b63942025-03-03 19:02:30 +0530232 ret = lmb_resize_regions(lmb_rgn_lst, i,
233 rgnbase, rgnsize);
234 if (ret < 0)
235 return -1;
Sughosh Ganua92c5bb2025-03-03 19:02:27 +0530236
Sughosh Ganu86b63942025-03-03 19:02:30 +0530237 coalesced++;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200238 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600239 }
240
241 if (coalesced)
Ilias Apalodimas98491252024-10-23 18:22:00 +0300242 return 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530243
244 if (alist_full(lmb_rgn_lst) &&
245 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600246 return -1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530247 rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600248
249 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530250 for (i = lmb_rgn_lst->count; i >= 0; i--) {
251 if (i && base < rgn[i - 1].base) {
252 rgn[i] = rgn[i - 1];
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600253 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530254 rgn[i].base = base;
255 rgn[i].size = size;
256 rgn[i].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600257 break;
258 }
259 }
260
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530261 lmb_rgn_lst->count++;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600262
263 return 0;
264}
265
Janne Grunaud9c70402024-11-11 07:56:31 +0100266static long _lmb_free(struct alist *lmb_rgn_lst, phys_addr_t base,
267 phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500268{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530269 struct lmb_region *rgn;
Andy Fleming09d2a712008-07-07 14:24:39 -0500270 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100271 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500272 int i;
273
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600274 /* Suppress GCC warnings */
275 rgnbegin = 0;
276 rgnend = 0;
277
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530278 rgn = lmb_rgn_lst->data;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500279 /* Find the region where (base, size) belongs to */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530280 for (i = 0; i < lmb_rgn_lst->count; i++) {
281 rgnbegin = rgn[i].base;
282 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500283
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600284 if (rgnbegin <= base && end <= rgnend)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500285 break;
286 }
287
288 /* Didn't find the region */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530289 if (i == lmb_rgn_lst->count)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500290 return -1;
291
292 /* Check to see if we are removing entire region */
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600293 if (rgnbegin == base && rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530294 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500295 return 0;
296 }
297
298 /* Check to see if region is matching at the front */
299 if (rgnbegin == base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530300 rgn[i].base = end + 1;
301 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500302 return 0;
303 }
304
305 /* Check to see if the region is matching at the end */
306 if (rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530307 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500308 return 0;
309 }
310
311 /*
312 * We need to split the entry - adjust the current one to the
313 * beginging of the hole and add the region after hole.
314 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530315 rgn[i].size = base - rgn[i].base;
316 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
317 rgn[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500318}
319
Janne Grunau5631f442024-11-11 07:56:32 +0100320static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
321 phys_size_t size)
322{
323 unsigned long i;
324 struct lmb_region *rgn = lmb_rgn_lst->data;
325
326 for (i = 0; i < lmb_rgn_lst->count; i++) {
327 phys_addr_t rgnbase = rgn[i].base;
328 phys_size_t rgnsize = rgn[i].size;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600329
Janne Grunau5631f442024-11-11 07:56:32 +0100330 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
331 break;
332 }
333
334 return (i < lmb_rgn_lst->count) ? i : -1;
335}
336
Janne Grunau5631f442024-11-11 07:56:32 +0100337/*
Janne Grunaue818d3c2024-11-11 07:56:33 +0100338 * IOVA LMB memory maps using lmb pointers instead of the global LMB memory map.
339 */
340
341int io_lmb_setup(struct lmb *io_lmb)
342{
343 int ret;
344
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200345 ret = alist_init(&io_lmb->available_mem, sizeof(struct lmb_region),
Janne Grunaue818d3c2024-11-11 07:56:33 +0100346 (uint)LMB_ALIST_INITIAL_SIZE);
347 if (!ret) {
348 log_debug("Unable to initialise the list for LMB free IOVA\n");
349 return -ENOMEM;
350 }
351
352 ret = alist_init(&io_lmb->used_mem, sizeof(struct lmb_region),
353 (uint)LMB_ALIST_INITIAL_SIZE);
354 if (!ret) {
355 log_debug("Unable to initialise the list for LMB used IOVA\n");
356 return -ENOMEM;
357 }
358
359 io_lmb->test = false;
360
361 return 0;
362}
363
364void io_lmb_teardown(struct lmb *io_lmb)
365{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200366 alist_uninit(&io_lmb->available_mem);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100367 alist_uninit(&io_lmb->used_mem);
368}
369
370long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
371{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200372 return lmb_add_region_flags(&io_lmb->available_mem, base, size, LMB_NONE);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100373}
374
375/* derived and simplified from _lmb_alloc_base() */
376phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align)
377{
378 long i, rgn;
379 phys_addr_t base = 0;
380 phys_addr_t res_base;
381 struct lmb_region *lmb_used = io_lmb->used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200382 struct lmb_region *lmb_memory = io_lmb->available_mem.data;
Janne Grunaue818d3c2024-11-11 07:56:33 +0100383
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200384 for (i = io_lmb->available_mem.count - 1; i >= 0; i--) {
Janne Grunaue818d3c2024-11-11 07:56:33 +0100385 phys_addr_t lmbbase = lmb_memory[i].base;
386 phys_size_t lmbsize = lmb_memory[i].size;
387
388 if (lmbsize < size)
389 continue;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200390 base = ALIGN_DOWN(lmbbase + lmbsize - size, align);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100391
392 while (base && lmbbase <= base) {
393 rgn = lmb_overlaps_region(&io_lmb->used_mem, base, size);
394 if (rgn < 0) {
395 /* This area isn't reserved, take it */
396 if (lmb_add_region_flags(&io_lmb->used_mem, base,
397 size, LMB_NONE) < 0)
398 return 0;
399
400 return base;
401 }
402
403 res_base = lmb_used[rgn].base;
404 if (res_base < size)
405 break;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200406 base = ALIGN_DOWN(res_base - size, align);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100407 }
408 }
409 return 0;
410}
411
412long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
413{
414 return _lmb_free(&io_lmb->used_mem, base, size);
415}
416
417/*
Janne Grunau5631f442024-11-11 07:56:32 +0100418 * Low level LMB functions are used to manage IOVA memory maps for the Apple
419 * dart iommu. They must not access the global LMB memory map.
420 * So keep the global LMB variable declaration unreachable from them.
421 */
422
423static struct lmb lmb;
424
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100425static int lmb_map_update_notify(phys_addr_t addr, phys_size_t size,
426 enum lmb_map_op op, u32 flags)
Janne Grunau5631f442024-11-11 07:56:32 +0100427{
Heinrich Schuchardtfabdb732025-02-16 12:12:40 +0100428 if (CONFIG_IS_ENABLED(EFI_LOADER) &&
429 !lmb.test && !(flags & LMB_NONOTIFY))
430 return efi_map_update_notify(addr, size, op);
Janne Grunau5631f442024-11-11 07:56:32 +0100431
432 return 0;
433}
434
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200435static void lmb_print_region_flags(u32 flags)
Janne Grunau5631f442024-11-11 07:56:32 +0100436{
Sam Protsenko41bf8f02024-12-10 20:25:48 -0600437 const char * const flag_str[] = { "none", "no-map", "no-overwrite",
438 "no-notify" };
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100439 unsigned int pflags = flags &
440 (LMB_NOMAP | LMB_NOOVERWRITE | LMB_NONOTIFY);
441
442 if (flags != pflags) {
443 printf("invalid %#x\n", flags);
444 return;
445 }
Janne Grunau5631f442024-11-11 07:56:32 +0100446
447 do {
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100448 int bitpos = pflags ? fls(pflags) - 1 : 0;
449
Janne Grunau5631f442024-11-11 07:56:32 +0100450 printf("%s", flag_str[bitpos]);
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100451 pflags &= ~(1u << bitpos);
452 puts(pflags ? ", " : "\n");
453 } while (pflags);
Janne Grunau5631f442024-11-11 07:56:32 +0100454}
455
456static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
457{
458 struct lmb_region *rgn = lmb_rgn_lst->data;
459 unsigned long long base, size, end;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200460 u32 flags;
Janne Grunau5631f442024-11-11 07:56:32 +0100461 int i;
462
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100463 printf(" %s.count = %#x\n", name, lmb_rgn_lst->count);
Janne Grunau5631f442024-11-11 07:56:32 +0100464
465 for (i = 0; i < lmb_rgn_lst->count; i++) {
466 base = rgn[i].base;
467 size = rgn[i].size;
468 end = base + size - 1;
469 flags = rgn[i].flags;
470
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100471 printf(" %s[%d]\t[%#llx-%#llx], %#llx bytes, flags: ",
Janne Grunau5631f442024-11-11 07:56:32 +0100472 name, i, base, end, size);
473 lmb_print_region_flags(flags);
474 }
475}
476
477void lmb_dump_all_force(void)
478{
479 printf("lmb_dump_all:\n");
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200480 lmb_dump_region(&lmb.available_mem, "memory");
Janne Grunau5631f442024-11-11 07:56:32 +0100481 lmb_dump_region(&lmb.used_mem, "reserved");
482}
483
484void lmb_dump_all(void)
485{
486#ifdef DEBUG
487 lmb_dump_all_force();
488#endif
489}
490
491static void lmb_reserve_uboot_region(void)
492{
493 int bank;
494 ulong end, bank_end;
495 phys_addr_t rsv_start;
496
497 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
498 end = gd->ram_top;
499
500 /*
501 * Reserve memory from aligned address below the bottom of U-Boot stack
502 * until end of RAM area to prevent LMB from overwriting that memory.
503 */
504 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
505
506 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
507 if (!gd->bd->bi_dram[bank].size ||
508 rsv_start < gd->bd->bi_dram[bank].start)
509 continue;
510 /* Watch out for RAM at end of address space! */
511 bank_end = gd->bd->bi_dram[bank].start +
512 gd->bd->bi_dram[bank].size - 1;
513 if (rsv_start > bank_end)
514 continue;
515 if (bank_end > end)
516 bank_end = end - 1;
517
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200518 lmb_reserve(rsv_start, bank_end - rsv_start + 1, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100519
520 if (gd->flags & GD_FLG_SKIP_RELOC)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200521 lmb_reserve((phys_addr_t)(uintptr_t)_start,
522 gd->mon_len, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100523
524 break;
525 }
526}
527
528static void lmb_reserve_common(void *fdt_blob)
529{
530 lmb_reserve_uboot_region();
531
532 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
533 boot_fdt_add_mem_rsv_regions(fdt_blob);
534}
535
536static __maybe_unused void lmb_reserve_common_spl(void)
537{
538 phys_addr_t rsv_start;
539 phys_size_t rsv_size;
540
541 /*
542 * Assume a SPL stack of 16KB. This must be
543 * more than enough for the SPL stage.
544 */
545 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
546 rsv_start = gd->start_addr_sp - 16384;
547 rsv_size = 16384;
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200548 lmb_reserve(rsv_start, rsv_size, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100549 }
550
551 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
552 /* Reserve the bss region */
553 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
554 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
555 (phys_addr_t)(uintptr_t)__bss_start;
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200556 lmb_reserve(rsv_start, rsv_size, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100557 }
558}
559
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530560/**
561 * lmb_can_reserve_region() - check if the region can be reserved
562 * @base: base address of region to be reserved
563 * @size: size of region to be reserved
564 * @flags: flag of the region to be reserved
565 *
566 * Go through all the reserved regions and ensure that the requested
567 * region does not overlap with any existing regions. An overlap is
568 * allowed only when the flag of the request region and the existing
569 * region is LMB_NONE.
570 *
571 * Return: true if region can be reserved, false otherwise
572 */
573static bool lmb_can_reserve_region(phys_addr_t base, phys_size_t size,
574 u32 flags)
575{
576 uint i;
577 struct lmb_region *lmb_reserved = lmb.used_mem.data;
578
579 for (i = 0; i < lmb.used_mem.count; i++) {
580 u32 rgnflags = lmb_reserved[i].flags;
581 phys_addr_t rgnbase = lmb_reserved[i].base;
582 phys_size_t rgnsize = lmb_reserved[i].size;
583
584 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
585 if (flags != LMB_NONE || flags != rgnflags)
586 return false;
587 }
588 }
589
590 return true;
591}
592
Janne Grunau5631f442024-11-11 07:56:32 +0100593void lmb_add_memory(void)
594{
595 int i;
Sughosh Ganued220b82024-12-02 12:36:24 +0530596 phys_addr_t bank_end;
Janne Grunau5631f442024-11-11 07:56:32 +0100597 phys_size_t size;
598 u64 ram_top = gd->ram_top;
599 struct bd_info *bd = gd->bd;
600
601 if (CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP))
602 return lmb_arch_add_memory();
603
604 /* Assume a 4GB ram_top if not defined */
605 if (!ram_top)
606 ram_top = 0x100000000ULL;
607
608 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
609 size = bd->bi_dram[i].size;
Sughosh Ganued220b82024-12-02 12:36:24 +0530610 bank_end = bd->bi_dram[i].start + size;
611
Janne Grunau5631f442024-11-11 07:56:32 +0100612 if (size) {
613 lmb_add(bd->bi_dram[i].start, size);
614
615 /*
616 * Reserve memory above ram_top as
617 * no-overwrite so that it cannot be
618 * allocated
619 */
620 if (bd->bi_dram[i].start >= ram_top)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200621 lmb_reserve(bd->bi_dram[i].start, size,
622 LMB_NOOVERWRITE);
Sughosh Ganued220b82024-12-02 12:36:24 +0530623 else if (bank_end > ram_top)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200624 lmb_reserve(ram_top, bank_end - ram_top,
625 LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100626 }
627 }
628}
629
Janne Grunau5631f442024-11-11 07:56:32 +0100630/* This routine may be called with relocation disabled. */
631long lmb_add(phys_addr_t base, phys_size_t size)
632{
633 long ret;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200634 struct alist *lmb_rgn_lst = &lmb.available_mem;
Janne Grunau5631f442024-11-11 07:56:32 +0100635
Ilias Apalodimase2a6f972024-12-18 09:02:34 +0200636 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Janne Grunau5631f442024-11-11 07:56:32 +0100637 if (ret)
638 return ret;
639
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100640 return lmb_map_update_notify(base, size, LMB_MAP_OP_ADD, LMB_NONE);
Janne Grunau5631f442024-11-11 07:56:32 +0100641}
642
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530643long lmb_free_flags(phys_addr_t base, phys_size_t size,
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530644 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530645{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530646 long ret;
647
Janne Grunaud9c70402024-11-11 07:56:31 +0100648 ret = _lmb_free(&lmb.used_mem, base, size);
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530649 if (ret < 0)
650 return ret;
651
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100652 return lmb_map_update_notify(base, size, LMB_MAP_OP_FREE, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530653}
654
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530655long lmb_free(phys_addr_t base, phys_size_t size)
656{
657 return lmb_free_flags(base, size, LMB_NONE);
658}
659
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200660long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600661{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530662 long ret = 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530663 struct alist *lmb_rgn_lst = &lmb.used_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600664
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530665 if (!lmb_can_reserve_region(base, size, flags))
666 return -EEXIST;
667
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530668 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Ilias Apalodimas98491252024-10-23 18:22:00 +0300669 if (ret)
670 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530671
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100672 return lmb_map_update_notify(base, size, LMB_MAP_OP_RESERVE, flags);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200673}
674
Sughosh Ganu99b59662024-10-15 21:07:17 +0530675static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align,
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200676 phys_addr_t max_addr, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600677{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530678 int ret;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100679 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500680 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500681 phys_addr_t res_base;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530682 struct lmb_region *lmb_used = lmb.used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200683 struct lmb_region *lmb_memory = lmb.available_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600684
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200685 for (i = lmb.available_mem.count - 1; i >= 0; i--) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530686 phys_addr_t lmbbase = lmb_memory[i].base;
687 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600688
Andy Fleming78bd5a72008-06-16 13:58:55 -0500689 if (lmbsize < size)
690 continue;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600691
692 if (max_addr == LMB_ALLOC_ANYWHERE) {
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200693 base = ALIGN_DOWN(lmbbase + lmbsize - size, align);
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600694 } else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600695 base = lmbbase + lmbsize;
696 if (base < lmbbase)
697 base = -1;
698 base = min(base, max_addr);
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200699 base = ALIGN_DOWN(base - size, align);
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600700 } else {
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600701 continue;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600702 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600703
Andy Fleming78bd5a72008-06-16 13:58:55 -0500704 while (base && lmbbase <= base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530705 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100706 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500707 /* This area isn't reserved, take it */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530708 if (lmb_add_region_flags(&lmb.used_mem, base,
Ilias Apalodimas98491252024-10-23 18:22:00 +0300709 size, flags))
Andy Fleming78bd5a72008-06-16 13:58:55 -0500710 return 0;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530711
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300712 ret = lmb_map_update_notify(base, size,
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100713 LMB_MAP_OP_RESERVE,
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300714 flags);
715 if (ret)
716 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530717
Andy Fleming78bd5a72008-06-16 13:58:55 -0500718 return base;
719 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530720
721 res_base = lmb_used[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500722 if (res_base < size)
723 break;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200724 base = ALIGN_DOWN(res_base - size, align);
Andy Fleming78bd5a72008-06-16 13:58:55 -0500725 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600726 }
Sughosh Ganu2c85ba22025-03-03 19:02:31 +0530727
728 log_debug("%s: Failed to allocate 0x%lx bytes below 0x%lx\n",
729 __func__, (ulong)size, (ulong)max_addr);
730
Andy Fleming78bd5a72008-06-16 13:58:55 -0500731 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600732}
733
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530734phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530735{
Sughosh Ganu2c85ba22025-03-03 19:02:31 +0530736 return _lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE, LMB_NONE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530737}
738
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200739phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr,
740 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530741{
Sughosh Ganu2c85ba22025-03-03 19:02:31 +0530742 return _lmb_alloc_base(size, align, max_addr, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530743}
744
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200745int lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100746{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100747 long rgn;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200748 struct lmb_region *lmb_memory = lmb.available_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100749
750 /* Check if the requested address is in one of the memory regions */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200751 rgn = lmb_overlaps_region(&lmb.available_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100752 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100753 /*
754 * Check if the requested end address is in the same memory
755 * region we found.
756 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530757 if (lmb_addrs_overlap(lmb_memory[rgn].base,
758 lmb_memory[rgn].size,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100759 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100760 /* ok, reserve the memory */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200761 if (!lmb_reserve(base, size, flags))
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200762 return 0;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100763 }
764 }
Sughosh Ganu50e27272024-08-26 17:29:19 +0530765
Ilias Apalodimas299c87d2025-03-14 12:57:02 +0200766 return -1;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100767}
768
769/* Return number of bytes from a given address that are free */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530770phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100771{
772 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100773 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530774 struct lmb_region *lmb_used = lmb.used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200775 struct lmb_region *lmb_memory = lmb.available_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100776
777 /* check if the requested address is in the memory regions */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200778 rgn = lmb_overlaps_region(&lmb.available_mem, addr, 1);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100779 if (rgn >= 0) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530780 for (i = 0; i < lmb.used_mem.count; i++) {
781 if (addr < lmb_used[i].base) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100782 /* first reserved range > requested address */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530783 return lmb_used[i].base - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100784 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530785 if (lmb_used[i].base +
786 lmb_used[i].size > addr) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100787 /* requested addr is in this reserved range */
788 return 0;
789 }
790 }
791 /* if we come here: no reserved ranges above requested addr */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200792 return lmb_memory[lmb.available_mem.count - 1].base +
793 lmb_memory[lmb.available_mem.count - 1].size - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100794 }
795 return 0;
796}
797
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530798int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600799{
800 int i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530801 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600802
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530803 for (i = 0; i < lmb.used_mem.count; i++) {
804 phys_addr_t upper = lmb_used[i].base +
805 lmb_used[i].size - 1;
806 if (addr >= lmb_used[i].base && addr <= upper)
807 return (lmb_used[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600808 }
809 return 0;
810}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500811
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530812static int lmb_setup(bool test)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530813{
814 bool ret;
815
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200816 ret = alist_init(&lmb.available_mem, sizeof(struct lmb_region),
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530817 (uint)LMB_ALIST_INITIAL_SIZE);
818 if (!ret) {
819 log_debug("Unable to initialise the list for LMB free memory\n");
820 return -ENOMEM;
821 }
822
823 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
824 (uint)LMB_ALIST_INITIAL_SIZE);
825 if (!ret) {
826 log_debug("Unable to initialise the list for LMB used memory\n");
827 return -ENOMEM;
828 }
829
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530830 lmb.test = test;
831
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530832 return 0;
833}
834
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530835int lmb_init(void)
836{
837 int ret;
838
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530839 ret = lmb_setup(false);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530840 if (ret) {
841 log_info("Unable to init LMB\n");
842 return ret;
843 }
844
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530845 lmb_add_memory();
846
Sughosh Ganu6518b412024-08-26 17:29:24 +0530847 /* Reserve the U-Boot image region once U-Boot has relocated */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600848 if (xpl_phase() == PHASE_SPL)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530849 lmb_reserve_common_spl();
Simon Glassd4dce4a2024-09-29 19:49:36 -0600850 else if (xpl_phase() == PHASE_BOARD_R)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530851 lmb_reserve_common((void *)gd->fdt_blob);
852
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530853 return 0;
854}
855
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530856struct lmb *lmb_get(void)
857{
858 return &lmb;
859}
860
Simon Glass176eba52024-10-21 10:19:31 +0200861#if CONFIG_IS_ENABLED(UNIT_TEST)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530862int lmb_push(struct lmb *store)
863{
864 int ret;
865
866 *store = lmb;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530867 ret = lmb_setup(true);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530868 if (ret)
869 return ret;
870
871 return 0;
872}
873
874void lmb_pop(struct lmb *store)
875{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200876 alist_uninit(&lmb.available_mem);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530877 alist_uninit(&lmb.used_mem);
878 lmb = *store;
879}
880#endif /* UNIT_TEST */