blob: 44f98205310146f1bf45ab569b067a81dc153879 [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
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +01009#include <efi_loader.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060010#include <image.h>
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +010011#include <mapmem.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060012#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060015
Marek Vasuta2eec022021-09-10 22:47:09 +020016#include <asm/global_data.h>
Marek Vasut0fcae7f2021-11-13 18:34:37 +010017#include <asm/sections.h>
Marek Vasuta2eec022021-09-10 22:47:09 +020018
19DECLARE_GLOBAL_DATA_PTR;
20
Kumar Gala6d7bfa82008-02-27 21:51:47 -060021#define LMB_ALLOC_ANYWHERE 0
22
Patrick Delaunay48120022021-05-07 14:50:31 +020023static void lmb_dump_region(struct lmb_region *rgn, char *name)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060024{
Patrick Delaunay48120022021-05-07 14:50:31 +020025 unsigned long long base, size, end;
26 enum lmb_flags flags;
27 int i;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060028
Patrick Delaunay6c230522023-03-22 19:12:26 +010029 printf(" %s.cnt = 0x%lx / max = 0x%lx\n", name, rgn->cnt, rgn->max);
Patrick Delaunay48120022021-05-07 14:50:31 +020030
31 for (i = 0; i < rgn->cnt; i++) {
32 base = rgn->region[i].base;
33 size = rgn->region[i].size;
34 end = base + size - 1;
35 flags = rgn->region[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060036
Patrick Delaunay48120022021-05-07 14:50:31 +020037 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
38 name, i, base, end, size, flags);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060039 }
Tero Kristo97c418b2020-07-20 11:10:45 +030040}
41
Patrick Delaunay48120022021-05-07 14:50:31 +020042void lmb_dump_all_force(struct lmb *lmb)
43{
44 printf("lmb_dump_all:\n");
45 lmb_dump_region(&lmb->memory, "memory");
46 lmb_dump_region(&lmb->reserved, "reserved");
47}
48
Tero Kristo97c418b2020-07-20 11:10:45 +030049void lmb_dump_all(struct lmb *lmb)
50{
51#ifdef DEBUG
52 lmb_dump_all_force(lmb);
53#endif
Kumar Gala6d7bfa82008-02-27 21:51:47 -060054}
55
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010056static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
57 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060058{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010059 const phys_addr_t base1_end = base1 + size1 - 1;
60 const phys_addr_t base2_end = base2 + size2 - 1;
61
62 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala6d7bfa82008-02-27 21:51:47 -060063}
64
Becky Bruced26d67c2008-06-09 20:37:18 -050065static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010066 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060067{
68 if (base2 == base1 + size1)
69 return 1;
70 else if (base1 == base2 + size2)
71 return -1;
72
73 return 0;
74}
75
Udit Kumar4e8e6632023-09-26 16:54:42 +053076static long lmb_regions_overlap(struct lmb_region *rgn, unsigned long r1,
77 unsigned long r2)
78{
79 phys_addr_t base1 = rgn->region[r1].base;
80 phys_size_t size1 = rgn->region[r1].size;
81 phys_addr_t base2 = rgn->region[r2].base;
82 phys_size_t size2 = rgn->region[r2].size;
83
84 return lmb_addrs_overlap(base1, size1, base2, size2);
85}
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010086static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
87 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060088{
Becky Bruced26d67c2008-06-09 20:37:18 -050089 phys_addr_t base1 = rgn->region[r1].base;
90 phys_size_t size1 = rgn->region[r1].size;
91 phys_addr_t base2 = rgn->region[r2].base;
92 phys_size_t size2 = rgn->region[r2].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060093 return lmb_addrs_adjacent(base1, size1, base2, size2);
94}
95
96static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
97{
98 unsigned long i;
99
100 for (i = r; i < rgn->cnt - 1; i++) {
101 rgn->region[i].base = rgn->region[i + 1].base;
102 rgn->region[i].size = rgn->region[i + 1].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200103 rgn->region[i].flags = rgn->region[i + 1].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600104 }
105 rgn->cnt--;
106}
107
108/* Assumption: base addr of region 1 < base addr of region 2 */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100109static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
110 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600111{
112 rgn->region[r1].size += rgn->region[r2].size;
113 lmb_remove_region(rgn, r2);
114}
115
Udit Kumar4e8e6632023-09-26 16:54:42 +0530116/*Assumption : base addr of region 1 < base addr of region 2*/
117static void lmb_fix_over_lap_regions(struct lmb_region *rgn, unsigned long r1,
118 unsigned long r2)
119{
120 phys_addr_t base1 = rgn->region[r1].base;
121 phys_size_t size1 = rgn->region[r1].size;
122 phys_addr_t base2 = rgn->region[r2].base;
123 phys_size_t size2 = rgn->region[r2].size;
124
125 if (base1 + size1 > base2 + size2) {
126 printf("This will not be a case any time\n");
127 return;
128 }
129 rgn->region[r1].size = base2 + size2 - base1;
130 lmb_remove_region(rgn, r2);
131}
132
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600133void lmb_init(struct lmb *lmb)
134{
Patrick Delaunay71cc9c52021-03-10 10:16:31 +0100135#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
Patrick Delaunaycd831af2021-03-10 10:16:28 +0100136 lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
137 lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
Patrick Delaunay479de712023-03-22 19:12:25 +0100138#else
Patrick Delaunay71cc9c52021-03-10 10:16:31 +0100139 lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS;
140 lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS;
141 lmb->memory.region = lmb->memory_regions;
142 lmb->reserved.region = lmb->reserved_regions;
143#endif
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100144 lmb->memory.cnt = 0;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100145 lmb->reserved.cnt = 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600146}
147
Marek Vasuta2eec022021-09-10 22:47:09 +0200148void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align)
149{
150 ulong bank_end;
151 int bank;
152
153 /*
154 * Reserve memory from aligned address below the bottom of U-Boot stack
155 * until end of U-Boot area using LMB to prevent U-Boot from overwriting
156 * that memory.
157 */
158 debug("## Current stack ends at 0x%08lx ", sp);
159
160 /* adjust sp by 4K to be safe */
161 sp -= align;
162 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
163 if (!gd->bd->bi_dram[bank].size ||
164 sp < gd->bd->bi_dram[bank].start)
165 continue;
166 /* Watch out for RAM at end of address space! */
167 bank_end = gd->bd->bi_dram[bank].start +
168 gd->bd->bi_dram[bank].size - 1;
169 if (sp > bank_end)
170 continue;
171 if (bank_end > end)
172 bank_end = end - 1;
173
174 lmb_reserve(lmb, sp, bank_end - sp + 1);
Marek Vasut0fcae7f2021-11-13 18:34:37 +0100175
176 if (gd->flags & GD_FLG_SKIP_RELOC)
177 lmb_reserve(lmb, (phys_addr_t)(uintptr_t)_start, gd->mon_len);
178
Marek Vasuta2eec022021-09-10 22:47:09 +0200179 break;
180 }
181}
182
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100183/**
184 * efi_lmb_reserve() - add reservations for EFI memory
185 *
186 * Add reservations for all EFI memory areas that are not
187 * EFI_CONVENTIONAL_MEMORY.
188 *
189 * @lmb: lmb environment
190 * Return: 0 on success, 1 on failure
191 */
192static __maybe_unused int efi_lmb_reserve(struct lmb *lmb)
193{
194 struct efi_mem_desc *memmap = NULL, *map;
195 efi_uintn_t i, map_size = 0;
196 efi_status_t ret;
197
198 ret = efi_get_memory_map_alloc(&map_size, &memmap);
199 if (ret != EFI_SUCCESS)
200 return 1;
201
202 for (i = 0, map = memmap; i < map_size / sizeof(*map); ++map, ++i) {
Sjoerd Simons47a5a842023-01-19 09:38:18 +0100203 if (map->type != EFI_CONVENTIONAL_MEMORY) {
204 lmb_reserve_flags(lmb,
205 map_to_sysmem((void *)(uintptr_t)
206 map->physical_start),
207 map->num_pages * EFI_PAGE_SIZE,
208 map->type == EFI_RESERVED_MEMORY_TYPE
209 ? LMB_NOMAP : LMB_NONE);
210 }
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100211 }
212 efi_free_pool(memmap);
213
214 return 0;
215}
216
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100217static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100218{
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100219 arch_lmb_reserve(lmb);
220 board_lmb_reserve(lmb);
221
Simon Glass85c057e2021-09-25 19:43:21 -0600222 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100223 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100224
225 if (CONFIG_IS_ENABLED(EFI_LOADER))
226 efi_lmb_reserve(lmb);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100227}
228
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100229/* Initialize the struct, add memory and call arch/board reserve functions */
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900230void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100231{
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100232 int i;
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100233
234 lmb_init(lmb);
Stefan Roesebbc88462020-08-12 11:55:46 +0200235
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100236 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
237 if (bd->bi_dram[i].size) {
238 lmb_add(lmb, bd->bi_dram[i].start,
239 bd->bi_dram[i].size);
240 }
241 }
Stefan Roesebbc88462020-08-12 11:55:46 +0200242
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100243 lmb_reserve_common(lmb, fdt_blob);
244}
245
246/* Initialize the struct, add memory and call arch/board reserve functions */
247void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
248 phys_size_t size, void *fdt_blob)
249{
250 lmb_init(lmb);
251 lmb_add(lmb, base, size);
252 lmb_reserve_common(lmb, fdt_blob);
253}
254
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600255/* This routine called with relocation disabled. */
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200256static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
257 phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600258{
259 unsigned long coalesced = 0;
260 long adjacent, i;
261
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100262 if (rgn->cnt == 0) {
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600263 rgn->region[0].base = base;
264 rgn->region[0].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200265 rgn->region[0].flags = flags;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100266 rgn->cnt = 1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600267 return 0;
268 }
269
270 /* First try and coalesce this LMB with another. */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100271 for (i = 0; i < rgn->cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500272 phys_addr_t rgnbase = rgn->region[i].base;
273 phys_size_t rgnsize = rgn->region[i].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200274 phys_size_t rgnflags = rgn->region[i].flags;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100275 phys_addr_t end = base + size - 1;
276 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100277 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200278 if (flags == rgnflags)
279 /* Already have this region, so we're done */
280 return 0;
281 else
282 return -1; /* regions with new flags */
283 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600284
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100285 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
286 if (adjacent > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200287 if (flags != rgnflags)
288 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600289 rgn->region[i].base -= size;
290 rgn->region[i].size += size;
291 coalesced++;
292 break;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100293 } else if (adjacent < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200294 if (flags != rgnflags)
295 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600296 rgn->region[i].size += size;
297 coalesced++;
298 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100299 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
300 /* regions overlap */
301 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600302 }
303 }
304
Udit Kumar4e8e6632023-09-26 16:54:42 +0530305 if (i < rgn->cnt - 1 && rgn->region[i].flags == rgn->region[i + 1].flags) {
306 if (lmb_regions_adjacent(rgn, i, i + 1)) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200307 lmb_coalesce_regions(rgn, i, i + 1);
308 coalesced++;
Udit Kumar4e8e6632023-09-26 16:54:42 +0530309 } else if (lmb_regions_overlap(rgn, i, i + 1)) {
310 /* fix overlapping area */
311 lmb_fix_over_lap_regions(rgn, i, i + 1);
312 coalesced++;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200313 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600314 }
315
316 if (coalesced)
317 return coalesced;
Patrick Delaunay8c69fc22021-03-10 10:16:27 +0100318 if (rgn->cnt >= rgn->max)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600319 return -1;
320
321 /* Couldn't coalesce the LMB, so add it to the sorted table. */
322 for (i = rgn->cnt-1; i >= 0; i--) {
323 if (base < rgn->region[i].base) {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100324 rgn->region[i + 1].base = rgn->region[i].base;
325 rgn->region[i + 1].size = rgn->region[i].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200326 rgn->region[i + 1].flags = rgn->region[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600327 } else {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100328 rgn->region[i + 1].base = base;
329 rgn->region[i + 1].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200330 rgn->region[i + 1].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600331 break;
332 }
333 }
334
335 if (base < rgn->region[0].base) {
336 rgn->region[0].base = base;
337 rgn->region[0].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200338 rgn->region[0].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600339 }
340
341 rgn->cnt++;
342
343 return 0;
344}
345
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200346static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base,
347 phys_size_t size)
348{
349 return lmb_add_region_flags(rgn, base, size, LMB_NONE);
350}
351
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600352/* This routine may be called with relocation disabled. */
Becky Bruced26d67c2008-06-09 20:37:18 -0500353long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600354{
355 struct lmb_region *_rgn = &(lmb->memory);
356
357 return lmb_add_region(_rgn, base, size);
358}
359
Andy Fleming09d2a712008-07-07 14:24:39 -0500360long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500361{
362 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming09d2a712008-07-07 14:24:39 -0500363 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100364 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500365 int i;
366
367 rgnbegin = rgnend = 0; /* supress gcc warnings */
368
369 /* Find the region where (base, size) belongs to */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100370 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming1ae346c2008-06-16 13:58:54 -0500371 rgnbegin = rgn->region[i].base;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100372 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500373
374 if ((rgnbegin <= base) && (end <= rgnend))
375 break;
376 }
377
378 /* Didn't find the region */
379 if (i == rgn->cnt)
380 return -1;
381
382 /* Check to see if we are removing entire region */
383 if ((rgnbegin == base) && (rgnend == end)) {
384 lmb_remove_region(rgn, i);
385 return 0;
386 }
387
388 /* Check to see if region is matching at the front */
389 if (rgnbegin == base) {
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100390 rgn->region[i].base = end + 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500391 rgn->region[i].size -= size;
392 return 0;
393 }
394
395 /* Check to see if the region is matching at the end */
396 if (rgnend == end) {
397 rgn->region[i].size -= size;
398 return 0;
399 }
400
401 /*
402 * We need to split the entry - adjust the current one to the
403 * beginging of the hole and add the region after hole.
404 */
405 rgn->region[i].size = base - rgn->region[i].base;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200406 return lmb_add_region_flags(rgn, end + 1, rgnend - end,
407 rgn->region[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500408}
409
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200410long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size,
411 enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600412{
413 struct lmb_region *_rgn = &(lmb->reserved);
414
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200415 return lmb_add_region_flags(_rgn, base, size, flags);
416}
417
418long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
419{
420 return lmb_reserve_flags(lmb, base, size, LMB_NONE);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600421}
422
Jeroen Hofsteea0209722014-10-08 22:57:39 +0200423static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruced26d67c2008-06-09 20:37:18 -0500424 phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600425{
426 unsigned long i;
427
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100428 for (i = 0; i < rgn->cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500429 phys_addr_t rgnbase = rgn->region[i].base;
430 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100431 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600432 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600433 }
434
435 return (i < rgn->cnt) ? i : -1;
436}
437
Becky Bruced26d67c2008-06-09 20:37:18 -0500438phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600439{
440 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
441}
442
Becky Bruced26d67c2008-06-09 20:37:18 -0500443phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600444{
Becky Bruced26d67c2008-06-09 20:37:18 -0500445 phys_addr_t alloc;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600446
447 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
448
449 if (alloc == 0)
450 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100451 (ulong)size, (ulong)max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600452
453 return alloc;
454}
455
Becky Bruced26d67c2008-06-09 20:37:18 -0500456static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600457{
458 return addr & ~(size - 1);
459}
460
Becky Bruced26d67c2008-06-09 20:37:18 -0500461phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600462{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100463 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500464 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500465 phys_addr_t res_base;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600466
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100467 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500468 phys_addr_t lmbbase = lmb->memory.region[i].base;
469 phys_size_t lmbsize = lmb->memory.region[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600470
Andy Fleming78bd5a72008-06-16 13:58:55 -0500471 if (lmbsize < size)
472 continue;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600473 if (max_addr == LMB_ALLOC_ANYWHERE)
474 base = lmb_align_down(lmbbase + lmbsize - size, align);
475 else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600476 base = lmbbase + lmbsize;
477 if (base < lmbbase)
478 base = -1;
479 base = min(base, max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600480 base = lmb_align_down(base - size, align);
481 } else
482 continue;
483
Andy Fleming78bd5a72008-06-16 13:58:55 -0500484 while (base && lmbbase <= base) {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100485 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
486 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500487 /* This area isn't reserved, take it */
488 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100489 size) < 0)
Andy Fleming78bd5a72008-06-16 13:58:55 -0500490 return 0;
491 return base;
492 }
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100493 res_base = lmb->reserved.region[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500494 if (res_base < size)
495 break;
496 base = lmb_align_down(res_base - size, align);
497 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600498 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500499 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600500}
501
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100502/*
503 * Try to allocate a specific address range: must be in defined memory but not
504 * reserved
505 */
506phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
507{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100508 long rgn;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100509
510 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100511 rgn = lmb_overlaps_region(&lmb->memory, base, size);
512 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100513 /*
514 * Check if the requested end address is in the same memory
515 * region we found.
516 */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100517 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
518 lmb->memory.region[rgn].size,
519 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100520 /* ok, reserve the memory */
521 if (lmb_reserve(lmb, base, size) >= 0)
522 return base;
523 }
524 }
525 return 0;
526}
527
528/* Return number of bytes from a given address that are free */
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100529phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100530{
531 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100532 long rgn;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100533
534 /* check if the requested address is in the memory regions */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100535 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
536 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100537 for (i = 0; i < lmb->reserved.cnt; i++) {
538 if (addr < lmb->reserved.region[i].base) {
539 /* first reserved range > requested address */
540 return lmb->reserved.region[i].base - addr;
541 }
542 if (lmb->reserved.region[i].base +
543 lmb->reserved.region[i].size > addr) {
544 /* requested addr is in this reserved range */
545 return 0;
546 }
547 }
548 /* if we come here: no reserved ranges above requested addr */
549 return lmb->memory.region[lmb->memory.cnt - 1].base +
550 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
551 }
552 return 0;
553}
554
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200555int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600556{
557 int i;
558
559 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500560 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600561 lmb->reserved.region[i].size - 1;
562 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200563 return (lmb->reserved.region[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600564 }
565 return 0;
566}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500567
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200568int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
569{
570 return lmb_is_reserved_flags(lmb, addr, LMB_NONE);
571}
572
Jeroen Hofstee0461dda2014-06-26 20:04:37 +0200573__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera0dadf82009-11-03 11:35:59 -0500574{
575 /* please define platform specific board_lmb_reserve() */
576}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500577
Jeroen Hofstee0461dda2014-06-26 20:04:37 +0200578__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera0dadf82009-11-03 11:35:59 -0500579{
580 /* please define platform specific arch_lmb_reserve() */
581}