blob: 32c787f8adfca995b1df732f900e383a7b9745d9 [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
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100429static int lmb_map_update_notify(phys_addr_t addr, phys_size_t size,
430 enum lmb_map_op op, u32 flags)
Janne Grunau5631f442024-11-11 07:56:32 +0100431{
Heinrich Schuchardtfabdb732025-02-16 12:12:40 +0100432 if (CONFIG_IS_ENABLED(EFI_LOADER) &&
433 !lmb.test && !(flags & LMB_NONOTIFY))
434 return efi_map_update_notify(addr, size, op);
Janne Grunau5631f442024-11-11 07:56:32 +0100435
436 return 0;
437}
438
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200439static void lmb_print_region_flags(u32 flags)
Janne Grunau5631f442024-11-11 07:56:32 +0100440{
Sam Protsenko41bf8f02024-12-10 20:25:48 -0600441 const char * const flag_str[] = { "none", "no-map", "no-overwrite",
442 "no-notify" };
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100443 unsigned int pflags = flags &
444 (LMB_NOMAP | LMB_NOOVERWRITE | LMB_NONOTIFY);
445
446 if (flags != pflags) {
447 printf("invalid %#x\n", flags);
448 return;
449 }
Janne Grunau5631f442024-11-11 07:56:32 +0100450
451 do {
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100452 int bitpos = pflags ? fls(pflags) - 1 : 0;
453
Janne Grunau5631f442024-11-11 07:56:32 +0100454 printf("%s", flag_str[bitpos]);
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100455 pflags &= ~(1u << bitpos);
456 puts(pflags ? ", " : "\n");
457 } while (pflags);
Janne Grunau5631f442024-11-11 07:56:32 +0100458}
459
460static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
461{
462 struct lmb_region *rgn = lmb_rgn_lst->data;
463 unsigned long long base, size, end;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200464 u32 flags;
Janne Grunau5631f442024-11-11 07:56:32 +0100465 int i;
466
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100467 printf(" %s.count = %#x\n", name, lmb_rgn_lst->count);
Janne Grunau5631f442024-11-11 07:56:32 +0100468
469 for (i = 0; i < lmb_rgn_lst->count; i++) {
470 base = rgn[i].base;
471 size = rgn[i].size;
472 end = base + size - 1;
473 flags = rgn[i].flags;
474
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100475 printf(" %s[%d]\t[%#llx-%#llx], %#llx bytes, flags: ",
Janne Grunau5631f442024-11-11 07:56:32 +0100476 name, i, base, end, size);
477 lmb_print_region_flags(flags);
478 }
479}
480
481void lmb_dump_all_force(void)
482{
483 printf("lmb_dump_all:\n");
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200484 lmb_dump_region(&lmb.available_mem, "memory");
Janne Grunau5631f442024-11-11 07:56:32 +0100485 lmb_dump_region(&lmb.used_mem, "reserved");
486}
487
488void lmb_dump_all(void)
489{
490#ifdef DEBUG
491 lmb_dump_all_force();
492#endif
493}
494
495static void lmb_reserve_uboot_region(void)
496{
497 int bank;
498 ulong end, bank_end;
499 phys_addr_t rsv_start;
500
501 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
502 end = gd->ram_top;
503
504 /*
505 * Reserve memory from aligned address below the bottom of U-Boot stack
506 * until end of RAM area to prevent LMB from overwriting that memory.
507 */
508 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
509
510 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
511 if (!gd->bd->bi_dram[bank].size ||
512 rsv_start < gd->bd->bi_dram[bank].start)
513 continue;
514 /* Watch out for RAM at end of address space! */
515 bank_end = gd->bd->bi_dram[bank].start +
516 gd->bd->bi_dram[bank].size - 1;
517 if (rsv_start > bank_end)
518 continue;
519 if (bank_end > end)
520 bank_end = end - 1;
521
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200522 lmb_reserve(rsv_start, bank_end - rsv_start + 1, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100523
524 if (gd->flags & GD_FLG_SKIP_RELOC)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200525 lmb_reserve((phys_addr_t)(uintptr_t)_start,
526 gd->mon_len, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100527
528 break;
529 }
530}
531
532static void lmb_reserve_common(void *fdt_blob)
533{
534 lmb_reserve_uboot_region();
535
536 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
537 boot_fdt_add_mem_rsv_regions(fdt_blob);
538}
539
540static __maybe_unused void lmb_reserve_common_spl(void)
541{
542 phys_addr_t rsv_start;
543 phys_size_t rsv_size;
544
545 /*
546 * Assume a SPL stack of 16KB. This must be
547 * more than enough for the SPL stage.
548 */
549 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
550 rsv_start = gd->start_addr_sp - 16384;
551 rsv_size = 16384;
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200552 lmb_reserve(rsv_start, rsv_size, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100553 }
554
555 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
556 /* Reserve the bss region */
557 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
558 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
559 (phys_addr_t)(uintptr_t)__bss_start;
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200560 lmb_reserve(rsv_start, rsv_size, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100561 }
562}
563
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530564/**
565 * lmb_can_reserve_region() - check if the region can be reserved
566 * @base: base address of region to be reserved
567 * @size: size of region to be reserved
568 * @flags: flag of the region to be reserved
569 *
570 * Go through all the reserved regions and ensure that the requested
571 * region does not overlap with any existing regions. An overlap is
572 * allowed only when the flag of the request region and the existing
573 * region is LMB_NONE.
574 *
575 * Return: true if region can be reserved, false otherwise
576 */
577static bool lmb_can_reserve_region(phys_addr_t base, phys_size_t size,
578 u32 flags)
579{
580 uint i;
581 struct lmb_region *lmb_reserved = lmb.used_mem.data;
582
583 for (i = 0; i < lmb.used_mem.count; i++) {
584 u32 rgnflags = lmb_reserved[i].flags;
585 phys_addr_t rgnbase = lmb_reserved[i].base;
586 phys_size_t rgnsize = lmb_reserved[i].size;
587
588 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
589 if (flags != LMB_NONE || flags != rgnflags)
590 return false;
591 }
592 }
593
594 return true;
595}
596
Janne Grunau5631f442024-11-11 07:56:32 +0100597void lmb_add_memory(void)
598{
599 int i;
Sughosh Ganued220b82024-12-02 12:36:24 +0530600 phys_addr_t bank_end;
Janne Grunau5631f442024-11-11 07:56:32 +0100601 phys_size_t size;
602 u64 ram_top = gd->ram_top;
603 struct bd_info *bd = gd->bd;
604
605 if (CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP))
606 return lmb_arch_add_memory();
607
608 /* Assume a 4GB ram_top if not defined */
609 if (!ram_top)
610 ram_top = 0x100000000ULL;
611
612 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
613 size = bd->bi_dram[i].size;
Sughosh Ganued220b82024-12-02 12:36:24 +0530614 bank_end = bd->bi_dram[i].start + size;
615
Janne Grunau5631f442024-11-11 07:56:32 +0100616 if (size) {
617 lmb_add(bd->bi_dram[i].start, size);
618
619 /*
620 * Reserve memory above ram_top as
621 * no-overwrite so that it cannot be
622 * allocated
623 */
624 if (bd->bi_dram[i].start >= ram_top)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200625 lmb_reserve(bd->bi_dram[i].start, size,
626 LMB_NOOVERWRITE);
Sughosh Ganued220b82024-12-02 12:36:24 +0530627 else if (bank_end > ram_top)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200628 lmb_reserve(ram_top, bank_end - ram_top,
629 LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100630 }
631 }
632}
633
Janne Grunau5631f442024-11-11 07:56:32 +0100634/* This routine may be called with relocation disabled. */
635long lmb_add(phys_addr_t base, phys_size_t size)
636{
637 long ret;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200638 struct alist *lmb_rgn_lst = &lmb.available_mem;
Janne Grunau5631f442024-11-11 07:56:32 +0100639
Ilias Apalodimase2a6f972024-12-18 09:02:34 +0200640 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Janne Grunau5631f442024-11-11 07:56:32 +0100641 if (ret)
642 return ret;
643
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100644 return lmb_map_update_notify(base, size, LMB_MAP_OP_ADD, LMB_NONE);
Janne Grunau5631f442024-11-11 07:56:32 +0100645}
646
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530647long lmb_free_flags(phys_addr_t base, phys_size_t size,
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530648 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530649{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530650 long ret;
651
Janne Grunaud9c70402024-11-11 07:56:31 +0100652 ret = _lmb_free(&lmb.used_mem, base, size);
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530653 if (ret < 0)
654 return ret;
655
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100656 return lmb_map_update_notify(base, size, LMB_MAP_OP_FREE, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530657}
658
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530659long lmb_free(phys_addr_t base, phys_size_t size)
660{
661 return lmb_free_flags(base, size, LMB_NONE);
662}
663
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200664long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600665{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530666 long ret = 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530667 struct alist *lmb_rgn_lst = &lmb.used_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600668
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530669 if (!lmb_can_reserve_region(base, size, flags))
670 return -EEXIST;
671
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530672 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Ilias Apalodimas98491252024-10-23 18:22:00 +0300673 if (ret)
674 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530675
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100676 return lmb_map_update_notify(base, size, LMB_MAP_OP_RESERVE, flags);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200677}
678
Sughosh Ganu99b59662024-10-15 21:07:17 +0530679static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align,
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200680 phys_addr_t max_addr, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600681{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530682 int ret;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100683 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500684 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500685 phys_addr_t res_base;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530686 struct lmb_region *lmb_used = lmb.used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200687 struct lmb_region *lmb_memory = lmb.available_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600688
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200689 for (i = lmb.available_mem.count - 1; i >= 0; i--) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530690 phys_addr_t lmbbase = lmb_memory[i].base;
691 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600692
Andy Fleming78bd5a72008-06-16 13:58:55 -0500693 if (lmbsize < size)
694 continue;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600695
696 if (max_addr == LMB_ALLOC_ANYWHERE) {
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200697 base = ALIGN_DOWN(lmbbase + lmbsize - size, align);
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600698 } else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600699 base = lmbbase + lmbsize;
700 if (base < lmbbase)
701 base = -1;
702 base = min(base, max_addr);
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200703 base = ALIGN_DOWN(base - size, align);
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600704 } else {
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600705 continue;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600706 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600707
Andy Fleming78bd5a72008-06-16 13:58:55 -0500708 while (base && lmbbase <= base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530709 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100710 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500711 /* This area isn't reserved, take it */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530712 if (lmb_add_region_flags(&lmb.used_mem, base,
Ilias Apalodimas98491252024-10-23 18:22:00 +0300713 size, flags))
Andy Fleming78bd5a72008-06-16 13:58:55 -0500714 return 0;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530715
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300716 ret = lmb_map_update_notify(base, size,
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100717 LMB_MAP_OP_RESERVE,
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300718 flags);
719 if (ret)
720 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530721
Andy Fleming78bd5a72008-06-16 13:58:55 -0500722 return base;
723 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530724
725 res_base = lmb_used[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500726 if (res_base < size)
727 break;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200728 base = ALIGN_DOWN(res_base - size, align);
Andy Fleming78bd5a72008-06-16 13:58:55 -0500729 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600730 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500731 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600732}
733
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530734phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530735{
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200736 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE, LMB_NONE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530737}
738
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200739phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr,
740 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530741{
742 phys_addr_t alloc;
743
Sughosh Ganu99b59662024-10-15 21:07:17 +0530744 alloc = _lmb_alloc_base(size, align, max_addr, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530745
746 if (alloc == 0)
747 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
748 (ulong)size, (ulong)max_addr);
749
750 return alloc;
751}
752
Ilias Apalodimas31bf8f12024-12-18 09:02:37 +0200753phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100754{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100755 long rgn;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200756 struct lmb_region *lmb_memory = lmb.available_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100757
758 /* Check if the requested address is in one of the memory regions */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200759 rgn = lmb_overlaps_region(&lmb.available_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100760 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100761 /*
762 * Check if the requested end address is in the same memory
763 * region we found.
764 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530765 if (lmb_addrs_overlap(lmb_memory[rgn].base,
766 lmb_memory[rgn].size,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100767 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100768 /* ok, reserve the memory */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200769 if (!lmb_reserve(base, size, flags))
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100770 return base;
771 }
772 }
Sughosh Ganu50e27272024-08-26 17:29:19 +0530773
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100774 return 0;
775}
776
777/* Return number of bytes from a given address that are free */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530778phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100779{
780 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100781 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530782 struct lmb_region *lmb_used = lmb.used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200783 struct lmb_region *lmb_memory = lmb.available_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100784
785 /* check if the requested address is in the memory regions */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200786 rgn = lmb_overlaps_region(&lmb.available_mem, addr, 1);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100787 if (rgn >= 0) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530788 for (i = 0; i < lmb.used_mem.count; i++) {
789 if (addr < lmb_used[i].base) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100790 /* first reserved range > requested address */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530791 return lmb_used[i].base - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100792 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530793 if (lmb_used[i].base +
794 lmb_used[i].size > addr) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100795 /* requested addr is in this reserved range */
796 return 0;
797 }
798 }
799 /* if we come here: no reserved ranges above requested addr */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200800 return lmb_memory[lmb.available_mem.count - 1].base +
801 lmb_memory[lmb.available_mem.count - 1].size - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100802 }
803 return 0;
804}
805
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530806int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600807{
808 int i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530809 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600810
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530811 for (i = 0; i < lmb.used_mem.count; i++) {
812 phys_addr_t upper = lmb_used[i].base +
813 lmb_used[i].size - 1;
814 if (addr >= lmb_used[i].base && addr <= upper)
815 return (lmb_used[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600816 }
817 return 0;
818}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500819
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530820static int lmb_setup(bool test)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530821{
822 bool ret;
823
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200824 ret = alist_init(&lmb.available_mem, sizeof(struct lmb_region),
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530825 (uint)LMB_ALIST_INITIAL_SIZE);
826 if (!ret) {
827 log_debug("Unable to initialise the list for LMB free memory\n");
828 return -ENOMEM;
829 }
830
831 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
832 (uint)LMB_ALIST_INITIAL_SIZE);
833 if (!ret) {
834 log_debug("Unable to initialise the list for LMB used memory\n");
835 return -ENOMEM;
836 }
837
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530838 lmb.test = test;
839
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530840 return 0;
841}
842
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530843int lmb_init(void)
844{
845 int ret;
846
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530847 ret = lmb_setup(false);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530848 if (ret) {
849 log_info("Unable to init LMB\n");
850 return ret;
851 }
852
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530853 lmb_add_memory();
854
Sughosh Ganu6518b412024-08-26 17:29:24 +0530855 /* Reserve the U-Boot image region once U-Boot has relocated */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600856 if (xpl_phase() == PHASE_SPL)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530857 lmb_reserve_common_spl();
Simon Glassd4dce4a2024-09-29 19:49:36 -0600858 else if (xpl_phase() == PHASE_BOARD_R)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530859 lmb_reserve_common((void *)gd->fdt_blob);
860
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530861 return 0;
862}
863
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530864struct lmb *lmb_get(void)
865{
866 return &lmb;
867}
868
Simon Glass176eba52024-10-21 10:19:31 +0200869#if CONFIG_IS_ENABLED(UNIT_TEST)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530870int lmb_push(struct lmb *store)
871{
872 int ret;
873
874 *store = lmb;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530875 ret = lmb_setup(true);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530876 if (ret)
877 return ret;
878
879 return 0;
880}
881
882void lmb_pop(struct lmb *store)
883{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200884 alist_uninit(&lmb.available_mem);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530885 alist_uninit(&lmb.used_mem);
886 lmb = *store;
887}
888#endif /* UNIT_TEST */