blob: 7534a231c993eff63214710ed643354f4c419115 [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
Udit Kumar4e8e6632023-09-26 16:54:42 +053099/*Assumption : base addr of region 1 < base addr of region 2*/
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530100static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst,
101 unsigned long r1, unsigned long r2)
Udit Kumar4e8e6632023-09-26 16:54:42 +0530102{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530103 struct lmb_region *rgn = lmb_rgn_lst->data;
104
105 phys_addr_t base1 = rgn[r1].base;
106 phys_size_t size1 = rgn[r1].size;
107 phys_addr_t base2 = rgn[r2].base;
108 phys_size_t size2 = rgn[r2].size;
Udit Kumar4e8e6632023-09-26 16:54:42 +0530109
110 if (base1 + size1 > base2 + size2) {
111 printf("This will not be a case any time\n");
112 return;
113 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530114 rgn[r1].size = base2 + size2 - base1;
115 lmb_remove_region(lmb_rgn_lst, r2);
Udit Kumar4e8e6632023-09-26 16:54:42 +0530116}
117
Sughosh Ganu50e27272024-08-26 17:29:19 +0530118static long lmb_resize_regions(struct alist *lmb_rgn_lst,
119 unsigned long idx_start,
120 phys_addr_t base, phys_size_t size)
121{
122 phys_size_t rgnsize;
123 unsigned long rgn_cnt, idx, idx_end;
124 phys_addr_t rgnbase, rgnend;
125 phys_addr_t mergebase, mergeend;
126 struct lmb_region *rgn = lmb_rgn_lst->data;
127
128 rgn_cnt = 0;
129 idx = idx_start;
130 idx_end = idx_start;
131
132 /*
133 * First thing to do is to identify how many regions
134 * the requested region overlaps.
135 * If the flags match, combine all these overlapping
136 * regions into a single region, and remove the merged
137 * regions.
138 */
139 while (idx <= lmb_rgn_lst->count - 1) {
140 rgnbase = rgn[idx].base;
141 rgnsize = rgn[idx].size;
142
143 if (lmb_addrs_overlap(base, size, rgnbase,
144 rgnsize)) {
145 if (rgn[idx].flags != LMB_NONE)
146 return -1;
147 rgn_cnt++;
148 idx_end = idx;
149 }
150 idx++;
151 }
152
153 /* The merged region's base and size */
154 rgnbase = rgn[idx_start].base;
155 mergebase = min(base, rgnbase);
156 rgnend = rgn[idx_end].base + rgn[idx_end].size;
157 mergeend = max(rgnend, (base + size));
158
159 rgn[idx_start].base = mergebase;
160 rgn[idx_start].size = mergeend - mergebase;
161
162 /* Now remove the merged regions */
163 while (--rgn_cnt)
164 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
165
166 return 0;
167}
168
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530169/**
170 * lmb_add_region_flags() - Add an lmb region to the given list
171 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
172 * @base: Start address of the region
173 * @size: Size of the region to be added
174 * @flags: Attributes of the LMB region
175 *
176 * Add a region of memory to the list. If the region does not exist, add
177 * it to the list. Depending on the attributes of the region to be added,
178 * the function might resize an already existing region or coalesce two
179 * adjacent regions.
180 *
Sam Protsenkob4f81102024-12-10 20:17:01 -0600181 * Return:
182 * * %0 - Added successfully, or it's already added (only if LMB_NONE)
183 * * %-EEXIST - The region is already added, and flags != LMB_NONE
184 * * %-1 - Failure
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530185 */
186static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200187 phys_size_t size, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600188{
189 unsigned long coalesced = 0;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530190 long ret, i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530191 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600192
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530193 if (alist_err(lmb_rgn_lst))
194 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600195
196 /* First try and coalesce this LMB with another. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530197 for (i = 0; i < lmb_rgn_lst->count; i++) {
198 phys_addr_t rgnbase = rgn[i].base;
199 phys_size_t rgnsize = rgn[i].size;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200200 u32 rgnflags = rgn[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600201
Sughosh Ganu50e27272024-08-26 17:29:19 +0530202 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
203 if (ret > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200204 if (flags != rgnflags)
205 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530206 rgn[i].base -= size;
207 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600208 coalesced++;
209 break;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530210 } else if (ret < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200211 if (flags != rgnflags)
212 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530213 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600214 coalesced++;
215 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100216 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sam Protsenkob4f81102024-12-10 20:17:01 -0600217 if (flags != LMB_NONE)
218 return -EEXIST;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530219
Sam Protsenkob4f81102024-12-10 20:17:01 -0600220 ret = lmb_resize_regions(lmb_rgn_lst, i, base, size);
221 if (ret < 0)
Sughosh Ganu50e27272024-08-26 17:29:19 +0530222 return -1;
Sam Protsenkob4f81102024-12-10 20:17:01 -0600223
224 coalesced++;
225 break;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600226
227 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600228 }
229 }
230
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530231 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
232 rgn = lmb_rgn_lst->data;
233 if (rgn[i].flags == rgn[i + 1].flags) {
234 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
235 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
236 coalesced++;
237 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
238 /* fix overlapping area */
239 lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
240 coalesced++;
241 }
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200242 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600243 }
244
245 if (coalesced)
Ilias Apalodimas98491252024-10-23 18:22:00 +0300246 return 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530247
248 if (alist_full(lmb_rgn_lst) &&
249 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600250 return -1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530251 rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600252
253 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530254 for (i = lmb_rgn_lst->count; i >= 0; i--) {
255 if (i && base < rgn[i - 1].base) {
256 rgn[i] = rgn[i - 1];
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600257 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530258 rgn[i].base = base;
259 rgn[i].size = size;
260 rgn[i].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600261 break;
262 }
263 }
264
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530265 lmb_rgn_lst->count++;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600266
267 return 0;
268}
269
Janne Grunaud9c70402024-11-11 07:56:31 +0100270static long _lmb_free(struct alist *lmb_rgn_lst, phys_addr_t base,
271 phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500272{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530273 struct lmb_region *rgn;
Andy Fleming09d2a712008-07-07 14:24:39 -0500274 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100275 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500276 int i;
277
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600278 /* Suppress GCC warnings */
279 rgnbegin = 0;
280 rgnend = 0;
281
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530282 rgn = lmb_rgn_lst->data;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500283 /* Find the region where (base, size) belongs to */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530284 for (i = 0; i < lmb_rgn_lst->count; i++) {
285 rgnbegin = rgn[i].base;
286 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500287
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600288 if (rgnbegin <= base && end <= rgnend)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500289 break;
290 }
291
292 /* Didn't find the region */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530293 if (i == lmb_rgn_lst->count)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500294 return -1;
295
296 /* Check to see if we are removing entire region */
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600297 if (rgnbegin == base && rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530298 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500299 return 0;
300 }
301
302 /* Check to see if region is matching at the front */
303 if (rgnbegin == base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530304 rgn[i].base = end + 1;
305 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500306 return 0;
307 }
308
309 /* Check to see if the region is matching at the end */
310 if (rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530311 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500312 return 0;
313 }
314
315 /*
316 * We need to split the entry - adjust the current one to the
317 * beginging of the hole and add the region after hole.
318 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530319 rgn[i].size = base - rgn[i].base;
320 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
321 rgn[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500322}
323
Janne Grunau5631f442024-11-11 07:56:32 +0100324static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
325 phys_size_t size)
326{
327 unsigned long i;
328 struct lmb_region *rgn = lmb_rgn_lst->data;
329
330 for (i = 0; i < lmb_rgn_lst->count; i++) {
331 phys_addr_t rgnbase = rgn[i].base;
332 phys_size_t rgnsize = rgn[i].size;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600333
Janne Grunau5631f442024-11-11 07:56:32 +0100334 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
335 break;
336 }
337
338 return (i < lmb_rgn_lst->count) ? i : -1;
339}
340
Janne Grunau5631f442024-11-11 07:56:32 +0100341/*
Janne Grunaue818d3c2024-11-11 07:56:33 +0100342 * IOVA LMB memory maps using lmb pointers instead of the global LMB memory map.
343 */
344
345int io_lmb_setup(struct lmb *io_lmb)
346{
347 int ret;
348
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200349 ret = alist_init(&io_lmb->available_mem, sizeof(struct lmb_region),
Janne Grunaue818d3c2024-11-11 07:56:33 +0100350 (uint)LMB_ALIST_INITIAL_SIZE);
351 if (!ret) {
352 log_debug("Unable to initialise the list for LMB free IOVA\n");
353 return -ENOMEM;
354 }
355
356 ret = alist_init(&io_lmb->used_mem, sizeof(struct lmb_region),
357 (uint)LMB_ALIST_INITIAL_SIZE);
358 if (!ret) {
359 log_debug("Unable to initialise the list for LMB used IOVA\n");
360 return -ENOMEM;
361 }
362
363 io_lmb->test = false;
364
365 return 0;
366}
367
368void io_lmb_teardown(struct lmb *io_lmb)
369{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200370 alist_uninit(&io_lmb->available_mem);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100371 alist_uninit(&io_lmb->used_mem);
372}
373
374long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
375{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200376 return lmb_add_region_flags(&io_lmb->available_mem, base, size, LMB_NONE);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100377}
378
379/* derived and simplified from _lmb_alloc_base() */
380phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align)
381{
382 long i, rgn;
383 phys_addr_t base = 0;
384 phys_addr_t res_base;
385 struct lmb_region *lmb_used = io_lmb->used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200386 struct lmb_region *lmb_memory = io_lmb->available_mem.data;
Janne Grunaue818d3c2024-11-11 07:56:33 +0100387
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200388 for (i = io_lmb->available_mem.count - 1; i >= 0; i--) {
Janne Grunaue818d3c2024-11-11 07:56:33 +0100389 phys_addr_t lmbbase = lmb_memory[i].base;
390 phys_size_t lmbsize = lmb_memory[i].size;
391
392 if (lmbsize < size)
393 continue;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200394 base = ALIGN_DOWN(lmbbase + lmbsize - size, align);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100395
396 while (base && lmbbase <= base) {
397 rgn = lmb_overlaps_region(&io_lmb->used_mem, base, size);
398 if (rgn < 0) {
399 /* This area isn't reserved, take it */
400 if (lmb_add_region_flags(&io_lmb->used_mem, base,
401 size, LMB_NONE) < 0)
402 return 0;
403
404 return base;
405 }
406
407 res_base = lmb_used[rgn].base;
408 if (res_base < size)
409 break;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200410 base = ALIGN_DOWN(res_base - size, align);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100411 }
412 }
413 return 0;
414}
415
416long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
417{
418 return _lmb_free(&io_lmb->used_mem, base, size);
419}
420
421/*
Janne Grunau5631f442024-11-11 07:56:32 +0100422 * Low level LMB functions are used to manage IOVA memory maps for the Apple
423 * dart iommu. They must not access the global LMB memory map.
424 * So keep the global LMB variable declaration unreachable from them.
425 */
426
427static struct lmb lmb;
428
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200429static bool lmb_should_notify(u32 flags)
Janne Grunau5631f442024-11-11 07:56:32 +0100430{
431 return !lmb.test && !(flags & LMB_NONOTIFY) &&
432 CONFIG_IS_ENABLED(EFI_LOADER);
433}
434
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100435static int lmb_map_update_notify(phys_addr_t addr, phys_size_t size,
436 enum lmb_map_op op, u32 flags)
Janne Grunau5631f442024-11-11 07:56:32 +0100437{
438 u64 efi_addr;
439 u64 pages;
440 efi_status_t status;
441
Janne Grunau5631f442024-11-11 07:56:32 +0100442 if (!lmb_should_notify(flags))
443 return 0;
444
445 efi_addr = (uintptr_t)map_sysmem(addr, 0);
446 pages = efi_size_in_pages(size + (efi_addr & EFI_PAGE_MASK));
447 efi_addr &= ~EFI_PAGE_MASK;
448
449 status = efi_add_memory_map_pg(efi_addr, pages,
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100450 op == LMB_MAP_OP_RESERVE ?
Janne Grunau5631f442024-11-11 07:56:32 +0100451 EFI_BOOT_SERVICES_DATA :
452 EFI_CONVENTIONAL_MEMORY,
453 false);
454 if (status != EFI_SUCCESS) {
455 log_err("%s: LMB Map notify failure %lu\n", __func__,
456 status & ~EFI_ERROR_MASK);
457 return -1;
458 }
459 unmap_sysmem((void *)(uintptr_t)efi_addr);
460
461 return 0;
462}
463
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200464static void lmb_print_region_flags(u32 flags)
Janne Grunau5631f442024-11-11 07:56:32 +0100465{
Sam Protsenko41bf8f02024-12-10 20:25:48 -0600466 const char * const flag_str[] = { "none", "no-map", "no-overwrite",
467 "no-notify" };
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100468 unsigned int pflags = flags &
469 (LMB_NOMAP | LMB_NOOVERWRITE | LMB_NONOTIFY);
470
471 if (flags != pflags) {
472 printf("invalid %#x\n", flags);
473 return;
474 }
Janne Grunau5631f442024-11-11 07:56:32 +0100475
476 do {
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100477 int bitpos = pflags ? fls(pflags) - 1 : 0;
478
Janne Grunau5631f442024-11-11 07:56:32 +0100479 printf("%s", flag_str[bitpos]);
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100480 pflags &= ~(1u << bitpos);
481 puts(pflags ? ", " : "\n");
482 } while (pflags);
Janne Grunau5631f442024-11-11 07:56:32 +0100483}
484
485static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
486{
487 struct lmb_region *rgn = lmb_rgn_lst->data;
488 unsigned long long base, size, end;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200489 u32 flags;
Janne Grunau5631f442024-11-11 07:56:32 +0100490 int i;
491
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100492 printf(" %s.count = %#x\n", name, lmb_rgn_lst->count);
Janne Grunau5631f442024-11-11 07:56:32 +0100493
494 for (i = 0; i < lmb_rgn_lst->count; i++) {
495 base = rgn[i].base;
496 size = rgn[i].size;
497 end = base + size - 1;
498 flags = rgn[i].flags;
499
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100500 printf(" %s[%d]\t[%#llx-%#llx], %#llx bytes, flags: ",
Janne Grunau5631f442024-11-11 07:56:32 +0100501 name, i, base, end, size);
502 lmb_print_region_flags(flags);
503 }
504}
505
506void lmb_dump_all_force(void)
507{
508 printf("lmb_dump_all:\n");
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200509 lmb_dump_region(&lmb.available_mem, "memory");
Janne Grunau5631f442024-11-11 07:56:32 +0100510 lmb_dump_region(&lmb.used_mem, "reserved");
511}
512
513void lmb_dump_all(void)
514{
515#ifdef DEBUG
516 lmb_dump_all_force();
517#endif
518}
519
520static void lmb_reserve_uboot_region(void)
521{
522 int bank;
523 ulong end, bank_end;
524 phys_addr_t rsv_start;
525
526 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
527 end = gd->ram_top;
528
529 /*
530 * Reserve memory from aligned address below the bottom of U-Boot stack
531 * until end of RAM area to prevent LMB from overwriting that memory.
532 */
533 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
534
535 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
536 if (!gd->bd->bi_dram[bank].size ||
537 rsv_start < gd->bd->bi_dram[bank].start)
538 continue;
539 /* Watch out for RAM at end of address space! */
540 bank_end = gd->bd->bi_dram[bank].start +
541 gd->bd->bi_dram[bank].size - 1;
542 if (rsv_start > bank_end)
543 continue;
544 if (bank_end > end)
545 bank_end = end - 1;
546
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200547 lmb_reserve(rsv_start, bank_end - rsv_start + 1, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100548
549 if (gd->flags & GD_FLG_SKIP_RELOC)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200550 lmb_reserve((phys_addr_t)(uintptr_t)_start,
551 gd->mon_len, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100552
553 break;
554 }
555}
556
557static void lmb_reserve_common(void *fdt_blob)
558{
559 lmb_reserve_uboot_region();
560
561 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
562 boot_fdt_add_mem_rsv_regions(fdt_blob);
563}
564
565static __maybe_unused void lmb_reserve_common_spl(void)
566{
567 phys_addr_t rsv_start;
568 phys_size_t rsv_size;
569
570 /*
571 * Assume a SPL stack of 16KB. This must be
572 * more than enough for the SPL stage.
573 */
574 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
575 rsv_start = gd->start_addr_sp - 16384;
576 rsv_size = 16384;
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200577 lmb_reserve(rsv_start, rsv_size, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100578 }
579
580 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
581 /* Reserve the bss region */
582 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
583 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
584 (phys_addr_t)(uintptr_t)__bss_start;
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200585 lmb_reserve(rsv_start, rsv_size, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100586 }
587}
588
Janne Grunau5631f442024-11-11 07:56:32 +0100589void lmb_add_memory(void)
590{
591 int i;
Sughosh Ganued220b82024-12-02 12:36:24 +0530592 phys_addr_t bank_end;
Janne Grunau5631f442024-11-11 07:56:32 +0100593 phys_size_t size;
594 u64 ram_top = gd->ram_top;
595 struct bd_info *bd = gd->bd;
596
597 if (CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP))
598 return lmb_arch_add_memory();
599
600 /* Assume a 4GB ram_top if not defined */
601 if (!ram_top)
602 ram_top = 0x100000000ULL;
603
604 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
605 size = bd->bi_dram[i].size;
Sughosh Ganued220b82024-12-02 12:36:24 +0530606 bank_end = bd->bi_dram[i].start + size;
607
Janne Grunau5631f442024-11-11 07:56:32 +0100608 if (size) {
609 lmb_add(bd->bi_dram[i].start, size);
610
611 /*
612 * Reserve memory above ram_top as
613 * no-overwrite so that it cannot be
614 * allocated
615 */
616 if (bd->bi_dram[i].start >= ram_top)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200617 lmb_reserve(bd->bi_dram[i].start, size,
618 LMB_NOOVERWRITE);
Sughosh Ganued220b82024-12-02 12:36:24 +0530619 else if (bank_end > ram_top)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200620 lmb_reserve(ram_top, bank_end - ram_top,
621 LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100622 }
623 }
624}
625
Janne Grunau5631f442024-11-11 07:56:32 +0100626/* This routine may be called with relocation disabled. */
627long lmb_add(phys_addr_t base, phys_size_t size)
628{
629 long ret;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200630 struct alist *lmb_rgn_lst = &lmb.available_mem;
Janne Grunau5631f442024-11-11 07:56:32 +0100631
Ilias Apalodimase2a6f972024-12-18 09:02:34 +0200632 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Janne Grunau5631f442024-11-11 07:56:32 +0100633 if (ret)
634 return ret;
635
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100636 return lmb_map_update_notify(base, size, LMB_MAP_OP_ADD, LMB_NONE);
Janne Grunau5631f442024-11-11 07:56:32 +0100637}
638
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530639long lmb_free_flags(phys_addr_t base, phys_size_t size,
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530640 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530641{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530642 long ret;
643
Janne Grunaud9c70402024-11-11 07:56:31 +0100644 ret = _lmb_free(&lmb.used_mem, base, size);
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530645 if (ret < 0)
646 return ret;
647
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100648 return lmb_map_update_notify(base, size, LMB_MAP_OP_FREE, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530649}
650
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530651long lmb_free(phys_addr_t base, phys_size_t size)
652{
653 return lmb_free_flags(base, size, LMB_NONE);
654}
655
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200656long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600657{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530658 long ret = 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530659 struct alist *lmb_rgn_lst = &lmb.used_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600660
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530661 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Ilias Apalodimas98491252024-10-23 18:22:00 +0300662 if (ret)
663 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530664
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100665 return lmb_map_update_notify(base, size, LMB_MAP_OP_RESERVE, flags);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200666}
667
Sughosh Ganu99b59662024-10-15 21:07:17 +0530668static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align,
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200669 phys_addr_t max_addr, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600670{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530671 int ret;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100672 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500673 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500674 phys_addr_t res_base;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530675 struct lmb_region *lmb_used = lmb.used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200676 struct lmb_region *lmb_memory = lmb.available_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600677
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200678 for (i = lmb.available_mem.count - 1; i >= 0; i--) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530679 phys_addr_t lmbbase = lmb_memory[i].base;
680 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600681
Andy Fleming78bd5a72008-06-16 13:58:55 -0500682 if (lmbsize < size)
683 continue;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600684
685 if (max_addr == LMB_ALLOC_ANYWHERE) {
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200686 base = ALIGN_DOWN(lmbbase + lmbsize - size, align);
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600687 } else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600688 base = lmbbase + lmbsize;
689 if (base < lmbbase)
690 base = -1;
691 base = min(base, max_addr);
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200692 base = ALIGN_DOWN(base - size, align);
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600693 } else {
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600694 continue;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600695 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600696
Andy Fleming78bd5a72008-06-16 13:58:55 -0500697 while (base && lmbbase <= base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530698 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100699 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500700 /* This area isn't reserved, take it */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530701 if (lmb_add_region_flags(&lmb.used_mem, base,
Ilias Apalodimas98491252024-10-23 18:22:00 +0300702 size, flags))
Andy Fleming78bd5a72008-06-16 13:58:55 -0500703 return 0;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530704
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300705 ret = lmb_map_update_notify(base, size,
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100706 LMB_MAP_OP_RESERVE,
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300707 flags);
708 if (ret)
709 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530710
Andy Fleming78bd5a72008-06-16 13:58:55 -0500711 return base;
712 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530713
714 res_base = lmb_used[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500715 if (res_base < size)
716 break;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200717 base = ALIGN_DOWN(res_base - size, align);
Andy Fleming78bd5a72008-06-16 13:58:55 -0500718 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600719 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500720 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600721}
722
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530723phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530724{
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200725 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE, LMB_NONE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530726}
727
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200728phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr,
729 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530730{
731 phys_addr_t alloc;
732
Sughosh Ganu99b59662024-10-15 21:07:17 +0530733 alloc = _lmb_alloc_base(size, align, max_addr, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530734
735 if (alloc == 0)
736 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
737 (ulong)size, (ulong)max_addr);
738
739 return alloc;
740}
741
Ilias Apalodimas31bf8f12024-12-18 09:02:37 +0200742phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100743{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100744 long rgn;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200745 struct lmb_region *lmb_memory = lmb.available_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100746
747 /* Check if the requested address is in one of the memory regions */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200748 rgn = lmb_overlaps_region(&lmb.available_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100749 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100750 /*
751 * Check if the requested end address is in the same memory
752 * region we found.
753 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530754 if (lmb_addrs_overlap(lmb_memory[rgn].base,
755 lmb_memory[rgn].size,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100756 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100757 /* ok, reserve the memory */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200758 if (!lmb_reserve(base, size, flags))
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100759 return base;
760 }
761 }
Sughosh Ganu50e27272024-08-26 17:29:19 +0530762
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100763 return 0;
764}
765
766/* Return number of bytes from a given address that are free */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530767phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100768{
769 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100770 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530771 struct lmb_region *lmb_used = lmb.used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200772 struct lmb_region *lmb_memory = lmb.available_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100773
774 /* check if the requested address is in the memory regions */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200775 rgn = lmb_overlaps_region(&lmb.available_mem, addr, 1);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100776 if (rgn >= 0) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530777 for (i = 0; i < lmb.used_mem.count; i++) {
778 if (addr < lmb_used[i].base) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100779 /* first reserved range > requested address */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530780 return lmb_used[i].base - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100781 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530782 if (lmb_used[i].base +
783 lmb_used[i].size > addr) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100784 /* requested addr is in this reserved range */
785 return 0;
786 }
787 }
788 /* if we come here: no reserved ranges above requested addr */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200789 return lmb_memory[lmb.available_mem.count - 1].base +
790 lmb_memory[lmb.available_mem.count - 1].size - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100791 }
792 return 0;
793}
794
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530795int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600796{
797 int i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530798 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600799
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530800 for (i = 0; i < lmb.used_mem.count; i++) {
801 phys_addr_t upper = lmb_used[i].base +
802 lmb_used[i].size - 1;
803 if (addr >= lmb_used[i].base && addr <= upper)
804 return (lmb_used[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600805 }
806 return 0;
807}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500808
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530809static int lmb_setup(bool test)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530810{
811 bool ret;
812
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200813 ret = alist_init(&lmb.available_mem, sizeof(struct lmb_region),
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530814 (uint)LMB_ALIST_INITIAL_SIZE);
815 if (!ret) {
816 log_debug("Unable to initialise the list for LMB free memory\n");
817 return -ENOMEM;
818 }
819
820 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
821 (uint)LMB_ALIST_INITIAL_SIZE);
822 if (!ret) {
823 log_debug("Unable to initialise the list for LMB used memory\n");
824 return -ENOMEM;
825 }
826
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530827 lmb.test = test;
828
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530829 return 0;
830}
831
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530832int lmb_init(void)
833{
834 int ret;
835
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530836 ret = lmb_setup(false);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530837 if (ret) {
838 log_info("Unable to init LMB\n");
839 return ret;
840 }
841
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530842 lmb_add_memory();
843
Sughosh Ganu6518b412024-08-26 17:29:24 +0530844 /* Reserve the U-Boot image region once U-Boot has relocated */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600845 if (xpl_phase() == PHASE_SPL)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530846 lmb_reserve_common_spl();
Simon Glassd4dce4a2024-09-29 19:49:36 -0600847 else if (xpl_phase() == PHASE_BOARD_R)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530848 lmb_reserve_common((void *)gd->fdt_blob);
849
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530850 return 0;
851}
852
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530853struct lmb *lmb_get(void)
854{
855 return &lmb;
856}
857
Simon Glass176eba52024-10-21 10:19:31 +0200858#if CONFIG_IS_ENABLED(UNIT_TEST)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530859int lmb_push(struct lmb *store)
860{
861 int ret;
862
863 *store = lmb;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530864 ret = lmb_setup(true);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530865 if (ret)
866 return ret;
867
868 return 0;
869}
870
871void lmb_pop(struct lmb *store)
872{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200873 alist_uninit(&lmb.available_mem);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530874 alist_uninit(&lmb.used_mem);
875 lmb = *store;
876}
877#endif /* UNIT_TEST */