blob: 7bd1255f7a4c14cd8d0f6000c176b9151d98df0d [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
15#define LMB_ALLOC_ANYWHERE 0
16
Patrick Delaunay48120022021-05-07 14:50:31 +020017static void lmb_dump_region(struct lmb_region *rgn, char *name)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060018{
Patrick Delaunay48120022021-05-07 14:50:31 +020019 unsigned long long base, size, end;
20 enum lmb_flags flags;
21 int i;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060022
Patrick Delaunay48120022021-05-07 14:50:31 +020023 printf(" %s.cnt = 0x%lx\n", name, rgn->cnt);
24
25 for (i = 0; i < rgn->cnt; i++) {
26 base = rgn->region[i].base;
27 size = rgn->region[i].size;
28 end = base + size - 1;
29 flags = rgn->region[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060030
Patrick Delaunay48120022021-05-07 14:50:31 +020031 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
32 name, i, base, end, size, flags);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060033 }
Tero Kristo97c418b2020-07-20 11:10:45 +030034}
35
Patrick Delaunay48120022021-05-07 14:50:31 +020036void lmb_dump_all_force(struct lmb *lmb)
37{
38 printf("lmb_dump_all:\n");
39 lmb_dump_region(&lmb->memory, "memory");
40 lmb_dump_region(&lmb->reserved, "reserved");
41}
42
Tero Kristo97c418b2020-07-20 11:10:45 +030043void lmb_dump_all(struct lmb *lmb)
44{
45#ifdef DEBUG
46 lmb_dump_all_force(lmb);
47#endif
Kumar Gala6d7bfa82008-02-27 21:51:47 -060048}
49
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010050static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
51 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060052{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010053 const phys_addr_t base1_end = base1 + size1 - 1;
54 const phys_addr_t base2_end = base2 + size2 - 1;
55
56 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala6d7bfa82008-02-27 21:51:47 -060057}
58
Becky Bruced26d67c2008-06-09 20:37:18 -050059static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010060 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060061{
62 if (base2 == base1 + size1)
63 return 1;
64 else if (base1 == base2 + size2)
65 return -1;
66
67 return 0;
68}
69
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010070static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
71 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060072{
Becky Bruced26d67c2008-06-09 20:37:18 -050073 phys_addr_t base1 = rgn->region[r1].base;
74 phys_size_t size1 = rgn->region[r1].size;
75 phys_addr_t base2 = rgn->region[r2].base;
76 phys_size_t size2 = rgn->region[r2].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060077
78 return lmb_addrs_adjacent(base1, size1, base2, size2);
79}
80
81static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
82{
83 unsigned long i;
84
85 for (i = r; i < rgn->cnt - 1; i++) {
86 rgn->region[i].base = rgn->region[i + 1].base;
87 rgn->region[i].size = rgn->region[i + 1].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +020088 rgn->region[i].flags = rgn->region[i + 1].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060089 }
90 rgn->cnt--;
91}
92
93/* Assumption: base addr of region 1 < base addr of region 2 */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010094static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
95 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060096{
97 rgn->region[r1].size += rgn->region[r2].size;
98 lmb_remove_region(rgn, r2);
99}
100
101void lmb_init(struct lmb *lmb)
102{
Patrick Delaunay71cc9c52021-03-10 10:16:31 +0100103#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
Patrick Delaunaycd831af2021-03-10 10:16:28 +0100104 lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
105 lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
Patrick Delaunay71cc9c52021-03-10 10:16:31 +0100106#else
107 lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS;
108 lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS;
109 lmb->memory.region = lmb->memory_regions;
110 lmb->reserved.region = lmb->reserved_regions;
111#endif
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100112 lmb->memory.cnt = 0;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100113 lmb->reserved.cnt = 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600114}
115
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100116static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100117{
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100118 arch_lmb_reserve(lmb);
119 board_lmb_reserve(lmb);
120
121 if (IMAGE_ENABLE_OF_LIBFDT && fdt_blob)
122 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
123}
124
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100125/* Initialize the struct, add memory and call arch/board reserve functions */
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900126void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100127{
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100128 int i;
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100129
130 lmb_init(lmb);
Stefan Roesebbc88462020-08-12 11:55:46 +0200131
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100132 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
133 if (bd->bi_dram[i].size) {
134 lmb_add(lmb, bd->bi_dram[i].start,
135 bd->bi_dram[i].size);
136 }
137 }
Stefan Roesebbc88462020-08-12 11:55:46 +0200138
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100139 lmb_reserve_common(lmb, fdt_blob);
140}
141
142/* Initialize the struct, add memory and call arch/board reserve functions */
143void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
144 phys_size_t size, void *fdt_blob)
145{
146 lmb_init(lmb);
147 lmb_add(lmb, base, size);
148 lmb_reserve_common(lmb, fdt_blob);
149}
150
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600151/* This routine called with relocation disabled. */
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200152static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
153 phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600154{
155 unsigned long coalesced = 0;
156 long adjacent, i;
157
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100158 if (rgn->cnt == 0) {
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600159 rgn->region[0].base = base;
160 rgn->region[0].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200161 rgn->region[0].flags = flags;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100162 rgn->cnt = 1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600163 return 0;
164 }
165
166 /* First try and coalesce this LMB with another. */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100167 for (i = 0; i < rgn->cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500168 phys_addr_t rgnbase = rgn->region[i].base;
169 phys_size_t rgnsize = rgn->region[i].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200170 phys_size_t rgnflags = rgn->region[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600171
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200172 if (rgnbase == base && rgnsize == size) {
173 if (flags == rgnflags)
174 /* Already have this region, so we're done */
175 return 0;
176 else
177 return -1; /* regions with new flags */
178 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600179
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100180 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
181 if (adjacent > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200182 if (flags != rgnflags)
183 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600184 rgn->region[i].base -= size;
185 rgn->region[i].size += size;
186 coalesced++;
187 break;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100188 } else if (adjacent < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200189 if (flags != rgnflags)
190 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600191 rgn->region[i].size += size;
192 coalesced++;
193 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100194 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
195 /* regions overlap */
196 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600197 }
198 }
199
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100200 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200201 if (rgn->region[i].flags == rgn->region[i + 1].flags) {
202 lmb_coalesce_regions(rgn, i, i + 1);
203 coalesced++;
204 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600205 }
206
207 if (coalesced)
208 return coalesced;
Patrick Delaunay8c69fc22021-03-10 10:16:27 +0100209 if (rgn->cnt >= rgn->max)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600210 return -1;
211
212 /* Couldn't coalesce the LMB, so add it to the sorted table. */
213 for (i = rgn->cnt-1; i >= 0; i--) {
214 if (base < rgn->region[i].base) {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100215 rgn->region[i + 1].base = rgn->region[i].base;
216 rgn->region[i + 1].size = rgn->region[i].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200217 rgn->region[i + 1].flags = rgn->region[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600218 } else {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100219 rgn->region[i + 1].base = base;
220 rgn->region[i + 1].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200221 rgn->region[i + 1].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600222 break;
223 }
224 }
225
226 if (base < rgn->region[0].base) {
227 rgn->region[0].base = base;
228 rgn->region[0].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200229 rgn->region[0].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600230 }
231
232 rgn->cnt++;
233
234 return 0;
235}
236
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200237static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base,
238 phys_size_t size)
239{
240 return lmb_add_region_flags(rgn, base, size, LMB_NONE);
241}
242
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600243/* This routine may be called with relocation disabled. */
Becky Bruced26d67c2008-06-09 20:37:18 -0500244long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600245{
246 struct lmb_region *_rgn = &(lmb->memory);
247
248 return lmb_add_region(_rgn, base, size);
249}
250
Andy Fleming09d2a712008-07-07 14:24:39 -0500251long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500252{
253 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming09d2a712008-07-07 14:24:39 -0500254 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100255 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500256 int i;
257
258 rgnbegin = rgnend = 0; /* supress gcc warnings */
259
260 /* Find the region where (base, size) belongs to */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100261 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming1ae346c2008-06-16 13:58:54 -0500262 rgnbegin = rgn->region[i].base;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100263 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500264
265 if ((rgnbegin <= base) && (end <= rgnend))
266 break;
267 }
268
269 /* Didn't find the region */
270 if (i == rgn->cnt)
271 return -1;
272
273 /* Check to see if we are removing entire region */
274 if ((rgnbegin == base) && (rgnend == end)) {
275 lmb_remove_region(rgn, i);
276 return 0;
277 }
278
279 /* Check to see if region is matching at the front */
280 if (rgnbegin == base) {
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100281 rgn->region[i].base = end + 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500282 rgn->region[i].size -= size;
283 return 0;
284 }
285
286 /* Check to see if the region is matching at the end */
287 if (rgnend == end) {
288 rgn->region[i].size -= size;
289 return 0;
290 }
291
292 /*
293 * We need to split the entry - adjust the current one to the
294 * beginging of the hole and add the region after hole.
295 */
296 rgn->region[i].size = base - rgn->region[i].base;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200297 return lmb_add_region_flags(rgn, end + 1, rgnend - end,
298 rgn->region[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500299}
300
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200301long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size,
302 enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600303{
304 struct lmb_region *_rgn = &(lmb->reserved);
305
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200306 return lmb_add_region_flags(_rgn, base, size, flags);
307}
308
309long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
310{
311 return lmb_reserve_flags(lmb, base, size, LMB_NONE);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600312}
313
Jeroen Hofsteea0209722014-10-08 22:57:39 +0200314static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruced26d67c2008-06-09 20:37:18 -0500315 phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600316{
317 unsigned long i;
318
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100319 for (i = 0; i < rgn->cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500320 phys_addr_t rgnbase = rgn->region[i].base;
321 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100322 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600323 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600324 }
325
326 return (i < rgn->cnt) ? i : -1;
327}
328
Becky Bruced26d67c2008-06-09 20:37:18 -0500329phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600330{
331 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
332}
333
Becky Bruced26d67c2008-06-09 20:37:18 -0500334phys_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 -0600335{
Becky Bruced26d67c2008-06-09 20:37:18 -0500336 phys_addr_t alloc;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600337
338 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
339
340 if (alloc == 0)
341 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100342 (ulong)size, (ulong)max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600343
344 return alloc;
345}
346
Becky Bruced26d67c2008-06-09 20:37:18 -0500347static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600348{
349 return addr & ~(size - 1);
350}
351
Becky Bruced26d67c2008-06-09 20:37:18 -0500352phys_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 -0600353{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100354 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500355 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500356 phys_addr_t res_base;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600357
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100358 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500359 phys_addr_t lmbbase = lmb->memory.region[i].base;
360 phys_size_t lmbsize = lmb->memory.region[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600361
Andy Fleming78bd5a72008-06-16 13:58:55 -0500362 if (lmbsize < size)
363 continue;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600364 if (max_addr == LMB_ALLOC_ANYWHERE)
365 base = lmb_align_down(lmbbase + lmbsize - size, align);
366 else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600367 base = lmbbase + lmbsize;
368 if (base < lmbbase)
369 base = -1;
370 base = min(base, max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600371 base = lmb_align_down(base - size, align);
372 } else
373 continue;
374
Andy Fleming78bd5a72008-06-16 13:58:55 -0500375 while (base && lmbbase <= base) {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100376 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
377 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500378 /* This area isn't reserved, take it */
379 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100380 size) < 0)
Andy Fleming78bd5a72008-06-16 13:58:55 -0500381 return 0;
382 return base;
383 }
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100384 res_base = lmb->reserved.region[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500385 if (res_base < size)
386 break;
387 base = lmb_align_down(res_base - size, align);
388 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600389 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500390 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600391}
392
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100393/*
394 * Try to allocate a specific address range: must be in defined memory but not
395 * reserved
396 */
397phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
398{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100399 long rgn;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100400
401 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100402 rgn = lmb_overlaps_region(&lmb->memory, base, size);
403 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100404 /*
405 * Check if the requested end address is in the same memory
406 * region we found.
407 */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100408 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
409 lmb->memory.region[rgn].size,
410 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100411 /* ok, reserve the memory */
412 if (lmb_reserve(lmb, base, size) >= 0)
413 return base;
414 }
415 }
416 return 0;
417}
418
419/* Return number of bytes from a given address that are free */
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100420phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100421{
422 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100423 long rgn;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100424
425 /* check if the requested address is in the memory regions */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100426 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
427 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100428 for (i = 0; i < lmb->reserved.cnt; i++) {
429 if (addr < lmb->reserved.region[i].base) {
430 /* first reserved range > requested address */
431 return lmb->reserved.region[i].base - addr;
432 }
433 if (lmb->reserved.region[i].base +
434 lmb->reserved.region[i].size > addr) {
435 /* requested addr is in this reserved range */
436 return 0;
437 }
438 }
439 /* if we come here: no reserved ranges above requested addr */
440 return lmb->memory.region[lmb->memory.cnt - 1].base +
441 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
442 }
443 return 0;
444}
445
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200446int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600447{
448 int i;
449
450 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500451 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600452 lmb->reserved.region[i].size - 1;
453 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200454 return (lmb->reserved.region[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600455 }
456 return 0;
457}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500458
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200459int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
460{
461 return lmb_is_reserved_flags(lmb, addr, LMB_NONE);
462}
463
Jeroen Hofstee0461dda2014-06-26 20:04:37 +0200464__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera0dadf82009-11-03 11:35:59 -0500465{
466 /* please define platform specific board_lmb_reserve() */
467}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500468
Jeroen Hofstee0461dda2014-06-26 20:04:37 +0200469__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera0dadf82009-11-03 11:35:59 -0500470{
471 /* please define platform specific arch_lmb_reserve() */
472}