blob: 53af96fa2a9edf4a2c26515f158818f7c7dbdd85 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Gala6d7bfa82008-02-27 21:51:47 -06002/*
3 * Procedures for maintaining information about logical memory blocks.
4 *
5 * Peter Bergner, IBM Corp. June 2001.
6 * Copyright (C) 2001 Peter Bergner.
Kumar Gala6d7bfa82008-02-27 21:51:47 -06007 */
8
Sughosh Ganu291bf9c2024-08-26 17:29:18 +05309#include <alist.h>
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +010010#include <efi_loader.h>
Sughosh Ganua3af5ba2024-10-15 21:07:07 +053011#include <event.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060012#include <image.h>
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +010013#include <mapmem.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060014#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <malloc.h>
Sughosh Ganu6518b412024-08-26 17:29:24 +053017#include <spl.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060018
Marek Vasuta2eec022021-09-10 22:47:09 +020019#include <asm/global_data.h>
Marek Vasut0fcae7f2021-11-13 18:34:37 +010020#include <asm/sections.h>
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053021#include <linux/kernel.h>
Sughosh Ganue5348c72024-08-26 17:29:30 +053022#include <linux/sizes.h>
Marek Vasuta2eec022021-09-10 22:47:09 +020023
24DECLARE_GLOBAL_DATA_PTR;
25
Janne Grunau5631f442024-11-11 07:56:32 +010026/*
27 * The following low level LMB functions must not access the global LMB memory
28 * map since they are also used to manage IOVA memory maps in iommu drivers like
29 * apple_dart.
30 */
Kumar Gala6d7bfa82008-02-27 21:51:47 -060031
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010032static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
33 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060034{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010035 const phys_addr_t base1_end = base1 + size1 - 1;
36 const phys_addr_t base2_end = base2 + size2 - 1;
37
38 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala6d7bfa82008-02-27 21:51:47 -060039}
40
Becky Bruced26d67c2008-06-09 20:37:18 -050041static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010042 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060043{
44 if (base2 == base1 + size1)
45 return 1;
46 else if (base1 == base2 + size2)
47 return -1;
48
49 return 0;
50}
51
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053052static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1,
Udit Kumar4e8e6632023-09-26 16:54:42 +053053 unsigned long r2)
54{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053055 struct lmb_region *rgn = lmb_rgn_lst->data;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053056 phys_addr_t base1 = rgn[r1].base;
57 phys_size_t size1 = rgn[r1].size;
58 phys_addr_t base2 = rgn[r2].base;
59 phys_size_t size2 = rgn[r2].size;
Udit Kumar4e8e6632023-09-26 16:54:42 +053060
61 return lmb_addrs_overlap(base1, size1, base2, size2);
62}
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053063
64static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010065 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060066{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053067 struct lmb_region *rgn = lmb_rgn_lst->data;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053068 phys_addr_t base1 = rgn[r1].base;
69 phys_size_t size1 = rgn[r1].size;
70 phys_addr_t base2 = rgn[r2].base;
71 phys_size_t size2 = rgn[r2].size;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -060072
Kumar Gala6d7bfa82008-02-27 21:51:47 -060073 return lmb_addrs_adjacent(base1, size1, base2, size2);
74}
75
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053076static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060077{
78 unsigned long i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053079 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060080
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053081 for (i = r; i < lmb_rgn_lst->count - 1; i++) {
82 rgn[i].base = rgn[i + 1].base;
83 rgn[i].size = rgn[i + 1].size;
84 rgn[i].flags = rgn[i + 1].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060085 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053086 lmb_rgn_lst->count--;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060087}
88
89/* Assumption: base addr of region 1 < base addr of region 2 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053090static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010091 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060092{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053093 struct lmb_region *rgn = lmb_rgn_lst->data;
94
95 rgn[r1].size += rgn[r2].size;
96 lmb_remove_region(lmb_rgn_lst, r2);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060097}
98
Sughosh Ganu50e27272024-08-26 17:29:19 +053099static long lmb_resize_regions(struct alist *lmb_rgn_lst,
100 unsigned long idx_start,
101 phys_addr_t base, phys_size_t size)
102{
103 phys_size_t rgnsize;
104 unsigned long rgn_cnt, idx, idx_end;
105 phys_addr_t rgnbase, rgnend;
106 phys_addr_t mergebase, mergeend;
107 struct lmb_region *rgn = lmb_rgn_lst->data;
108
109 rgn_cnt = 0;
110 idx = idx_start;
111 idx_end = idx_start;
112
113 /*
114 * First thing to do is to identify how many regions
115 * the requested region overlaps.
116 * If the flags match, combine all these overlapping
117 * regions into a single region, and remove the merged
118 * regions.
119 */
120 while (idx <= lmb_rgn_lst->count - 1) {
121 rgnbase = rgn[idx].base;
122 rgnsize = rgn[idx].size;
123
124 if (lmb_addrs_overlap(base, size, rgnbase,
125 rgnsize)) {
126 if (rgn[idx].flags != LMB_NONE)
127 return -1;
128 rgn_cnt++;
129 idx_end = idx;
130 }
131 idx++;
132 }
133
134 /* The merged region's base and size */
135 rgnbase = rgn[idx_start].base;
136 mergebase = min(base, rgnbase);
137 rgnend = rgn[idx_end].base + rgn[idx_end].size;
138 mergeend = max(rgnend, (base + size));
139
140 rgn[idx_start].base = mergebase;
141 rgn[idx_start].size = mergeend - mergebase;
142
143 /* Now remove the merged regions */
144 while (--rgn_cnt)
145 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
146
147 return 0;
148}
149
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530150/**
151 * lmb_add_region_flags() - Add an lmb region to the given list
152 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
153 * @base: Start address of the region
154 * @size: Size of the region to be added
155 * @flags: Attributes of the LMB region
156 *
157 * Add a region of memory to the list. If the region does not exist, add
158 * it to the list. Depending on the attributes of the region to be added,
159 * the function might resize an already existing region or coalesce two
160 * adjacent regions.
161 *
Sam Protsenkob4f81102024-12-10 20:17:01 -0600162 * Return:
163 * * %0 - Added successfully, or it's already added (only if LMB_NONE)
164 * * %-EEXIST - The region is already added, and flags != LMB_NONE
165 * * %-1 - Failure
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530166 */
167static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200168 phys_size_t size, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600169{
170 unsigned long coalesced = 0;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530171 long ret, i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530172 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600173
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530174 if (alist_err(lmb_rgn_lst))
175 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600176
177 /* First try and coalesce this LMB with another. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530178 for (i = 0; i < lmb_rgn_lst->count; i++) {
179 phys_addr_t rgnbase = rgn[i].base;
180 phys_size_t rgnsize = rgn[i].size;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200181 u32 rgnflags = rgn[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600182
Sughosh Ganu50e27272024-08-26 17:29:19 +0530183 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
184 if (ret > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200185 if (flags != rgnflags)
186 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530187 rgn[i].base -= size;
188 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600189 coalesced++;
190 break;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530191 } else if (ret < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200192 if (flags != rgnflags)
Sughosh Ganu4ee65882025-03-03 19:02:28 +0530193 continue;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530194 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600195 coalesced++;
196 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100197 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sam Protsenkob4f81102024-12-10 20:17:01 -0600198 if (flags != LMB_NONE)
199 return -EEXIST;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530200
Sam Protsenkob4f81102024-12-10 20:17:01 -0600201 ret = lmb_resize_regions(lmb_rgn_lst, i, base, size);
202 if (ret < 0)
Sughosh Ganu50e27272024-08-26 17:29:19 +0530203 return -1;
Sam Protsenkob4f81102024-12-10 20:17:01 -0600204
205 coalesced++;
206 break;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600207
208 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600209 }
210 }
211
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530212 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
213 rgn = lmb_rgn_lst->data;
214 if (rgn[i].flags == rgn[i + 1].flags) {
215 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
216 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
217 coalesced++;
218 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
Sughosh Ganua92c5bb2025-03-03 19:02:27 +0530219 /* fix overlapping areas */
220 phys_addr_t rgnbase = rgn[i].base;
221 phys_size_t rgnsize = rgn[i].size;
222
223 ret = lmb_resize_regions(lmb_rgn_lst, i,
224 rgnbase, rgnsize);
225 if (ret < 0)
226 return -1;
227
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530228 coalesced++;
229 }
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200230 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600231 }
232
233 if (coalesced)
Ilias Apalodimas98491252024-10-23 18:22:00 +0300234 return 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530235
236 if (alist_full(lmb_rgn_lst) &&
237 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600238 return -1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530239 rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600240
241 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530242 for (i = lmb_rgn_lst->count; i >= 0; i--) {
243 if (i && base < rgn[i - 1].base) {
244 rgn[i] = rgn[i - 1];
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600245 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530246 rgn[i].base = base;
247 rgn[i].size = size;
248 rgn[i].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600249 break;
250 }
251 }
252
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530253 lmb_rgn_lst->count++;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600254
255 return 0;
256}
257
Janne Grunaud9c70402024-11-11 07:56:31 +0100258static long _lmb_free(struct alist *lmb_rgn_lst, phys_addr_t base,
259 phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500260{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530261 struct lmb_region *rgn;
Andy Fleming09d2a712008-07-07 14:24:39 -0500262 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100263 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500264 int i;
265
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600266 /* Suppress GCC warnings */
267 rgnbegin = 0;
268 rgnend = 0;
269
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530270 rgn = lmb_rgn_lst->data;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500271 /* Find the region where (base, size) belongs to */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530272 for (i = 0; i < lmb_rgn_lst->count; i++) {
273 rgnbegin = rgn[i].base;
274 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500275
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600276 if (rgnbegin <= base && end <= rgnend)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500277 break;
278 }
279
280 /* Didn't find the region */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530281 if (i == lmb_rgn_lst->count)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500282 return -1;
283
284 /* Check to see if we are removing entire region */
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600285 if (rgnbegin == base && rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530286 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500287 return 0;
288 }
289
290 /* Check to see if region is matching at the front */
291 if (rgnbegin == base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530292 rgn[i].base = end + 1;
293 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500294 return 0;
295 }
296
297 /* Check to see if the region is matching at the end */
298 if (rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530299 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500300 return 0;
301 }
302
303 /*
304 * We need to split the entry - adjust the current one to the
305 * beginging of the hole and add the region after hole.
306 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530307 rgn[i].size = base - rgn[i].base;
308 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
309 rgn[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500310}
311
Janne Grunau5631f442024-11-11 07:56:32 +0100312static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
313 phys_size_t size)
314{
315 unsigned long i;
316 struct lmb_region *rgn = lmb_rgn_lst->data;
317
318 for (i = 0; i < lmb_rgn_lst->count; i++) {
319 phys_addr_t rgnbase = rgn[i].base;
320 phys_size_t rgnsize = rgn[i].size;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600321
Janne Grunau5631f442024-11-11 07:56:32 +0100322 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
323 break;
324 }
325
326 return (i < lmb_rgn_lst->count) ? i : -1;
327}
328
Janne Grunau5631f442024-11-11 07:56:32 +0100329/*
Janne Grunaue818d3c2024-11-11 07:56:33 +0100330 * IOVA LMB memory maps using lmb pointers instead of the global LMB memory map.
331 */
332
333int io_lmb_setup(struct lmb *io_lmb)
334{
335 int ret;
336
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200337 ret = alist_init(&io_lmb->available_mem, sizeof(struct lmb_region),
Janne Grunaue818d3c2024-11-11 07:56:33 +0100338 (uint)LMB_ALIST_INITIAL_SIZE);
339 if (!ret) {
340 log_debug("Unable to initialise the list for LMB free IOVA\n");
341 return -ENOMEM;
342 }
343
344 ret = alist_init(&io_lmb->used_mem, sizeof(struct lmb_region),
345 (uint)LMB_ALIST_INITIAL_SIZE);
346 if (!ret) {
347 log_debug("Unable to initialise the list for LMB used IOVA\n");
348 return -ENOMEM;
349 }
350
351 io_lmb->test = false;
352
353 return 0;
354}
355
356void io_lmb_teardown(struct lmb *io_lmb)
357{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200358 alist_uninit(&io_lmb->available_mem);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100359 alist_uninit(&io_lmb->used_mem);
360}
361
362long io_lmb_add(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
363{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200364 return lmb_add_region_flags(&io_lmb->available_mem, base, size, LMB_NONE);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100365}
366
367/* derived and simplified from _lmb_alloc_base() */
368phys_addr_t io_lmb_alloc(struct lmb *io_lmb, phys_size_t size, ulong align)
369{
370 long i, rgn;
371 phys_addr_t base = 0;
372 phys_addr_t res_base;
373 struct lmb_region *lmb_used = io_lmb->used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200374 struct lmb_region *lmb_memory = io_lmb->available_mem.data;
Janne Grunaue818d3c2024-11-11 07:56:33 +0100375
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200376 for (i = io_lmb->available_mem.count - 1; i >= 0; i--) {
Janne Grunaue818d3c2024-11-11 07:56:33 +0100377 phys_addr_t lmbbase = lmb_memory[i].base;
378 phys_size_t lmbsize = lmb_memory[i].size;
379
380 if (lmbsize < size)
381 continue;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200382 base = ALIGN_DOWN(lmbbase + lmbsize - size, align);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100383
384 while (base && lmbbase <= base) {
385 rgn = lmb_overlaps_region(&io_lmb->used_mem, base, size);
386 if (rgn < 0) {
387 /* This area isn't reserved, take it */
388 if (lmb_add_region_flags(&io_lmb->used_mem, base,
389 size, LMB_NONE) < 0)
390 return 0;
391
392 return base;
393 }
394
395 res_base = lmb_used[rgn].base;
396 if (res_base < size)
397 break;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200398 base = ALIGN_DOWN(res_base - size, align);
Janne Grunaue818d3c2024-11-11 07:56:33 +0100399 }
400 }
401 return 0;
402}
403
404long io_lmb_free(struct lmb *io_lmb, phys_addr_t base, phys_size_t size)
405{
406 return _lmb_free(&io_lmb->used_mem, base, size);
407}
408
409/*
Janne Grunau5631f442024-11-11 07:56:32 +0100410 * Low level LMB functions are used to manage IOVA memory maps for the Apple
411 * dart iommu. They must not access the global LMB memory map.
412 * So keep the global LMB variable declaration unreachable from them.
413 */
414
415static struct lmb lmb;
416
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100417static int lmb_map_update_notify(phys_addr_t addr, phys_size_t size,
418 enum lmb_map_op op, u32 flags)
Janne Grunau5631f442024-11-11 07:56:32 +0100419{
Heinrich Schuchardtfabdb732025-02-16 12:12:40 +0100420 if (CONFIG_IS_ENABLED(EFI_LOADER) &&
421 !lmb.test && !(flags & LMB_NONOTIFY))
422 return efi_map_update_notify(addr, size, op);
Janne Grunau5631f442024-11-11 07:56:32 +0100423
424 return 0;
425}
426
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200427static void lmb_print_region_flags(u32 flags)
Janne Grunau5631f442024-11-11 07:56:32 +0100428{
Sam Protsenko41bf8f02024-12-10 20:25:48 -0600429 const char * const flag_str[] = { "none", "no-map", "no-overwrite",
430 "no-notify" };
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100431 unsigned int pflags = flags &
432 (LMB_NOMAP | LMB_NOOVERWRITE | LMB_NONOTIFY);
433
434 if (flags != pflags) {
435 printf("invalid %#x\n", flags);
436 return;
437 }
Janne Grunau5631f442024-11-11 07:56:32 +0100438
439 do {
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100440 int bitpos = pflags ? fls(pflags) - 1 : 0;
441
Janne Grunau5631f442024-11-11 07:56:32 +0100442 printf("%s", flag_str[bitpos]);
Heinrich Schuchardt55738202024-11-02 07:32:26 +0100443 pflags &= ~(1u << bitpos);
444 puts(pflags ? ", " : "\n");
445 } while (pflags);
Janne Grunau5631f442024-11-11 07:56:32 +0100446}
447
448static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
449{
450 struct lmb_region *rgn = lmb_rgn_lst->data;
451 unsigned long long base, size, end;
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200452 u32 flags;
Janne Grunau5631f442024-11-11 07:56:32 +0100453 int i;
454
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100455 printf(" %s.count = %#x\n", name, lmb_rgn_lst->count);
Janne Grunau5631f442024-11-11 07:56:32 +0100456
457 for (i = 0; i < lmb_rgn_lst->count; i++) {
458 base = rgn[i].base;
459 size = rgn[i].size;
460 end = base + size - 1;
461 flags = rgn[i].flags;
462
Heinrich Schuchardt2abe3052024-11-07 11:14:42 +0100463 printf(" %s[%d]\t[%#llx-%#llx], %#llx bytes, flags: ",
Janne Grunau5631f442024-11-11 07:56:32 +0100464 name, i, base, end, size);
465 lmb_print_region_flags(flags);
466 }
467}
468
469void lmb_dump_all_force(void)
470{
471 printf("lmb_dump_all:\n");
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200472 lmb_dump_region(&lmb.available_mem, "memory");
Janne Grunau5631f442024-11-11 07:56:32 +0100473 lmb_dump_region(&lmb.used_mem, "reserved");
474}
475
476void lmb_dump_all(void)
477{
478#ifdef DEBUG
479 lmb_dump_all_force();
480#endif
481}
482
483static void lmb_reserve_uboot_region(void)
484{
485 int bank;
486 ulong end, bank_end;
487 phys_addr_t rsv_start;
488
489 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
490 end = gd->ram_top;
491
492 /*
493 * Reserve memory from aligned address below the bottom of U-Boot stack
494 * until end of RAM area to prevent LMB from overwriting that memory.
495 */
496 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
497
498 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
499 if (!gd->bd->bi_dram[bank].size ||
500 rsv_start < gd->bd->bi_dram[bank].start)
501 continue;
502 /* Watch out for RAM at end of address space! */
503 bank_end = gd->bd->bi_dram[bank].start +
504 gd->bd->bi_dram[bank].size - 1;
505 if (rsv_start > bank_end)
506 continue;
507 if (bank_end > end)
508 bank_end = end - 1;
509
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200510 lmb_reserve(rsv_start, bank_end - rsv_start + 1, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100511
512 if (gd->flags & GD_FLG_SKIP_RELOC)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200513 lmb_reserve((phys_addr_t)(uintptr_t)_start,
514 gd->mon_len, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100515
516 break;
517 }
518}
519
520static void lmb_reserve_common(void *fdt_blob)
521{
522 lmb_reserve_uboot_region();
523
524 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
525 boot_fdt_add_mem_rsv_regions(fdt_blob);
526}
527
528static __maybe_unused void lmb_reserve_common_spl(void)
529{
530 phys_addr_t rsv_start;
531 phys_size_t rsv_size;
532
533 /*
534 * Assume a SPL stack of 16KB. This must be
535 * more than enough for the SPL stage.
536 */
537 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
538 rsv_start = gd->start_addr_sp - 16384;
539 rsv_size = 16384;
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200540 lmb_reserve(rsv_start, rsv_size, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100541 }
542
543 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
544 /* Reserve the bss region */
545 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
546 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
547 (phys_addr_t)(uintptr_t)__bss_start;
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200548 lmb_reserve(rsv_start, rsv_size, LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100549 }
550}
551
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530552/**
553 * lmb_can_reserve_region() - check if the region can be reserved
554 * @base: base address of region to be reserved
555 * @size: size of region to be reserved
556 * @flags: flag of the region to be reserved
557 *
558 * Go through all the reserved regions and ensure that the requested
559 * region does not overlap with any existing regions. An overlap is
560 * allowed only when the flag of the request region and the existing
561 * region is LMB_NONE.
562 *
563 * Return: true if region can be reserved, false otherwise
564 */
565static bool lmb_can_reserve_region(phys_addr_t base, phys_size_t size,
566 u32 flags)
567{
568 uint i;
569 struct lmb_region *lmb_reserved = lmb.used_mem.data;
570
571 for (i = 0; i < lmb.used_mem.count; i++) {
572 u32 rgnflags = lmb_reserved[i].flags;
573 phys_addr_t rgnbase = lmb_reserved[i].base;
574 phys_size_t rgnsize = lmb_reserved[i].size;
575
576 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
577 if (flags != LMB_NONE || flags != rgnflags)
578 return false;
579 }
580 }
581
582 return true;
583}
584
Janne Grunau5631f442024-11-11 07:56:32 +0100585void lmb_add_memory(void)
586{
587 int i;
Sughosh Ganued220b82024-12-02 12:36:24 +0530588 phys_addr_t bank_end;
Janne Grunau5631f442024-11-11 07:56:32 +0100589 phys_size_t size;
590 u64 ram_top = gd->ram_top;
591 struct bd_info *bd = gd->bd;
592
593 if (CONFIG_IS_ENABLED(LMB_ARCH_MEM_MAP))
594 return lmb_arch_add_memory();
595
596 /* Assume a 4GB ram_top if not defined */
597 if (!ram_top)
598 ram_top = 0x100000000ULL;
599
600 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
601 size = bd->bi_dram[i].size;
Sughosh Ganued220b82024-12-02 12:36:24 +0530602 bank_end = bd->bi_dram[i].start + size;
603
Janne Grunau5631f442024-11-11 07:56:32 +0100604 if (size) {
605 lmb_add(bd->bi_dram[i].start, size);
606
607 /*
608 * Reserve memory above ram_top as
609 * no-overwrite so that it cannot be
610 * allocated
611 */
612 if (bd->bi_dram[i].start >= ram_top)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200613 lmb_reserve(bd->bi_dram[i].start, size,
614 LMB_NOOVERWRITE);
Sughosh Ganued220b82024-12-02 12:36:24 +0530615 else if (bank_end > ram_top)
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200616 lmb_reserve(ram_top, bank_end - ram_top,
617 LMB_NOOVERWRITE);
Janne Grunau5631f442024-11-11 07:56:32 +0100618 }
619 }
620}
621
Janne Grunau5631f442024-11-11 07:56:32 +0100622/* This routine may be called with relocation disabled. */
623long lmb_add(phys_addr_t base, phys_size_t size)
624{
625 long ret;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200626 struct alist *lmb_rgn_lst = &lmb.available_mem;
Janne Grunau5631f442024-11-11 07:56:32 +0100627
Ilias Apalodimase2a6f972024-12-18 09:02:34 +0200628 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Janne Grunau5631f442024-11-11 07:56:32 +0100629 if (ret)
630 return ret;
631
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100632 return lmb_map_update_notify(base, size, LMB_MAP_OP_ADD, LMB_NONE);
Janne Grunau5631f442024-11-11 07:56:32 +0100633}
634
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530635long lmb_free_flags(phys_addr_t base, phys_size_t size,
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530636 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530637{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530638 long ret;
639
Janne Grunaud9c70402024-11-11 07:56:31 +0100640 ret = _lmb_free(&lmb.used_mem, base, size);
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530641 if (ret < 0)
642 return ret;
643
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100644 return lmb_map_update_notify(base, size, LMB_MAP_OP_FREE, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530645}
646
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530647long lmb_free(phys_addr_t base, phys_size_t size)
648{
649 return lmb_free_flags(base, size, LMB_NONE);
650}
651
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200652long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600653{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530654 long ret = 0;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530655 struct alist *lmb_rgn_lst = &lmb.used_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600656
Sughosh Ganu9fa71c92025-03-03 19:02:26 +0530657 if (!lmb_can_reserve_region(base, size, flags))
658 return -EEXIST;
659
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530660 ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Ilias Apalodimas98491252024-10-23 18:22:00 +0300661 if (ret)
662 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530663
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100664 return lmb_map_update_notify(base, size, LMB_MAP_OP_RESERVE, flags);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200665}
666
Sughosh Ganu99b59662024-10-15 21:07:17 +0530667static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align,
Ilias Apalodimasd8462bf2024-12-18 09:02:31 +0200668 phys_addr_t max_addr, u32 flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600669{
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530670 int ret;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100671 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500672 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500673 phys_addr_t res_base;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530674 struct lmb_region *lmb_used = lmb.used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200675 struct lmb_region *lmb_memory = lmb.available_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600676
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200677 for (i = lmb.available_mem.count - 1; i >= 0; i--) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530678 phys_addr_t lmbbase = lmb_memory[i].base;
679 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600680
Andy Fleming78bd5a72008-06-16 13:58:55 -0500681 if (lmbsize < size)
682 continue;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600683
684 if (max_addr == LMB_ALLOC_ANYWHERE) {
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200685 base = ALIGN_DOWN(lmbbase + lmbsize - size, align);
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600686 } else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600687 base = lmbbase + lmbsize;
688 if (base < lmbbase)
689 base = -1;
690 base = min(base, max_addr);
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200691 base = ALIGN_DOWN(base - size, align);
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600692 } else {
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600693 continue;
Sam Protsenkoc9a8b362024-12-10 20:25:49 -0600694 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600695
Andy Fleming78bd5a72008-06-16 13:58:55 -0500696 while (base && lmbbase <= base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530697 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100698 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500699 /* This area isn't reserved, take it */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530700 if (lmb_add_region_flags(&lmb.used_mem, base,
Ilias Apalodimas98491252024-10-23 18:22:00 +0300701 size, flags))
Andy Fleming78bd5a72008-06-16 13:58:55 -0500702 return 0;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530703
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300704 ret = lmb_map_update_notify(base, size,
Heinrich Schuchardt63bb30b2025-02-16 12:12:39 +0100705 LMB_MAP_OP_RESERVE,
Ilias Apalodimas2930f892024-10-23 18:22:01 +0300706 flags);
707 if (ret)
708 return ret;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530709
Andy Fleming78bd5a72008-06-16 13:58:55 -0500710 return base;
711 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530712
713 res_base = lmb_used[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500714 if (res_base < size)
715 break;
Ilias Apalodimas41e55ad2024-12-18 09:02:30 +0200716 base = ALIGN_DOWN(res_base - size, align);
Andy Fleming78bd5a72008-06-16 13:58:55 -0500717 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600718 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500719 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600720}
721
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530722phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530723{
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200724 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE, LMB_NONE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530725}
726
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200727phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr,
728 uint flags)
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530729{
730 phys_addr_t alloc;
731
Sughosh Ganu99b59662024-10-15 21:07:17 +0530732 alloc = _lmb_alloc_base(size, align, max_addr, flags);
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530733
734 if (alloc == 0)
735 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
736 (ulong)size, (ulong)max_addr);
737
738 return alloc;
739}
740
Ilias Apalodimas31bf8f12024-12-18 09:02:37 +0200741phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100742{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100743 long rgn;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200744 struct lmb_region *lmb_memory = lmb.available_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100745
746 /* Check if the requested address is in one of the memory regions */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200747 rgn = lmb_overlaps_region(&lmb.available_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100748 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100749 /*
750 * Check if the requested end address is in the same memory
751 * region we found.
752 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530753 if (lmb_addrs_overlap(lmb_memory[rgn].base,
754 lmb_memory[rgn].size,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100755 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100756 /* ok, reserve the memory */
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200757 if (!lmb_reserve(base, size, flags))
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100758 return base;
759 }
760 }
Sughosh Ganu50e27272024-08-26 17:29:19 +0530761
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100762 return 0;
763}
764
765/* Return number of bytes from a given address that are free */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530766phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100767{
768 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100769 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530770 struct lmb_region *lmb_used = lmb.used_mem.data;
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200771 struct lmb_region *lmb_memory = lmb.available_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100772
773 /* check if the requested address is in the memory regions */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200774 rgn = lmb_overlaps_region(&lmb.available_mem, addr, 1);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100775 if (rgn >= 0) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530776 for (i = 0; i < lmb.used_mem.count; i++) {
777 if (addr < lmb_used[i].base) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100778 /* first reserved range > requested address */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530779 return lmb_used[i].base - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100780 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530781 if (lmb_used[i].base +
782 lmb_used[i].size > addr) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100783 /* requested addr is in this reserved range */
784 return 0;
785 }
786 }
787 /* if we come here: no reserved ranges above requested addr */
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200788 return lmb_memory[lmb.available_mem.count - 1].base +
789 lmb_memory[lmb.available_mem.count - 1].size - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100790 }
791 return 0;
792}
793
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530794int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600795{
796 int i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530797 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600798
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530799 for (i = 0; i < lmb.used_mem.count; i++) {
800 phys_addr_t upper = lmb_used[i].base +
801 lmb_used[i].size - 1;
802 if (addr >= lmb_used[i].base && addr <= upper)
803 return (lmb_used[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600804 }
805 return 0;
806}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500807
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530808static int lmb_setup(bool test)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530809{
810 bool ret;
811
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200812 ret = alist_init(&lmb.available_mem, sizeof(struct lmb_region),
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530813 (uint)LMB_ALIST_INITIAL_SIZE);
814 if (!ret) {
815 log_debug("Unable to initialise the list for LMB free memory\n");
816 return -ENOMEM;
817 }
818
819 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
820 (uint)LMB_ALIST_INITIAL_SIZE);
821 if (!ret) {
822 log_debug("Unable to initialise the list for LMB used memory\n");
823 return -ENOMEM;
824 }
825
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530826 lmb.test = test;
827
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530828 return 0;
829}
830
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530831int lmb_init(void)
832{
833 int ret;
834
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530835 ret = lmb_setup(false);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530836 if (ret) {
837 log_info("Unable to init LMB\n");
838 return ret;
839 }
840
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530841 lmb_add_memory();
842
Sughosh Ganu6518b412024-08-26 17:29:24 +0530843 /* Reserve the U-Boot image region once U-Boot has relocated */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600844 if (xpl_phase() == PHASE_SPL)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530845 lmb_reserve_common_spl();
Simon Glassd4dce4a2024-09-29 19:49:36 -0600846 else if (xpl_phase() == PHASE_BOARD_R)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530847 lmb_reserve_common((void *)gd->fdt_blob);
848
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530849 return 0;
850}
851
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530852struct lmb *lmb_get(void)
853{
854 return &lmb;
855}
856
Simon Glass176eba52024-10-21 10:19:31 +0200857#if CONFIG_IS_ENABLED(UNIT_TEST)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530858int lmb_push(struct lmb *store)
859{
860 int ret;
861
862 *store = lmb;
Sughosh Ganua3af5ba2024-10-15 21:07:07 +0530863 ret = lmb_setup(true);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530864 if (ret)
865 return ret;
866
867 return 0;
868}
869
870void lmb_pop(struct lmb *store)
871{
Ilias Apalodimas5421c332024-12-18 09:02:33 +0200872 alist_uninit(&lmb.available_mem);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530873 alist_uninit(&lmb.used_mem);
874 lmb = *store;
875}
876#endif /* UNIT_TEST */