blob: 14b9b8466ff29f9b02cbc03686b10eddf805df4f [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
Janne Grunau5631f442024-11-11 07:56:32 +010030/*
31 * The following low level LMB functions must not access the global LMB memory
32 * map since they are also used to manage IOVA memory maps in iommu drivers like
33 * apple_dart.
34 */
Kumar Gala6d7bfa82008-02-27 21:51:47 -060035
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010036static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
37 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060038{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010039 const phys_addr_t base1_end = base1 + size1 - 1;
40 const phys_addr_t base2_end = base2 + size2 - 1;
41
42 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala6d7bfa82008-02-27 21:51:47 -060043}
44
Becky Bruced26d67c2008-06-09 20:37:18 -050045static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010046 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060047{
48 if (base2 == base1 + size1)
49 return 1;
50 else if (base1 == base2 + size2)
51 return -1;
52
53 return 0;
54}
55
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053056static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1,
Udit Kumar4e8e6632023-09-26 16:54:42 +053057 unsigned long r2)
58{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053059 struct lmb_region *rgn = lmb_rgn_lst->data;
60
61 phys_addr_t base1 = rgn[r1].base;
62 phys_size_t size1 = rgn[r1].size;
63 phys_addr_t base2 = rgn[r2].base;
64 phys_size_t size2 = rgn[r2].size;
Udit Kumar4e8e6632023-09-26 16:54:42 +053065
66 return lmb_addrs_overlap(base1, size1, base2, size2);
67}
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053068
69static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010070 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060071{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053072 struct lmb_region *rgn = lmb_rgn_lst->data;
73
74 phys_addr_t base1 = rgn[r1].base;
75 phys_size_t size1 = rgn[r1].size;
76 phys_addr_t base2 = rgn[r2].base;
77 phys_size_t size2 = rgn[r2].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060078 return lmb_addrs_adjacent(base1, size1, base2, size2);
79}
80
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053081static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060082{
83 unsigned long i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053084 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060085
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053086 for (i = r; i < lmb_rgn_lst->count - 1; i++) {
87 rgn[i].base = rgn[i + 1].base;
88 rgn[i].size = rgn[i + 1].size;
89 rgn[i].flags = rgn[i + 1].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060090 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053091 lmb_rgn_lst->count--;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060092}
93
94/* Assumption: base addr of region 1 < base addr of region 2 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053095static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010096 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060097{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053098 struct lmb_region *rgn = lmb_rgn_lst->data;
99
100 rgn[r1].size += rgn[r2].size;
101 lmb_remove_region(lmb_rgn_lst, r2);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600102}
103
Udit Kumar4e8e6632023-09-26 16:54:42 +0530104/*Assumption : base addr of region 1 < base addr of region 2*/
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530105static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst,
106 unsigned long r1, unsigned long r2)
Udit Kumar4e8e6632023-09-26 16:54:42 +0530107{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530108 struct lmb_region *rgn = lmb_rgn_lst->data;
109
110 phys_addr_t base1 = rgn[r1].base;
111 phys_size_t size1 = rgn[r1].size;
112 phys_addr_t base2 = rgn[r2].base;
113 phys_size_t size2 = rgn[r2].size;
Udit Kumar4e8e6632023-09-26 16:54:42 +0530114
115 if (base1 + size1 > base2 + size2) {
116 printf("This will not be a case any time\n");
117 return;
118 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530119 rgn[r1].size = base2 + size2 - base1;
120 lmb_remove_region(lmb_rgn_lst, r2);
Udit Kumar4e8e6632023-09-26 16:54:42 +0530121}
122
Sughosh Ganu50e27272024-08-26 17:29:19 +0530123static long lmb_resize_regions(struct alist *lmb_rgn_lst,
124 unsigned long idx_start,
125 phys_addr_t base, phys_size_t size)
126{
127 phys_size_t rgnsize;
128 unsigned long rgn_cnt, idx, idx_end;
129 phys_addr_t rgnbase, rgnend;
130 phys_addr_t mergebase, mergeend;
131 struct lmb_region *rgn = lmb_rgn_lst->data;
132
133 rgn_cnt = 0;
134 idx = idx_start;
135 idx_end = idx_start;
136
137 /*
138 * First thing to do is to identify how many regions
139 * the requested region overlaps.
140 * If the flags match, combine all these overlapping
141 * regions into a single region, and remove the merged
142 * regions.
143 */
144 while (idx <= lmb_rgn_lst->count - 1) {
145 rgnbase = rgn[idx].base;
146 rgnsize = rgn[idx].size;
147
148 if (lmb_addrs_overlap(base, size, rgnbase,
149 rgnsize)) {
150 if (rgn[idx].flags != LMB_NONE)
151 return -1;
152 rgn_cnt++;
153 idx_end = idx;
154 }
155 idx++;
156 }
157
158 /* The merged region's base and size */
159 rgnbase = rgn[idx_start].base;
160 mergebase = min(base, rgnbase);
161 rgnend = rgn[idx_end].base + rgn[idx_end].size;
162 mergeend = max(rgnend, (base + size));
163
164 rgn[idx_start].base = mergebase;
165 rgn[idx_start].size = mergeend - mergebase;
166
167 /* Now remove the merged regions */
168 while (--rgn_cnt)
169 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
170
171 return 0;
172}
173
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530174/**
175 * lmb_add_region_flags() - Add an lmb region to the given list
176 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
177 * @base: Start address of the region
178 * @size: Size of the region to be added
179 * @flags: Attributes of the LMB region
180 *
181 * Add a region of memory to the list. If the region does not exist, add
182 * it to the list. Depending on the attributes of the region to be added,
183 * the function might resize an already existing region or coalesce two
184 * adjacent regions.
185 *
186 *
187 * Returns: 0 if the region addition successful, -1 on failure
188 */
189static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200190 phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600191{
192 unsigned long coalesced = 0;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530193 long ret, i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530194 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600195
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530196 if (alist_err(lmb_rgn_lst))
197 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600198
199 /* First try and coalesce this LMB with another. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530200 for (i = 0; i < lmb_rgn_lst->count; i++) {
201 phys_addr_t rgnbase = rgn[i].base;
202 phys_size_t rgnsize = rgn[i].size;
203 phys_size_t rgnflags = rgn[i].flags;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100204 phys_addr_t end = base + size - 1;
205 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100206 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200207 if (flags == rgnflags)
208 /* Already have this region, so we're done */
209 return 0;
210 else
211 return -1; /* regions with new flags */
212 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600213
Sughosh Ganu50e27272024-08-26 17:29:19 +0530214 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
215 if (ret > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200216 if (flags != rgnflags)
217 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530218 rgn[i].base -= size;
219 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600220 coalesced++;
221 break;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530222 } else if (ret < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200223 if (flags != rgnflags)
224 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530225 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600226 coalesced++;
227 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100228 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sughosh Ganu50e27272024-08-26 17:29:19 +0530229 if (flags == LMB_NONE) {
230 ret = lmb_resize_regions(lmb_rgn_lst, i, base,
231 size);
232 if (ret < 0)
233 return -1;
234
235 coalesced++;
236 break;
237 } else {
238 return -1;
239 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600240 }
241 }
242
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530243 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
244 rgn = lmb_rgn_lst->data;
245 if (rgn[i].flags == rgn[i + 1].flags) {
246 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
247 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
248 coalesced++;
249 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
250 /* fix overlapping area */
251 lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
252 coalesced++;
253 }
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200254 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600255 }
256
257 if (coalesced)
Ilias Apalodimas98491252024-10-23 18:22:00 +0300258 return 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530259
260 if (alist_full(lmb_rgn_lst) &&
261 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600262 return -1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530263 rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600264
265 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530266 for (i = lmb_rgn_lst->count; i >= 0; i--) {
267 if (i && base < rgn[i - 1].base) {
268 rgn[i] = rgn[i - 1];
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600269 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530270 rgn[i].base = base;
271 rgn[i].size = size;
272 rgn[i].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600273 break;
274 }
275 }
276
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530277 lmb_rgn_lst->count++;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600278
279 return 0;
280}
281
Janne Grunaud9c70402024-11-11 07:56:31 +0100282static long _lmb_free(struct alist *lmb_rgn_lst, phys_addr_t base,
283 phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500284{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530285 struct lmb_region *rgn;
Andy Fleming09d2a712008-07-07 14:24:39 -0500286 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100287 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500288 int i;
289
290 rgnbegin = rgnend = 0; /* supress gcc warnings */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530291 rgn = lmb_rgn_lst->data;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500292 /* Find the region where (base, size) belongs to */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530293 for (i = 0; i < lmb_rgn_lst->count; i++) {
294 rgnbegin = rgn[i].base;
295 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500296
297 if ((rgnbegin <= base) && (end <= rgnend))
298 break;
299 }
300
301 /* Didn't find the region */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530302 if (i == lmb_rgn_lst->count)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500303 return -1;
304
305 /* Check to see if we are removing entire region */
306 if ((rgnbegin == base) && (rgnend == end)) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530307 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500308 return 0;
309 }
310
311 /* Check to see if region is matching at the front */
312 if (rgnbegin == base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530313 rgn[i].base = end + 1;
314 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500315 return 0;
316 }
317
318 /* Check to see if the region is matching at the end */
319 if (rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530320 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500321 return 0;
322 }
323
324 /*
325 * We need to split the entry - adjust the current one to the
326 * beginging of the hole and add the region after hole.
327 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530328 rgn[i].size = base - rgn[i].base;
329 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
330 rgn[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500331}
332
Janne Grunau5631f442024-11-11 07:56:32 +0100333static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
334 phys_size_t size)
335{
336 unsigned long i;
337 struct lmb_region *rgn = lmb_rgn_lst->data;
338
339 for (i = 0; i < lmb_rgn_lst->count; i++) {
340 phys_addr_t rgnbase = rgn[i].base;
341 phys_size_t rgnsize = rgn[i].size;
342 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
343 break;
344 }
345
346 return (i < lmb_rgn_lst->count) ? i : -1;
347}
348
349static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
350{
351 return addr & ~(size - 1);
352}
353
354/*
Janne Grunaue818d3c2024-11-11 07:56:33 +0100355 * IOVA LMB memory maps using lmb pointers instead of the global LMB memory map.
356 */
357
358int io_lmb_setup(struct lmb *io_lmb)
359{
360 int ret;
361
362 ret = alist_init(&io_lmb->free_mem, sizeof(struct lmb_region),
363 (uint)LMB_ALIST_INITIAL_SIZE);
364 if (!ret) {
365 log_debug("Unable to initialise the list for LMB free IOVA\n");
366 return -ENOMEM;
367 }
368
369 ret = alist_init(&io_lmb->used_mem, sizeof(struct lmb_region),
370 (uint)LMB_ALIST_INITIAL_SIZE);
371 if (!ret) {
372 log_debug("Unable to initialise the list for LMB used IOVA\n");
373 return -ENOMEM;
374 }
375
376 io_lmb->test = false;
377
378 return 0;
379}
380
381void io_lmb_teardown(struct lmb *io_lmb)
382{
383 alist_uninit(&io_lmb->free_mem);
384 alist_uninit(&io_lmb->used_mem);
385}
386
387long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
388{
389 return lmb_add_region_flags(&io_lmb->free_mem, base, size, LMB_NONE);
390}
391
392/* derived and simplified from _lmb_alloc_base() */
393phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align)
394{
395 long i, rgn;
396 phys_addr_t base = 0;
397 phys_addr_t res_base;
398 struct lmb_region *lmb_used = io_lmb->used_mem.data;
399 struct lmb_region *lmb_memory = io_lmb->free_mem.data;
400
401 for (i = io_lmb->free_mem.count - 1; i >= 0; i--) {
402 phys_addr_t lmbbase = lmb_memory[i].base;
403 phys_size_t lmbsize = lmb_memory[i].size;
404
405 if (lmbsize < size)
406 continue;
407 base = lmb_align_down(lmbbase + lmbsize - size, align);
408
409 while (base && lmbbase <= base) {
410 rgn = lmb_overlaps_region(&io_lmb->used_mem, base, size);
411 if (rgn < 0) {
412 /* This area isn't reserved, take it */
413 if (lmb_add_region_flags(&io_lmb->used_mem, base,
414 size, LMB_NONE) < 0)
415 return 0;
416
417 return base;
418 }
419
420 res_base = lmb_used[rgn].base;
421 if (res_base < size)
422 break;
423 base = lmb_align_down(res_base - size, align);
424 }
425 }
426 return 0;
427}
428
429long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
430{
431 return _lmb_free(&io_lmb->used_mem, base, size);
432}
433
434/*
Janne Grunau5631f442024-11-11 07:56:32 +0100435 * Low level LMB functions are used to manage IOVA memory maps for the Apple
436 * dart iommu. They must not access the global LMB memory map.
437 * So keep the global LMB variable declaration unreachable from them.
438 */
439
440static struct lmb lmb;
441
442static bool lmb_should_notify(enum lmb_flags flags)
443{
444 return !lmb.test && !(flags & LMB_NONOTIFY) &&
445 CONFIG_IS_ENABLED(EFI_LOADER);
446}
447
448static int lmb_map_update_notify(phys_addr_t addr, phys_size_t size, u8 op,
449 enum lmb_flags flags)
450{
451 u64 efi_addr;
452 u64 pages;
453 efi_status_t status;
454
455 if (op != MAP_OP_RESERVE && op != MAP_OP_FREE && op != MAP_OP_ADD) {
456 log_err("Invalid map update op received (%d)\n", op);
457 return -1;
458 }
459
460 if (!lmb_should_notify(flags))
461 return 0;
462
463 efi_addr = (uintptr_t)map_sysmem(addr, 0);
464 pages = efi_size_in_pages(size + (efi_addr & EFI_PAGE_MASK));
465 efi_addr &= ~EFI_PAGE_MASK;
466
467 status = efi_add_memory_map_pg(efi_addr, pages,
468 op == MAP_OP_RESERVE ?
469 EFI_BOOT_SERVICES_DATA :
470 EFI_CONVENTIONAL_MEMORY,
471 false);
472 if (status != EFI_SUCCESS) {
473 log_err("%s: LMB Map notify failure %lu\n", __func__,
474 status & ~EFI_ERROR_MASK);
475 return -1;
476 }
477 unmap_sysmem((void *)(uintptr_t)efi_addr);
478
479 return 0;
480}
481
482static void lmb_print_region_flags(enum lmb_flags flags)
483{
Janne Grunau5631f442024-11-11 07:56:32 +0100484 const char *flag_str[] = { "none", "no-map", "no-overwrite", "no-notify" };
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100485 unsigned int pflags = flags &
486 (LMB_NOMAP | LMB_NOOVERWRITE | LMB_NONOTIFY);
487
488 if (flags != pflags) {
489 printf("invalid %#x\n", flags);
490 return;
491 }
Janne Grunau5631f442024-11-11 07:56:32 +0100492
493 do {
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100494 int bitpos = pflags ? fls(pflags) - 1 : 0;
495
Janne Grunau5631f442024-11-11 07:56:32 +0100496 printf("%s", flag_str[bitpos]);
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100497 pflags &= ~(1u << bitpos);
498 puts(pflags ? ", " : "\n");
499 } while (pflags);
Janne Grunau5631f442024-11-11 07:56:32 +0100500}
501
502static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
503{
504 struct lmb_region *rgn = lmb_rgn_lst->data;
505 unsigned long long base, size, end;
506 enum lmb_flags flags;
507 int i;
508
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100509 printf(" %s.count = %#x\n", name, lmb_rgn_lst->count);
Janne Grunau5631f442024-11-11 07:56:32 +0100510
511 for (i = 0; i < lmb_rgn_lst->count; i++) {
512 base = rgn[i].base;
513 size = rgn[i].size;
514 end = base + size - 1;
515 flags = rgn[i].flags;
516
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100517 printf(" %s[%d]\t[%#llx-%#llx], %#llx bytes, flags: ",
Janne Grunau5631f442024-11-11 07:56:32 +0100518 name, i, base, end, size);
519 lmb_print_region_flags(flags);
520 }
521}
522
523void lmb_dump_all_force(void)
524{
525 printf("lmb_dump_all:\n");
526 lmb_dump_region(&lmb.free_mem, "memory");
527 lmb_dump_region(&lmb.used_mem, "reserved");
528}
529
530void lmb_dump_all(void)
531{
532#ifdef DEBUG
533 lmb_dump_all_force();
534#endif
535}
536
537static void lmb_reserve_uboot_region(void)
538{
539 int bank;
540 ulong end, bank_end;
541 phys_addr_t rsv_start;
542
543 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
544 end = gd->ram_top;
545
546 /*
547 * Reserve memory from aligned address below the bottom of U-Boot stack
548 * until end of RAM area to prevent LMB from overwriting that memory.
549 */
550 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
551
552 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
553 if (!gd->bd->bi_dram[bank].size ||
554 rsv_start < gd->bd->bi_dram[bank].start)
555 continue;
556 /* Watch out for RAM at end of address space! */
557 bank_end = gd->bd->bi_dram[bank].start +
558 gd->bd->bi_dram[bank].size - 1;
559 if (rsv_start > bank_end)
560 continue;
561 if (bank_end > end)
562 bank_end = end - 1;
563
564 lmb_reserve_flags(rsv_start, bank_end - rsv_start + 1,
565 LMB_NOOVERWRITE);
566
567 if (gd->flags & GD_FLG_SKIP_RELOC)
568 lmb_reserve_flags((phys_addr_t)(uintptr_t)_start,
569 gd->mon_len, LMB_NOOVERWRITE);
570
571 break;
572 }
573}
574
575static void lmb_reserve_common(void *fdt_blob)
576{
577 lmb_reserve_uboot_region();
578
579 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
580 boot_fdt_add_mem_rsv_regions(fdt_blob);
581}
582
583static __maybe_unused void lmb_reserve_common_spl(void)
584{
585 phys_addr_t rsv_start;
586 phys_size_t rsv_size;
587
588 /*
589 * Assume a SPL stack of 16KB. This must be
590 * more than enough for the SPL stage.
591 */
592 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
593 rsv_start = gd->start_addr_sp - 16384;
594 rsv_size = 16384;
595 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
596 }
597
598 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
599 /* Reserve the bss region */
600 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
601 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
602 (phys_addr_t)(uintptr_t)__bss_start;
603 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
604 }
605}
606
607/**
608 * lmb_add_memory() - Add memory range for LMB allocations
609 *
610 * Add the entire available memory range to the pool of memory that
611 * can be used by the LMB module for allocations.
612 *
613 * Return: None
614 */
615void lmb_add_memory(void)
616{
617 int i;
618 phys_size_t size;
619 u64 ram_top = gd->ram_top;
620 struct bd_info *bd = gd->bd;
621
622 if (CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP))
623 return lmb_arch_add_memory();
624
625 /* Assume a 4GB ram_top if not defined */
626 if (!ram_top)
627 ram_top = 0x100000000ULL;
628
629 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
630 size = bd->bi_dram[i].size;
631 if (size) {
632 lmb_add(bd->bi_dram[i].start, size);
633
634 /*
635 * Reserve memory above ram_top as
636 * no-overwrite so that it cannot be
637 * allocated
638 */
639 if (bd->bi_dram[i].start >= ram_top)
640 lmb_reserve_flags(bd->bi_dram[i].start, size,
641 LMB_NOOVERWRITE);
642 }
643 }
644}
645
646static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base,
647 phys_size_t size)
648{
649 return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
650}
651
652/* This routine may be called with relocation disabled. */
653long lmb_add(phys_addr_t base, phys_size_t size)
654{
655 long ret;
656 struct alist *lmb_rgn_lst = &lmb.free_mem;
657
658 ret = lmb_add_region(lmb_rgn_lst, base, size);
659 if (ret)
660 return ret;
661
662 return lmb_map_update_notify(base, size, MAP_OP_ADD, LMB_NONE);
663}
664
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530665/**
666 * lmb_free_flags() - Free up a region of memory
667 * @base: Base Address of region to be freed
668 * @size: Size of the region to be freed
669 * @flags: Memory region attributes
670 *
671 * Free up a region of memory.
672 *
673 * Return: 0 if successful, -1 on failure
674 */
675long lmb_free_flags(phys_addr_t base, phys_size_t size,
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530676 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530677{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530678 long ret;
679
Janne Grunaud9c70402024-11-11 07:56:31 +0100680 ret = _lmb_free(&lmb.used_mem, base, size);
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530681 if (ret < 0)
682 return ret;
683
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300684 return lmb_map_update_notify(base, size, MAP_OP_FREE, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530685}
686
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530687long lmb_free(phys_addr_t base, phys_size_t size)
688{
689 return lmb_free_flags(base, size, LMB_NONE);
690}
691
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530692long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600693{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530694 long ret = 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530695 struct alist *lmb_rgn_lst = &lmb.used_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600696
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530697 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Ilias Apalodimas98491252024-10-23 18:22:00 +0300698 if (ret)
699 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530700
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300701 return lmb_map_update_notify(base, size, MAP_OP_RESERVE, flags);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200702}
703
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530704long lmb_reserve(phys_addr_t base, phys_size_t size)
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200705{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530706 return lmb_reserve_flags(base, size, LMB_NONE);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600707}
708
Sughosh Ganu99b59662024-10-15 21:07:17 +0530709static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align,
Sughosh Ganu50e27272024-08-26 17:29:19 +0530710 phys_addr_t max_addr, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600711{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530712 int ret;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100713 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500714 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500715 phys_addr_t res_base;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530716 struct lmb_region *lmb_used = lmb.used_mem.data;
717 struct lmb_region *lmb_memory = lmb.free_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600718
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530719 for (i = lmb.free_mem.count - 1; i >= 0; i--) {
720 phys_addr_t lmbbase = lmb_memory[i].base;
721 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600722
Andy Fleming78bd5a72008-06-16 13:58:55 -0500723 if (lmbsize < size)
724 continue;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600725 if (max_addr == LMB_ALLOC_ANYWHERE)
726 base = lmb_align_down(lmbbase + lmbsize - size, align);
727 else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600728 base = lmbbase + lmbsize;
729 if (base < lmbbase)
730 base = -1;
731 base = min(base, max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600732 base = lmb_align_down(base - size, align);
733 } else
734 continue;
735
Andy Fleming78bd5a72008-06-16 13:58:55 -0500736 while (base && lmbbase <= base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530737 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100738 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500739 /* This area isn't reserved, take it */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530740 if (lmb_add_region_flags(&lmb.used_mem, base,
Ilias Apalodimas98491252024-10-23 18:22:00 +0300741 size, flags))
Andy Fleming78bd5a72008-06-16 13:58:55 -0500742 return 0;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530743
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300744 ret = lmb_map_update_notify(base, size,
745 MAP_OP_RESERVE,
746 flags);
747 if (ret)
748 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530749
Andy Fleming78bd5a72008-06-16 13:58:55 -0500750 return base;
751 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530752
753 res_base = lmb_used[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500754 if (res_base < size)
755 break;
756 base = lmb_align_down(res_base - size, align);
757 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600758 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500759 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600760}
761
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530762phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530763{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530764 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530765}
766
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530767phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530768{
769 phys_addr_t alloc;
770
Sughosh Ganu99b59662024-10-15 21:07:17 +0530771 alloc = _lmb_alloc_base(size, align, max_addr, LMB_NONE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530772
773 if (alloc == 0)
774 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
775 (ulong)size, (ulong)max_addr);
776
777 return alloc;
778}
779
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530780/**
781 * lmb_alloc_base_flags() - Allocate specified memory region with specified attributes
782 * @size: Size of the region requested
783 * @align: Alignment of the memory region requested
784 * @max_addr: Maximum address of the requested region
785 * @flags: Memory region attributes to be set
786 *
787 * Allocate a region of memory with the attributes specified through the
788 * parameter. The max_addr parameter is used to specify the maximum address
789 * below which the requested region should be allocated.
790 *
791 * Return: base address on success, 0 on error
792 */
793phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
794 phys_addr_t max_addr, uint flags)
795{
796 phys_addr_t alloc;
797
Sughosh Ganu99b59662024-10-15 21:07:17 +0530798 alloc = _lmb_alloc_base(size, align, max_addr, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530799
800 if (alloc == 0)
801 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
802 (ulong)size, (ulong)max_addr);
803
804 return alloc;
805}
806
Sughosh Ganu99b59662024-10-15 21:07:17 +0530807static phys_addr_t _lmb_alloc_addr(phys_addr_t base, phys_size_t size,
Sughosh Ganu50e27272024-08-26 17:29:19 +0530808 enum lmb_flags flags)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100809{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100810 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530811 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100812
813 /* Check if the requested address is in one of the memory regions */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530814 rgn = lmb_overlaps_region(&lmb.free_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100815 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100816 /*
817 * Check if the requested end address is in the same memory
818 * region we found.
819 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530820 if (lmb_addrs_overlap(lmb_memory[rgn].base,
821 lmb_memory[rgn].size,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100822 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100823 /* ok, reserve the memory */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530824 if (lmb_reserve_flags(base, size, flags) >= 0)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100825 return base;
826 }
827 }
Sughosh Ganu50e27272024-08-26 17:29:19 +0530828
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100829 return 0;
830}
831
Sughosh Ganu50e27272024-08-26 17:29:19 +0530832/*
833 * Try to allocate a specific address range: must be in defined memory but not
834 * reserved
835 */
836phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
837{
Sughosh Ganu99b59662024-10-15 21:07:17 +0530838 return _lmb_alloc_addr(base, size, LMB_NONE);
Sughosh Ganu50e27272024-08-26 17:29:19 +0530839}
840
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530841/**
842 * lmb_alloc_addr_flags() - Allocate specified memory address with specified attributes
843 * @base: Base Address requested
844 * @size: Size of the region requested
845 * @flags: Memory region attributes to be set
846 *
847 * Allocate a region of memory with the attributes specified through the
848 * parameter. The base parameter is used to specify the base address
849 * of the requested region.
850 *
851 * Return: base address on success, 0 on error
852 */
853phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
854 uint flags)
855{
Sughosh Ganu99b59662024-10-15 21:07:17 +0530856 return _lmb_alloc_addr(base, size, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530857}
858
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100859/* Return number of bytes from a given address that are free */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530860phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100861{
862 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100863 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530864 struct lmb_region *lmb_used = lmb.used_mem.data;
865 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100866
867 /* check if the requested address is in the memory regions */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530868 rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100869 if (rgn >= 0) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530870 for (i = 0; i < lmb.used_mem.count; i++) {
871 if (addr < lmb_used[i].base) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100872 /* first reserved range > requested address */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530873 return lmb_used[i].base - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100874 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530875 if (lmb_used[i].base +
876 lmb_used[i].size > addr) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100877 /* requested addr is in this reserved range */
878 return 0;
879 }
880 }
881 /* if we come here: no reserved ranges above requested addr */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530882 return lmb_memory[lmb.free_mem.count - 1].base +
883 lmb_memory[lmb.free_mem.count - 1].size - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100884 }
885 return 0;
886}
887
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530888int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600889{
890 int i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530891 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600892
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530893 for (i = 0; i < lmb.used_mem.count; i++) {
894 phys_addr_t upper = lmb_used[i].base +
895 lmb_used[i].size - 1;
896 if (addr >= lmb_used[i].base && addr <= upper)
897 return (lmb_used[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600898 }
899 return 0;
900}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500901
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530902static int lmb_setup(bool test)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530903{
904 bool ret;
905
906 ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region),
907 (uint)LMB_ALIST_INITIAL_SIZE);
908 if (!ret) {
909 log_debug("Unable to initialise the list for LMB free memory\n");
910 return -ENOMEM;
911 }
912
913 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
914 (uint)LMB_ALIST_INITIAL_SIZE);
915 if (!ret) {
916 log_debug("Unable to initialise the list for LMB used memory\n");
917 return -ENOMEM;
918 }
919
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530920 lmb.test = test;
921
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530922 return 0;
923}
924
925/**
926 * lmb_init() - Initialise the LMB module
927 *
928 * Initialise the LMB lists needed for keeping the memory map. There
929 * are two lists, in form of alloced list data structure. One for the
930 * available memory, and one for the used memory. Initialise the two
931 * lists as part of board init. Add memory to the available memory
932 * list and reserve common areas by adding them to the used memory
933 * list.
934 *
935 * Return: 0 on success, -ve on error
936 */
937int lmb_init(void)
938{
939 int ret;
940
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530941 ret = lmb_setup(false);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530942 if (ret) {
943 log_info("Unable to init LMB\n");
944 return ret;
945 }
946
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530947 lmb_add_memory();
948
Sughosh Ganu6518b412024-08-26 17:29:24 +0530949 /* Reserve the U-Boot image region once U-Boot has relocated */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600950 if (xpl_phase() == PHASE_SPL)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530951 lmb_reserve_common_spl();
Simon Glassd4dce4a2024-09-29 19:49:36 -0600952 else if (xpl_phase() == PHASE_BOARD_R)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530953 lmb_reserve_common((void *)gd->fdt_blob);
954
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530955 return 0;
956}
957
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530958struct lmb *lmb_get(void)
959{
960 return &lmb;
961}
962
Simon Glass176eba52024-10-21 10:19:31 +0200963#if CONFIG_IS_ENABLED(UNIT_TEST)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530964int lmb_push(struct lmb *store)
965{
966 int ret;
967
968 *store = lmb;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530969 ret = lmb_setup(true);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530970 if (ret)
971 return ret;
972
973 return 0;
974}
975
976void lmb_pop(struct lmb *store)
977{
978 alist_uninit(&lmb.free_mem);
979 alist_uninit(&lmb.used_mem);
980 lmb = *store;
981}
982#endif /* UNIT_TEST */