blob: 6d3dcf4e09bce5cd2623f64d6972871b2531df80 [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>
10#include <lmb.h>
11
12#define LMB_ALLOC_ANYWHERE 0
13
14void lmb_dump_all(struct lmb *lmb)
15{
16#ifdef DEBUG
17 unsigned long i;
18
19 debug("lmb_dump_all:\n");
20 debug(" memory.cnt = 0x%lx\n", lmb->memory.cnt);
Becky Bruced26d67c2008-06-09 20:37:18 -050021 debug(" memory.size = 0x%llx\n",
22 (unsigned long long)lmb->memory.size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060023 for (i=0; i < lmb->memory.cnt ;i++) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +020024 debug(" memory.reg[0x%lx].base = 0x%llx\n", i,
25 (long long unsigned)lmb->memory.region[i].base);
Becky Bruced26d67c2008-06-09 20:37:18 -050026 debug(" .size = 0x%llx\n",
Wolfgang Denk12cec0a2008-07-11 01:16:00 +020027 (long long unsigned)lmb->memory.region[i].size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060028 }
29
Wolfgang Denk12cec0a2008-07-11 01:16:00 +020030 debug("\n reserved.cnt = 0x%lx\n",
31 lmb->reserved.cnt);
32 debug(" reserved.size = 0x%llx\n",
33 (long long unsigned)lmb->reserved.size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060034 for (i=0; i < lmb->reserved.cnt ;i++) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +020035 debug(" reserved.reg[0x%lx].base = 0x%llx\n", i,
36 (long long unsigned)lmb->reserved.region[i].base);
Becky Bruced26d67c2008-06-09 20:37:18 -050037 debug(" .size = 0x%llx\n",
Wolfgang Denk12cec0a2008-07-11 01:16:00 +020038 (long long unsigned)lmb->reserved.region[i].size);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060039 }
40#endif /* DEBUG */
41}
42
Becky Bruced26d67c2008-06-09 20:37:18 -050043static long lmb_addrs_overlap(phys_addr_t base1,
44 phys_size_t size1, phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060045{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010046 const phys_addr_t base1_end = base1 + size1 - 1;
47 const phys_addr_t base2_end = base2 + size2 - 1;
48
49 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala6d7bfa82008-02-27 21:51:47 -060050}
51
Becky Bruced26d67c2008-06-09 20:37:18 -050052static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
53 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060054{
55 if (base2 == base1 + size1)
56 return 1;
57 else if (base1 == base2 + size2)
58 return -1;
59
60 return 0;
61}
62
63static long lmb_regions_adjacent(struct lmb_region *rgn,
64 unsigned long r1, unsigned long r2)
65{
Becky Bruced26d67c2008-06-09 20:37:18 -050066 phys_addr_t base1 = rgn->region[r1].base;
67 phys_size_t size1 = rgn->region[r1].size;
68 phys_addr_t base2 = rgn->region[r2].base;
69 phys_size_t size2 = rgn->region[r2].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060070
71 return lmb_addrs_adjacent(base1, size1, base2, size2);
72}
73
74static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
75{
76 unsigned long i;
77
78 for (i = r; i < rgn->cnt - 1; i++) {
79 rgn->region[i].base = rgn->region[i + 1].base;
80 rgn->region[i].size = rgn->region[i + 1].size;
81 }
82 rgn->cnt--;
83}
84
85/* Assumption: base addr of region 1 < base addr of region 2 */
86static void lmb_coalesce_regions(struct lmb_region *rgn,
87 unsigned long r1, unsigned long r2)
88{
89 rgn->region[r1].size += rgn->region[r2].size;
90 lmb_remove_region(rgn, r2);
91}
92
93void lmb_init(struct lmb *lmb)
94{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010095 lmb->memory.cnt = 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060096 lmb->memory.size = 0;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010097 lmb->reserved.cnt = 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060098 lmb->reserved.size = 0;
99}
100
101/* This routine called with relocation disabled. */
Becky Bruced26d67c2008-06-09 20:37:18 -0500102static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600103{
104 unsigned long coalesced = 0;
105 long adjacent, i;
106
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100107 if (rgn->cnt == 0) {
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600108 rgn->region[0].base = base;
109 rgn->region[0].size = size;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100110 rgn->cnt = 1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600111 return 0;
112 }
113
114 /* First try and coalesce this LMB with another. */
115 for (i=0; i < rgn->cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500116 phys_addr_t rgnbase = rgn->region[i].base;
117 phys_size_t rgnsize = rgn->region[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600118
119 if ((rgnbase == base) && (rgnsize == size))
120 /* Already have this region, so we're done */
121 return 0;
122
123 adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize);
124 if ( adjacent > 0 ) {
125 rgn->region[i].base -= size;
126 rgn->region[i].size += size;
127 coalesced++;
128 break;
129 }
130 else if ( adjacent < 0 ) {
131 rgn->region[i].size += size;
132 coalesced++;
133 break;
134 }
135 }
136
137 if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) {
138 lmb_coalesce_regions(rgn, i, i+1);
139 coalesced++;
140 }
141
142 if (coalesced)
143 return coalesced;
144 if (rgn->cnt >= MAX_LMB_REGIONS)
145 return -1;
146
147 /* Couldn't coalesce the LMB, so add it to the sorted table. */
148 for (i = rgn->cnt-1; i >= 0; i--) {
149 if (base < rgn->region[i].base) {
150 rgn->region[i+1].base = rgn->region[i].base;
151 rgn->region[i+1].size = rgn->region[i].size;
152 } else {
153 rgn->region[i+1].base = base;
154 rgn->region[i+1].size = size;
155 break;
156 }
157 }
158
159 if (base < rgn->region[0].base) {
160 rgn->region[0].base = base;
161 rgn->region[0].size = size;
162 }
163
164 rgn->cnt++;
165
166 return 0;
167}
168
169/* This routine may be called with relocation disabled. */
Becky Bruced26d67c2008-06-09 20:37:18 -0500170long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600171{
172 struct lmb_region *_rgn = &(lmb->memory);
173
174 return lmb_add_region(_rgn, base, size);
175}
176
Andy Fleming09d2a712008-07-07 14:24:39 -0500177long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500178{
179 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming09d2a712008-07-07 14:24:39 -0500180 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100181 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500182 int i;
183
184 rgnbegin = rgnend = 0; /* supress gcc warnings */
185
186 /* Find the region where (base, size) belongs to */
187 for (i=0; i < rgn->cnt; i++) {
188 rgnbegin = rgn->region[i].base;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100189 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500190
191 if ((rgnbegin <= base) && (end <= rgnend))
192 break;
193 }
194
195 /* Didn't find the region */
196 if (i == rgn->cnt)
197 return -1;
198
199 /* Check to see if we are removing entire region */
200 if ((rgnbegin == base) && (rgnend == end)) {
201 lmb_remove_region(rgn, i);
202 return 0;
203 }
204
205 /* Check to see if region is matching at the front */
206 if (rgnbegin == base) {
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100207 rgn->region[i].base = end + 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500208 rgn->region[i].size -= size;
209 return 0;
210 }
211
212 /* Check to see if the region is matching at the end */
213 if (rgnend == end) {
214 rgn->region[i].size -= size;
215 return 0;
216 }
217
218 /*
219 * We need to split the entry - adjust the current one to the
220 * beginging of the hole and add the region after hole.
221 */
222 rgn->region[i].size = base - rgn->region[i].base;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100223 return lmb_add_region(rgn, end + 1, rgnend - end);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500224}
225
Becky Bruced26d67c2008-06-09 20:37:18 -0500226long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600227{
228 struct lmb_region *_rgn = &(lmb->reserved);
229
230 return lmb_add_region(_rgn, base, size);
231}
232
Jeroen Hofsteea0209722014-10-08 22:57:39 +0200233static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruced26d67c2008-06-09 20:37:18 -0500234 phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600235{
236 unsigned long i;
237
238 for (i=0; i < rgn->cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500239 phys_addr_t rgnbase = rgn->region[i].base;
240 phys_size_t rgnsize = rgn->region[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600241 if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) {
242 break;
243 }
244 }
245
246 return (i < rgn->cnt) ? i : -1;
247}
248
Becky Bruced26d67c2008-06-09 20:37:18 -0500249phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600250{
251 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
252}
253
Becky Bruced26d67c2008-06-09 20:37:18 -0500254phys_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 -0600255{
Becky Bruced26d67c2008-06-09 20:37:18 -0500256 phys_addr_t alloc;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600257
258 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
259
260 if (alloc == 0)
261 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
Wolfgang Denk12cec0a2008-07-11 01:16:00 +0200262 (ulong)size, (ulong)max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600263
264 return alloc;
265}
266
Becky Bruced26d67c2008-06-09 20:37:18 -0500267static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600268{
269 return addr & ~(size - 1);
270}
271
Becky Bruced26d67c2008-06-09 20:37:18 -0500272static phys_addr_t lmb_align_up(phys_addr_t addr, ulong size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600273{
274 return (addr + (size - 1)) & ~(size - 1);
275}
276
Becky Bruced26d67c2008-06-09 20:37:18 -0500277phys_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 -0600278{
279 long i, j;
Becky Bruced26d67c2008-06-09 20:37:18 -0500280 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500281 phys_addr_t res_base;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600282
283 for (i = lmb->memory.cnt-1; i >= 0; i--) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500284 phys_addr_t lmbbase = lmb->memory.region[i].base;
285 phys_size_t lmbsize = lmb->memory.region[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600286
Andy Fleming78bd5a72008-06-16 13:58:55 -0500287 if (lmbsize < size)
288 continue;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600289 if (max_addr == LMB_ALLOC_ANYWHERE)
290 base = lmb_align_down(lmbbase + lmbsize - size, align);
291 else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600292 base = lmbbase + lmbsize;
293 if (base < lmbbase)
294 base = -1;
295 base = min(base, max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600296 base = lmb_align_down(base - size, align);
297 } else
298 continue;
299
Andy Fleming78bd5a72008-06-16 13:58:55 -0500300 while (base && lmbbase <= base) {
301 j = lmb_overlaps_region(&lmb->reserved, base, size);
302 if (j < 0) {
303 /* This area isn't reserved, take it */
304 if (lmb_add_region(&lmb->reserved, base,
305 lmb_align_up(size,
306 align)) < 0)
307 return 0;
308 return base;
309 }
310 res_base = lmb->reserved.region[j].base;
311 if (res_base < size)
312 break;
313 base = lmb_align_down(res_base - size, align);
314 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600315 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500316 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600317}
318
Becky Bruced26d67c2008-06-09 20:37:18 -0500319int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600320{
321 int i;
322
323 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500324 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600325 lmb->reserved.region[i].size - 1;
326 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
327 return 1;
328 }
329 return 0;
330}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500331
Jeroen Hofstee0461dda2014-06-26 20:04:37 +0200332__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera0dadf82009-11-03 11:35:59 -0500333{
334 /* please define platform specific board_lmb_reserve() */
335}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500336
Jeroen Hofstee0461dda2014-06-26 20:04:37 +0200337__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera0dadf82009-11-03 11:35:59 -0500338{
339 /* please define platform specific arch_lmb_reserve() */
340}