blob: 44f98205310146f1bf45ab569b067a81dc153879 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Gala4ed65522008-02-27 21:51:47 -06002/*
3 * Procedures for maintaining information about logical memory blocks.
4 *
5 * Peter Bergner, IBM Corp. June 2001.
6 * Copyright (C) 2001 Peter Bergner.
Kumar Gala4ed65522008-02-27 21:51:47 -06007 */
8
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +01009#include <efi_loader.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060010#include <image.h>
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +010011#include <mapmem.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060012#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <malloc.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060015
Marek Vasut12746982021-09-10 22:47:09 +020016#include <asm/global_data.h>
Marek Vasutbd994c02021-11-13 18:34:37 +010017#include <asm/sections.h>
Marek Vasut12746982021-09-10 22:47:09 +020018
19DECLARE_GLOBAL_DATA_PTR;
20
Kumar Gala4ed65522008-02-27 21:51:47 -060021#define LMB_ALLOC_ANYWHERE 0
22
Patrick Delaunay358c7782021-05-07 14:50:31 +020023static void lmb_dump_region(struct lmb_region *rgn, char *name)
24{
25 unsigned long long base, size, end;
26 enum lmb_flags flags;
27 int i;
28
Patrick Delaunayc9eb37a2023-03-22 19:12:26 +010029 printf(" %s.cnt = 0x%lx / max = 0x%lx\n", name, rgn->cnt, rgn->max);
Patrick Delaunay358c7782021-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;
36
37 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
38 name, i, base, end, size, flags);
39 }
40}
41
Tero Kristo9996cea2020-07-20 11:10:45 +030042void lmb_dump_all_force(struct lmb *lmb)
43{
Tero Kristo9996cea2020-07-20 11:10:45 +030044 printf("lmb_dump_all:\n");
Patrick Delaunay358c7782021-05-07 14:50:31 +020045 lmb_dump_region(&lmb->memory, "memory");
46 lmb_dump_region(&lmb->reserved, "reserved");
Tero Kristo9996cea2020-07-20 11:10:45 +030047}
48
Kumar Gala4ed65522008-02-27 21:51:47 -060049void lmb_dump_all(struct lmb *lmb)
50{
51#ifdef DEBUG
Tero Kristo9996cea2020-07-20 11:10:45 +030052 lmb_dump_all_force(lmb);
53#endif
Kumar Gala4ed65522008-02-27 21:51:47 -060054}
55
Simon Goldschmidte35d2a72019-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 Gala4ed65522008-02-27 21:51:47 -060058{
Simon Goldschmidtd67f33c2019-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 Gala4ed65522008-02-27 21:51:47 -060063}
64
Becky Bruce391fd932008-06-09 20:37:18 -050065static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010066 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-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 Kumaredb58242023-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 Goldschmidte35d2a72019-01-21 20:29:56 +010086static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
87 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -060088{
Becky Bruce391fd932008-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 Gala4ed65522008-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 Delaunay59c0ea52021-05-07 14:50:29 +0200103 rgn->region[i].flags = rgn->region[i + 1].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600104 }
105 rgn->cnt--;
106}
107
108/* Assumption: base addr of region 1 < base addr of region 2 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100109static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
110 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600111{
112 rgn->region[r1].size += rgn->region[r2].size;
113 lmb_remove_region(rgn, r2);
114}
115
Udit Kumaredb58242023-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 Gala4ed65522008-02-27 21:51:47 -0600133void lmb_init(struct lmb *lmb)
134{
Patrick Delaunay6d665022021-03-10 10:16:31 +0100135#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
Patrick Delaunay4fa01502021-03-10 10:16:28 +0100136 lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
137 lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
Patrick Delaunay94c8da22023-03-22 19:12:25 +0100138#else
Patrick Delaunay6d665022021-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 Goldschmidtd67f33c2019-01-14 22:38:15 +0100144 lmb->memory.cnt = 0;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100145 lmb->reserved.cnt = 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600146}
147
Marek Vasut12746982021-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 Vasutbd994c02021-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 Vasut12746982021-09-10 22:47:09 +0200179 break;
180 }
181}
182
Heinrich Schuchardt06d514d2023-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 Simonsc5279ea2023-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 Schuchardt06d514d2023-01-04 01:36:14 +0100211 }
212 efi_free_pool(memmap);
213
214 return 0;
215}
216
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100217static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100218{
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100219 arch_lmb_reserve(lmb);
220 board_lmb_reserve(lmb);
221
Simon Glass0c303f92021-09-25 19:43:21 -0600222 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100223 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +0100224
225 if (CONFIG_IS_ENABLED(EFI_LOADER))
226 efi_lmb_reserve(lmb);
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100227}
228
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100229/* Initialize the struct, add memory and call arch/board reserve functions */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900230void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100231{
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100232 int i;
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100233
234 lmb_init(lmb);
Stefan Roesedfaf6a52020-08-12 11:55:46 +0200235
Simon Goldschmidt9cc23232019-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 Roesedfaf6a52020-08-12 11:55:46 +0200242
Simon Goldschmidt9cc23232019-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 Gala4ed65522008-02-27 21:51:47 -0600255/* This routine called with relocation disabled. */
Patrick Delaunay59c0ea52021-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 Gala4ed65522008-02-27 21:51:47 -0600258{
259 unsigned long coalesced = 0;
260 long adjacent, i;
261
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100262 if (rgn->cnt == 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600263 rgn->region[0].base = base;
264 rgn->region[0].size = size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200265 rgn->region[0].flags = flags;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100266 rgn->cnt = 1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600267 return 0;
268 }
269
270 /* First try and coalesce this LMB with another. */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100271 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500272 phys_addr_t rgnbase = rgn->region[i].base;
273 phys_size_t rgnsize = rgn->region[i].size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200274 phys_size_t rgnflags = rgn->region[i].flags;
Sjoerd Simons0d91c882023-02-12 16:07:05 +0100275 phys_addr_t end = base + size - 1;
276 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Sjoerd Simons0d91c882023-02-12 16:07:05 +0100277 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunay59c0ea52021-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 Gala4ed65522008-02-27 21:51:47 -0600284
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100285 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
286 if (adjacent > 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200287 if (flags != rgnflags)
288 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600289 rgn->region[i].base -= size;
290 rgn->region[i].size += size;
291 coalesced++;
292 break;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100293 } else if (adjacent < 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200294 if (flags != rgnflags)
295 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600296 rgn->region[i].size += size;
297 coalesced++;
298 break;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100299 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
300 /* regions overlap */
301 return -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600302 }
303 }
304
Udit Kumaredb58242023-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 Delaunay59c0ea52021-05-07 14:50:29 +0200307 lmb_coalesce_regions(rgn, i, i + 1);
308 coalesced++;
Udit Kumaredb58242023-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 Delaunay59c0ea52021-05-07 14:50:29 +0200313 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600314 }
315
316 if (coalesced)
317 return coalesced;
Patrick Delaunay00fd8da2021-03-10 10:16:27 +0100318 if (rgn->cnt >= rgn->max)
Kumar Gala4ed65522008-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 Goldschmidte35d2a72019-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 Delaunay59c0ea52021-05-07 14:50:29 +0200326 rgn->region[i + 1].flags = rgn->region[i].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600327 } else {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100328 rgn->region[i + 1].base = base;
329 rgn->region[i + 1].size = size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200330 rgn->region[i + 1].flags = flags;
Kumar Gala4ed65522008-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 Delaunay59c0ea52021-05-07 14:50:29 +0200338 rgn->region[0].flags = flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600339 }
340
341 rgn->cnt++;
342
343 return 0;
344}
345
Patrick Delaunay59c0ea52021-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 Gala4ed65522008-02-27 21:51:47 -0600352/* This routine may be called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500353long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-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 Fleming98874ff2008-07-07 14:24:39 -0500360long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming63796c42008-06-16 13:58:54 -0500361{
362 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming98874ff2008-07-07 14:24:39 -0500363 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100364 phys_addr_t end = base + size - 1;
Andy Fleming63796c42008-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 Goldschmidte35d2a72019-01-21 20:29:56 +0100370 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming63796c42008-06-16 13:58:54 -0500371 rgnbegin = rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100372 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming63796c42008-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 Goldschmidtd67f33c2019-01-14 22:38:15 +0100390 rgn->region[i].base = end + 1;
Andy Fleming63796c42008-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 Delaunay59c0ea52021-05-07 14:50:29 +0200406 return lmb_add_region_flags(rgn, end + 1, rgnend - end,
407 rgn->region[i].flags);
408}
409
410long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size,
411 enum lmb_flags flags)
412{
413 struct lmb_region *_rgn = &(lmb->reserved);
414
415 return lmb_add_region_flags(_rgn, base, size, flags);
Andy Fleming63796c42008-06-16 13:58:54 -0500416}
417
Becky Bruce391fd932008-06-09 20:37:18 -0500418long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600419{
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200420 return lmb_reserve_flags(lmb, base, size, LMB_NONE);
Kumar Gala4ed65522008-02-27 21:51:47 -0600421}
422
Jeroen Hofstee750a6ff2014-10-08 22:57:39 +0200423static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruce391fd932008-06-09 20:37:18 -0500424 phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600425{
426 unsigned long i;
427
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100428 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500429 phys_addr_t rgnbase = rgn->region[i].base;
430 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100431 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala4ed65522008-02-27 21:51:47 -0600432 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600433 }
434
435 return (i < rgn->cnt) ? i : -1;
436}
437
Becky Bruce391fd932008-06-09 20:37:18 -0500438phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala4ed65522008-02-27 21:51:47 -0600439{
440 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
441}
442
Becky Bruce391fd932008-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 Gala4ed65522008-02-27 21:51:47 -0600444{
Becky Bruce391fd932008-06-09 20:37:18 -0500445 phys_addr_t alloc;
Kumar Gala4ed65522008-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 Goldschmidte35d2a72019-01-21 20:29:56 +0100451 (ulong)size, (ulong)max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600452
453 return alloc;
454}
455
Becky Bruce391fd932008-06-09 20:37:18 -0500456static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600457{
458 return addr & ~(size - 1);
459}
460
Becky Bruce391fd932008-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 Gala4ed65522008-02-27 21:51:47 -0600462{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100463 long i, rgn;
Becky Bruce391fd932008-06-09 20:37:18 -0500464 phys_addr_t base = 0;
Andy Fleming7570a992008-06-16 13:58:55 -0500465 phys_addr_t res_base;
Kumar Gala4ed65522008-02-27 21:51:47 -0600466
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100467 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruce391fd932008-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 Gala4ed65522008-02-27 21:51:47 -0600470
Andy Fleming7570a992008-06-16 13:58:55 -0500471 if (lmbsize < size)
472 continue;
Kumar Gala4ed65522008-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 Warrenad3fda52014-07-31 13:40:07 -0600476 base = lmbbase + lmbsize;
477 if (base < lmbbase)
478 base = -1;
479 base = min(base, max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600480 base = lmb_align_down(base - size, align);
481 } else
482 continue;
483
Andy Fleming7570a992008-06-16 13:58:55 -0500484 while (base && lmbbase <= base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100485 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
486 if (rgn < 0) {
Andy Fleming7570a992008-06-16 13:58:55 -0500487 /* This area isn't reserved, take it */
488 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100489 size) < 0)
Andy Fleming7570a992008-06-16 13:58:55 -0500490 return 0;
491 return base;
492 }
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100493 res_base = lmb->reserved.region[rgn].base;
Andy Fleming7570a992008-06-16 13:58:55 -0500494 if (res_base < size)
495 break;
496 base = lmb_align_down(res_base - size, align);
497 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600498 }
Andy Fleming7570a992008-06-16 13:58:55 -0500499 return 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600500}
501
Simon Goldschmidt4cc8af82019-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 Goldschmidte35d2a72019-01-21 20:29:56 +0100508 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100509
510 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100511 rgn = lmb_overlaps_region(&lmb->memory, base, size);
512 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100513 /*
514 * Check if the requested end address is in the same memory
515 * region we found.
516 */
Simon Goldschmidte35d2a72019-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 Goldschmidt4cc8af82019-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 Goldschmidt65304aa2019-01-21 20:29:55 +0100529phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100530{
531 int i;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100532 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100533
534 /* check if the requested address is in the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100535 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
536 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-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 Delaunaye359a4a2021-05-07 14:50:30 +0200555int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600556{
557 int i;
558
559 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500560 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala4ed65522008-02-27 21:51:47 -0600561 lmb->reserved.region[i].size - 1;
562 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
Patrick Delaunaye359a4a2021-05-07 14:50:30 +0200563 return (lmb->reserved.region[i].flags & flags) == flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600564 }
565 return 0;
566}
Mike Frysingera16028d2009-11-03 11:35:59 -0500567
Patrick Delaunaye359a4a2021-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 Hofstee2c34f3f2014-06-26 20:04:37 +0200573__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500574{
575 /* please define platform specific board_lmb_reserve() */
576}
Mike Frysingera16028d2009-11-03 11:35:59 -0500577
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200578__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500579{
580 /* please define platform specific arch_lmb_reserve() */
581}