blob: 7aff2c248fe2942841d764833f925182aa196e0f [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>
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 Bruce391fd932008-06-09 20:37:18 -050021 debug(" memory.size = 0x%llx\n",
22 (unsigned long long)lmb->memory.size);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010023 for (i = 0; i < lmb->memory.cnt; i++) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +020024 debug(" memory.reg[0x%lx].base = 0x%llx\n", i,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010025 (unsigned long long)lmb->memory.region[i].base);
Becky Bruce391fd932008-06-09 20:37:18 -050026 debug(" .size = 0x%llx\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010027 (unsigned long long)lmb->memory.region[i].size);
Kumar Gala4ed65522008-02-27 21:51:47 -060028 }
29
Wolfgang Denk9b55a252008-07-11 01:16:00 +020030 debug("\n reserved.cnt = 0x%lx\n",
31 lmb->reserved.cnt);
32 debug(" reserved.size = 0x%llx\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010033 (unsigned long long)lmb->reserved.size);
34 for (i = 0; i < lmb->reserved.cnt; i++) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +020035 debug(" reserved.reg[0x%lx].base = 0x%llx\n", i,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010036 (unsigned long long)lmb->reserved.region[i].base);
Becky Bruce391fd932008-06-09 20:37:18 -050037 debug(" .size = 0x%llx\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010038 (unsigned long long)lmb->reserved.region[i].size);
Kumar Gala4ed65522008-02-27 21:51:47 -060039 }
40#endif /* DEBUG */
41}
42
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010043static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
44 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -060045{
Simon Goldschmidtd67f33c2019-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 Gala4ed65522008-02-27 21:51:47 -060050}
51
Becky Bruce391fd932008-06-09 20:37:18 -050052static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010053 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-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
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010063static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
64 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -060065{
Becky Bruce391fd932008-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 Gala4ed65522008-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 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010086static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
87 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -060088{
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 Goldschmidtd67f33c2019-01-14 22:38:15 +010095 lmb->memory.cnt = 0;
Kumar Gala4ed65522008-02-27 21:51:47 -060096 lmb->memory.size = 0;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +010097 lmb->reserved.cnt = 0;
Kumar Gala4ed65522008-02-27 21:51:47 -060098 lmb->reserved.size = 0;
99}
100
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100101/* Initialize the struct, add memory and call arch/board reserve functions */
102void lmb_init_and_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size,
103 void *fdt_blob)
104{
105 lmb_init(lmb);
106 lmb_add(lmb, base, size);
107 arch_lmb_reserve(lmb);
108 board_lmb_reserve(lmb);
109
110 if (IMAGE_ENABLE_OF_LIBFDT && fdt_blob)
111 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
112}
113
Kumar Gala4ed65522008-02-27 21:51:47 -0600114/* This routine called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500115static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600116{
117 unsigned long coalesced = 0;
118 long adjacent, i;
119
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100120 if (rgn->cnt == 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600121 rgn->region[0].base = base;
122 rgn->region[0].size = size;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100123 rgn->cnt = 1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600124 return 0;
125 }
126
127 /* First try and coalesce this LMB with another. */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100128 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500129 phys_addr_t rgnbase = rgn->region[i].base;
130 phys_size_t rgnsize = rgn->region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600131
132 if ((rgnbase == base) && (rgnsize == size))
133 /* Already have this region, so we're done */
134 return 0;
135
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100136 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
137 if (adjacent > 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600138 rgn->region[i].base -= size;
139 rgn->region[i].size += size;
140 coalesced++;
141 break;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100142 } else if (adjacent < 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600143 rgn->region[i].size += size;
144 coalesced++;
145 break;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100146 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
147 /* regions overlap */
148 return -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600149 }
150 }
151
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100152 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
153 lmb_coalesce_regions(rgn, i, i + 1);
Kumar Gala4ed65522008-02-27 21:51:47 -0600154 coalesced++;
155 }
156
157 if (coalesced)
158 return coalesced;
159 if (rgn->cnt >= MAX_LMB_REGIONS)
160 return -1;
161
162 /* Couldn't coalesce the LMB, so add it to the sorted table. */
163 for (i = rgn->cnt-1; i >= 0; i--) {
164 if (base < rgn->region[i].base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100165 rgn->region[i + 1].base = rgn->region[i].base;
166 rgn->region[i + 1].size = rgn->region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600167 } else {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100168 rgn->region[i + 1].base = base;
169 rgn->region[i + 1].size = size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600170 break;
171 }
172 }
173
174 if (base < rgn->region[0].base) {
175 rgn->region[0].base = base;
176 rgn->region[0].size = size;
177 }
178
179 rgn->cnt++;
180
181 return 0;
182}
183
184/* This routine may be called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500185long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600186{
187 struct lmb_region *_rgn = &(lmb->memory);
188
189 return lmb_add_region(_rgn, base, size);
190}
191
Andy Fleming98874ff2008-07-07 14:24:39 -0500192long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming63796c42008-06-16 13:58:54 -0500193{
194 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming98874ff2008-07-07 14:24:39 -0500195 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100196 phys_addr_t end = base + size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500197 int i;
198
199 rgnbegin = rgnend = 0; /* supress gcc warnings */
200
201 /* Find the region where (base, size) belongs to */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100202 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming63796c42008-06-16 13:58:54 -0500203 rgnbegin = rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100204 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500205
206 if ((rgnbegin <= base) && (end <= rgnend))
207 break;
208 }
209
210 /* Didn't find the region */
211 if (i == rgn->cnt)
212 return -1;
213
214 /* Check to see if we are removing entire region */
215 if ((rgnbegin == base) && (rgnend == end)) {
216 lmb_remove_region(rgn, i);
217 return 0;
218 }
219
220 /* Check to see if region is matching at the front */
221 if (rgnbegin == base) {
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100222 rgn->region[i].base = end + 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500223 rgn->region[i].size -= size;
224 return 0;
225 }
226
227 /* Check to see if the region is matching at the end */
228 if (rgnend == end) {
229 rgn->region[i].size -= size;
230 return 0;
231 }
232
233 /*
234 * We need to split the entry - adjust the current one to the
235 * beginging of the hole and add the region after hole.
236 */
237 rgn->region[i].size = base - rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100238 return lmb_add_region(rgn, end + 1, rgnend - end);
Andy Fleming63796c42008-06-16 13:58:54 -0500239}
240
Becky Bruce391fd932008-06-09 20:37:18 -0500241long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600242{
243 struct lmb_region *_rgn = &(lmb->reserved);
244
245 return lmb_add_region(_rgn, base, size);
246}
247
Jeroen Hofstee750a6ff2014-10-08 22:57:39 +0200248static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruce391fd932008-06-09 20:37:18 -0500249 phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600250{
251 unsigned long i;
252
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100253 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500254 phys_addr_t rgnbase = rgn->region[i].base;
255 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100256 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala4ed65522008-02-27 21:51:47 -0600257 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600258 }
259
260 return (i < rgn->cnt) ? i : -1;
261}
262
Becky Bruce391fd932008-06-09 20:37:18 -0500263phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala4ed65522008-02-27 21:51:47 -0600264{
265 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
266}
267
Becky Bruce391fd932008-06-09 20:37:18 -0500268phys_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 -0600269{
Becky Bruce391fd932008-06-09 20:37:18 -0500270 phys_addr_t alloc;
Kumar Gala4ed65522008-02-27 21:51:47 -0600271
272 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
273
274 if (alloc == 0)
275 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100276 (ulong)size, (ulong)max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600277
278 return alloc;
279}
280
Becky Bruce391fd932008-06-09 20:37:18 -0500281static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600282{
283 return addr & ~(size - 1);
284}
285
Becky Bruce391fd932008-06-09 20:37:18 -0500286phys_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 -0600287{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100288 long i, rgn;
Becky Bruce391fd932008-06-09 20:37:18 -0500289 phys_addr_t base = 0;
Andy Fleming7570a992008-06-16 13:58:55 -0500290 phys_addr_t res_base;
Kumar Gala4ed65522008-02-27 21:51:47 -0600291
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100292 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruce391fd932008-06-09 20:37:18 -0500293 phys_addr_t lmbbase = lmb->memory.region[i].base;
294 phys_size_t lmbsize = lmb->memory.region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600295
Andy Fleming7570a992008-06-16 13:58:55 -0500296 if (lmbsize < size)
297 continue;
Kumar Gala4ed65522008-02-27 21:51:47 -0600298 if (max_addr == LMB_ALLOC_ANYWHERE)
299 base = lmb_align_down(lmbbase + lmbsize - size, align);
300 else if (lmbbase < max_addr) {
Stephen Warrenad3fda52014-07-31 13:40:07 -0600301 base = lmbbase + lmbsize;
302 if (base < lmbbase)
303 base = -1;
304 base = min(base, max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600305 base = lmb_align_down(base - size, align);
306 } else
307 continue;
308
Andy Fleming7570a992008-06-16 13:58:55 -0500309 while (base && lmbbase <= base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100310 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
311 if (rgn < 0) {
Andy Fleming7570a992008-06-16 13:58:55 -0500312 /* This area isn't reserved, take it */
313 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100314 size) < 0)
Andy Fleming7570a992008-06-16 13:58:55 -0500315 return 0;
316 return base;
317 }
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100318 res_base = lmb->reserved.region[rgn].base;
Andy Fleming7570a992008-06-16 13:58:55 -0500319 if (res_base < size)
320 break;
321 base = lmb_align_down(res_base - size, align);
322 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600323 }
Andy Fleming7570a992008-06-16 13:58:55 -0500324 return 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600325}
326
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100327/*
328 * Try to allocate a specific address range: must be in defined memory but not
329 * reserved
330 */
331phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
332{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100333 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100334
335 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100336 rgn = lmb_overlaps_region(&lmb->memory, base, size);
337 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100338 /*
339 * Check if the requested end address is in the same memory
340 * region we found.
341 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100342 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
343 lmb->memory.region[rgn].size,
344 base + size - 1, 1)) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100345 /* ok, reserve the memory */
346 if (lmb_reserve(lmb, base, size) >= 0)
347 return base;
348 }
349 }
350 return 0;
351}
352
353/* Return number of bytes from a given address that are free */
Simon Goldschmidt65304aa2019-01-21 20:29:55 +0100354phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100355{
356 int i;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100357 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100358
359 /* check if the requested address is in the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100360 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
361 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100362 for (i = 0; i < lmb->reserved.cnt; i++) {
363 if (addr < lmb->reserved.region[i].base) {
364 /* first reserved range > requested address */
365 return lmb->reserved.region[i].base - addr;
366 }
367 if (lmb->reserved.region[i].base +
368 lmb->reserved.region[i].size > addr) {
369 /* requested addr is in this reserved range */
370 return 0;
371 }
372 }
373 /* if we come here: no reserved ranges above requested addr */
374 return lmb->memory.region[lmb->memory.cnt - 1].base +
375 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
376 }
377 return 0;
378}
379
Becky Bruce391fd932008-06-09 20:37:18 -0500380int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
Kumar Gala4ed65522008-02-27 21:51:47 -0600381{
382 int i;
383
384 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500385 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala4ed65522008-02-27 21:51:47 -0600386 lmb->reserved.region[i].size - 1;
387 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
388 return 1;
389 }
390 return 0;
391}
Mike Frysingera16028d2009-11-03 11:35:59 -0500392
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200393__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500394{
395 /* please define platform specific board_lmb_reserve() */
396}
Mike Frysingera16028d2009-11-03 11:35:59 -0500397
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200398__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500399{
400 /* please define platform specific arch_lmb_reserve() */
401}