blob: 3ed570fb29b11c78eca2d2ed4b3653c334fa2baf [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>
Simon Glass2dc9c342020-05-10 11:40:01 -060011#include <image.h>
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +010012#include <mapmem.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060013#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <malloc.h>
Sughosh Ganu6518b412024-08-26 17:29:24 +053016#include <spl.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060017
Marek Vasuta2eec022021-09-10 22:47:09 +020018#include <asm/global_data.h>
Marek Vasut0fcae7f2021-11-13 18:34:37 +010019#include <asm/sections.h>
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053020#include <linux/kernel.h>
Sughosh Ganue5348c72024-08-26 17:29:30 +053021#include <linux/sizes.h>
Marek Vasuta2eec022021-09-10 22:47:09 +020022
23DECLARE_GLOBAL_DATA_PTR;
24
Kumar Gala6d7bfa82008-02-27 21:51:47 -060025#define LMB_ALLOC_ANYWHERE 0
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053026#define LMB_ALIST_INITIAL_SIZE 4
Kumar Gala6d7bfa82008-02-27 21:51:47 -060027
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053028static struct lmb lmb;
29
Sughosh Ganuf04416f2024-08-26 17:29:40 +053030static void lmb_print_region_flags(enum lmb_flags flags)
31{
32 u64 bitpos;
33 const char *flag_str[] = { "none", "no-map", "no-overwrite" };
34
35 do {
36 bitpos = flags ? fls(flags) - 1 : 0;
37 printf("%s", flag_str[bitpos]);
38 flags &= ~(1ull << bitpos);
39 puts(flags ? ", " : "\n");
40 } while (flags);
41}
42
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053043static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060044{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053045 struct lmb_region *rgn = lmb_rgn_lst->data;
Patrick Delaunay48120022021-05-07 14:50:31 +020046 unsigned long long base, size, end;
47 enum lmb_flags flags;
48 int i;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060049
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053050 printf(" %s.count = 0x%x\n", name, lmb_rgn_lst->count);
Patrick Delaunay48120022021-05-07 14:50:31 +020051
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053052 for (i = 0; i < lmb_rgn_lst->count; i++) {
53 base = rgn[i].base;
54 size = rgn[i].size;
Patrick Delaunay48120022021-05-07 14:50:31 +020055 end = base + size - 1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053056 flags = rgn[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060057
Sughosh Ganuf04416f2024-08-26 17:29:40 +053058 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: ",
59 name, i, base, end, size);
60 lmb_print_region_flags(flags);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060061 }
Tero Kristo97c418b2020-07-20 11:10:45 +030062}
63
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053064void lmb_dump_all_force(void)
Patrick Delaunay48120022021-05-07 14:50:31 +020065{
66 printf("lmb_dump_all:\n");
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053067 lmb_dump_region(&lmb.free_mem, "memory");
68 lmb_dump_region(&lmb.used_mem, "reserved");
Patrick Delaunay48120022021-05-07 14:50:31 +020069}
70
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053071void lmb_dump_all(void)
Tero Kristo97c418b2020-07-20 11:10:45 +030072{
73#ifdef DEBUG
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053074 lmb_dump_all_force();
Tero Kristo97c418b2020-07-20 11:10:45 +030075#endif
Kumar Gala6d7bfa82008-02-27 21:51:47 -060076}
77
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010078static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
79 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060080{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010081 const phys_addr_t base1_end = base1 + size1 - 1;
82 const phys_addr_t base2_end = base2 + size2 - 1;
83
84 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala6d7bfa82008-02-27 21:51:47 -060085}
86
Becky Bruced26d67c2008-06-09 20:37:18 -050087static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010088 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060089{
90 if (base2 == base1 + size1)
91 return 1;
92 else if (base1 == base2 + size2)
93 return -1;
94
95 return 0;
96}
97
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053098static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1,
Udit Kumar4e8e6632023-09-26 16:54:42 +053099 unsigned long r2)
100{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530101 struct lmb_region *rgn = lmb_rgn_lst->data;
102
103 phys_addr_t base1 = rgn[r1].base;
104 phys_size_t size1 = rgn[r1].size;
105 phys_addr_t base2 = rgn[r2].base;
106 phys_size_t size2 = rgn[r2].size;
Udit Kumar4e8e6632023-09-26 16:54:42 +0530107
108 return lmb_addrs_overlap(base1, size1, base2, size2);
109}
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530110
111static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100112 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600113{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530114 struct lmb_region *rgn = lmb_rgn_lst->data;
115
116 phys_addr_t base1 = rgn[r1].base;
117 phys_size_t size1 = rgn[r1].size;
118 phys_addr_t base2 = rgn[r2].base;
119 phys_size_t size2 = rgn[r2].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600120 return lmb_addrs_adjacent(base1, size1, base2, size2);
121}
122
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530123static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600124{
125 unsigned long i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530126 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600127
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530128 for (i = r; i < lmb_rgn_lst->count - 1; i++) {
129 rgn[i].base = rgn[i + 1].base;
130 rgn[i].size = rgn[i + 1].size;
131 rgn[i].flags = rgn[i + 1].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600132 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530133 lmb_rgn_lst->count--;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600134}
135
136/* Assumption: base addr of region 1 < base addr of region 2 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530137static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100138 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600139{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530140 struct lmb_region *rgn = lmb_rgn_lst->data;
141
142 rgn[r1].size += rgn[r2].size;
143 lmb_remove_region(lmb_rgn_lst, r2);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600144}
145
Udit Kumar4e8e6632023-09-26 16:54:42 +0530146/*Assumption : base addr of region 1 < base addr of region 2*/
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530147static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst,
148 unsigned long r1, unsigned long r2)
Udit Kumar4e8e6632023-09-26 16:54:42 +0530149{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530150 struct lmb_region *rgn = lmb_rgn_lst->data;
151
152 phys_addr_t base1 = rgn[r1].base;
153 phys_size_t size1 = rgn[r1].size;
154 phys_addr_t base2 = rgn[r2].base;
155 phys_size_t size2 = rgn[r2].size;
Udit Kumar4e8e6632023-09-26 16:54:42 +0530156
157 if (base1 + size1 > base2 + size2) {
158 printf("This will not be a case any time\n");
159 return;
160 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530161 rgn[r1].size = base2 + size2 - base1;
162 lmb_remove_region(lmb_rgn_lst, r2);
Udit Kumar4e8e6632023-09-26 16:54:42 +0530163}
164
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100165/**
166 * efi_lmb_reserve() - add reservations for EFI memory
167 *
168 * Add reservations for all EFI memory areas that are not
169 * EFI_CONVENTIONAL_MEMORY.
170 *
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100171 * Return: 0 on success, 1 on failure
172 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530173static __maybe_unused int efi_lmb_reserve(void)
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100174{
175 struct efi_mem_desc *memmap = NULL, *map;
176 efi_uintn_t i, map_size = 0;
177 efi_status_t ret;
178
179 ret = efi_get_memory_map_alloc(&map_size, &memmap);
180 if (ret != EFI_SUCCESS)
181 return 1;
182
183 for (i = 0, map = memmap; i < map_size / sizeof(*map); ++map, ++i) {
Sjoerd Simons47a5a842023-01-19 09:38:18 +0100184 if (map->type != EFI_CONVENTIONAL_MEMORY) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530185 lmb_reserve_flags(map_to_sysmem((void *)(uintptr_t)
Sjoerd Simons47a5a842023-01-19 09:38:18 +0100186 map->physical_start),
187 map->num_pages * EFI_PAGE_SIZE,
188 map->type == EFI_RESERVED_MEMORY_TYPE
189 ? LMB_NOMAP : LMB_NONE);
190 }
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100191 }
192 efi_free_pool(memmap);
193
194 return 0;
195}
196
Sughosh Ganue5348c72024-08-26 17:29:30 +0530197static void lmb_reserve_uboot_region(void)
198{
199 int bank;
200 ulong end, bank_end;
201 phys_addr_t rsv_start;
202
203 rsv_start = gd->start_addr_sp - CONFIG_STACK_SIZE;
204 end = gd->ram_top;
205
206 /*
207 * Reserve memory from aligned address below the bottom of U-Boot stack
208 * until end of RAM area to prevent LMB from overwriting that memory.
209 */
210 debug("## Current stack ends at 0x%08lx ", (ulong)rsv_start);
211
212 /* adjust sp by 16K to be safe */
213 rsv_start -= SZ_16K;
214 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
215 if (!gd->bd->bi_dram[bank].size ||
216 rsv_start < gd->bd->bi_dram[bank].start)
217 continue;
218 /* Watch out for RAM at end of address space! */
219 bank_end = gd->bd->bi_dram[bank].start +
220 gd->bd->bi_dram[bank].size - 1;
221 if (rsv_start > bank_end)
222 continue;
223 if (bank_end > end)
224 bank_end = end - 1;
225
226 lmb_reserve_flags(rsv_start, bank_end - rsv_start + 1,
227 LMB_NOOVERWRITE);
228
229 if (gd->flags & GD_FLG_SKIP_RELOC)
230 lmb_reserve_flags((phys_addr_t)(uintptr_t)_start,
231 gd->mon_len, LMB_NOOVERWRITE);
232
233 break;
234 }
235}
236
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530237static void lmb_reserve_common(void *fdt_blob)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100238{
Sughosh Ganue5348c72024-08-26 17:29:30 +0530239 lmb_reserve_uboot_region();
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100240
Simon Glass85c057e2021-09-25 19:43:21 -0600241 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530242 boot_fdt_add_mem_rsv_regions(fdt_blob);
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100243
244 if (CONFIG_IS_ENABLED(EFI_LOADER))
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530245 efi_lmb_reserve();
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100246}
247
Sughosh Ganu6518b412024-08-26 17:29:24 +0530248static __maybe_unused void lmb_reserve_common_spl(void)
249{
250 phys_addr_t rsv_start;
251 phys_size_t rsv_size;
252
253 /*
254 * Assume a SPL stack of 16KB. This must be
255 * more than enough for the SPL stage.
256 */
257 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
258 rsv_start = gd->start_addr_sp - 16384;
259 rsv_size = 16384;
260 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
261 }
262
263 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
264 /* Reserve the bss region */
265 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
266 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
267 (phys_addr_t)(uintptr_t)__bss_start;
268 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
269 }
270}
271
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530272/**
273 * lmb_add_memory() - Add memory range for LMB allocations
274 *
275 * Add the entire available memory range to the pool of memory that
276 * can be used by the LMB module for allocations.
277 *
278 * Return: None
279 */
280void lmb_add_memory(void)
281{
282 int i;
283 phys_size_t size;
284 phys_addr_t rgn_top;
285 u64 ram_top = gd->ram_top;
286 struct bd_info *bd = gd->bd;
287
288 /* Assume a 4GB ram_top if not defined */
289 if (!ram_top)
290 ram_top = 0x100000000ULL;
291
292 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
293 size = bd->bi_dram[i].size;
294 if (size) {
295 if (bd->bi_dram[i].start > ram_top)
296 continue;
297
298 rgn_top = bd->bi_dram[i].start +
299 bd->bi_dram[i].size;
300
301 if (rgn_top > ram_top)
302 size -= rgn_top - ram_top;
303
304 lmb_add(bd->bi_dram[i].start, size);
305 }
306 }
307}
308
Sughosh Ganu50e27272024-08-26 17:29:19 +0530309static long lmb_resize_regions(struct alist *lmb_rgn_lst,
310 unsigned long idx_start,
311 phys_addr_t base, phys_size_t size)
312{
313 phys_size_t rgnsize;
314 unsigned long rgn_cnt, idx, idx_end;
315 phys_addr_t rgnbase, rgnend;
316 phys_addr_t mergebase, mergeend;
317 struct lmb_region *rgn = lmb_rgn_lst->data;
318
319 rgn_cnt = 0;
320 idx = idx_start;
321 idx_end = idx_start;
322
323 /*
324 * First thing to do is to identify how many regions
325 * the requested region overlaps.
326 * If the flags match, combine all these overlapping
327 * regions into a single region, and remove the merged
328 * regions.
329 */
330 while (idx <= lmb_rgn_lst->count - 1) {
331 rgnbase = rgn[idx].base;
332 rgnsize = rgn[idx].size;
333
334 if (lmb_addrs_overlap(base, size, rgnbase,
335 rgnsize)) {
336 if (rgn[idx].flags != LMB_NONE)
337 return -1;
338 rgn_cnt++;
339 idx_end = idx;
340 }
341 idx++;
342 }
343
344 /* The merged region's base and size */
345 rgnbase = rgn[idx_start].base;
346 mergebase = min(base, rgnbase);
347 rgnend = rgn[idx_end].base + rgn[idx_end].size;
348 mergeend = max(rgnend, (base + size));
349
350 rgn[idx_start].base = mergebase;
351 rgn[idx_start].size = mergeend - mergebase;
352
353 /* Now remove the merged regions */
354 while (--rgn_cnt)
355 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
356
357 return 0;
358}
359
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530360/**
361 * lmb_add_region_flags() - Add an lmb region to the given list
362 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
363 * @base: Start address of the region
364 * @size: Size of the region to be added
365 * @flags: Attributes of the LMB region
366 *
367 * Add a region of memory to the list. If the region does not exist, add
368 * it to the list. Depending on the attributes of the region to be added,
369 * the function might resize an already existing region or coalesce two
370 * adjacent regions.
371 *
372 *
373 * Returns: 0 if the region addition successful, -1 on failure
374 */
375static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200376 phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600377{
378 unsigned long coalesced = 0;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530379 long ret, i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530380 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600381
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530382 if (alist_err(lmb_rgn_lst))
383 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600384
385 /* First try and coalesce this LMB with another. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530386 for (i = 0; i < lmb_rgn_lst->count; i++) {
387 phys_addr_t rgnbase = rgn[i].base;
388 phys_size_t rgnsize = rgn[i].size;
389 phys_size_t rgnflags = rgn[i].flags;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100390 phys_addr_t end = base + size - 1;
391 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100392 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200393 if (flags == rgnflags)
394 /* Already have this region, so we're done */
395 return 0;
396 else
397 return -1; /* regions with new flags */
398 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600399
Sughosh Ganu50e27272024-08-26 17:29:19 +0530400 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
401 if (ret > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200402 if (flags != rgnflags)
403 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530404 rgn[i].base -= size;
405 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600406 coalesced++;
407 break;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530408 } else if (ret < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200409 if (flags != rgnflags)
410 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530411 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600412 coalesced++;
413 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100414 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sughosh Ganu50e27272024-08-26 17:29:19 +0530415 if (flags == LMB_NONE) {
416 ret = lmb_resize_regions(lmb_rgn_lst, i, base,
417 size);
418 if (ret < 0)
419 return -1;
420
421 coalesced++;
422 break;
423 } else {
424 return -1;
425 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600426 }
427 }
428
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530429 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
430 rgn = lmb_rgn_lst->data;
431 if (rgn[i].flags == rgn[i + 1].flags) {
432 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
433 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
434 coalesced++;
435 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
436 /* fix overlapping area */
437 lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
438 coalesced++;
439 }
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200440 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600441 }
442
443 if (coalesced)
444 return coalesced;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530445
446 if (alist_full(lmb_rgn_lst) &&
447 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600448 return -1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530449 rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600450
451 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530452 for (i = lmb_rgn_lst->count; i >= 0; i--) {
453 if (i && base < rgn[i - 1].base) {
454 rgn[i] = rgn[i - 1];
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600455 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530456 rgn[i].base = base;
457 rgn[i].size = size;
458 rgn[i].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600459 break;
460 }
461 }
462
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530463 lmb_rgn_lst->count++;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600464
465 return 0;
466}
467
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530468static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200469 phys_size_t size)
470{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530471 return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200472}
473
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600474/* This routine may be called with relocation disabled. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530475long lmb_add(phys_addr_t base, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600476{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530477 struct alist *lmb_rgn_lst = &lmb.free_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600478
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530479 return lmb_add_region(lmb_rgn_lst, base, size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600480}
481
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530482long lmb_free(phys_addr_t base, phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500483{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530484 struct lmb_region *rgn;
485 struct alist *lmb_rgn_lst = &lmb.used_mem;
Andy Fleming09d2a712008-07-07 14:24:39 -0500486 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100487 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500488 int i;
489
490 rgnbegin = rgnend = 0; /* supress gcc warnings */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530491 rgn = lmb_rgn_lst->data;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500492 /* Find the region where (base, size) belongs to */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530493 for (i = 0; i < lmb_rgn_lst->count; i++) {
494 rgnbegin = rgn[i].base;
495 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500496
497 if ((rgnbegin <= base) && (end <= rgnend))
498 break;
499 }
500
501 /* Didn't find the region */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530502 if (i == lmb_rgn_lst->count)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500503 return -1;
504
505 /* Check to see if we are removing entire region */
506 if ((rgnbegin == base) && (rgnend == end)) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530507 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500508 return 0;
509 }
510
511 /* Check to see if region is matching at the front */
512 if (rgnbegin == base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530513 rgn[i].base = end + 1;
514 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500515 return 0;
516 }
517
518 /* Check to see if the region is matching at the end */
519 if (rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530520 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500521 return 0;
522 }
523
524 /*
525 * We need to split the entry - adjust the current one to the
526 * beginging of the hole and add the region after hole.
527 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530528 rgn[i].size = base - rgn[i].base;
529 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
530 rgn[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500531}
532
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530533long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600534{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530535 struct alist *lmb_rgn_lst = &lmb.used_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600536
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530537 return lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200538}
539
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530540long lmb_reserve(phys_addr_t base, phys_size_t size)
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200541{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530542 return lmb_reserve_flags(base, size, LMB_NONE);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600543}
544
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530545static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Becky Bruced26d67c2008-06-09 20:37:18 -0500546 phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600547{
548 unsigned long i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530549 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600550
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530551 for (i = 0; i < lmb_rgn_lst->count; i++) {
552 phys_addr_t rgnbase = rgn[i].base;
553 phys_size_t rgnsize = rgn[i].size;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100554 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600555 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600556 }
557
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530558 return (i < lmb_rgn_lst->count) ? i : -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600559}
560
Becky Bruced26d67c2008-06-09 20:37:18 -0500561static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600562{
563 return addr & ~(size - 1);
564}
565
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530566static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align,
Sughosh Ganu50e27272024-08-26 17:29:19 +0530567 phys_addr_t max_addr, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600568{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100569 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500570 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500571 phys_addr_t res_base;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530572 struct lmb_region *lmb_used = lmb.used_mem.data;
573 struct lmb_region *lmb_memory = lmb.free_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600574
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530575 for (i = lmb.free_mem.count - 1; i >= 0; i--) {
576 phys_addr_t lmbbase = lmb_memory[i].base;
577 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600578
Andy Fleming78bd5a72008-06-16 13:58:55 -0500579 if (lmbsize < size)
580 continue;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600581 if (max_addr == LMB_ALLOC_ANYWHERE)
582 base = lmb_align_down(lmbbase + lmbsize - size, align);
583 else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600584 base = lmbbase + lmbsize;
585 if (base < lmbbase)
586 base = -1;
587 base = min(base, max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600588 base = lmb_align_down(base - size, align);
589 } else
590 continue;
591
Andy Fleming78bd5a72008-06-16 13:58:55 -0500592 while (base && lmbbase <= base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530593 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100594 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500595 /* This area isn't reserved, take it */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530596 if (lmb_add_region_flags(&lmb.used_mem, base,
597 size, flags) < 0)
Andy Fleming78bd5a72008-06-16 13:58:55 -0500598 return 0;
599 return base;
600 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530601
602 res_base = lmb_used[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500603 if (res_base < size)
604 break;
605 base = lmb_align_down(res_base - size, align);
606 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600607 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500608 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600609}
610
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530611phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530612{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530613 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530614}
615
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530616phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530617{
618 phys_addr_t alloc;
619
Sughosh Ganu50e27272024-08-26 17:29:19 +0530620 alloc = __lmb_alloc_base(size, align, max_addr, LMB_NONE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530621
622 if (alloc == 0)
623 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
624 (ulong)size, (ulong)max_addr);
625
626 return alloc;
627}
628
Sughosh Ganu50e27272024-08-26 17:29:19 +0530629static phys_addr_t __lmb_alloc_addr(phys_addr_t base, phys_size_t size,
630 enum lmb_flags flags)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100631{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100632 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530633 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100634
635 /* Check if the requested address is in one of the memory regions */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530636 rgn = lmb_overlaps_region(&lmb.free_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100637 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100638 /*
639 * Check if the requested end address is in the same memory
640 * region we found.
641 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530642 if (lmb_addrs_overlap(lmb_memory[rgn].base,
643 lmb_memory[rgn].size,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100644 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100645 /* ok, reserve the memory */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530646 if (lmb_reserve_flags(base, size, flags) >= 0)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100647 return base;
648 }
649 }
Sughosh Ganu50e27272024-08-26 17:29:19 +0530650
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100651 return 0;
652}
653
Sughosh Ganu50e27272024-08-26 17:29:19 +0530654/*
655 * Try to allocate a specific address range: must be in defined memory but not
656 * reserved
657 */
658phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
659{
660 return __lmb_alloc_addr(base, size, LMB_NONE);
661}
662
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100663/* Return number of bytes from a given address that are free */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530664phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100665{
666 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100667 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530668 struct lmb_region *lmb_used = lmb.used_mem.data;
669 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100670
671 /* check if the requested address is in the memory regions */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530672 rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100673 if (rgn >= 0) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530674 for (i = 0; i < lmb.used_mem.count; i++) {
675 if (addr < lmb_used[i].base) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100676 /* first reserved range > requested address */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530677 return lmb_used[i].base - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100678 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530679 if (lmb_used[i].base +
680 lmb_used[i].size > addr) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100681 /* requested addr is in this reserved range */
682 return 0;
683 }
684 }
685 /* if we come here: no reserved ranges above requested addr */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530686 return lmb_memory[lmb.free_mem.count - 1].base +
687 lmb_memory[lmb.free_mem.count - 1].size - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100688 }
689 return 0;
690}
691
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530692int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600693{
694 int i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530695 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600696
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530697 for (i = 0; i < lmb.used_mem.count; i++) {
698 phys_addr_t upper = lmb_used[i].base +
699 lmb_used[i].size - 1;
700 if (addr >= lmb_used[i].base && addr <= upper)
701 return (lmb_used[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600702 }
703 return 0;
704}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500705
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530706static int lmb_setup(void)
707{
708 bool ret;
709
710 ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region),
711 (uint)LMB_ALIST_INITIAL_SIZE);
712 if (!ret) {
713 log_debug("Unable to initialise the list for LMB free memory\n");
714 return -ENOMEM;
715 }
716
717 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
718 (uint)LMB_ALIST_INITIAL_SIZE);
719 if (!ret) {
720 log_debug("Unable to initialise the list for LMB used memory\n");
721 return -ENOMEM;
722 }
723
724 return 0;
725}
726
727/**
728 * lmb_init() - Initialise the LMB module
729 *
730 * Initialise the LMB lists needed for keeping the memory map. There
731 * are two lists, in form of alloced list data structure. One for the
732 * available memory, and one for the used memory. Initialise the two
733 * lists as part of board init. Add memory to the available memory
734 * list and reserve common areas by adding them to the used memory
735 * list.
736 *
737 * Return: 0 on success, -ve on error
738 */
739int lmb_init(void)
740{
741 int ret;
742
743 ret = lmb_setup();
744 if (ret) {
745 log_info("Unable to init LMB\n");
746 return ret;
747 }
748
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530749 lmb_add_memory();
750
Sughosh Ganu6518b412024-08-26 17:29:24 +0530751 /* Reserve the U-Boot image region once U-Boot has relocated */
752 if (spl_phase() == PHASE_SPL)
753 lmb_reserve_common_spl();
754 else if (spl_phase() == PHASE_BOARD_R)
755 lmb_reserve_common((void *)gd->fdt_blob);
756
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530757 return 0;
758}
759
760#if CONFIG_IS_ENABLED(UNIT_TEST)
761struct lmb *lmb_get(void)
762{
763 return &lmb;
764}
765
766int lmb_push(struct lmb *store)
767{
768 int ret;
769
770 *store = lmb;
771 ret = lmb_setup();
772 if (ret)
773 return ret;
774
775 return 0;
776}
777
778void lmb_pop(struct lmb *store)
779{
780 alist_uninit(&lmb.free_mem);
781 alist_uninit(&lmb.used_mem);
782 lmb = *store;
783}
784#endif /* UNIT_TEST */