blob: dbbc19ede927a1c850d6a2cdce14915dda1e50eb [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Gala4ed65522008-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 Gala4ed65522008-02-27 21:51:47 -06007 */
8
Sughosh Ganued17a332024-08-26 17:29:18 +05309#include <alist.h>
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +010010#include <efi_loader.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060011#include <image.h>
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +010012#include <mapmem.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060013#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <malloc.h>
Sughosh Ganuf4fb1542024-08-26 17:29:24 +053016#include <spl.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060017
Marek Vasut12746982021-09-10 22:47:09 +020018#include <asm/global_data.h>
Marek Vasutbd994c02021-11-13 18:34:37 +010019#include <asm/sections.h>
Sughosh Ganued17a332024-08-26 17:29:18 +053020#include <linux/kernel.h>
Sughosh Ganu6534d262024-08-26 17:29:30 +053021#include <linux/sizes.h>
Marek Vasut12746982021-09-10 22:47:09 +020022
23DECLARE_GLOBAL_DATA_PTR;
24
Kumar Gala4ed65522008-02-27 21:51:47 -060025#define LMB_ALLOC_ANYWHERE 0
Sughosh Ganued17a332024-08-26 17:29:18 +053026#define LMB_ALIST_INITIAL_SIZE 4
Kumar Gala4ed65522008-02-27 21:51:47 -060027
Sughosh Ganued17a332024-08-26 17:29:18 +053028static struct lmb lmb;
29
30static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
Patrick Delaunay358c7782021-05-07 14:50:31 +020031{
Sughosh Ganued17a332024-08-26 17:29:18 +053032 struct lmb_region *rgn = lmb_rgn_lst->data;
Patrick Delaunay358c7782021-05-07 14:50:31 +020033 unsigned long long base, size, end;
34 enum lmb_flags flags;
35 int i;
36
Sughosh Ganued17a332024-08-26 17:29:18 +053037 printf(" %s.count = 0x%x\n", name, lmb_rgn_lst->count);
Patrick Delaunay358c7782021-05-07 14:50:31 +020038
Sughosh Ganued17a332024-08-26 17:29:18 +053039 for (i = 0; i < lmb_rgn_lst->count; i++) {
40 base = rgn[i].base;
41 size = rgn[i].size;
Patrick Delaunay358c7782021-05-07 14:50:31 +020042 end = base + size - 1;
Sughosh Ganued17a332024-08-26 17:29:18 +053043 flags = rgn[i].flags;
Patrick Delaunay358c7782021-05-07 14:50:31 +020044
45 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
46 name, i, base, end, size, flags);
47 }
48}
49
Sughosh Ganued17a332024-08-26 17:29:18 +053050void lmb_dump_all_force(void)
Tero Kristo9996cea2020-07-20 11:10:45 +030051{
Tero Kristo9996cea2020-07-20 11:10:45 +030052 printf("lmb_dump_all:\n");
Sughosh Ganued17a332024-08-26 17:29:18 +053053 lmb_dump_region(&lmb.free_mem, "memory");
54 lmb_dump_region(&lmb.used_mem, "reserved");
Tero Kristo9996cea2020-07-20 11:10:45 +030055}
56
Sughosh Ganued17a332024-08-26 17:29:18 +053057void lmb_dump_all(void)
Kumar Gala4ed65522008-02-27 21:51:47 -060058{
59#ifdef DEBUG
Sughosh Ganued17a332024-08-26 17:29:18 +053060 lmb_dump_all_force();
Tero Kristo9996cea2020-07-20 11:10:45 +030061#endif
Kumar Gala4ed65522008-02-27 21:51:47 -060062}
63
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010064static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
65 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -060066{
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +010067 const phys_addr_t base1_end = base1 + size1 - 1;
68 const phys_addr_t base2_end = base2 + size2 - 1;
69
70 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala4ed65522008-02-27 21:51:47 -060071}
72
Becky Bruce391fd932008-06-09 20:37:18 -050073static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010074 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -060075{
76 if (base2 == base1 + size1)
77 return 1;
78 else if (base1 == base2 + size2)
79 return -1;
80
81 return 0;
82}
83
Sughosh Ganued17a332024-08-26 17:29:18 +053084static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1,
Udit Kumaredb58242023-09-26 16:54:42 +053085 unsigned long r2)
86{
Sughosh Ganued17a332024-08-26 17:29:18 +053087 struct lmb_region *rgn = lmb_rgn_lst->data;
88
89 phys_addr_t base1 = rgn[r1].base;
90 phys_size_t size1 = rgn[r1].size;
91 phys_addr_t base2 = rgn[r2].base;
92 phys_size_t size2 = rgn[r2].size;
Udit Kumaredb58242023-09-26 16:54:42 +053093
94 return lmb_addrs_overlap(base1, size1, base2, size2);
95}
Sughosh Ganued17a332024-08-26 17:29:18 +053096
97static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010098 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -060099{
Sughosh Ganued17a332024-08-26 17:29:18 +0530100 struct lmb_region *rgn = lmb_rgn_lst->data;
101
102 phys_addr_t base1 = rgn[r1].base;
103 phys_size_t size1 = rgn[r1].size;
104 phys_addr_t base2 = rgn[r2].base;
105 phys_size_t size2 = rgn[r2].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600106 return lmb_addrs_adjacent(base1, size1, base2, size2);
107}
108
Sughosh Ganued17a332024-08-26 17:29:18 +0530109static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
Kumar Gala4ed65522008-02-27 21:51:47 -0600110{
111 unsigned long i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530112 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600113
Sughosh Ganued17a332024-08-26 17:29:18 +0530114 for (i = r; i < lmb_rgn_lst->count - 1; i++) {
115 rgn[i].base = rgn[i + 1].base;
116 rgn[i].size = rgn[i + 1].size;
117 rgn[i].flags = rgn[i + 1].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600118 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530119 lmb_rgn_lst->count--;
Kumar Gala4ed65522008-02-27 21:51:47 -0600120}
121
122/* Assumption: base addr of region 1 < base addr of region 2 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530123static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100124 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600125{
Sughosh Ganued17a332024-08-26 17:29:18 +0530126 struct lmb_region *rgn = lmb_rgn_lst->data;
127
128 rgn[r1].size += rgn[r2].size;
129 lmb_remove_region(lmb_rgn_lst, r2);
Kumar Gala4ed65522008-02-27 21:51:47 -0600130}
131
Udit Kumaredb58242023-09-26 16:54:42 +0530132/*Assumption : base addr of region 1 < base addr of region 2*/
Sughosh Ganued17a332024-08-26 17:29:18 +0530133static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst,
134 unsigned long r1, unsigned long r2)
Udit Kumaredb58242023-09-26 16:54:42 +0530135{
Sughosh Ganued17a332024-08-26 17:29:18 +0530136 struct lmb_region *rgn = lmb_rgn_lst->data;
137
138 phys_addr_t base1 = rgn[r1].base;
139 phys_size_t size1 = rgn[r1].size;
140 phys_addr_t base2 = rgn[r2].base;
141 phys_size_t size2 = rgn[r2].size;
Udit Kumaredb58242023-09-26 16:54:42 +0530142
143 if (base1 + size1 > base2 + size2) {
144 printf("This will not be a case any time\n");
145 return;
146 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530147 rgn[r1].size = base2 + size2 - base1;
148 lmb_remove_region(lmb_rgn_lst, r2);
Udit Kumaredb58242023-09-26 16:54:42 +0530149}
150
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +0100151/**
152 * efi_lmb_reserve() - add reservations for EFI memory
153 *
154 * Add reservations for all EFI memory areas that are not
155 * EFI_CONVENTIONAL_MEMORY.
156 *
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +0100157 * Return: 0 on success, 1 on failure
158 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530159static __maybe_unused int efi_lmb_reserve(void)
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +0100160{
161 struct efi_mem_desc *memmap = NULL, *map;
162 efi_uintn_t i, map_size = 0;
163 efi_status_t ret;
164
165 ret = efi_get_memory_map_alloc(&map_size, &memmap);
166 if (ret != EFI_SUCCESS)
167 return 1;
168
169 for (i = 0, map = memmap; i < map_size / sizeof(*map); ++map, ++i) {
Sjoerd Simonsc5279ea2023-01-19 09:38:18 +0100170 if (map->type != EFI_CONVENTIONAL_MEMORY) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530171 lmb_reserve_flags(map_to_sysmem((void *)(uintptr_t)
Sjoerd Simonsc5279ea2023-01-19 09:38:18 +0100172 map->physical_start),
173 map->num_pages * EFI_PAGE_SIZE,
174 map->type == EFI_RESERVED_MEMORY_TYPE
175 ? LMB_NOMAP : LMB_NONE);
176 }
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +0100177 }
178 efi_free_pool(memmap);
179
180 return 0;
181}
182
Sughosh Ganu6534d262024-08-26 17:29:30 +0530183static void lmb_reserve_uboot_region(void)
184{
185 int bank;
186 ulong end, bank_end;
187 phys_addr_t rsv_start;
188
189 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
190 end = gd->ram_top;
191
192 /*
193 * Reserve memory from aligned address below the bottom of U-Boot stack
194 * until end of RAM area to prevent LMB from overwriting that memory.
195 */
196 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
197
198 /* adjust sp by 16K to be safe */
199 rsv_start -= SZ_16K;
200 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
201 if (!gd->bd->bi_dram[bank].size ||
202 rsv_start < gd->bd->bi_dram[bank].start)
203 continue;
204 /* Watch out for RAM at end of address space! */
205 bank_end = gd->bd->bi_dram[bank].start +
206 gd->bd->bi_dram[bank].size - 1;
207 if (rsv_start > bank_end)
208 continue;
209 if (bank_end > end)
210 bank_end = end - 1;
211
212 lmb_reserve_flags(rsv_start, bank_end - rsv_start + 1,
213 LMB_NOOVERWRITE);
214
215 if (gd->flags & GD_FLG_SKIP_RELOC)
216 lmb_reserve_flags((phys_addr_t)(uintptr_t)_start,
217 gd->mon_len, LMB_NOOVERWRITE);
218
219 break;
220 }
221}
222
Sughosh Ganued17a332024-08-26 17:29:18 +0530223static void lmb_reserve_common(void *fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100224{
Sughosh Ganued17a332024-08-26 17:29:18 +0530225 board_lmb_reserve();
Sughosh Ganu6534d262024-08-26 17:29:30 +0530226 lmb_reserve_uboot_region();
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100227
Simon Glass0c303f92021-09-25 19:43:21 -0600228 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Sughosh Ganued17a332024-08-26 17:29:18 +0530229 boot_fdt_add_mem_rsv_regions(fdt_blob);
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +0100230
231 if (CONFIG_IS_ENABLED(EFI_LOADER))
Sughosh Ganued17a332024-08-26 17:29:18 +0530232 efi_lmb_reserve();
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100233}
234
Sughosh Ganuf4fb1542024-08-26 17:29:24 +0530235static __maybe_unused void lmb_reserve_common_spl(void)
236{
237 phys_addr_t rsv_start;
238 phys_size_t rsv_size;
239
240 /*
241 * Assume a SPL stack of 16KB. This must be
242 * more than enough for the SPL stage.
243 */
244 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
245 rsv_start = gd->start_addr_sp - 16384;
246 rsv_size = 16384;
247 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
248 }
249
250 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
251 /* Reserve the bss region */
252 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
253 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
254 (phys_addr_t)(uintptr_t)__bss_start;
255 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
256 }
257}
258
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530259/**
260 * lmb_add_memory() - Add memory range for LMB allocations
261 *
262 * Add the entire available memory range to the pool of memory that
263 * can be used by the LMB module for allocations.
264 *
265 * Return: None
266 */
267void lmb_add_memory(void)
268{
269 int i;
270 phys_size_t size;
271 phys_addr_t rgn_top;
272 u64 ram_top = gd->ram_top;
273 struct bd_info *bd = gd->bd;
274
275 /* Assume a 4GB ram_top if not defined */
276 if (!ram_top)
277 ram_top = 0x100000000ULL;
278
279 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
280 size = bd->bi_dram[i].size;
281 if (size) {
282 if (bd->bi_dram[i].start > ram_top)
283 continue;
284
285 rgn_top = bd->bi_dram[i].start +
286 bd->bi_dram[i].size;
287
288 if (rgn_top > ram_top)
289 size -= rgn_top - ram_top;
290
291 lmb_add(bd->bi_dram[i].start, size);
292 }
293 }
294}
295
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530296static long lmb_resize_regions(struct alist *lmb_rgn_lst,
297 unsigned long idx_start,
298 phys_addr_t base, phys_size_t size)
299{
300 phys_size_t rgnsize;
301 unsigned long rgn_cnt, idx, idx_end;
302 phys_addr_t rgnbase, rgnend;
303 phys_addr_t mergebase, mergeend;
304 struct lmb_region *rgn = lmb_rgn_lst->data;
305
306 rgn_cnt = 0;
307 idx = idx_start;
308 idx_end = idx_start;
309
310 /*
311 * First thing to do is to identify how many regions
312 * the requested region overlaps.
313 * If the flags match, combine all these overlapping
314 * regions into a single region, and remove the merged
315 * regions.
316 */
317 while (idx <= lmb_rgn_lst->count - 1) {
318 rgnbase = rgn[idx].base;
319 rgnsize = rgn[idx].size;
320
321 if (lmb_addrs_overlap(base, size, rgnbase,
322 rgnsize)) {
323 if (rgn[idx].flags != LMB_NONE)
324 return -1;
325 rgn_cnt++;
326 idx_end = idx;
327 }
328 idx++;
329 }
330
331 /* The merged region's base and size */
332 rgnbase = rgn[idx_start].base;
333 mergebase = min(base, rgnbase);
334 rgnend = rgn[idx_end].base + rgn[idx_end].size;
335 mergeend = max(rgnend, (base + size));
336
337 rgn[idx_start].base = mergebase;
338 rgn[idx_start].size = mergeend - mergebase;
339
340 /* Now remove the merged regions */
341 while (--rgn_cnt)
342 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
343
344 return 0;
345}
346
Sughosh Ganued17a332024-08-26 17:29:18 +0530347/**
348 * lmb_add_region_flags() - Add an lmb region to the given list
349 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
350 * @base: Start address of the region
351 * @size: Size of the region to be added
352 * @flags: Attributes of the LMB region
353 *
354 * Add a region of memory to the list. If the region does not exist, add
355 * it to the list. Depending on the attributes of the region to be added,
356 * the function might resize an already existing region or coalesce two
357 * adjacent regions.
358 *
359 *
360 * Returns: 0 if the region addition successful, -1 on failure
361 */
362static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200363 phys_size_t size, enum lmb_flags flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600364{
365 unsigned long coalesced = 0;
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530366 long ret, i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530367 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600368
Sughosh Ganued17a332024-08-26 17:29:18 +0530369 if (alist_err(lmb_rgn_lst))
370 return -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600371
372 /* First try and coalesce this LMB with another. */
Sughosh Ganued17a332024-08-26 17:29:18 +0530373 for (i = 0; i < lmb_rgn_lst->count; i++) {
374 phys_addr_t rgnbase = rgn[i].base;
375 phys_size_t rgnsize = rgn[i].size;
376 phys_size_t rgnflags = rgn[i].flags;
Sjoerd Simons0d91c882023-02-12 16:07:05 +0100377 phys_addr_t end = base + size - 1;
378 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Sjoerd Simons0d91c882023-02-12 16:07:05 +0100379 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200380 if (flags == rgnflags)
381 /* Already have this region, so we're done */
382 return 0;
383 else
384 return -1; /* regions with new flags */
385 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600386
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530387 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
388 if (ret > 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200389 if (flags != rgnflags)
390 break;
Sughosh Ganued17a332024-08-26 17:29:18 +0530391 rgn[i].base -= size;
392 rgn[i].size += size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600393 coalesced++;
394 break;
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530395 } else if (ret < 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200396 if (flags != rgnflags)
397 break;
Sughosh Ganued17a332024-08-26 17:29:18 +0530398 rgn[i].size += size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600399 coalesced++;
400 break;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100401 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530402 if (flags == LMB_NONE) {
403 ret = lmb_resize_regions(lmb_rgn_lst, i, base,
404 size);
405 if (ret < 0)
406 return -1;
407
408 coalesced++;
409 break;
410 } else {
411 return -1;
412 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600413 }
414 }
415
Sughosh Ganued17a332024-08-26 17:29:18 +0530416 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
417 rgn = lmb_rgn_lst->data;
418 if (rgn[i].flags == rgn[i + 1].flags) {
419 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
420 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
421 coalesced++;
422 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
423 /* fix overlapping area */
424 lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
425 coalesced++;
426 }
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200427 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600428 }
429
430 if (coalesced)
431 return coalesced;
Sughosh Ganued17a332024-08-26 17:29:18 +0530432
433 if (alist_full(lmb_rgn_lst) &&
434 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala4ed65522008-02-27 21:51:47 -0600435 return -1;
Sughosh Ganued17a332024-08-26 17:29:18 +0530436 rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600437
438 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganued17a332024-08-26 17:29:18 +0530439 for (i = lmb_rgn_lst->count; i >= 0; i--) {
440 if (i && base < rgn[i - 1].base) {
441 rgn[i] = rgn[i - 1];
Kumar Gala4ed65522008-02-27 21:51:47 -0600442 } else {
Sughosh Ganued17a332024-08-26 17:29:18 +0530443 rgn[i].base = base;
444 rgn[i].size = size;
445 rgn[i].flags = flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600446 break;
447 }
448 }
449
Sughosh Ganued17a332024-08-26 17:29:18 +0530450 lmb_rgn_lst->count++;
Kumar Gala4ed65522008-02-27 21:51:47 -0600451
452 return 0;
453}
454
Sughosh Ganued17a332024-08-26 17:29:18 +0530455static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200456 phys_size_t size)
457{
Sughosh Ganued17a332024-08-26 17:29:18 +0530458 return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200459}
460
Kumar Gala4ed65522008-02-27 21:51:47 -0600461/* This routine may be called with relocation disabled. */
Sughosh Ganued17a332024-08-26 17:29:18 +0530462long lmb_add(phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600463{
Sughosh Ganued17a332024-08-26 17:29:18 +0530464 struct alist *lmb_rgn_lst = &lmb.free_mem;
Kumar Gala4ed65522008-02-27 21:51:47 -0600465
Sughosh Ganued17a332024-08-26 17:29:18 +0530466 return lmb_add_region(lmb_rgn_lst, base, size);
Kumar Gala4ed65522008-02-27 21:51:47 -0600467}
468
Sughosh Ganued17a332024-08-26 17:29:18 +0530469long lmb_free(phys_addr_t base, phys_size_t size)
Andy Fleming63796c42008-06-16 13:58:54 -0500470{
Sughosh Ganued17a332024-08-26 17:29:18 +0530471 struct lmb_region *rgn;
472 struct alist *lmb_rgn_lst = &lmb.used_mem;
Andy Fleming98874ff2008-07-07 14:24:39 -0500473 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100474 phys_addr_t end = base + size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500475 int i;
476
477 rgnbegin = rgnend = 0; /* supress gcc warnings */
Sughosh Ganued17a332024-08-26 17:29:18 +0530478 rgn = lmb_rgn_lst->data;
Andy Fleming63796c42008-06-16 13:58:54 -0500479 /* Find the region where (base, size) belongs to */
Sughosh Ganued17a332024-08-26 17:29:18 +0530480 for (i = 0; i < lmb_rgn_lst->count; i++) {
481 rgnbegin = rgn[i].base;
482 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500483
484 if ((rgnbegin <= base) && (end <= rgnend))
485 break;
486 }
487
488 /* Didn't find the region */
Sughosh Ganued17a332024-08-26 17:29:18 +0530489 if (i == lmb_rgn_lst->count)
Andy Fleming63796c42008-06-16 13:58:54 -0500490 return -1;
491
492 /* Check to see if we are removing entire region */
493 if ((rgnbegin == base) && (rgnend == end)) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530494 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming63796c42008-06-16 13:58:54 -0500495 return 0;
496 }
497
498 /* Check to see if region is matching at the front */
499 if (rgnbegin == base) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530500 rgn[i].base = end + 1;
501 rgn[i].size -= size;
Andy Fleming63796c42008-06-16 13:58:54 -0500502 return 0;
503 }
504
505 /* Check to see if the region is matching at the end */
506 if (rgnend == end) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530507 rgn[i].size -= size;
Andy Fleming63796c42008-06-16 13:58:54 -0500508 return 0;
509 }
510
511 /*
512 * We need to split the entry - adjust the current one to the
513 * beginging of the hole and add the region after hole.
514 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530515 rgn[i].size = base - rgn[i].base;
516 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
517 rgn[i].flags);
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200518}
519
Sughosh Ganued17a332024-08-26 17:29:18 +0530520long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags)
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200521{
Sughosh Ganued17a332024-08-26 17:29:18 +0530522 struct alist *lmb_rgn_lst = &lmb.used_mem;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200523
Sughosh Ganued17a332024-08-26 17:29:18 +0530524 return lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Andy Fleming63796c42008-06-16 13:58:54 -0500525}
526
Sughosh Ganued17a332024-08-26 17:29:18 +0530527long lmb_reserve(phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600528{
Sughosh Ganued17a332024-08-26 17:29:18 +0530529 return lmb_reserve_flags(base, size, LMB_NONE);
Kumar Gala4ed65522008-02-27 21:51:47 -0600530}
531
Sughosh Ganued17a332024-08-26 17:29:18 +0530532static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Becky Bruce391fd932008-06-09 20:37:18 -0500533 phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600534{
535 unsigned long i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530536 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600537
Sughosh Ganued17a332024-08-26 17:29:18 +0530538 for (i = 0; i < lmb_rgn_lst->count; i++) {
539 phys_addr_t rgnbase = rgn[i].base;
540 phys_size_t rgnsize = rgn[i].size;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100541 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala4ed65522008-02-27 21:51:47 -0600542 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600543 }
544
Sughosh Ganued17a332024-08-26 17:29:18 +0530545 return (i < lmb_rgn_lst->count) ? i : -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600546}
547
Becky Bruce391fd932008-06-09 20:37:18 -0500548static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600549{
550 return addr & ~(size - 1);
551}
552
Sughosh Ganued17a332024-08-26 17:29:18 +0530553static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align,
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530554 phys_addr_t max_addr, enum lmb_flags flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600555{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100556 long i, rgn;
Becky Bruce391fd932008-06-09 20:37:18 -0500557 phys_addr_t base = 0;
Andy Fleming7570a992008-06-16 13:58:55 -0500558 phys_addr_t res_base;
Sughosh Ganued17a332024-08-26 17:29:18 +0530559 struct lmb_region *lmb_used = lmb.used_mem.data;
560 struct lmb_region *lmb_memory = lmb.free_mem.data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600561
Sughosh Ganued17a332024-08-26 17:29:18 +0530562 for (i = lmb.free_mem.count - 1; i >= 0; i--) {
563 phys_addr_t lmbbase = lmb_memory[i].base;
564 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600565
Andy Fleming7570a992008-06-16 13:58:55 -0500566 if (lmbsize < size)
567 continue;
Kumar Gala4ed65522008-02-27 21:51:47 -0600568 if (max_addr == LMB_ALLOC_ANYWHERE)
569 base = lmb_align_down(lmbbase + lmbsize - size, align);
570 else if (lmbbase < max_addr) {
Stephen Warrenad3fda52014-07-31 13:40:07 -0600571 base = lmbbase + lmbsize;
572 if (base < lmbbase)
573 base = -1;
574 base = min(base, max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600575 base = lmb_align_down(base - size, align);
576 } else
577 continue;
578
Andy Fleming7570a992008-06-16 13:58:55 -0500579 while (base && lmbbase <= base) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530580 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100581 if (rgn < 0) {
Andy Fleming7570a992008-06-16 13:58:55 -0500582 /* This area isn't reserved, take it */
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530583 if (lmb_add_region_flags(&lmb.used_mem, base,
584 size, flags) < 0)
Andy Fleming7570a992008-06-16 13:58:55 -0500585 return 0;
586 return base;
587 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530588
589 res_base = lmb_used[rgn].base;
Andy Fleming7570a992008-06-16 13:58:55 -0500590 if (res_base < size)
591 break;
592 base = lmb_align_down(res_base - size, align);
593 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600594 }
Andy Fleming7570a992008-06-16 13:58:55 -0500595 return 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600596}
597
Sughosh Ganued17a332024-08-26 17:29:18 +0530598phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530599{
Sughosh Ganued17a332024-08-26 17:29:18 +0530600 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530601}
602
Sughosh Ganued17a332024-08-26 17:29:18 +0530603phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530604{
605 phys_addr_t alloc;
606
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530607 alloc = __lmb_alloc_base(size, align, max_addr, LMB_NONE);
Sughosh Ganu3d679ae2024-08-26 17:29:16 +0530608
609 if (alloc == 0)
610 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
611 (ulong)size, (ulong)max_addr);
612
613 return alloc;
614}
615
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530616static phys_addr_t __lmb_alloc_addr(phys_addr_t base, phys_size_t size,
617 enum lmb_flags flags)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100618{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100619 long rgn;
Sughosh Ganued17a332024-08-26 17:29:18 +0530620 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100621
622 /* Check if the requested address is in one of the memory regions */
Sughosh Ganued17a332024-08-26 17:29:18 +0530623 rgn = lmb_overlaps_region(&lmb.free_mem, base, size);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100624 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100625 /*
626 * Check if the requested end address is in the same memory
627 * region we found.
628 */
Sughosh Ganued17a332024-08-26 17:29:18 +0530629 if (lmb_addrs_overlap(lmb_memory[rgn].base,
630 lmb_memory[rgn].size,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100631 base + size - 1, 1)) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100632 /* ok, reserve the memory */
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530633 if (lmb_reserve_flags(base, size, flags) >= 0)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100634 return base;
635 }
636 }
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530637
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100638 return 0;
639}
640
Sughosh Ganu5e9553c2024-08-26 17:29:19 +0530641/*
642 * Try to allocate a specific address range: must be in defined memory but not
643 * reserved
644 */
645phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
646{
647 return __lmb_alloc_addr(base, size, LMB_NONE);
648}
649
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100650/* Return number of bytes from a given address that are free */
Sughosh Ganued17a332024-08-26 17:29:18 +0530651phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100652{
653 int i;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100654 long rgn;
Sughosh Ganued17a332024-08-26 17:29:18 +0530655 struct lmb_region *lmb_used = lmb.used_mem.data;
656 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100657
658 /* check if the requested address is in the memory regions */
Sughosh Ganued17a332024-08-26 17:29:18 +0530659 rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100660 if (rgn >= 0) {
Sughosh Ganued17a332024-08-26 17:29:18 +0530661 for (i = 0; i < lmb.used_mem.count; i++) {
662 if (addr < lmb_used[i].base) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100663 /* first reserved range > requested address */
Sughosh Ganued17a332024-08-26 17:29:18 +0530664 return lmb_used[i].base - addr;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100665 }
Sughosh Ganued17a332024-08-26 17:29:18 +0530666 if (lmb_used[i].base +
667 lmb_used[i].size > addr) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100668 /* requested addr is in this reserved range */
669 return 0;
670 }
671 }
672 /* if we come here: no reserved ranges above requested addr */
Sughosh Ganued17a332024-08-26 17:29:18 +0530673 return lmb_memory[lmb.free_mem.count - 1].base +
674 lmb_memory[lmb.free_mem.count - 1].size - addr;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100675 }
676 return 0;
677}
678
Sughosh Ganued17a332024-08-26 17:29:18 +0530679int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600680{
681 int i;
Sughosh Ganued17a332024-08-26 17:29:18 +0530682 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala4ed65522008-02-27 21:51:47 -0600683
Sughosh Ganued17a332024-08-26 17:29:18 +0530684 for (i = 0; i < lmb.used_mem.count; i++) {
685 phys_addr_t upper = lmb_used[i].base +
686 lmb_used[i].size - 1;
687 if (addr >= lmb_used[i].base && addr <= upper)
688 return (lmb_used[i].flags & flags) == flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600689 }
690 return 0;
691}
Mike Frysingera16028d2009-11-03 11:35:59 -0500692
Sughosh Ganued17a332024-08-26 17:29:18 +0530693__weak void board_lmb_reserve(void)
Mike Frysingera16028d2009-11-03 11:35:59 -0500694{
695 /* please define platform specific board_lmb_reserve() */
696}
Mike Frysingera16028d2009-11-03 11:35:59 -0500697
Sughosh Ganued17a332024-08-26 17:29:18 +0530698static int lmb_setup(void)
699{
700 bool ret;
701
702 ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region),
703 (uint)LMB_ALIST_INITIAL_SIZE);
704 if (!ret) {
705 log_debug("Unable to initialise the list for LMB free memory\n");
706 return -ENOMEM;
707 }
708
709 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
710 (uint)LMB_ALIST_INITIAL_SIZE);
711 if (!ret) {
712 log_debug("Unable to initialise the list for LMB used memory\n");
713 return -ENOMEM;
714 }
715
716 return 0;
717}
718
719/**
720 * lmb_init() - Initialise the LMB module
721 *
722 * Initialise the LMB lists needed for keeping the memory map. There
723 * are two lists, in form of alloced list data structure. One for the
724 * available memory, and one for the used memory. Initialise the two
725 * lists as part of board init. Add memory to the available memory
726 * list and reserve common areas by adding them to the used memory
727 * list.
728 *
729 * Return: 0 on success, -ve on error
730 */
731int lmb_init(void)
732{
733 int ret;
734
735 ret = lmb_setup();
736 if (ret) {
737 log_info("Unable to init LMB\n");
738 return ret;
739 }
740
Sughosh Ganu8a9fc302024-08-26 17:29:23 +0530741 lmb_add_memory();
742
Sughosh Ganuf4fb1542024-08-26 17:29:24 +0530743 /* Reserve the U-Boot image region once U-Boot has relocated */
744 if (spl_phase() == PHASE_SPL)
745 lmb_reserve_common_spl();
746 else if (spl_phase() == PHASE_BOARD_R)
747 lmb_reserve_common((void *)gd->fdt_blob);
748
Sughosh Ganued17a332024-08-26 17:29:18 +0530749 return 0;
750}
751
752#if CONFIG_IS_ENABLED(UNIT_TEST)
753struct lmb *lmb_get(void)
754{
755 return &lmb;
756}
757
758int lmb_push(struct lmb *store)
759{
760 int ret;
761
762 *store = lmb;
763 ret = lmb_setup();
764 if (ret)
765 return ret;
766
767 return 0;
768}
769
770void lmb_pop(struct lmb *store)
771{
772 alist_uninit(&lmb.free_mem);
773 alist_uninit(&lmb.used_mem);
774 lmb = *store;
775}
776#endif /* UNIT_TEST */