blob: 7bd1255f7a4c14cd8d0f6000c176b9151d98df0d [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
15#define LMB_ALLOC_ANYWHERE 0
16
Patrick Delaunay358c7782021-05-07 14:50:31 +020017static void lmb_dump_region(struct lmb_region *rgn, char *name)
18{
19 unsigned long long base, size, end;
20 enum lmb_flags flags;
21 int i;
22
23 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;
30
31 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
32 name, i, base, end, size, flags);
33 }
34}
35
Tero Kristo9996cea2020-07-20 11:10:45 +030036void lmb_dump_all_force(struct lmb *lmb)
37{
Tero Kristo9996cea2020-07-20 11:10:45 +030038 printf("lmb_dump_all:\n");
Patrick Delaunay358c7782021-05-07 14:50:31 +020039 lmb_dump_region(&lmb->memory, "memory");
40 lmb_dump_region(&lmb->reserved, "reserved");
Tero Kristo9996cea2020-07-20 11:10:45 +030041}
42
Kumar Gala4ed65522008-02-27 21:51:47 -060043void lmb_dump_all(struct lmb *lmb)
44{
45#ifdef DEBUG
Tero Kristo9996cea2020-07-20 11:10:45 +030046 lmb_dump_all_force(lmb);
47#endif
Kumar Gala4ed65522008-02-27 21:51:47 -060048}
49
Simon Goldschmidte35d2a72019-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 Gala4ed65522008-02-27 21:51:47 -060052{
Simon Goldschmidtd67f33c2019-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 Gala4ed65522008-02-27 21:51:47 -060057}
58
Becky Bruce391fd932008-06-09 20:37:18 -050059static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010060 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-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 Goldschmidte35d2a72019-01-21 20:29:56 +010070static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
71 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -060072{
Becky Bruce391fd932008-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 Gala4ed65522008-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 Delaunay59c0ea52021-05-07 14:50:29 +020088 rgn->region[i].flags = rgn->region[i + 1].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -060089 }
90 rgn->cnt--;
91}
92
93/* Assumption: base addr of region 1 < base addr of region 2 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010094static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
95 unsigned long r2)
Kumar Gala4ed65522008-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 Delaunay6d665022021-03-10 10:16:31 +0100103#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
Patrick Delaunay4fa01502021-03-10 10:16:28 +0100104 lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
105 lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
Patrick Delaunay6d665022021-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 Goldschmidtd67f33c2019-01-14 22:38:15 +0100112 lmb->memory.cnt = 0;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100113 lmb->reserved.cnt = 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600114}
115
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100116static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100117{
Simon Goldschmidtaa3c6092019-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 Goldschmidt9cc23232019-01-26 22:13:04 +0100125/* Initialize the struct, add memory and call arch/board reserve functions */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900126void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100127{
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100128 int i;
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100129
130 lmb_init(lmb);
Stefan Roesedfaf6a52020-08-12 11:55:46 +0200131
Simon Goldschmidt9cc23232019-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 Roesedfaf6a52020-08-12 11:55:46 +0200138
Simon Goldschmidt9cc23232019-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 Gala4ed65522008-02-27 21:51:47 -0600151/* This routine called with relocation disabled. */
Patrick Delaunay59c0ea52021-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 Gala4ed65522008-02-27 21:51:47 -0600154{
155 unsigned long coalesced = 0;
156 long adjacent, i;
157
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100158 if (rgn->cnt == 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600159 rgn->region[0].base = base;
160 rgn->region[0].size = size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200161 rgn->region[0].flags = flags;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100162 rgn->cnt = 1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600163 return 0;
164 }
165
166 /* First try and coalesce this LMB with another. */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100167 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500168 phys_addr_t rgnbase = rgn->region[i].base;
169 phys_size_t rgnsize = rgn->region[i].size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200170 phys_size_t rgnflags = rgn->region[i].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600171
Patrick Delaunay59c0ea52021-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 Gala4ed65522008-02-27 21:51:47 -0600179
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100180 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
181 if (adjacent > 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200182 if (flags != rgnflags)
183 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600184 rgn->region[i].base -= size;
185 rgn->region[i].size += size;
186 coalesced++;
187 break;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100188 } else if (adjacent < 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200189 if (flags != rgnflags)
190 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600191 rgn->region[i].size += size;
192 coalesced++;
193 break;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100194 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
195 /* regions overlap */
196 return -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600197 }
198 }
199
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100200 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
Patrick Delaunay59c0ea52021-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 Gala4ed65522008-02-27 21:51:47 -0600205 }
206
207 if (coalesced)
208 return coalesced;
Patrick Delaunay00fd8da2021-03-10 10:16:27 +0100209 if (rgn->cnt >= rgn->max)
Kumar Gala4ed65522008-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 Goldschmidte35d2a72019-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 Delaunay59c0ea52021-05-07 14:50:29 +0200217 rgn->region[i + 1].flags = rgn->region[i].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600218 } else {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100219 rgn->region[i + 1].base = base;
220 rgn->region[i + 1].size = size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200221 rgn->region[i + 1].flags = flags;
Kumar Gala4ed65522008-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 Delaunay59c0ea52021-05-07 14:50:29 +0200229 rgn->region[0].flags = flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600230 }
231
232 rgn->cnt++;
233
234 return 0;
235}
236
Patrick Delaunay59c0ea52021-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 Gala4ed65522008-02-27 21:51:47 -0600243/* This routine may be called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500244long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-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 Fleming98874ff2008-07-07 14:24:39 -0500251long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming63796c42008-06-16 13:58:54 -0500252{
253 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming98874ff2008-07-07 14:24:39 -0500254 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100255 phys_addr_t end = base + size - 1;
Andy Fleming63796c42008-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 Goldschmidte35d2a72019-01-21 20:29:56 +0100261 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming63796c42008-06-16 13:58:54 -0500262 rgnbegin = rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100263 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming63796c42008-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 Goldschmidtd67f33c2019-01-14 22:38:15 +0100281 rgn->region[i].base = end + 1;
Andy Fleming63796c42008-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 Delaunay59c0ea52021-05-07 14:50:29 +0200297 return lmb_add_region_flags(rgn, end + 1, rgnend - end,
298 rgn->region[i].flags);
299}
300
301long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size,
302 enum lmb_flags flags)
303{
304 struct lmb_region *_rgn = &(lmb->reserved);
305
306 return lmb_add_region_flags(_rgn, base, size, flags);
Andy Fleming63796c42008-06-16 13:58:54 -0500307}
308
Becky Bruce391fd932008-06-09 20:37:18 -0500309long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600310{
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200311 return lmb_reserve_flags(lmb, base, size, LMB_NONE);
Kumar Gala4ed65522008-02-27 21:51:47 -0600312}
313
Jeroen Hofstee750a6ff2014-10-08 22:57:39 +0200314static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruce391fd932008-06-09 20:37:18 -0500315 phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600316{
317 unsigned long i;
318
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100319 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500320 phys_addr_t rgnbase = rgn->region[i].base;
321 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100322 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala4ed65522008-02-27 21:51:47 -0600323 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600324 }
325
326 return (i < rgn->cnt) ? i : -1;
327}
328
Becky Bruce391fd932008-06-09 20:37:18 -0500329phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala4ed65522008-02-27 21:51:47 -0600330{
331 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
332}
333
Becky Bruce391fd932008-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 Gala4ed65522008-02-27 21:51:47 -0600335{
Becky Bruce391fd932008-06-09 20:37:18 -0500336 phys_addr_t alloc;
Kumar Gala4ed65522008-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 Goldschmidte35d2a72019-01-21 20:29:56 +0100342 (ulong)size, (ulong)max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600343
344 return alloc;
345}
346
Becky Bruce391fd932008-06-09 20:37:18 -0500347static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600348{
349 return addr & ~(size - 1);
350}
351
Becky Bruce391fd932008-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 Gala4ed65522008-02-27 21:51:47 -0600353{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100354 long i, rgn;
Becky Bruce391fd932008-06-09 20:37:18 -0500355 phys_addr_t base = 0;
Andy Fleming7570a992008-06-16 13:58:55 -0500356 phys_addr_t res_base;
Kumar Gala4ed65522008-02-27 21:51:47 -0600357
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100358 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruce391fd932008-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 Gala4ed65522008-02-27 21:51:47 -0600361
Andy Fleming7570a992008-06-16 13:58:55 -0500362 if (lmbsize < size)
363 continue;
Kumar Gala4ed65522008-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 Warrenad3fda52014-07-31 13:40:07 -0600367 base = lmbbase + lmbsize;
368 if (base < lmbbase)
369 base = -1;
370 base = min(base, max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600371 base = lmb_align_down(base - size, align);
372 } else
373 continue;
374
Andy Fleming7570a992008-06-16 13:58:55 -0500375 while (base && lmbbase <= base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100376 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
377 if (rgn < 0) {
Andy Fleming7570a992008-06-16 13:58:55 -0500378 /* This area isn't reserved, take it */
379 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100380 size) < 0)
Andy Fleming7570a992008-06-16 13:58:55 -0500381 return 0;
382 return base;
383 }
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100384 res_base = lmb->reserved.region[rgn].base;
Andy Fleming7570a992008-06-16 13:58:55 -0500385 if (res_base < size)
386 break;
387 base = lmb_align_down(res_base - size, align);
388 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600389 }
Andy Fleming7570a992008-06-16 13:58:55 -0500390 return 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600391}
392
Simon Goldschmidt4cc8af82019-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 Goldschmidte35d2a72019-01-21 20:29:56 +0100399 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100400
401 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100402 rgn = lmb_overlaps_region(&lmb->memory, base, size);
403 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100404 /*
405 * Check if the requested end address is in the same memory
406 * region we found.
407 */
Simon Goldschmidte35d2a72019-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 Goldschmidt4cc8af82019-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 Goldschmidt65304aa2019-01-21 20:29:55 +0100420phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100421{
422 int i;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100423 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100424
425 /* check if the requested address is in the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100426 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
427 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-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 Delaunaye359a4a2021-05-07 14:50:30 +0200446int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600447{
448 int i;
449
450 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500451 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala4ed65522008-02-27 21:51:47 -0600452 lmb->reserved.region[i].size - 1;
453 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
Patrick Delaunaye359a4a2021-05-07 14:50:30 +0200454 return (lmb->reserved.region[i].flags & flags) == flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600455 }
456 return 0;
457}
Mike Frysingera16028d2009-11-03 11:35:59 -0500458
Patrick Delaunaye359a4a2021-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 Hofstee2c34f3f2014-06-26 20:04:37 +0200464__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500465{
466 /* please define platform specific board_lmb_reserve() */
467}
Mike Frysingera16028d2009-11-03 11:35:59 -0500468
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200469__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500470{
471 /* please define platform specific arch_lmb_reserve() */
472}