blob: 676b3a0bda6f0d2270c47db95c20aef686a77a5a [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
9#include <common.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060010#include <image.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060011#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060014
Marek Vasuta2eec022021-09-10 22:47:09 +020015#include <asm/global_data.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
Kumar Gala6d7bfa82008-02-27 21:51:47 -060019#define LMB_ALLOC_ANYWHERE 0
20
Patrick Delaunay48120022021-05-07 14:50:31 +020021static void lmb_dump_region(struct lmb_region *rgn, char *name)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060022{
Patrick Delaunay48120022021-05-07 14:50:31 +020023 unsigned long long base, size, end;
24 enum lmb_flags flags;
25 int i;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060026
Patrick Delaunay48120022021-05-07 14:50:31 +020027 printf(" %s.cnt = 0x%lx\n", name, rgn->cnt);
28
29 for (i = 0; i < rgn->cnt; i++) {
30 base = rgn->region[i].base;
31 size = rgn->region[i].size;
32 end = base + size - 1;
33 flags = rgn->region[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060034
Patrick Delaunay48120022021-05-07 14:50:31 +020035 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
36 name, i, base, end, size, flags);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060037 }
Tero Kristo97c418b2020-07-20 11:10:45 +030038}
39
Patrick Delaunay48120022021-05-07 14:50:31 +020040void lmb_dump_all_force(struct lmb *lmb)
41{
42 printf("lmb_dump_all:\n");
43 lmb_dump_region(&lmb->memory, "memory");
44 lmb_dump_region(&lmb->reserved, "reserved");
45}
46
Tero Kristo97c418b2020-07-20 11:10:45 +030047void lmb_dump_all(struct lmb *lmb)
48{
49#ifdef DEBUG
50 lmb_dump_all_force(lmb);
51#endif
Kumar Gala6d7bfa82008-02-27 21:51:47 -060052}
53
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010054static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
55 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060056{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010057 const phys_addr_t base1_end = base1 + size1 - 1;
58 const phys_addr_t base2_end = base2 + size2 - 1;
59
60 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala6d7bfa82008-02-27 21:51:47 -060061}
62
Becky Bruced26d67c2008-06-09 20:37:18 -050063static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010064 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060065{
66 if (base2 == base1 + size1)
67 return 1;
68 else if (base1 == base2 + size2)
69 return -1;
70
71 return 0;
72}
73
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010074static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
75 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060076{
Becky Bruced26d67c2008-06-09 20:37:18 -050077 phys_addr_t base1 = rgn->region[r1].base;
78 phys_size_t size1 = rgn->region[r1].size;
79 phys_addr_t base2 = rgn->region[r2].base;
80 phys_size_t size2 = rgn->region[r2].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060081
82 return lmb_addrs_adjacent(base1, size1, base2, size2);
83}
84
85static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
86{
87 unsigned long i;
88
89 for (i = r; i < rgn->cnt - 1; i++) {
90 rgn->region[i].base = rgn->region[i + 1].base;
91 rgn->region[i].size = rgn->region[i + 1].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +020092 rgn->region[i].flags = rgn->region[i + 1].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060093 }
94 rgn->cnt--;
95}
96
97/* Assumption: base addr of region 1 < base addr of region 2 */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010098static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
99 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600100{
101 rgn->region[r1].size += rgn->region[r2].size;
102 lmb_remove_region(rgn, r2);
103}
104
105void lmb_init(struct lmb *lmb)
106{
Patrick Delaunay71cc9c52021-03-10 10:16:31 +0100107#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
Patrick Delaunaycd831af2021-03-10 10:16:28 +0100108 lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
109 lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
Patrick Delaunay71cc9c52021-03-10 10:16:31 +0100110#else
111 lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS;
112 lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS;
113 lmb->memory.region = lmb->memory_regions;
114 lmb->reserved.region = lmb->reserved_regions;
115#endif
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100116 lmb->memory.cnt = 0;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100117 lmb->reserved.cnt = 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600118}
119
Marek Vasuta2eec022021-09-10 22:47:09 +0200120void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align)
121{
122 ulong bank_end;
123 int bank;
124
125 /*
126 * Reserve memory from aligned address below the bottom of U-Boot stack
127 * until end of U-Boot area using LMB to prevent U-Boot from overwriting
128 * that memory.
129 */
130 debug("## Current stack ends at 0x%08lx ", sp);
131
132 /* adjust sp by 4K to be safe */
133 sp -= align;
134 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
135 if (!gd->bd->bi_dram[bank].size ||
136 sp < gd->bd->bi_dram[bank].start)
137 continue;
138 /* Watch out for RAM at end of address space! */
139 bank_end = gd->bd->bi_dram[bank].start +
140 gd->bd->bi_dram[bank].size - 1;
141 if (sp > bank_end)
142 continue;
143 if (bank_end > end)
144 bank_end = end - 1;
145
146 lmb_reserve(lmb, sp, bank_end - sp + 1);
147 break;
148 }
149}
150
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100151static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100152{
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100153 arch_lmb_reserve(lmb);
154 board_lmb_reserve(lmb);
155
Simon Glass85c057e2021-09-25 19:43:21 -0600156 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100157 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
158}
159
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100160/* Initialize the struct, add memory and call arch/board reserve functions */
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900161void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100162{
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100163 int i;
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100164
165 lmb_init(lmb);
Stefan Roesebbc88462020-08-12 11:55:46 +0200166
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100167 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
168 if (bd->bi_dram[i].size) {
169 lmb_add(lmb, bd->bi_dram[i].start,
170 bd->bi_dram[i].size);
171 }
172 }
Stefan Roesebbc88462020-08-12 11:55:46 +0200173
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100174 lmb_reserve_common(lmb, fdt_blob);
175}
176
177/* Initialize the struct, add memory and call arch/board reserve functions */
178void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
179 phys_size_t size, void *fdt_blob)
180{
181 lmb_init(lmb);
182 lmb_add(lmb, base, size);
183 lmb_reserve_common(lmb, fdt_blob);
184}
185
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600186/* This routine called with relocation disabled. */
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200187static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
188 phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600189{
190 unsigned long coalesced = 0;
191 long adjacent, i;
192
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100193 if (rgn->cnt == 0) {
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600194 rgn->region[0].base = base;
195 rgn->region[0].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200196 rgn->region[0].flags = flags;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100197 rgn->cnt = 1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600198 return 0;
199 }
200
201 /* First try and coalesce this LMB with another. */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100202 for (i = 0; i < rgn->cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500203 phys_addr_t rgnbase = rgn->region[i].base;
204 phys_size_t rgnsize = rgn->region[i].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200205 phys_size_t rgnflags = rgn->region[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600206
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200207 if (rgnbase == base && rgnsize == size) {
208 if (flags == rgnflags)
209 /* Already have this region, so we're done */
210 return 0;
211 else
212 return -1; /* regions with new flags */
213 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600214
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100215 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
216 if (adjacent > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200217 if (flags != rgnflags)
218 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600219 rgn->region[i].base -= size;
220 rgn->region[i].size += size;
221 coalesced++;
222 break;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100223 } else if (adjacent < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200224 if (flags != rgnflags)
225 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600226 rgn->region[i].size += size;
227 coalesced++;
228 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100229 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
230 /* regions overlap */
231 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600232 }
233 }
234
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100235 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200236 if (rgn->region[i].flags == rgn->region[i + 1].flags) {
237 lmb_coalesce_regions(rgn, i, i + 1);
238 coalesced++;
239 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600240 }
241
242 if (coalesced)
243 return coalesced;
Patrick Delaunay8c69fc22021-03-10 10:16:27 +0100244 if (rgn->cnt >= rgn->max)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600245 return -1;
246
247 /* Couldn't coalesce the LMB, so add it to the sorted table. */
248 for (i = rgn->cnt-1; i >= 0; i--) {
249 if (base < rgn->region[i].base) {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100250 rgn->region[i + 1].base = rgn->region[i].base;
251 rgn->region[i + 1].size = rgn->region[i].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200252 rgn->region[i + 1].flags = rgn->region[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600253 } else {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100254 rgn->region[i + 1].base = base;
255 rgn->region[i + 1].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200256 rgn->region[i + 1].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600257 break;
258 }
259 }
260
261 if (base < rgn->region[0].base) {
262 rgn->region[0].base = base;
263 rgn->region[0].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200264 rgn->region[0].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600265 }
266
267 rgn->cnt++;
268
269 return 0;
270}
271
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200272static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base,
273 phys_size_t size)
274{
275 return lmb_add_region_flags(rgn, base, size, LMB_NONE);
276}
277
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600278/* This routine may be called with relocation disabled. */
Becky Bruced26d67c2008-06-09 20:37:18 -0500279long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600280{
281 struct lmb_region *_rgn = &(lmb->memory);
282
283 return lmb_add_region(_rgn, base, size);
284}
285
Andy Fleming09d2a712008-07-07 14:24:39 -0500286long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500287{
288 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming09d2a712008-07-07 14:24:39 -0500289 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100290 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500291 int i;
292
293 rgnbegin = rgnend = 0; /* supress gcc warnings */
294
295 /* Find the region where (base, size) belongs to */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100296 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming1ae346c2008-06-16 13:58:54 -0500297 rgnbegin = rgn->region[i].base;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100298 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500299
300 if ((rgnbegin <= base) && (end <= rgnend))
301 break;
302 }
303
304 /* Didn't find the region */
305 if (i == rgn->cnt)
306 return -1;
307
308 /* Check to see if we are removing entire region */
309 if ((rgnbegin == base) && (rgnend == end)) {
310 lmb_remove_region(rgn, i);
311 return 0;
312 }
313
314 /* Check to see if region is matching at the front */
315 if (rgnbegin == base) {
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100316 rgn->region[i].base = end + 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500317 rgn->region[i].size -= size;
318 return 0;
319 }
320
321 /* Check to see if the region is matching at the end */
322 if (rgnend == end) {
323 rgn->region[i].size -= size;
324 return 0;
325 }
326
327 /*
328 * We need to split the entry - adjust the current one to the
329 * beginging of the hole and add the region after hole.
330 */
331 rgn->region[i].size = base - rgn->region[i].base;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200332 return lmb_add_region_flags(rgn, end + 1, rgnend - end,
333 rgn->region[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500334}
335
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200336long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size,
337 enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600338{
339 struct lmb_region *_rgn = &(lmb->reserved);
340
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200341 return lmb_add_region_flags(_rgn, base, size, flags);
342}
343
344long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
345{
346 return lmb_reserve_flags(lmb, base, size, LMB_NONE);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600347}
348
Jeroen Hofsteea0209722014-10-08 22:57:39 +0200349static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruced26d67c2008-06-09 20:37:18 -0500350 phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600351{
352 unsigned long i;
353
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100354 for (i = 0; i < rgn->cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500355 phys_addr_t rgnbase = rgn->region[i].base;
356 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100357 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600358 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600359 }
360
361 return (i < rgn->cnt) ? i : -1;
362}
363
Becky Bruced26d67c2008-06-09 20:37:18 -0500364phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600365{
366 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
367}
368
Becky Bruced26d67c2008-06-09 20:37:18 -0500369phys_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 -0600370{
Becky Bruced26d67c2008-06-09 20:37:18 -0500371 phys_addr_t alloc;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600372
373 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
374
375 if (alloc == 0)
376 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100377 (ulong)size, (ulong)max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600378
379 return alloc;
380}
381
Becky Bruced26d67c2008-06-09 20:37:18 -0500382static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600383{
384 return addr & ~(size - 1);
385}
386
Becky Bruced26d67c2008-06-09 20:37:18 -0500387phys_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 -0600388{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100389 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500390 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500391 phys_addr_t res_base;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600392
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100393 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500394 phys_addr_t lmbbase = lmb->memory.region[i].base;
395 phys_size_t lmbsize = lmb->memory.region[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600396
Andy Fleming78bd5a72008-06-16 13:58:55 -0500397 if (lmbsize < size)
398 continue;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600399 if (max_addr == LMB_ALLOC_ANYWHERE)
400 base = lmb_align_down(lmbbase + lmbsize - size, align);
401 else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600402 base = lmbbase + lmbsize;
403 if (base < lmbbase)
404 base = -1;
405 base = min(base, max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600406 base = lmb_align_down(base - size, align);
407 } else
408 continue;
409
Andy Fleming78bd5a72008-06-16 13:58:55 -0500410 while (base && lmbbase <= base) {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100411 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
412 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500413 /* This area isn't reserved, take it */
414 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100415 size) < 0)
Andy Fleming78bd5a72008-06-16 13:58:55 -0500416 return 0;
417 return base;
418 }
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100419 res_base = lmb->reserved.region[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500420 if (res_base < size)
421 break;
422 base = lmb_align_down(res_base - size, align);
423 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600424 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500425 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600426}
427
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100428/*
429 * Try to allocate a specific address range: must be in defined memory but not
430 * reserved
431 */
432phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
433{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100434 long rgn;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100435
436 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100437 rgn = lmb_overlaps_region(&lmb->memory, base, size);
438 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100439 /*
440 * Check if the requested end address is in the same memory
441 * region we found.
442 */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100443 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
444 lmb->memory.region[rgn].size,
445 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100446 /* ok, reserve the memory */
447 if (lmb_reserve(lmb, base, size) >= 0)
448 return base;
449 }
450 }
451 return 0;
452}
453
454/* Return number of bytes from a given address that are free */
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100455phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100456{
457 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100458 long rgn;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100459
460 /* check if the requested address is in the memory regions */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100461 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
462 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100463 for (i = 0; i < lmb->reserved.cnt; i++) {
464 if (addr < lmb->reserved.region[i].base) {
465 /* first reserved range > requested address */
466 return lmb->reserved.region[i].base - addr;
467 }
468 if (lmb->reserved.region[i].base +
469 lmb->reserved.region[i].size > addr) {
470 /* requested addr is in this reserved range */
471 return 0;
472 }
473 }
474 /* if we come here: no reserved ranges above requested addr */
475 return lmb->memory.region[lmb->memory.cnt - 1].base +
476 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
477 }
478 return 0;
479}
480
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200481int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600482{
483 int i;
484
485 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500486 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600487 lmb->reserved.region[i].size - 1;
488 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200489 return (lmb->reserved.region[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600490 }
491 return 0;
492}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500493
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200494int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
495{
496 return lmb_is_reserved_flags(lmb, addr, LMB_NONE);
497}
498
Jeroen Hofstee0461dda2014-06-26 20:04:37 +0200499__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera0dadf82009-11-03 11:35:59 -0500500{
501 /* please define platform specific board_lmb_reserve() */
502}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500503
Jeroen Hofstee0461dda2014-06-26 20:04:37 +0200504__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera0dadf82009-11-03 11:35:59 -0500505{
506 /* please define platform specific arch_lmb_reserve() */
507}