blob: 0504a7b340777294bef7c73b65e2fd5f0df3ec8d [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;
Sughosh Ganu7ebbdd72024-10-15 21:07:04 +053033 const char *flag_str[] = { "none", "no-map", "no-overwrite", "no-notify" };
Sughosh Ganuf04416f2024-08-26 17:29:40 +053034
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;
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530284 u64 ram_top = gd->ram_top;
285 struct bd_info *bd = gd->bd;
286
287 /* Assume a 4GB ram_top if not defined */
288 if (!ram_top)
289 ram_top = 0x100000000ULL;
290
291 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
292 size = bd->bi_dram[i].size;
293 if (size) {
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530294 lmb_add(bd->bi_dram[i].start, size);
Sughosh Ganubafdac02024-10-15 21:07:05 +0530295
296 /*
297 * Reserve memory above ram_top as
298 * no-overwrite so that it cannot be
299 * allocated
300 */
301 if (bd->bi_dram[i].start >= ram_top)
302 lmb_reserve_flags(bd->bi_dram[i].start, size,
303 LMB_NOOVERWRITE);
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530304 }
305 }
306}
307
Sughosh Ganu50e27272024-08-26 17:29:19 +0530308static long lmb_resize_regions(struct alist *lmb_rgn_lst,
309 unsigned long idx_start,
310 phys_addr_t base, phys_size_t size)
311{
312 phys_size_t rgnsize;
313 unsigned long rgn_cnt, idx, idx_end;
314 phys_addr_t rgnbase, rgnend;
315 phys_addr_t mergebase, mergeend;
316 struct lmb_region *rgn = lmb_rgn_lst->data;
317
318 rgn_cnt = 0;
319 idx = idx_start;
320 idx_end = idx_start;
321
322 /*
323 * First thing to do is to identify how many regions
324 * the requested region overlaps.
325 * If the flags match, combine all these overlapping
326 * regions into a single region, and remove the merged
327 * regions.
328 */
329 while (idx <= lmb_rgn_lst->count - 1) {
330 rgnbase = rgn[idx].base;
331 rgnsize = rgn[idx].size;
332
333 if (lmb_addrs_overlap(base, size, rgnbase,
334 rgnsize)) {
335 if (rgn[idx].flags != LMB_NONE)
336 return -1;
337 rgn_cnt++;
338 idx_end = idx;
339 }
340 idx++;
341 }
342
343 /* The merged region's base and size */
344 rgnbase = rgn[idx_start].base;
345 mergebase = min(base, rgnbase);
346 rgnend = rgn[idx_end].base + rgn[idx_end].size;
347 mergeend = max(rgnend, (base + size));
348
349 rgn[idx_start].base = mergebase;
350 rgn[idx_start].size = mergeend - mergebase;
351
352 /* Now remove the merged regions */
353 while (--rgn_cnt)
354 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
355
356 return 0;
357}
358
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530359/**
360 * lmb_add_region_flags() - Add an lmb region to the given list
361 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
362 * @base: Start address of the region
363 * @size: Size of the region to be added
364 * @flags: Attributes of the LMB region
365 *
366 * Add a region of memory to the list. If the region does not exist, add
367 * it to the list. Depending on the attributes of the region to be added,
368 * the function might resize an already existing region or coalesce two
369 * adjacent regions.
370 *
371 *
372 * Returns: 0 if the region addition successful, -1 on failure
373 */
374static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200375 phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600376{
377 unsigned long coalesced = 0;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530378 long ret, i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530379 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600380
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530381 if (alist_err(lmb_rgn_lst))
382 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600383
384 /* First try and coalesce this LMB with another. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530385 for (i = 0; i < lmb_rgn_lst->count; i++) {
386 phys_addr_t rgnbase = rgn[i].base;
387 phys_size_t rgnsize = rgn[i].size;
388 phys_size_t rgnflags = rgn[i].flags;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100389 phys_addr_t end = base + size - 1;
390 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100391 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200392 if (flags == rgnflags)
393 /* Already have this region, so we're done */
394 return 0;
395 else
396 return -1; /* regions with new flags */
397 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600398
Sughosh Ganu50e27272024-08-26 17:29:19 +0530399 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
400 if (ret > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200401 if (flags != rgnflags)
402 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530403 rgn[i].base -= size;
404 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600405 coalesced++;
406 break;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530407 } else if (ret < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200408 if (flags != rgnflags)
409 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530410 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600411 coalesced++;
412 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100413 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sughosh Ganu50e27272024-08-26 17:29:19 +0530414 if (flags == LMB_NONE) {
415 ret = lmb_resize_regions(lmb_rgn_lst, i, base,
416 size);
417 if (ret < 0)
418 return -1;
419
420 coalesced++;
421 break;
422 } else {
423 return -1;
424 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600425 }
426 }
427
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530428 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
429 rgn = lmb_rgn_lst->data;
430 if (rgn[i].flags == rgn[i + 1].flags) {
431 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
432 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
433 coalesced++;
434 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
435 /* fix overlapping area */
436 lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
437 coalesced++;
438 }
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200439 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600440 }
441
442 if (coalesced)
443 return coalesced;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530444
445 if (alist_full(lmb_rgn_lst) &&
446 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600447 return -1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530448 rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600449
450 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530451 for (i = lmb_rgn_lst->count; i >= 0; i--) {
452 if (i && base < rgn[i - 1].base) {
453 rgn[i] = rgn[i - 1];
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600454 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530455 rgn[i].base = base;
456 rgn[i].size = size;
457 rgn[i].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600458 break;
459 }
460 }
461
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530462 lmb_rgn_lst->count++;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600463
464 return 0;
465}
466
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530467static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200468 phys_size_t size)
469{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530470 return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200471}
472
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600473/* This routine may be called with relocation disabled. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530474long lmb_add(phys_addr_t base, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600475{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530476 struct alist *lmb_rgn_lst = &lmb.free_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600477
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530478 return lmb_add_region(lmb_rgn_lst, base, size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600479}
480
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530481static long __lmb_free(phys_addr_t base, phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500482{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530483 struct lmb_region *rgn;
484 struct alist *lmb_rgn_lst = &lmb.used_mem;
Andy Fleming09d2a712008-07-07 14:24:39 -0500485 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100486 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500487 int i;
488
489 rgnbegin = rgnend = 0; /* supress gcc warnings */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530490 rgn = lmb_rgn_lst->data;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500491 /* Find the region where (base, size) belongs to */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530492 for (i = 0; i < lmb_rgn_lst->count; i++) {
493 rgnbegin = rgn[i].base;
494 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500495
496 if ((rgnbegin <= base) && (end <= rgnend))
497 break;
498 }
499
500 /* Didn't find the region */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530501 if (i == lmb_rgn_lst->count)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500502 return -1;
503
504 /* Check to see if we are removing entire region */
505 if ((rgnbegin == base) && (rgnend == end)) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530506 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500507 return 0;
508 }
509
510 /* Check to see if region is matching at the front */
511 if (rgnbegin == base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530512 rgn[i].base = end + 1;
513 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500514 return 0;
515 }
516
517 /* Check to see if the region is matching at the end */
518 if (rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530519 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500520 return 0;
521 }
522
523 /*
524 * We need to split the entry - adjust the current one to the
525 * beginging of the hole and add the region after hole.
526 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530527 rgn[i].size = base - rgn[i].base;
528 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
529 rgn[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500530}
531
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530532long lmb_free(phys_addr_t base, phys_size_t size)
533{
534 return __lmb_free(base, size);
535}
536
537/**
538 * lmb_free_flags() - Free up a region of memory
539 * @base: Base Address of region to be freed
540 * @size: Size of the region to be freed
541 * @flags: Memory region attributes
542 *
543 * Free up a region of memory.
544 *
545 * Return: 0 if successful, -1 on failure
546 */
547long lmb_free_flags(phys_addr_t base, phys_size_t size,
548 __always_unused uint flags)
549{
550 return __lmb_free(base, size);
551}
552
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530553long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600554{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530555 struct alist *lmb_rgn_lst = &lmb.used_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600556
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530557 return lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200558}
559
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530560long lmb_reserve(phys_addr_t base, phys_size_t size)
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200561{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530562 return lmb_reserve_flags(base, size, LMB_NONE);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600563}
564
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530565static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Becky Bruced26d67c2008-06-09 20:37:18 -0500566 phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600567{
568 unsigned long i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530569 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600570
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530571 for (i = 0; i < lmb_rgn_lst->count; i++) {
572 phys_addr_t rgnbase = rgn[i].base;
573 phys_size_t rgnsize = rgn[i].size;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100574 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600575 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600576 }
577
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530578 return (i < lmb_rgn_lst->count) ? i : -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600579}
580
Becky Bruced26d67c2008-06-09 20:37:18 -0500581static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600582{
583 return addr & ~(size - 1);
584}
585
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530586static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align,
Sughosh Ganu50e27272024-08-26 17:29:19 +0530587 phys_addr_t max_addr, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600588{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100589 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500590 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500591 phys_addr_t res_base;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530592 struct lmb_region *lmb_used = lmb.used_mem.data;
593 struct lmb_region *lmb_memory = lmb.free_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600594
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530595 for (i = lmb.free_mem.count - 1; i >= 0; i--) {
596 phys_addr_t lmbbase = lmb_memory[i].base;
597 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600598
Andy Fleming78bd5a72008-06-16 13:58:55 -0500599 if (lmbsize < size)
600 continue;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600601 if (max_addr == LMB_ALLOC_ANYWHERE)
602 base = lmb_align_down(lmbbase + lmbsize - size, align);
603 else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600604 base = lmbbase + lmbsize;
605 if (base < lmbbase)
606 base = -1;
607 base = min(base, max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600608 base = lmb_align_down(base - size, align);
609 } else
610 continue;
611
Andy Fleming78bd5a72008-06-16 13:58:55 -0500612 while (base && lmbbase <= base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530613 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100614 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500615 /* This area isn't reserved, take it */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530616 if (lmb_add_region_flags(&lmb.used_mem, base,
617 size, flags) < 0)
Andy Fleming78bd5a72008-06-16 13:58:55 -0500618 return 0;
619 return base;
620 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530621
622 res_base = lmb_used[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500623 if (res_base < size)
624 break;
625 base = lmb_align_down(res_base - size, align);
626 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600627 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500628 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600629}
630
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530631phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530632{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530633 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530634}
635
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530636/**
637 * lmb_alloc_flags() - Allocate memory region with specified attributes
638 * @size: Size of the region requested
639 * @align: Alignment of the memory region requested
640 * @flags: Memory region attributes to be set
641 *
642 * Allocate a region of memory with the attributes specified through the
643 * parameter.
644 *
645 * Return: base address on success, 0 on error
646 */
647phys_addr_t lmb_alloc_flags(phys_size_t size, ulong align, uint flags)
648{
649 return __lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE,
650 flags);
651}
652
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530653phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530654{
655 phys_addr_t alloc;
656
Sughosh Ganu50e27272024-08-26 17:29:19 +0530657 alloc = __lmb_alloc_base(size, align, max_addr, LMB_NONE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530658
659 if (alloc == 0)
660 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
661 (ulong)size, (ulong)max_addr);
662
663 return alloc;
664}
665
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530666/**
667 * lmb_alloc_base_flags() - Allocate specified memory region with specified attributes
668 * @size: Size of the region requested
669 * @align: Alignment of the memory region requested
670 * @max_addr: Maximum address of the requested region
671 * @flags: Memory region attributes to be set
672 *
673 * Allocate a region of memory with the attributes specified through the
674 * parameter. The max_addr parameter is used to specify the maximum address
675 * below which the requested region should be allocated.
676 *
677 * Return: base address on success, 0 on error
678 */
679phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
680 phys_addr_t max_addr, uint flags)
681{
682 phys_addr_t alloc;
683
684 alloc = __lmb_alloc_base(size, align, max_addr, flags);
685
686 if (alloc == 0)
687 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
688 (ulong)size, (ulong)max_addr);
689
690 return alloc;
691}
692
Sughosh Ganu50e27272024-08-26 17:29:19 +0530693static phys_addr_t __lmb_alloc_addr(phys_addr_t base, phys_size_t size,
694 enum lmb_flags flags)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100695{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100696 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530697 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100698
699 /* Check if the requested address is in one of the memory regions */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530700 rgn = lmb_overlaps_region(&lmb.free_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100701 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100702 /*
703 * Check if the requested end address is in the same memory
704 * region we found.
705 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530706 if (lmb_addrs_overlap(lmb_memory[rgn].base,
707 lmb_memory[rgn].size,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100708 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100709 /* ok, reserve the memory */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530710 if (lmb_reserve_flags(base, size, flags) >= 0)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100711 return base;
712 }
713 }
Sughosh Ganu50e27272024-08-26 17:29:19 +0530714
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100715 return 0;
716}
717
Sughosh Ganu50e27272024-08-26 17:29:19 +0530718/*
719 * Try to allocate a specific address range: must be in defined memory but not
720 * reserved
721 */
722phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
723{
724 return __lmb_alloc_addr(base, size, LMB_NONE);
725}
726
Sughosh Ganu7f15d322024-10-15 21:07:03 +0530727/**
728 * lmb_alloc_addr_flags() - Allocate specified memory address with specified attributes
729 * @base: Base Address requested
730 * @size: Size of the region requested
731 * @flags: Memory region attributes to be set
732 *
733 * Allocate a region of memory with the attributes specified through the
734 * parameter. The base parameter is used to specify the base address
735 * of the requested region.
736 *
737 * Return: base address on success, 0 on error
738 */
739phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
740 uint flags)
741{
742 return __lmb_alloc_addr(base, size, flags);
743}
744
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100745/* Return number of bytes from a given address that are free */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530746phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100747{
748 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100749 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530750 struct lmb_region *lmb_used = lmb.used_mem.data;
751 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100752
753 /* check if the requested address is in the memory regions */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530754 rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100755 if (rgn >= 0) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530756 for (i = 0; i < lmb.used_mem.count; i++) {
757 if (addr < lmb_used[i].base) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100758 /* first reserved range > requested address */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530759 return lmb_used[i].base - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100760 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530761 if (lmb_used[i].base +
762 lmb_used[i].size > addr) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100763 /* requested addr is in this reserved range */
764 return 0;
765 }
766 }
767 /* if we come here: no reserved ranges above requested addr */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530768 return lmb_memory[lmb.free_mem.count - 1].base +
769 lmb_memory[lmb.free_mem.count - 1].size - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100770 }
771 return 0;
772}
773
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530774int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600775{
776 int i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530777 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600778
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530779 for (i = 0; i < lmb.used_mem.count; i++) {
780 phys_addr_t upper = lmb_used[i].base +
781 lmb_used[i].size - 1;
782 if (addr >= lmb_used[i].base && addr <= upper)
783 return (lmb_used[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600784 }
785 return 0;
786}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500787
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530788static int lmb_setup(void)
789{
790 bool ret;
791
792 ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region),
793 (uint)LMB_ALIST_INITIAL_SIZE);
794 if (!ret) {
795 log_debug("Unable to initialise the list for LMB free memory\n");
796 return -ENOMEM;
797 }
798
799 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
800 (uint)LMB_ALIST_INITIAL_SIZE);
801 if (!ret) {
802 log_debug("Unable to initialise the list for LMB used memory\n");
803 return -ENOMEM;
804 }
805
806 return 0;
807}
808
809/**
810 * lmb_init() - Initialise the LMB module
811 *
812 * Initialise the LMB lists needed for keeping the memory map. There
813 * are two lists, in form of alloced list data structure. One for the
814 * available memory, and one for the used memory. Initialise the two
815 * lists as part of board init. Add memory to the available memory
816 * list and reserve common areas by adding them to the used memory
817 * list.
818 *
819 * Return: 0 on success, -ve on error
820 */
821int lmb_init(void)
822{
823 int ret;
824
825 ret = lmb_setup();
826 if (ret) {
827 log_info("Unable to init LMB\n");
828 return ret;
829 }
830
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530831 lmb_add_memory();
832
Sughosh Ganu6518b412024-08-26 17:29:24 +0530833 /* Reserve the U-Boot image region once U-Boot has relocated */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600834 if (xpl_phase() == PHASE_SPL)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530835 lmb_reserve_common_spl();
Simon Glassd4dce4a2024-09-29 19:49:36 -0600836 else if (xpl_phase() == PHASE_BOARD_R)
Sughosh Ganu6518b412024-08-26 17:29:24 +0530837 lmb_reserve_common((void *)gd->fdt_blob);
838
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530839 return 0;
840}
841
842#if CONFIG_IS_ENABLED(UNIT_TEST)
843struct lmb *lmb_get(void)
844{
845 return &lmb;
846}
847
848int lmb_push(struct lmb *store)
849{
850 int ret;
851
852 *store = lmb;
853 ret = lmb_setup();
854 if (ret)
855 return ret;
856
857 return 0;
858}
859
860void lmb_pop(struct lmb *store)
861{
862 alist_uninit(&lmb.free_mem);
863 alist_uninit(&lmb.used_mem);
864 lmb = *store;
865}
866#endif /* UNIT_TEST */