blob: 9af942c6b4284d01905b1f54673b8cfa1e6c2973 [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
Janne Grunau5631f442024-11-11 07:56:32 +010026/*
27 * The following low level LMB functions must not access the global LMB memory
28 * map since they are also used to manage IOVA memory maps in iommu drivers like
29 * apple_dart.
30 */
Kumar Gala6d7bfa82008-02-27 21:51:47 -060031
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010032static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
33 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060034{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010035 const phys_addr_t base1_end = base1 + size1 - 1;
36 const phys_addr_t base2_end = base2 + size2 - 1;
37
38 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala6d7bfa82008-02-27 21:51:47 -060039}
40
Becky Bruced26d67c2008-06-09 20:37:18 -050041static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010042 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060043{
44 if (base2 == base1 + size1)
45 return 1;
46 else if (base1 == base2 + size2)
47 return -1;
48
49 return 0;
50}
51
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053052static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1,
Udit Kumar4e8e6632023-09-26 16:54:42 +053053 unsigned long r2)
54{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053055 struct lmb_region *rgn = lmb_rgn_lst->data;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053056 phys_addr_t base1 = rgn[r1].base;
57 phys_size_t size1 = rgn[r1].size;
58 phys_addr_t base2 = rgn[r2].base;
59 phys_size_t size2 = rgn[r2].size;
Udit Kumar4e8e6632023-09-26 16:54:42 +053060
61 return lmb_addrs_overlap(base1, size1, base2, size2);
62}
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053063
64static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010065 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060066{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053067 struct lmb_region *rgn = lmb_rgn_lst->data;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053068 phys_addr_t base1 = rgn[r1].base;
69 phys_size_t size1 = rgn[r1].size;
70 phys_addr_t base2 = rgn[r2].base;
71 phys_size_t size2 = rgn[r2].size;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -060072
Kumar Gala6d7bfa82008-02-27 21:51:47 -060073 return lmb_addrs_adjacent(base1, size1, base2, size2);
74}
75
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053076static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060077{
78 unsigned long i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053079 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060080
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053081 for (i = r; i < lmb_rgn_lst->count - 1; i++) {
82 rgn[i].base = rgn[i + 1].base;
83 rgn[i].size = rgn[i + 1].size;
84 rgn[i].flags = rgn[i + 1].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060085 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053086 lmb_rgn_lst->count--;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060087}
88
89/* Assumption: base addr of region 1 < base addr of region 2 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053090static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010091 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060092{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053093 struct lmb_region *rgn = lmb_rgn_lst->data;
94
95 rgn[r1].size += rgn[r2].size;
96 lmb_remove_region(lmb_rgn_lst, r2);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060097}
98
Sughosh Ganu50e27272024-08-26 17:29:19 +053099static long lmb_resize_regions(struct alist *lmb_rgn_lst,
100 unsigned long idx_start,
101 phys_addr_t base, phys_size_t size)
102{
103 phys_size_t rgnsize;
104 unsigned long rgn_cnt, idx, idx_end;
105 phys_addr_t rgnbase, rgnend;
106 phys_addr_t mergebase, mergeend;
107 struct lmb_region *rgn = lmb_rgn_lst->data;
108
109 rgn_cnt = 0;
110 idx = idx_start;
111 idx_end = idx_start;
112
113 /*
114 * First thing to do is to identify how many regions
115 * the requested region overlaps.
116 * If the flags match, combine all these overlapping
117 * regions into a single region, and remove the merged
118 * regions.
119 */
120 while (idx <= lmb_rgn_lst->count - 1) {
121 rgnbase = rgn[idx].base;
122 rgnsize = rgn[idx].size;
123
124 if (lmb_addrs_overlap(base, size, rgnbase,
125 rgnsize)) {
126 if (rgn[idx].flags != LMB_NONE)
127 return -1;
128 rgn_cnt++;
129 idx_end = idx;
130 }
131 idx++;
132 }
133
134 /* The merged region's base and size */
135 rgnbase = rgn[idx_start].base;
136 mergebase = min(base, rgnbase);
137 rgnend = rgn[idx_end].base + rgn[idx_end].size;
138 mergeend = max(rgnend, (base + size));
139
140 rgn[idx_start].base = mergebase;
141 rgn[idx_start].size = mergeend - mergebase;
142
143 /* Now remove the merged regions */
144 while (--rgn_cnt)
145 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
146
147 return 0;
148}
149
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530150/**
151 * lmb_add_region_flags() - Add an lmb region to the given list
152 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
153 * @base: Start address of the region
154 * @size: Size of the region to be added
155 * @flags: Attributes of the LMB region
156 *
157 * Add a region of memory to the list. If the region does not exist, add
158 * it to the list. Depending on the attributes of the region to be added,
159 * the function might resize an already existing region or coalesce two
160 * adjacent regions.
161 *
Sam Protsenkob4f81102024-12-10 20:17:01 -0600162 * Return:
163 * * %0 - Added successfully, or it's already added (only if LMB_NONE)
164 * * %-EEXIST - The region is already added, and flags != LMB_NONE
165 * * %-1 - Failure
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530166 */
167static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200168 phys_size_t size, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600169{
170 unsigned long coalesced = 0;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530171 long ret, i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530172 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600173
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530174 if (alist_err(lmb_rgn_lst))
175 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600176
177 /* First try and coalesce this LMB with another. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530178 for (i = 0; i < lmb_rgn_lst->count; i++) {
179 phys_addr_t rgnbase = rgn[i].base;
180 phys_size_t rgnsize = rgn[i].size;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200181 u32 rgnflags = rgn[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600182
Sughosh Ganu50e27272024-08-26 17:29:19 +0530183 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
184 if (ret > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200185 if (flags != rgnflags)
186 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530187 rgn[i].base -= size;
188 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600189 coalesced++;
190 break;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530191 } else if (ret < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200192 if (flags != rgnflags)
Sughosh Ganu4ee65882025-03-03 19:02:28 +0530193 continue;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530194 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600195 coalesced++;
196 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100197 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sam Protsenkob4f81102024-12-10 20:17:01 -0600198 ret = lmb_resize_regions(lmb_rgn_lst, i, base, size);
199 if (ret < 0)
Sughosh Ganu50e27272024-08-26 17:29:19 +0530200 return -1;
Sam Protsenkob4f81102024-12-10 20:17:01 -0600201
202 coalesced++;
203 break;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600204
205 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600206 }
207 }
208
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530209 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
210 rgn = lmb_rgn_lst->data;
211 if (rgn[i].flags == rgn[i + 1].flags) {
212 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
213 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
214 coalesced++;
215 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
Sughosh Ganua92c5bb2025-03-03 19:02:27 +0530216 /* fix overlapping areas */
217 phys_addr_t rgnbase = rgn[i].base;
218 phys_size_t rgnsize = rgn[i].size;
219
220 ret = lmb_resize_regions(lmb_rgn_lst, i,
221 rgnbase, rgnsize);
222 if (ret < 0)
223 return -1;
224
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530225 coalesced++;
226 }
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200227 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600228 }
229
230 if (coalesced)
Ilias Apalodimas98491252024-10-23 18:22:00 +0300231 return 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530232
233 if (alist_full(lmb_rgn_lst) &&
234 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600235 return -1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530236 rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600237
238 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530239 for (i = lmb_rgn_lst->count; i >= 0; i--) {
240 if (i && base < rgn[i - 1].base) {
241 rgn[i] = rgn[i - 1];
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600242 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530243 rgn[i].base = base;
244 rgn[i].size = size;
245 rgn[i].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600246 break;
247 }
248 }
249
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530250 lmb_rgn_lst->count++;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600251
252 return 0;
253}
254
Janne Grunaud9c70402024-11-11 07:56:31 +0100255static long _lmb_free(struct alist *lmb_rgn_lst, phys_addr_t base,
256 phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500257{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530258 struct lmb_region *rgn;
Andy Fleming09d2a712008-07-07 14:24:39 -0500259 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100260 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500261 int i;
262
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600263 /* Suppress GCC warnings */
264 rgnbegin = 0;
265 rgnend = 0;
266
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530267 rgn = lmb_rgn_lst->data;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500268 /* Find the region where (base, size) belongs to */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530269 for (i = 0; i < lmb_rgn_lst->count; i++) {
270 rgnbegin = rgn[i].base;
271 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500272
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600273 if (rgnbegin <= base && end <= rgnend)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500274 break;
275 }
276
277 /* Didn't find the region */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530278 if (i == lmb_rgn_lst->count)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500279 return -1;
280
281 /* Check to see if we are removing entire region */
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600282 if (rgnbegin == base && rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530283 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500284 return 0;
285 }
286
287 /* Check to see if region is matching at the front */
288 if (rgnbegin == base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530289 rgn[i].base = end + 1;
290 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500291 return 0;
292 }
293
294 /* Check to see if the region is matching at the end */
295 if (rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530296 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500297 return 0;
298 }
299
300 /*
301 * We need to split the entry - adjust the current one to the
302 * beginging of the hole and add the region after hole.
303 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530304 rgn[i].size = base - rgn[i].base;
305 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
306 rgn[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500307}
308
Janne Grunau5631f442024-11-11 07:56:32 +0100309static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
310 phys_size_t size)
311{
312 unsigned long i;
313 struct lmb_region *rgn = lmb_rgn_lst->data;
314
315 for (i = 0; i < lmb_rgn_lst->count; i++) {
316 phys_addr_t rgnbase = rgn[i].base;
317 phys_size_t rgnsize = rgn[i].size;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600318
Janne Grunau5631f442024-11-11 07:56:32 +0100319 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
320 break;
321 }
322
323 return (i < lmb_rgn_lst->count) ? i : -1;
324}
325
Janne Grunau5631f442024-11-11 07:56:32 +0100326/*
Janne Grunaue818d3c2024-11-11 07:56:33 +0100327 * IOVA LMB memory maps using lmb pointers instead of the global LMB memory map.
328 */
329
330int io_lmb_setup(struct lmb *io_lmb)
331{
332 int ret;
333
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200334 ret = alist_init(&io_lmb->available_mem, sizeof(struct lmb_region),
Janne Grunaue818d3c2024-11-11 07:56:33 +0100335 (uint)LMB_ALIST_INITIAL_SIZE);
336 if (!ret) {
337 log_debug("Unable to initialise the list for LMB free IOVA\n");
338 return -ENOMEM;
339 }
340
341 ret = alist_init(&io_lmb->used_mem, sizeof(struct lmb_region),
342 (uint)LMB_ALIST_INITIAL_SIZE);
343 if (!ret) {
344 log_debug("Unable to initialise the list for LMB used IOVA\n");
345 return -ENOMEM;
346 }
347
348 io_lmb->test = false;
349
350 return 0;
351}
352
353void io_lmb_teardown(struct lmb *io_lmb)
354{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200355 alist_uninit(&io_lmb->available_mem);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100356 alist_uninit(&io_lmb->used_mem);
357}
358
359long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
360{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200361 return lmb_add_region_flags(&io_lmb->available_mem, base, size, LMB_NONE);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100362}
363
364/* derived and simplified from _lmb_alloc_base() */
365phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align)
366{
367 long i, rgn;
368 phys_addr_t base = 0;
369 phys_addr_t res_base;
370 struct lmb_region *lmb_used = io_lmb->used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200371 struct lmb_region *lmb_memory = io_lmb->available_mem.data;
Janne Grunaue818d3c2024-11-11 07:56:33 +0100372
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200373 for (i = io_lmb->available_mem.count - 1; i >= 0; i--) {
Janne Grunaue818d3c2024-11-11 07:56:33 +0100374 phys_addr_t lmbbase = lmb_memory[i].base;
375 phys_size_t lmbsize = lmb_memory[i].size;
376
377 if (lmbsize < size)
378 continue;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200379 base = ALIGN_DOWN(lmbbase + lmbsize - size, align);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100380
381 while (base && lmbbase <= base) {
382 rgn = lmb_overlaps_region(&io_lmb->used_mem, base, size);
383 if (rgn < 0) {
384 /* This area isn't reserved, take it */
385 if (lmb_add_region_flags(&io_lmb->used_mem, base,
386 size, LMB_NONE) < 0)
387 return 0;
388
389 return base;
390 }
391
392 res_base = lmb_used[rgn].base;
393 if (res_base < size)
394 break;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200395 base = ALIGN_DOWN(res_base - size, align);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100396 }
397 }
398 return 0;
399}
400
401long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
402{
403 return _lmb_free(&io_lmb->used_mem, base, size);
404}
405
406/*
Janne Grunau5631f442024-11-11 07:56:32 +0100407 * Low level LMB functions are used to manage IOVA memory maps for the Apple
408 * dart iommu. They must not access the global LMB memory map.
409 * So keep the global LMB variable declaration unreachable from them.
410 */
411
412static struct lmb lmb;
413
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100414static int lmb_map_update_notify(phys_addr_t addr, phys_size_t size,
415 enum lmb_map_op op, u32 flags)
Janne Grunau5631f442024-11-11 07:56:32 +0100416{
Heinrich Schuchardtfabdb732025-02-16 12:12:40 +0100417 if (CONFIG_IS_ENABLED(EFI_LOADER) &&
418 !lmb.test && !(flags & LMB_NONOTIFY))
419 return efi_map_update_notify(addr, size, op);
Janne Grunau5631f442024-11-11 07:56:32 +0100420
421 return 0;
422}
423
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200424static void lmb_print_region_flags(u32 flags)
Janne Grunau5631f442024-11-11 07:56:32 +0100425{
Sam Protsenko41bf8f02024-12-10 20:25:48 -0600426 const char * const flag_str[] = { "none", "no-map", "no-overwrite",
427 "no-notify" };
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100428 unsigned int pflags = flags &
429 (LMB_NOMAP | LMB_NOOVERWRITE | LMB_NONOTIFY);
430
431 if (flags != pflags) {
432 printf("invalid %#x\n", flags);
433 return;
434 }
Janne Grunau5631f442024-11-11 07:56:32 +0100435
436 do {
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100437 int bitpos = pflags ? fls(pflags) - 1 : 0;
438
Janne Grunau5631f442024-11-11 07:56:32 +0100439 printf("%s", flag_str[bitpos]);
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100440 pflags &= ~(1u << bitpos);
441 puts(pflags ? ", " : "\n");
442 } while (pflags);
Janne Grunau5631f442024-11-11 07:56:32 +0100443}
444
445static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
446{
447 struct lmb_region *rgn = lmb_rgn_lst->data;
448 unsigned long long base, size, end;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200449 u32 flags;
Janne Grunau5631f442024-11-11 07:56:32 +0100450 int i;
451
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100452 printf(" %s.count = %#x\n", name, lmb_rgn_lst->count);
Janne Grunau5631f442024-11-11 07:56:32 +0100453
454 for (i = 0; i < lmb_rgn_lst->count; i++) {
455 base = rgn[i].base;
456 size = rgn[i].size;
457 end = base + size - 1;
458 flags = rgn[i].flags;
459
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100460 printf(" %s[%d]\t[%#llx-%#llx], %#llx bytes, flags: ",
Janne Grunau5631f442024-11-11 07:56:32 +0100461 name, i, base, end, size);
462 lmb_print_region_flags(flags);
463 }
464}
465
466void lmb_dump_all_force(void)
467{
468 printf("lmb_dump_all:\n");
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200469 lmb_dump_region(&lmb.available_mem, "memory");
Janne Grunau5631f442024-11-11 07:56:32 +0100470 lmb_dump_region(&lmb.used_mem, "reserved");
471}
472
473void lmb_dump_all(void)
474{
475#ifdef DEBUG
476 lmb_dump_all_force();
477#endif
478}
479
480static void lmb_reserve_uboot_region(void)
481{
482 int bank;
483 ulong end, bank_end;
484 phys_addr_t rsv_start;
485
486 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
487 end = gd->ram_top;
488
489 /*
490 * Reserve memory from aligned address below the bottom of U-Boot stack
491 * until end of RAM area to prevent LMB from overwriting that memory.
492 */
493 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
494
495 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
496 if (!gd->bd->bi_dram[bank].size ||
497 rsv_start < gd->bd->bi_dram[bank].start)
498 continue;
499 /* Watch out for RAM at end of address space! */
500 bank_end = gd->bd->bi_dram[bank].start +
501 gd->bd->bi_dram[bank].size - 1;
502 if (rsv_start > bank_end)
503 continue;
504 if (bank_end > end)
505 bank_end = end - 1;
506
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200507 lmb_reserve(rsv_start, bank_end - rsv_start + 1, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100508
509 if (gd->flags & GD_FLG_SKIP_RELOC)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200510 lmb_reserve((phys_addr_t)(uintptr_t)_start,
511 gd->mon_len, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100512
513 break;
514 }
515}
516
517static void lmb_reserve_common(void *fdt_blob)
518{
519 lmb_reserve_uboot_region();
520
521 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
522 boot_fdt_add_mem_rsv_regions(fdt_blob);
523}
524
525static __maybe_unused void lmb_reserve_common_spl(void)
526{
527 phys_addr_t rsv_start;
528 phys_size_t rsv_size;
529
530 /*
531 * Assume a SPL stack of 16KB. This must be
532 * more than enough for the SPL stage.
533 */
534 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
535 rsv_start = gd->start_addr_sp - 16384;
536 rsv_size = 16384;
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200537 lmb_reserve(rsv_start, rsv_size, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100538 }
539
540 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
541 /* Reserve the bss region */
542 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
543 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
544 (phys_addr_t)(uintptr_t)__bss_start;
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200545 lmb_reserve(rsv_start, rsv_size, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100546 }
547}
548
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530549/**
550 * lmb_can_reserve_region() - check if the region can be reserved
551 * @base: base address of region to be reserved
552 * @size: size of region to be reserved
553 * @flags: flag of the region to be reserved
554 *
555 * Go through all the reserved regions and ensure that the requested
556 * region does not overlap with any existing regions. An overlap is
557 * allowed only when the flag of the request region and the existing
558 * region is LMB_NONE.
559 *
560 * Return: true if region can be reserved, false otherwise
561 */
562static bool lmb_can_reserve_region(phys_addr_t base, phys_size_t size,
563 u32 flags)
564{
565 uint i;
566 struct lmb_region *lmb_reserved = lmb.used_mem.data;
567
568 for (i = 0; i < lmb.used_mem.count; i++) {
569 u32 rgnflags = lmb_reserved[i].flags;
570 phys_addr_t rgnbase = lmb_reserved[i].base;
571 phys_size_t rgnsize = lmb_reserved[i].size;
572
573 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
574 if (flags != LMB_NONE || flags != rgnflags)
575 return false;
576 }
577 }
578
579 return true;
580}
581
Janne Grunau5631f442024-11-11 07:56:32 +0100582void lmb_add_memory(void)
583{
584 int i;
Sughosh Ganued220b82024-12-02 12:36:24 +0530585 phys_addr_t bank_end;
Janne Grunau5631f442024-11-11 07:56:32 +0100586 phys_size_t size;
587 u64 ram_top = gd->ram_top;
588 struct bd_info *bd = gd->bd;
589
590 if (CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP))
591 return lmb_arch_add_memory();
592
593 /* Assume a 4GB ram_top if not defined */
594 if (!ram_top)
595 ram_top = 0x100000000ULL;
596
597 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
598 size = bd->bi_dram[i].size;
Sughosh Ganued220b82024-12-02 12:36:24 +0530599 bank_end = bd->bi_dram[i].start + size;
600
Janne Grunau5631f442024-11-11 07:56:32 +0100601 if (size) {
602 lmb_add(bd->bi_dram[i].start, size);
603
604 /*
605 * Reserve memory above ram_top as
606 * no-overwrite so that it cannot be
607 * allocated
608 */
609 if (bd->bi_dram[i].start >= ram_top)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200610 lmb_reserve(bd->bi_dram[i].start, size,
611 LMB_NOOVERWRITE);
Sughosh Ganued220b82024-12-02 12:36:24 +0530612 else if (bank_end > ram_top)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200613 lmb_reserve(ram_top, bank_end - ram_top,
614 LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100615 }
616 }
617}
618
Janne Grunau5631f442024-11-11 07:56:32 +0100619/* This routine may be called with relocation disabled. */
620long lmb_add(phys_addr_t base, phys_size_t size)
621{
622 long ret;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200623 struct alist *lmb_rgn_lst = &lmb.available_mem;
Janne Grunau5631f442024-11-11 07:56:32 +0100624
Ilias Apalodimase2a6f972024-12-18 09:02:34 +0200625 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Janne Grunau5631f442024-11-11 07:56:32 +0100626 if (ret)
627 return ret;
628
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100629 return lmb_map_update_notify(base, size, LMB_MAP_OP_ADD, LMB_NONE);
Janne Grunau5631f442024-11-11 07:56:32 +0100630}
631
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530632long lmb_free_flags(phys_addr_t base, phys_size_t size,
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530633 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530634{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530635 long ret;
636
Janne Grunaud9c70402024-11-11 07:56:31 +0100637 ret = _lmb_free(&lmb.used_mem, base, size);
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530638 if (ret < 0)
639 return ret;
640
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100641 return lmb_map_update_notify(base, size, LMB_MAP_OP_FREE, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530642}
643
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530644long lmb_free(phys_addr_t base, phys_size_t size)
645{
646 return lmb_free_flags(base, size, LMB_NONE);
647}
648
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200649long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600650{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530651 long ret = 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530652 struct alist *lmb_rgn_lst = &lmb.used_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600653
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530654 if (!lmb_can_reserve_region(base, size, flags))
655 return -EEXIST;
656
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530657 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Ilias Apalodimas98491252024-10-23 18:22:00 +0300658 if (ret)
659 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530660
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100661 return lmb_map_update_notify(base, size, LMB_MAP_OP_RESERVE, flags);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200662}
663
Sughosh Ganu99b59662024-10-15 21:07:17 +0530664static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align,
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200665 phys_addr_t max_addr, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600666{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530667 int ret;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100668 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500669 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500670 phys_addr_t res_base;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530671 struct lmb_region *lmb_used = lmb.used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200672 struct lmb_region *lmb_memory = lmb.available_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600673
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200674 for (i = lmb.available_mem.count - 1; i >= 0; i--) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530675 phys_addr_t lmbbase = lmb_memory[i].base;
676 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600677
Andy Fleming78bd5a72008-06-16 13:58:55 -0500678 if (lmbsize < size)
679 continue;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600680
681 if (max_addr == LMB_ALLOC_ANYWHERE) {
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200682 base = ALIGN_DOWN(lmbbase + lmbsize - size, align);
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600683 } else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600684 base = lmbbase + lmbsize;
685 if (base < lmbbase)
686 base = -1;
687 base = min(base, max_addr);
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200688 base = ALIGN_DOWN(base - size, align);
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600689 } else {
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600690 continue;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600691 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600692
Andy Fleming78bd5a72008-06-16 13:58:55 -0500693 while (base && lmbbase <= base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530694 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100695 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500696 /* This area isn't reserved, take it */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530697 if (lmb_add_region_flags(&lmb.used_mem, base,
Ilias Apalodimas98491252024-10-23 18:22:00 +0300698 size, flags))
Andy Fleming78bd5a72008-06-16 13:58:55 -0500699 return 0;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530700
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300701 ret = lmb_map_update_notify(base, size,
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100702 LMB_MAP_OP_RESERVE,
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300703 flags);
704 if (ret)
705 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530706
Andy Fleming78bd5a72008-06-16 13:58:55 -0500707 return base;
708 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530709
710 res_base = lmb_used[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500711 if (res_base < size)
712 break;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200713 base = ALIGN_DOWN(res_base - size, align);
Andy Fleming78bd5a72008-06-16 13:58:55 -0500714 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600715 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500716 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600717}
718
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530719phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530720{
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200721 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE, LMB_NONE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530722}
723
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200724phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr,
725 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530726{
727 phys_addr_t alloc;
728
Sughosh Ganu99b59662024-10-15 21:07:17 +0530729 alloc = _lmb_alloc_base(size, align, max_addr, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530730
731 if (alloc == 0)
732 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
733 (ulong)size, (ulong)max_addr);
734
735 return alloc;
736}
737
Ilias Apalodimas31bf8f12024-12-18 09:02:37 +0200738phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100739{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100740 long rgn;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200741 struct lmb_region *lmb_memory = lmb.available_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100742
743 /* Check if the requested address is in one of the memory regions */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200744 rgn = lmb_overlaps_region(&lmb.available_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100745 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100746 /*
747 * Check if the requested end address is in the same memory
748 * region we found.
749 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530750 if (lmb_addrs_overlap(lmb_memory[rgn].base,
751 lmb_memory[rgn].size,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100752 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100753 /* ok, reserve the memory */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200754 if (!lmb_reserve(base, size, flags))
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100755 return base;
756 }
757 }
Sughosh Ganu50e27272024-08-26 17:29:19 +0530758
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100759 return 0;
760}
761
762/* Return number of bytes from a given address that are free */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530763phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100764{
765 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100766 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530767 struct lmb_region *lmb_used = lmb.used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200768 struct lmb_region *lmb_memory = lmb.available_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100769
770 /* check if the requested address is in the memory regions */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200771 rgn = lmb_overlaps_region(&lmb.available_mem, addr, 1);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100772 if (rgn >= 0) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530773 for (i = 0; i < lmb.used_mem.count; i++) {
774 if (addr < lmb_used[i].base) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100775 /* first reserved range > requested address */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530776 return lmb_used[i].base - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100777 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530778 if (lmb_used[i].base +
779 lmb_used[i].size > addr) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100780 /* requested addr is in this reserved range */
781 return 0;
782 }
783 }
784 /* if we come here: no reserved ranges above requested addr */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200785 return lmb_memory[lmb.available_mem.count - 1].base +
786 lmb_memory[lmb.available_mem.count - 1].size - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100787 }
788 return 0;
789}
790
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530791int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600792{
793 int i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530794 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600795
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530796 for (i = 0; i < lmb.used_mem.count; i++) {
797 phys_addr_t upper = lmb_used[i].base +
798 lmb_used[i].size - 1;
799 if (addr >= lmb_used[i].base && addr <= upper)
800 return (lmb_used[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600801 }
802 return 0;
803}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500804
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530805static int lmb_setup(bool test)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530806{
807 bool ret;
808
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200809 ret = alist_init(&lmb.available_mem, sizeof(struct lmb_region),
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530810 (uint)LMB_ALIST_INITIAL_SIZE);
811 if (!ret) {
812 log_debug("Unable to initialise the list for LMB free memory\n");
813 return -ENOMEM;
814 }
815
816 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
817 (uint)LMB_ALIST_INITIAL_SIZE);
818 if (!ret) {
819 log_debug("Unable to initialise the list for LMB used memory\n");
820 return -ENOMEM;
821 }
822
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530823 lmb.test = test;
824
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530825 return 0;
826}
827
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530828int lmb_init(void)
829{
830 int ret;
831
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530832 ret = lmb_setup(false);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530833 if (ret) {
834 log_info("Unable to init LMB\n");
835 return ret;
836 }
837
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530838 lmb_add_memory();
839
Sughosh Ganu6518b412024-08-26 17:29:24 +0530840 /* Reserve the U-Boot image region once U-Boot has relocated */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600841 if (xpl_phase() == PHASE_SPL)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530842 lmb_reserve_common_spl();
Simon Glassd4dce4a2024-09-29 19:49:36 -0600843 else if (xpl_phase() == PHASE_BOARD_R)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530844 lmb_reserve_common((void *)gd->fdt_blob);
845
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530846 return 0;
847}
848
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530849struct lmb *lmb_get(void)
850{
851 return &lmb;
852}
853
Simon Glass176eba52024-10-21 10:19:31 +0200854#if CONFIG_IS_ENABLED(UNIT_TEST)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530855int lmb_push(struct lmb *store)
856{
857 int ret;
858
859 *store = lmb;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530860 ret = lmb_setup(true);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530861 if (ret)
862 return ret;
863
864 return 0;
865}
866
867void lmb_pop(struct lmb *store)
868{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200869 alist_uninit(&lmb.available_mem);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530870 alist_uninit(&lmb.used_mem);
871 lmb = *store;
872}
873#endif /* UNIT_TEST */