blob: 9f1bb5ec7dea131eb880507a9169ca351823aa03 [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
30static void lmb_dump_region(struct alist *lmb_rgn_lst, char *name)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060031{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053032 struct lmb_region *rgn = lmb_rgn_lst->data;
Patrick Delaunay48120022021-05-07 14:50:31 +020033 unsigned long long base, size, end;
34 enum lmb_flags flags;
35 int i;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060036
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053037 printf(" %s.count = 0x%x\n", name, lmb_rgn_lst->count);
Patrick Delaunay48120022021-05-07 14:50:31 +020038
Sughosh Ganu291bf9c2024-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 Delaunay48120022021-05-07 14:50:31 +020042 end = base + size - 1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053043 flags = rgn[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060044
Patrick Delaunay48120022021-05-07 14:50:31 +020045 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
46 name, i, base, end, size, flags);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060047 }
Tero Kristo97c418b2020-07-20 11:10:45 +030048}
49
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053050void lmb_dump_all_force(void)
Patrick Delaunay48120022021-05-07 14:50:31 +020051{
52 printf("lmb_dump_all:\n");
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053053 lmb_dump_region(&lmb.free_mem, "memory");
54 lmb_dump_region(&lmb.used_mem, "reserved");
Patrick Delaunay48120022021-05-07 14:50:31 +020055}
56
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053057void lmb_dump_all(void)
Tero Kristo97c418b2020-07-20 11:10:45 +030058{
59#ifdef DEBUG
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053060 lmb_dump_all_force();
Tero Kristo97c418b2020-07-20 11:10:45 +030061#endif
Kumar Gala6d7bfa82008-02-27 21:51:47 -060062}
63
Simon Goldschmidtf41f7d62019-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 Gala6d7bfa82008-02-27 21:51:47 -060066{
Simon Goldschmidt6402d9b2019-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 Gala6d7bfa82008-02-27 21:51:47 -060071}
72
Becky Bruced26d67c2008-06-09 20:37:18 -050073static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010074 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-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 Ganu291bf9c2024-08-26 17:29:18 +053084static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1,
Udit Kumar4e8e6632023-09-26 16:54:42 +053085 unsigned long r2)
86{
Sughosh Ganu291bf9c2024-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 Kumar4e8e6632023-09-26 16:54:42 +053093
94 return lmb_addrs_overlap(base1, size1, base2, size2);
95}
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053096
97static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010098 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060099{
Sughosh Ganu291bf9c2024-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 Gala6d7bfa82008-02-27 21:51:47 -0600106 return lmb_addrs_adjacent(base1, size1, base2, size2);
107}
108
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530109static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600110{
111 unsigned long i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530112 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600113
Sughosh Ganu291bf9c2024-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 Gala6d7bfa82008-02-27 21:51:47 -0600118 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530119 lmb_rgn_lst->count--;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600120}
121
122/* Assumption: base addr of region 1 < base addr of region 2 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530123static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100124 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600125{
Sughosh Ganu291bf9c2024-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 Gala6d7bfa82008-02-27 21:51:47 -0600130}
131
Udit Kumar4e8e6632023-09-26 16:54:42 +0530132/*Assumption : base addr of region 1 < base addr of region 2*/
Sughosh Ganu291bf9c2024-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 Kumar4e8e6632023-09-26 16:54:42 +0530135{
Sughosh Ganu291bf9c2024-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 Kumar4e8e6632023-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 Ganu291bf9c2024-08-26 17:29:18 +0530147 rgn[r1].size = base2 + size2 - base1;
148 lmb_remove_region(lmb_rgn_lst, r2);
Udit Kumar4e8e6632023-09-26 16:54:42 +0530149}
150
Heinrich Schuchardtc9bb2eb2023-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 Schuchardtc9bb2eb2023-01-04 01:36:14 +0100157 * Return: 0 on success, 1 on failure
158 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530159static __maybe_unused int efi_lmb_reserve(void)
Heinrich Schuchardtc9bb2eb2023-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 Simons47a5a842023-01-19 09:38:18 +0100170 if (map->type != EFI_CONVENTIONAL_MEMORY) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530171 lmb_reserve_flags(map_to_sysmem((void *)(uintptr_t)
Sjoerd Simons47a5a842023-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 Schuchardtc9bb2eb2023-01-04 01:36:14 +0100177 }
178 efi_free_pool(memmap);
179
180 return 0;
181}
182
Sughosh Ganue5348c72024-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 Ganu291bf9c2024-08-26 17:29:18 +0530223static void lmb_reserve_common(void *fdt_blob)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100224{
Sughosh Ganue5348c72024-08-26 17:29:30 +0530225 lmb_reserve_uboot_region();
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100226
Simon Glass85c057e2021-09-25 19:43:21 -0600227 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530228 boot_fdt_add_mem_rsv_regions(fdt_blob);
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100229
230 if (CONFIG_IS_ENABLED(EFI_LOADER))
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530231 efi_lmb_reserve();
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100232}
233
Sughosh Ganu6518b412024-08-26 17:29:24 +0530234static __maybe_unused void lmb_reserve_common_spl(void)
235{
236 phys_addr_t rsv_start;
237 phys_size_t rsv_size;
238
239 /*
240 * Assume a SPL stack of 16KB. This must be
241 * more than enough for the SPL stage.
242 */
243 if (IS_ENABLED(CONFIG_SPL_STACK_R_ADDR)) {
244 rsv_start = gd->start_addr_sp - 16384;
245 rsv_size = 16384;
246 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
247 }
248
249 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) {
250 /* Reserve the bss region */
251 rsv_start = (phys_addr_t)(uintptr_t)__bss_start;
252 rsv_size = (phys_addr_t)(uintptr_t)__bss_end -
253 (phys_addr_t)(uintptr_t)__bss_start;
254 lmb_reserve_flags(rsv_start, rsv_size, LMB_NOOVERWRITE);
255 }
256}
257
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530258/**
259 * lmb_add_memory() - Add memory range for LMB allocations
260 *
261 * Add the entire available memory range to the pool of memory that
262 * can be used by the LMB module for allocations.
263 *
264 * Return: None
265 */
266void lmb_add_memory(void)
267{
268 int i;
269 phys_size_t size;
270 phys_addr_t rgn_top;
271 u64 ram_top = gd->ram_top;
272 struct bd_info *bd = gd->bd;
273
274 /* Assume a 4GB ram_top if not defined */
275 if (!ram_top)
276 ram_top = 0x100000000ULL;
277
278 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
279 size = bd->bi_dram[i].size;
280 if (size) {
281 if (bd->bi_dram[i].start > ram_top)
282 continue;
283
284 rgn_top = bd->bi_dram[i].start +
285 bd->bi_dram[i].size;
286
287 if (rgn_top > ram_top)
288 size -= rgn_top - ram_top;
289
290 lmb_add(bd->bi_dram[i].start, size);
291 }
292 }
293}
294
Sughosh Ganu50e27272024-08-26 17:29:19 +0530295static long lmb_resize_regions(struct alist *lmb_rgn_lst,
296 unsigned long idx_start,
297 phys_addr_t base, phys_size_t size)
298{
299 phys_size_t rgnsize;
300 unsigned long rgn_cnt, idx, idx_end;
301 phys_addr_t rgnbase, rgnend;
302 phys_addr_t mergebase, mergeend;
303 struct lmb_region *rgn = lmb_rgn_lst->data;
304
305 rgn_cnt = 0;
306 idx = idx_start;
307 idx_end = idx_start;
308
309 /*
310 * First thing to do is to identify how many regions
311 * the requested region overlaps.
312 * If the flags match, combine all these overlapping
313 * regions into a single region, and remove the merged
314 * regions.
315 */
316 while (idx <= lmb_rgn_lst->count - 1) {
317 rgnbase = rgn[idx].base;
318 rgnsize = rgn[idx].size;
319
320 if (lmb_addrs_overlap(base, size, rgnbase,
321 rgnsize)) {
322 if (rgn[idx].flags != LMB_NONE)
323 return -1;
324 rgn_cnt++;
325 idx_end = idx;
326 }
327 idx++;
328 }
329
330 /* The merged region's base and size */
331 rgnbase = rgn[idx_start].base;
332 mergebase = min(base, rgnbase);
333 rgnend = rgn[idx_end].base + rgn[idx_end].size;
334 mergeend = max(rgnend, (base + size));
335
336 rgn[idx_start].base = mergebase;
337 rgn[idx_start].size = mergeend - mergebase;
338
339 /* Now remove the merged regions */
340 while (--rgn_cnt)
341 lmb_remove_region(lmb_rgn_lst, idx_start + 1);
342
343 return 0;
344}
345
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530346/**
347 * lmb_add_region_flags() - Add an lmb region to the given list
348 * @lmb_rgn_lst: LMB list to which region is to be added(free/used)
349 * @base: Start address of the region
350 * @size: Size of the region to be added
351 * @flags: Attributes of the LMB region
352 *
353 * Add a region of memory to the list. If the region does not exist, add
354 * it to the list. Depending on the attributes of the region to be added,
355 * the function might resize an already existing region or coalesce two
356 * adjacent regions.
357 *
358 *
359 * Returns: 0 if the region addition successful, -1 on failure
360 */
361static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200362 phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600363{
364 unsigned long coalesced = 0;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530365 long ret, i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530366 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600367
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530368 if (alist_err(lmb_rgn_lst))
369 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600370
371 /* First try and coalesce this LMB with another. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530372 for (i = 0; i < lmb_rgn_lst->count; i++) {
373 phys_addr_t rgnbase = rgn[i].base;
374 phys_size_t rgnsize = rgn[i].size;
375 phys_size_t rgnflags = rgn[i].flags;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100376 phys_addr_t end = base + size - 1;
377 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100378 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200379 if (flags == rgnflags)
380 /* Already have this region, so we're done */
381 return 0;
382 else
383 return -1; /* regions with new flags */
384 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600385
Sughosh Ganu50e27272024-08-26 17:29:19 +0530386 ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
387 if (ret > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200388 if (flags != rgnflags)
389 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530390 rgn[i].base -= size;
391 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600392 coalesced++;
393 break;
Sughosh Ganu50e27272024-08-26 17:29:19 +0530394 } else if (ret < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200395 if (flags != rgnflags)
396 break;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530397 rgn[i].size += size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600398 coalesced++;
399 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100400 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
Sughosh Ganu50e27272024-08-26 17:29:19 +0530401 if (flags == LMB_NONE) {
402 ret = lmb_resize_regions(lmb_rgn_lst, i, base,
403 size);
404 if (ret < 0)
405 return -1;
406
407 coalesced++;
408 break;
409 } else {
410 return -1;
411 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600412 }
413 }
414
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530415 if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
416 rgn = lmb_rgn_lst->data;
417 if (rgn[i].flags == rgn[i + 1].flags) {
418 if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
419 lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
420 coalesced++;
421 } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
422 /* fix overlapping area */
423 lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
424 coalesced++;
425 }
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200426 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600427 }
428
429 if (coalesced)
430 return coalesced;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530431
432 if (alist_full(lmb_rgn_lst) &&
433 !alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600434 return -1;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530435 rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600436
437 /* Couldn't coalesce the LMB, so add it to the sorted table. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530438 for (i = lmb_rgn_lst->count; i >= 0; i--) {
439 if (i && base < rgn[i - 1].base) {
440 rgn[i] = rgn[i - 1];
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600441 } else {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530442 rgn[i].base = base;
443 rgn[i].size = size;
444 rgn[i].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600445 break;
446 }
447 }
448
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530449 lmb_rgn_lst->count++;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600450
451 return 0;
452}
453
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530454static long lmb_add_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200455 phys_size_t size)
456{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530457 return lmb_add_region_flags(lmb_rgn_lst, base, size, LMB_NONE);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200458}
459
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600460/* This routine may be called with relocation disabled. */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530461long lmb_add(phys_addr_t base, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600462{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530463 struct alist *lmb_rgn_lst = &lmb.free_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600464
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530465 return lmb_add_region(lmb_rgn_lst, base, size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600466}
467
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530468long lmb_free(phys_addr_t base, phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500469{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530470 struct lmb_region *rgn;
471 struct alist *lmb_rgn_lst = &lmb.used_mem;
Andy Fleming09d2a712008-07-07 14:24:39 -0500472 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100473 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500474 int i;
475
476 rgnbegin = rgnend = 0; /* supress gcc warnings */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530477 rgn = lmb_rgn_lst->data;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500478 /* Find the region where (base, size) belongs to */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530479 for (i = 0; i < lmb_rgn_lst->count; i++) {
480 rgnbegin = rgn[i].base;
481 rgnend = rgnbegin + rgn[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500482
483 if ((rgnbegin <= base) && (end <= rgnend))
484 break;
485 }
486
487 /* Didn't find the region */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530488 if (i == lmb_rgn_lst->count)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500489 return -1;
490
491 /* Check to see if we are removing entire region */
492 if ((rgnbegin == base) && (rgnend == end)) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530493 lmb_remove_region(lmb_rgn_lst, i);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500494 return 0;
495 }
496
497 /* Check to see if region is matching at the front */
498 if (rgnbegin == base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530499 rgn[i].base = end + 1;
500 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500501 return 0;
502 }
503
504 /* Check to see if the region is matching at the end */
505 if (rgnend == end) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530506 rgn[i].size -= size;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500507 return 0;
508 }
509
510 /*
511 * We need to split the entry - adjust the current one to the
512 * beginging of the hole and add the region after hole.
513 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530514 rgn[i].size = base - rgn[i].base;
515 return lmb_add_region_flags(lmb_rgn_lst, end + 1, rgnend - end,
516 rgn[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500517}
518
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530519long lmb_reserve_flags(phys_addr_t base, phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600520{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530521 struct alist *lmb_rgn_lst = &lmb.used_mem;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600522
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530523 return lmb_add_region_flags(lmb_rgn_lst, base, size, flags);
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200524}
525
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530526long lmb_reserve(phys_addr_t base, phys_size_t size)
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200527{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530528 return lmb_reserve_flags(base, size, LMB_NONE);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600529}
530
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530531static long lmb_overlaps_region(struct alist *lmb_rgn_lst, phys_addr_t base,
Becky Bruced26d67c2008-06-09 20:37:18 -0500532 phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600533{
534 unsigned long i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530535 struct lmb_region *rgn = lmb_rgn_lst->data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600536
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530537 for (i = 0; i < lmb_rgn_lst->count; i++) {
538 phys_addr_t rgnbase = rgn[i].base;
539 phys_size_t rgnsize = rgn[i].size;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100540 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600541 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600542 }
543
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530544 return (i < lmb_rgn_lst->count) ? i : -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600545}
546
Becky Bruced26d67c2008-06-09 20:37:18 -0500547static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600548{
549 return addr & ~(size - 1);
550}
551
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530552static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align,
Sughosh Ganu50e27272024-08-26 17:29:19 +0530553 phys_addr_t max_addr, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600554{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100555 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500556 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500557 phys_addr_t res_base;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530558 struct lmb_region *lmb_used = lmb.used_mem.data;
559 struct lmb_region *lmb_memory = lmb.free_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600560
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530561 for (i = lmb.free_mem.count - 1; i >= 0; i--) {
562 phys_addr_t lmbbase = lmb_memory[i].base;
563 phys_size_t lmbsize = lmb_memory[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600564
Andy Fleming78bd5a72008-06-16 13:58:55 -0500565 if (lmbsize < size)
566 continue;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600567 if (max_addr == LMB_ALLOC_ANYWHERE)
568 base = lmb_align_down(lmbbase + lmbsize - size, align);
569 else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600570 base = lmbbase + lmbsize;
571 if (base < lmbbase)
572 base = -1;
573 base = min(base, max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600574 base = lmb_align_down(base - size, align);
575 } else
576 continue;
577
Andy Fleming78bd5a72008-06-16 13:58:55 -0500578 while (base && lmbbase <= base) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530579 rgn = lmb_overlaps_region(&lmb.used_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100580 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500581 /* This area isn't reserved, take it */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530582 if (lmb_add_region_flags(&lmb.used_mem, base,
583 size, flags) < 0)
Andy Fleming78bd5a72008-06-16 13:58:55 -0500584 return 0;
585 return base;
586 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530587
588 res_base = lmb_used[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500589 if (res_base < size)
590 break;
591 base = lmb_align_down(res_base - size, align);
592 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600593 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500594 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600595}
596
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530597phys_addr_t lmb_alloc(phys_size_t size, ulong align)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530598{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530599 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530600}
601
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530602phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
Sughosh Ganu78435de2024-08-26 17:29:16 +0530603{
604 phys_addr_t alloc;
605
Sughosh Ganu50e27272024-08-26 17:29:19 +0530606 alloc = __lmb_alloc_base(size, align, max_addr, LMB_NONE);
Sughosh Ganu78435de2024-08-26 17:29:16 +0530607
608 if (alloc == 0)
609 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
610 (ulong)size, (ulong)max_addr);
611
612 return alloc;
613}
614
Sughosh Ganu50e27272024-08-26 17:29:19 +0530615static phys_addr_t __lmb_alloc_addr(phys_addr_t base, phys_size_t size,
616 enum lmb_flags flags)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100617{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100618 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530619 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100620
621 /* Check if the requested address is in one of the memory regions */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530622 rgn = lmb_overlaps_region(&lmb.free_mem, base, size);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100623 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100624 /*
625 * Check if the requested end address is in the same memory
626 * region we found.
627 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530628 if (lmb_addrs_overlap(lmb_memory[rgn].base,
629 lmb_memory[rgn].size,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100630 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100631 /* ok, reserve the memory */
Sughosh Ganu50e27272024-08-26 17:29:19 +0530632 if (lmb_reserve_flags(base, size, flags) >= 0)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100633 return base;
634 }
635 }
Sughosh Ganu50e27272024-08-26 17:29:19 +0530636
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100637 return 0;
638}
639
Sughosh Ganu50e27272024-08-26 17:29:19 +0530640/*
641 * Try to allocate a specific address range: must be in defined memory but not
642 * reserved
643 */
644phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
645{
646 return __lmb_alloc_addr(base, size, LMB_NONE);
647}
648
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100649/* Return number of bytes from a given address that are free */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530650phys_size_t lmb_get_free_size(phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100651{
652 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100653 long rgn;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530654 struct lmb_region *lmb_used = lmb.used_mem.data;
655 struct lmb_region *lmb_memory = lmb.free_mem.data;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100656
657 /* check if the requested address is in the memory regions */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530658 rgn = lmb_overlaps_region(&lmb.free_mem, addr, 1);
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100659 if (rgn >= 0) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530660 for (i = 0; i < lmb.used_mem.count; i++) {
661 if (addr < lmb_used[i].base) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100662 /* first reserved range > requested address */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530663 return lmb_used[i].base - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100664 }
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530665 if (lmb_used[i].base +
666 lmb_used[i].size > addr) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100667 /* requested addr is in this reserved range */
668 return 0;
669 }
670 }
671 /* if we come here: no reserved ranges above requested addr */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530672 return lmb_memory[lmb.free_mem.count - 1].base +
673 lmb_memory[lmb.free_mem.count - 1].size - addr;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100674 }
675 return 0;
676}
677
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530678int lmb_is_reserved_flags(phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600679{
680 int i;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530681 struct lmb_region *lmb_used = lmb.used_mem.data;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600682
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530683 for (i = 0; i < lmb.used_mem.count; i++) {
684 phys_addr_t upper = lmb_used[i].base +
685 lmb_used[i].size - 1;
686 if (addr >= lmb_used[i].base && addr <= upper)
687 return (lmb_used[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600688 }
689 return 0;
690}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500691
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530692static int lmb_setup(void)
693{
694 bool ret;
695
696 ret = alist_init(&lmb.free_mem, sizeof(struct lmb_region),
697 (uint)LMB_ALIST_INITIAL_SIZE);
698 if (!ret) {
699 log_debug("Unable to initialise the list for LMB free memory\n");
700 return -ENOMEM;
701 }
702
703 ret = alist_init(&lmb.used_mem, sizeof(struct lmb_region),
704 (uint)LMB_ALIST_INITIAL_SIZE);
705 if (!ret) {
706 log_debug("Unable to initialise the list for LMB used memory\n");
707 return -ENOMEM;
708 }
709
710 return 0;
711}
712
713/**
714 * lmb_init() - Initialise the LMB module
715 *
716 * Initialise the LMB lists needed for keeping the memory map. There
717 * are two lists, in form of alloced list data structure. One for the
718 * available memory, and one for the used memory. Initialise the two
719 * lists as part of board init. Add memory to the available memory
720 * list and reserve common areas by adding them to the used memory
721 * list.
722 *
723 * Return: 0 on success, -ve on error
724 */
725int lmb_init(void)
726{
727 int ret;
728
729 ret = lmb_setup();
730 if (ret) {
731 log_info("Unable to init LMB\n");
732 return ret;
733 }
734
Sughosh Ganu65597fa2024-08-26 17:29:23 +0530735 lmb_add_memory();
736
Sughosh Ganu6518b412024-08-26 17:29:24 +0530737 /* Reserve the U-Boot image region once U-Boot has relocated */
738 if (spl_phase() == PHASE_SPL)
739 lmb_reserve_common_spl();
740 else if (spl_phase() == PHASE_BOARD_R)
741 lmb_reserve_common((void *)gd->fdt_blob);
742
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530743 return 0;
744}
745
746#if CONFIG_IS_ENABLED(UNIT_TEST)
747struct lmb *lmb_get(void)
748{
749 return &lmb;
750}
751
752int lmb_push(struct lmb *store)
753{
754 int ret;
755
756 *store = lmb;
757 ret = lmb_setup();
758 if (ret)
759 return ret;
760
761 return 0;
762}
763
764void lmb_pop(struct lmb *store)
765{
766 alist_uninit(&lmb.free_mem);
767 alist_uninit(&lmb.used_mem);
768 lmb = *store;
769}
770#endif /* UNIT_TEST */