blob: c599608fa3808430acbd148b480bbd7c9e3d3694 [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
9#include <common.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060010#include <image.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060011#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060014
Marek Vasut12746982021-09-10 22:47:09 +020015#include <asm/global_data.h>
Marek Vasutbd994c02021-11-13 18:34:37 +010016#include <asm/sections.h>
Marek Vasut12746982021-09-10 22:47:09 +020017
18DECLARE_GLOBAL_DATA_PTR;
19
Kumar Gala4ed65522008-02-27 21:51:47 -060020#define LMB_ALLOC_ANYWHERE 0
21
Patrick Delaunay358c7782021-05-07 14:50:31 +020022static void lmb_dump_region(struct lmb_region *rgn, char *name)
23{
24 unsigned long long base, size, end;
25 enum lmb_flags flags;
26 int i;
27
28 printf(" %s.cnt = 0x%lx\n", name, rgn->cnt);
29
30 for (i = 0; i < rgn->cnt; i++) {
31 base = rgn->region[i].base;
32 size = rgn->region[i].size;
33 end = base + size - 1;
34 flags = rgn->region[i].flags;
35
36 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
37 name, i, base, end, size, flags);
38 }
39}
40
Tero Kristo9996cea2020-07-20 11:10:45 +030041void lmb_dump_all_force(struct lmb *lmb)
42{
Tero Kristo9996cea2020-07-20 11:10:45 +030043 printf("lmb_dump_all:\n");
Patrick Delaunay358c7782021-05-07 14:50:31 +020044 lmb_dump_region(&lmb->memory, "memory");
45 lmb_dump_region(&lmb->reserved, "reserved");
Tero Kristo9996cea2020-07-20 11:10:45 +030046}
47
Kumar Gala4ed65522008-02-27 21:51:47 -060048void lmb_dump_all(struct lmb *lmb)
49{
50#ifdef DEBUG
Tero Kristo9996cea2020-07-20 11:10:45 +030051 lmb_dump_all_force(lmb);
52#endif
Kumar Gala4ed65522008-02-27 21:51:47 -060053}
54
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010055static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
56 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -060057{
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +010058 const phys_addr_t base1_end = base1 + size1 - 1;
59 const phys_addr_t base2_end = base2 + size2 - 1;
60
61 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala4ed65522008-02-27 21:51:47 -060062}
63
Becky Bruce391fd932008-06-09 20:37:18 -050064static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010065 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -060066{
67 if (base2 == base1 + size1)
68 return 1;
69 else if (base1 == base2 + size2)
70 return -1;
71
72 return 0;
73}
74
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010075static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
76 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -060077{
Becky Bruce391fd932008-06-09 20:37:18 -050078 phys_addr_t base1 = rgn->region[r1].base;
79 phys_size_t size1 = rgn->region[r1].size;
80 phys_addr_t base2 = rgn->region[r2].base;
81 phys_size_t size2 = rgn->region[r2].size;
Kumar Gala4ed65522008-02-27 21:51:47 -060082
83 return lmb_addrs_adjacent(base1, size1, base2, size2);
84}
85
86static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
87{
88 unsigned long i;
89
90 for (i = r; i < rgn->cnt - 1; i++) {
91 rgn->region[i].base = rgn->region[i + 1].base;
92 rgn->region[i].size = rgn->region[i + 1].size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +020093 rgn->region[i].flags = rgn->region[i + 1].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -060094 }
95 rgn->cnt--;
96}
97
98/* Assumption: base addr of region 1 < base addr of region 2 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010099static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
100 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600101{
102 rgn->region[r1].size += rgn->region[r2].size;
103 lmb_remove_region(rgn, r2);
104}
105
106void lmb_init(struct lmb *lmb)
107{
Patrick Delaunay6d665022021-03-10 10:16:31 +0100108#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
Patrick Delaunay4fa01502021-03-10 10:16:28 +0100109 lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
110 lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
Patrice Chotard5e2548c2022-08-02 10:21:35 +0200111#elif defined(CONFIG_LMB_MEMORY_REGIONS)
Patrick Delaunay6d665022021-03-10 10:16:31 +0100112 lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS;
113 lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS;
114 lmb->memory.region = lmb->memory_regions;
115 lmb->reserved.region = lmb->reserved_regions;
116#endif
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100117 lmb->memory.cnt = 0;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100118 lmb->reserved.cnt = 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600119}
120
Marek Vasut12746982021-09-10 22:47:09 +0200121void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align)
122{
123 ulong bank_end;
124 int bank;
125
126 /*
127 * Reserve memory from aligned address below the bottom of U-Boot stack
128 * until end of U-Boot area using LMB to prevent U-Boot from overwriting
129 * that memory.
130 */
131 debug("## Current stack ends at 0x%08lx ", sp);
132
133 /* adjust sp by 4K to be safe */
134 sp -= align;
135 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
136 if (!gd->bd->bi_dram[bank].size ||
137 sp < gd->bd->bi_dram[bank].start)
138 continue;
139 /* Watch out for RAM at end of address space! */
140 bank_end = gd->bd->bi_dram[bank].start +
141 gd->bd->bi_dram[bank].size - 1;
142 if (sp > bank_end)
143 continue;
144 if (bank_end > end)
145 bank_end = end - 1;
146
147 lmb_reserve(lmb, sp, bank_end - sp + 1);
Marek Vasutbd994c02021-11-13 18:34:37 +0100148
149 if (gd->flags & GD_FLG_SKIP_RELOC)
150 lmb_reserve(lmb, (phys_addr_t)(uintptr_t)_start, gd->mon_len);
151
Marek Vasut12746982021-09-10 22:47:09 +0200152 break;
153 }
154}
155
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100156static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100157{
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100158 arch_lmb_reserve(lmb);
159 board_lmb_reserve(lmb);
160
Simon Glass0c303f92021-09-25 19:43:21 -0600161 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100162 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
163}
164
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100165/* Initialize the struct, add memory and call arch/board reserve functions */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900166void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100167{
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100168 int i;
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100169
170 lmb_init(lmb);
Stefan Roesedfaf6a52020-08-12 11:55:46 +0200171
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100172 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
173 if (bd->bi_dram[i].size) {
174 lmb_add(lmb, bd->bi_dram[i].start,
175 bd->bi_dram[i].size);
176 }
177 }
Stefan Roesedfaf6a52020-08-12 11:55:46 +0200178
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100179 lmb_reserve_common(lmb, fdt_blob);
180}
181
182/* Initialize the struct, add memory and call arch/board reserve functions */
183void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
184 phys_size_t size, void *fdt_blob)
185{
186 lmb_init(lmb);
187 lmb_add(lmb, base, size);
188 lmb_reserve_common(lmb, fdt_blob);
189}
190
Kumar Gala4ed65522008-02-27 21:51:47 -0600191/* This routine called with relocation disabled. */
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200192static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
193 phys_size_t size, enum lmb_flags flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600194{
195 unsigned long coalesced = 0;
196 long adjacent, i;
197
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100198 if (rgn->cnt == 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600199 rgn->region[0].base = base;
200 rgn->region[0].size = size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200201 rgn->region[0].flags = flags;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100202 rgn->cnt = 1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600203 return 0;
204 }
205
206 /* First try and coalesce this LMB with another. */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100207 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500208 phys_addr_t rgnbase = rgn->region[i].base;
209 phys_size_t rgnsize = rgn->region[i].size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200210 phys_size_t rgnflags = rgn->region[i].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600211
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200212 if (rgnbase == base && rgnsize == size) {
213 if (flags == rgnflags)
214 /* Already have this region, so we're done */
215 return 0;
216 else
217 return -1; /* regions with new flags */
218 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600219
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100220 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
221 if (adjacent > 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200222 if (flags != rgnflags)
223 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600224 rgn->region[i].base -= size;
225 rgn->region[i].size += size;
226 coalesced++;
227 break;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100228 } else if (adjacent < 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200229 if (flags != rgnflags)
230 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600231 rgn->region[i].size += size;
232 coalesced++;
233 break;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100234 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
235 /* regions overlap */
236 return -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600237 }
238 }
239
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100240 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200241 if (rgn->region[i].flags == rgn->region[i + 1].flags) {
242 lmb_coalesce_regions(rgn, i, i + 1);
243 coalesced++;
244 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600245 }
246
247 if (coalesced)
248 return coalesced;
Patrick Delaunay00fd8da2021-03-10 10:16:27 +0100249 if (rgn->cnt >= rgn->max)
Kumar Gala4ed65522008-02-27 21:51:47 -0600250 return -1;
251
252 /* Couldn't coalesce the LMB, so add it to the sorted table. */
253 for (i = rgn->cnt-1; i >= 0; i--) {
254 if (base < rgn->region[i].base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100255 rgn->region[i + 1].base = rgn->region[i].base;
256 rgn->region[i + 1].size = rgn->region[i].size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200257 rgn->region[i + 1].flags = rgn->region[i].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600258 } else {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100259 rgn->region[i + 1].base = base;
260 rgn->region[i + 1].size = size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200261 rgn->region[i + 1].flags = flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600262 break;
263 }
264 }
265
266 if (base < rgn->region[0].base) {
267 rgn->region[0].base = base;
268 rgn->region[0].size = size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200269 rgn->region[0].flags = flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600270 }
271
272 rgn->cnt++;
273
274 return 0;
275}
276
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200277static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base,
278 phys_size_t size)
279{
280 return lmb_add_region_flags(rgn, base, size, LMB_NONE);
281}
282
Kumar Gala4ed65522008-02-27 21:51:47 -0600283/* This routine may be called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500284long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600285{
286 struct lmb_region *_rgn = &(lmb->memory);
287
288 return lmb_add_region(_rgn, base, size);
289}
290
Andy Fleming98874ff2008-07-07 14:24:39 -0500291long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming63796c42008-06-16 13:58:54 -0500292{
293 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming98874ff2008-07-07 14:24:39 -0500294 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100295 phys_addr_t end = base + size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500296 int i;
297
298 rgnbegin = rgnend = 0; /* supress gcc warnings */
299
300 /* Find the region where (base, size) belongs to */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100301 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming63796c42008-06-16 13:58:54 -0500302 rgnbegin = rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100303 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500304
305 if ((rgnbegin <= base) && (end <= rgnend))
306 break;
307 }
308
309 /* Didn't find the region */
310 if (i == rgn->cnt)
311 return -1;
312
313 /* Check to see if we are removing entire region */
314 if ((rgnbegin == base) && (rgnend == end)) {
315 lmb_remove_region(rgn, i);
316 return 0;
317 }
318
319 /* Check to see if region is matching at the front */
320 if (rgnbegin == base) {
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100321 rgn->region[i].base = end + 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500322 rgn->region[i].size -= size;
323 return 0;
324 }
325
326 /* Check to see if the region is matching at the end */
327 if (rgnend == end) {
328 rgn->region[i].size -= size;
329 return 0;
330 }
331
332 /*
333 * We need to split the entry - adjust the current one to the
334 * beginging of the hole and add the region after hole.
335 */
336 rgn->region[i].size = base - rgn->region[i].base;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200337 return lmb_add_region_flags(rgn, end + 1, rgnend - end,
338 rgn->region[i].flags);
339}
340
341long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size,
342 enum lmb_flags flags)
343{
344 struct lmb_region *_rgn = &(lmb->reserved);
345
346 return lmb_add_region_flags(_rgn, base, size, flags);
Andy Fleming63796c42008-06-16 13:58:54 -0500347}
348
Becky Bruce391fd932008-06-09 20:37:18 -0500349long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600350{
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200351 return lmb_reserve_flags(lmb, base, size, LMB_NONE);
Kumar Gala4ed65522008-02-27 21:51:47 -0600352}
353
Jeroen Hofstee750a6ff2014-10-08 22:57:39 +0200354static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruce391fd932008-06-09 20:37:18 -0500355 phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600356{
357 unsigned long i;
358
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100359 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500360 phys_addr_t rgnbase = rgn->region[i].base;
361 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100362 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala4ed65522008-02-27 21:51:47 -0600363 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600364 }
365
366 return (i < rgn->cnt) ? i : -1;
367}
368
Becky Bruce391fd932008-06-09 20:37:18 -0500369phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala4ed65522008-02-27 21:51:47 -0600370{
371 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
372}
373
Becky Bruce391fd932008-06-09 20:37:18 -0500374phys_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 -0600375{
Becky Bruce391fd932008-06-09 20:37:18 -0500376 phys_addr_t alloc;
Kumar Gala4ed65522008-02-27 21:51:47 -0600377
378 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
379
380 if (alloc == 0)
381 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100382 (ulong)size, (ulong)max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600383
384 return alloc;
385}
386
Becky Bruce391fd932008-06-09 20:37:18 -0500387static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600388{
389 return addr & ~(size - 1);
390}
391
Becky Bruce391fd932008-06-09 20:37:18 -0500392phys_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 -0600393{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100394 long i, rgn;
Becky Bruce391fd932008-06-09 20:37:18 -0500395 phys_addr_t base = 0;
Andy Fleming7570a992008-06-16 13:58:55 -0500396 phys_addr_t res_base;
Kumar Gala4ed65522008-02-27 21:51:47 -0600397
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100398 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruce391fd932008-06-09 20:37:18 -0500399 phys_addr_t lmbbase = lmb->memory.region[i].base;
400 phys_size_t lmbsize = lmb->memory.region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600401
Andy Fleming7570a992008-06-16 13:58:55 -0500402 if (lmbsize < size)
403 continue;
Kumar Gala4ed65522008-02-27 21:51:47 -0600404 if (max_addr == LMB_ALLOC_ANYWHERE)
405 base = lmb_align_down(lmbbase + lmbsize - size, align);
406 else if (lmbbase < max_addr) {
Stephen Warrenad3fda52014-07-31 13:40:07 -0600407 base = lmbbase + lmbsize;
408 if (base < lmbbase)
409 base = -1;
410 base = min(base, max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600411 base = lmb_align_down(base - size, align);
412 } else
413 continue;
414
Andy Fleming7570a992008-06-16 13:58:55 -0500415 while (base && lmbbase <= base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100416 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
417 if (rgn < 0) {
Andy Fleming7570a992008-06-16 13:58:55 -0500418 /* This area isn't reserved, take it */
419 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100420 size) < 0)
Andy Fleming7570a992008-06-16 13:58:55 -0500421 return 0;
422 return base;
423 }
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100424 res_base = lmb->reserved.region[rgn].base;
Andy Fleming7570a992008-06-16 13:58:55 -0500425 if (res_base < size)
426 break;
427 base = lmb_align_down(res_base - size, align);
428 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600429 }
Andy Fleming7570a992008-06-16 13:58:55 -0500430 return 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600431}
432
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100433/*
434 * Try to allocate a specific address range: must be in defined memory but not
435 * reserved
436 */
437phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
438{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100439 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100440
441 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100442 rgn = lmb_overlaps_region(&lmb->memory, base, size);
443 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100444 /*
445 * Check if the requested end address is in the same memory
446 * region we found.
447 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100448 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
449 lmb->memory.region[rgn].size,
450 base + size - 1, 1)) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100451 /* ok, reserve the memory */
452 if (lmb_reserve(lmb, base, size) >= 0)
453 return base;
454 }
455 }
456 return 0;
457}
458
459/* Return number of bytes from a given address that are free */
Simon Goldschmidt65304aa2019-01-21 20:29:55 +0100460phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100461{
462 int i;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100463 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100464
465 /* check if the requested address is in the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100466 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
467 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100468 for (i = 0; i < lmb->reserved.cnt; i++) {
469 if (addr < lmb->reserved.region[i].base) {
470 /* first reserved range > requested address */
471 return lmb->reserved.region[i].base - addr;
472 }
473 if (lmb->reserved.region[i].base +
474 lmb->reserved.region[i].size > addr) {
475 /* requested addr is in this reserved range */
476 return 0;
477 }
478 }
479 /* if we come here: no reserved ranges above requested addr */
480 return lmb->memory.region[lmb->memory.cnt - 1].base +
481 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
482 }
483 return 0;
484}
485
Patrick Delaunaye359a4a2021-05-07 14:50:30 +0200486int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600487{
488 int i;
489
490 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500491 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala4ed65522008-02-27 21:51:47 -0600492 lmb->reserved.region[i].size - 1;
493 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
Patrick Delaunaye359a4a2021-05-07 14:50:30 +0200494 return (lmb->reserved.region[i].flags & flags) == flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600495 }
496 return 0;
497}
Mike Frysingera16028d2009-11-03 11:35:59 -0500498
Patrick Delaunaye359a4a2021-05-07 14:50:30 +0200499int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
500{
501 return lmb_is_reserved_flags(lmb, addr, LMB_NONE);
502}
503
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200504__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500505{
506 /* please define platform specific board_lmb_reserve() */
507}
Mike Frysingera16028d2009-11-03 11:35:59 -0500508
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200509__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500510{
511 /* please define platform specific arch_lmb_reserve() */
512}