blob: 008bcc7930d11c118a4dd03eaa1bfb7372f0cacc [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
17void lmb_dump_all(struct lmb *lmb)
18{
19#ifdef DEBUG
20 unsigned long i;
21
22 debug("lmb_dump_all:\n");
23 debug(" memory.cnt = 0x%lx\n", lmb->memory.cnt);
Becky Bruce391fd932008-06-09 20:37:18 -050024 debug(" memory.size = 0x%llx\n",
25 (unsigned long long)lmb->memory.size);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010026 for (i = 0; i < lmb->memory.cnt; i++) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +020027 debug(" memory.reg[0x%lx].base = 0x%llx\n", i,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010028 (unsigned long long)lmb->memory.region[i].base);
Becky Bruce391fd932008-06-09 20:37:18 -050029 debug(" .size = 0x%llx\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010030 (unsigned long long)lmb->memory.region[i].size);
Kumar Gala4ed65522008-02-27 21:51:47 -060031 }
32
Wolfgang Denk9b55a252008-07-11 01:16:00 +020033 debug("\n reserved.cnt = 0x%lx\n",
34 lmb->reserved.cnt);
35 debug(" reserved.size = 0x%llx\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010036 (unsigned long long)lmb->reserved.size);
37 for (i = 0; i < lmb->reserved.cnt; i++) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +020038 debug(" reserved.reg[0x%lx].base = 0x%llx\n", i,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010039 (unsigned long long)lmb->reserved.region[i].base);
Becky Bruce391fd932008-06-09 20:37:18 -050040 debug(" .size = 0x%llx\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010041 (unsigned long long)lmb->reserved.region[i].size);
Kumar Gala4ed65522008-02-27 21:51:47 -060042 }
43#endif /* DEBUG */
44}
45
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010046static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
47 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -060048{
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +010049 const phys_addr_t base1_end = base1 + size1 - 1;
50 const phys_addr_t base2_end = base2 + size2 - 1;
51
52 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala4ed65522008-02-27 21:51:47 -060053}
54
Becky Bruce391fd932008-06-09 20:37:18 -050055static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010056 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -060057{
58 if (base2 == base1 + size1)
59 return 1;
60 else if (base1 == base2 + size2)
61 return -1;
62
63 return 0;
64}
65
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010066static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
67 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -060068{
Becky Bruce391fd932008-06-09 20:37:18 -050069 phys_addr_t base1 = rgn->region[r1].base;
70 phys_size_t size1 = rgn->region[r1].size;
71 phys_addr_t base2 = rgn->region[r2].base;
72 phys_size_t size2 = rgn->region[r2].size;
Kumar Gala4ed65522008-02-27 21:51:47 -060073
74 return lmb_addrs_adjacent(base1, size1, base2, size2);
75}
76
77static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
78{
79 unsigned long i;
80
81 for (i = r; i < rgn->cnt - 1; i++) {
82 rgn->region[i].base = rgn->region[i + 1].base;
83 rgn->region[i].size = rgn->region[i + 1].size;
84 }
85 rgn->cnt--;
86}
87
88/* Assumption: base addr of region 1 < base addr of region 2 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010089static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
90 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -060091{
92 rgn->region[r1].size += rgn->region[r2].size;
93 lmb_remove_region(rgn, r2);
94}
95
96void lmb_init(struct lmb *lmb)
97{
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +010098 lmb->memory.cnt = 0;
Kumar Gala4ed65522008-02-27 21:51:47 -060099 lmb->memory.size = 0;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100100 lmb->reserved.cnt = 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600101 lmb->reserved.size = 0;
102}
103
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100104static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100105{
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100106 arch_lmb_reserve(lmb);
107 board_lmb_reserve(lmb);
108
109 if (IMAGE_ENABLE_OF_LIBFDT && fdt_blob)
110 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
111}
112
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100113/* Initialize the struct, add memory and call arch/board reserve functions */
114void lmb_init_and_reserve(struct lmb *lmb, bd_t *bd, void *fdt_blob)
115{
116#ifdef CONFIG_NR_DRAM_BANKS
117 int i;
118#endif
119
120 lmb_init(lmb);
121#ifdef CONFIG_NR_DRAM_BANKS
122 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
123 if (bd->bi_dram[i].size) {
124 lmb_add(lmb, bd->bi_dram[i].start,
125 bd->bi_dram[i].size);
126 }
127 }
128#else
129 if (bd->bi_memsize)
130 lmb_add(lmb, bd->bi_memstart, bd->bi_memsize);
131#endif
132 lmb_reserve_common(lmb, fdt_blob);
133}
134
135/* Initialize the struct, add memory and call arch/board reserve functions */
136void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
137 phys_size_t size, void *fdt_blob)
138{
139 lmb_init(lmb);
140 lmb_add(lmb, base, size);
141 lmb_reserve_common(lmb, fdt_blob);
142}
143
Kumar Gala4ed65522008-02-27 21:51:47 -0600144/* This routine called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500145static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600146{
147 unsigned long coalesced = 0;
148 long adjacent, i;
149
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100150 if (rgn->cnt == 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600151 rgn->region[0].base = base;
152 rgn->region[0].size = size;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100153 rgn->cnt = 1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600154 return 0;
155 }
156
157 /* First try and coalesce this LMB with another. */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100158 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500159 phys_addr_t rgnbase = rgn->region[i].base;
160 phys_size_t rgnsize = rgn->region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600161
162 if ((rgnbase == base) && (rgnsize == size))
163 /* Already have this region, so we're done */
164 return 0;
165
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100166 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
167 if (adjacent > 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600168 rgn->region[i].base -= size;
169 rgn->region[i].size += size;
170 coalesced++;
171 break;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100172 } else if (adjacent < 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600173 rgn->region[i].size += size;
174 coalesced++;
175 break;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100176 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
177 /* regions overlap */
178 return -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600179 }
180 }
181
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100182 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
183 lmb_coalesce_regions(rgn, i, i + 1);
Kumar Gala4ed65522008-02-27 21:51:47 -0600184 coalesced++;
185 }
186
187 if (coalesced)
188 return coalesced;
189 if (rgn->cnt >= MAX_LMB_REGIONS)
190 return -1;
191
192 /* Couldn't coalesce the LMB, so add it to the sorted table. */
193 for (i = rgn->cnt-1; i >= 0; i--) {
194 if (base < rgn->region[i].base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100195 rgn->region[i + 1].base = rgn->region[i].base;
196 rgn->region[i + 1].size = rgn->region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600197 } else {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100198 rgn->region[i + 1].base = base;
199 rgn->region[i + 1].size = size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600200 break;
201 }
202 }
203
204 if (base < rgn->region[0].base) {
205 rgn->region[0].base = base;
206 rgn->region[0].size = size;
207 }
208
209 rgn->cnt++;
210
211 return 0;
212}
213
214/* This routine may be called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500215long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600216{
217 struct lmb_region *_rgn = &(lmb->memory);
218
219 return lmb_add_region(_rgn, base, size);
220}
221
Andy Fleming98874ff2008-07-07 14:24:39 -0500222long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming63796c42008-06-16 13:58:54 -0500223{
224 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming98874ff2008-07-07 14:24:39 -0500225 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100226 phys_addr_t end = base + size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500227 int i;
228
229 rgnbegin = rgnend = 0; /* supress gcc warnings */
230
231 /* Find the region where (base, size) belongs to */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100232 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming63796c42008-06-16 13:58:54 -0500233 rgnbegin = rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100234 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500235
236 if ((rgnbegin <= base) && (end <= rgnend))
237 break;
238 }
239
240 /* Didn't find the region */
241 if (i == rgn->cnt)
242 return -1;
243
244 /* Check to see if we are removing entire region */
245 if ((rgnbegin == base) && (rgnend == end)) {
246 lmb_remove_region(rgn, i);
247 return 0;
248 }
249
250 /* Check to see if region is matching at the front */
251 if (rgnbegin == base) {
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100252 rgn->region[i].base = end + 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500253 rgn->region[i].size -= size;
254 return 0;
255 }
256
257 /* Check to see if the region is matching at the end */
258 if (rgnend == end) {
259 rgn->region[i].size -= size;
260 return 0;
261 }
262
263 /*
264 * We need to split the entry - adjust the current one to the
265 * beginging of the hole and add the region after hole.
266 */
267 rgn->region[i].size = base - rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100268 return lmb_add_region(rgn, end + 1, rgnend - end);
Andy Fleming63796c42008-06-16 13:58:54 -0500269}
270
Becky Bruce391fd932008-06-09 20:37:18 -0500271long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600272{
273 struct lmb_region *_rgn = &(lmb->reserved);
274
275 return lmb_add_region(_rgn, base, size);
276}
277
Jeroen Hofstee750a6ff2014-10-08 22:57:39 +0200278static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruce391fd932008-06-09 20:37:18 -0500279 phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600280{
281 unsigned long i;
282
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100283 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500284 phys_addr_t rgnbase = rgn->region[i].base;
285 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100286 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala4ed65522008-02-27 21:51:47 -0600287 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600288 }
289
290 return (i < rgn->cnt) ? i : -1;
291}
292
Becky Bruce391fd932008-06-09 20:37:18 -0500293phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala4ed65522008-02-27 21:51:47 -0600294{
295 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
296}
297
Becky Bruce391fd932008-06-09 20:37:18 -0500298phys_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 -0600299{
Becky Bruce391fd932008-06-09 20:37:18 -0500300 phys_addr_t alloc;
Kumar Gala4ed65522008-02-27 21:51:47 -0600301
302 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
303
304 if (alloc == 0)
305 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100306 (ulong)size, (ulong)max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600307
308 return alloc;
309}
310
Becky Bruce391fd932008-06-09 20:37:18 -0500311static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600312{
313 return addr & ~(size - 1);
314}
315
Becky Bruce391fd932008-06-09 20:37:18 -0500316phys_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 -0600317{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100318 long i, rgn;
Becky Bruce391fd932008-06-09 20:37:18 -0500319 phys_addr_t base = 0;
Andy Fleming7570a992008-06-16 13:58:55 -0500320 phys_addr_t res_base;
Kumar Gala4ed65522008-02-27 21:51:47 -0600321
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100322 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruce391fd932008-06-09 20:37:18 -0500323 phys_addr_t lmbbase = lmb->memory.region[i].base;
324 phys_size_t lmbsize = lmb->memory.region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600325
Andy Fleming7570a992008-06-16 13:58:55 -0500326 if (lmbsize < size)
327 continue;
Kumar Gala4ed65522008-02-27 21:51:47 -0600328 if (max_addr == LMB_ALLOC_ANYWHERE)
329 base = lmb_align_down(lmbbase + lmbsize - size, align);
330 else if (lmbbase < max_addr) {
Stephen Warrenad3fda52014-07-31 13:40:07 -0600331 base = lmbbase + lmbsize;
332 if (base < lmbbase)
333 base = -1;
334 base = min(base, max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600335 base = lmb_align_down(base - size, align);
336 } else
337 continue;
338
Andy Fleming7570a992008-06-16 13:58:55 -0500339 while (base && lmbbase <= base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100340 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
341 if (rgn < 0) {
Andy Fleming7570a992008-06-16 13:58:55 -0500342 /* This area isn't reserved, take it */
343 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100344 size) < 0)
Andy Fleming7570a992008-06-16 13:58:55 -0500345 return 0;
346 return base;
347 }
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100348 res_base = lmb->reserved.region[rgn].base;
Andy Fleming7570a992008-06-16 13:58:55 -0500349 if (res_base < size)
350 break;
351 base = lmb_align_down(res_base - size, align);
352 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600353 }
Andy Fleming7570a992008-06-16 13:58:55 -0500354 return 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600355}
356
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100357/*
358 * Try to allocate a specific address range: must be in defined memory but not
359 * reserved
360 */
361phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
362{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100363 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100364
365 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100366 rgn = lmb_overlaps_region(&lmb->memory, base, size);
367 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100368 /*
369 * Check if the requested end address is in the same memory
370 * region we found.
371 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100372 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
373 lmb->memory.region[rgn].size,
374 base + size - 1, 1)) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100375 /* ok, reserve the memory */
376 if (lmb_reserve(lmb, base, size) >= 0)
377 return base;
378 }
379 }
380 return 0;
381}
382
383/* Return number of bytes from a given address that are free */
Simon Goldschmidt65304aa2019-01-21 20:29:55 +0100384phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100385{
386 int i;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100387 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100388
389 /* check if the requested address is in the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100390 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
391 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100392 for (i = 0; i < lmb->reserved.cnt; i++) {
393 if (addr < lmb->reserved.region[i].base) {
394 /* first reserved range > requested address */
395 return lmb->reserved.region[i].base - addr;
396 }
397 if (lmb->reserved.region[i].base +
398 lmb->reserved.region[i].size > addr) {
399 /* requested addr is in this reserved range */
400 return 0;
401 }
402 }
403 /* if we come here: no reserved ranges above requested addr */
404 return lmb->memory.region[lmb->memory.cnt - 1].base +
405 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
406 }
407 return 0;
408}
409
Becky Bruce391fd932008-06-09 20:37:18 -0500410int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
Kumar Gala4ed65522008-02-27 21:51:47 -0600411{
412 int i;
413
414 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500415 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala4ed65522008-02-27 21:51:47 -0600416 lmb->reserved.region[i].size - 1;
417 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
418 return 1;
419 }
420 return 0;
421}
Mike Frysingera16028d2009-11-03 11:35:59 -0500422
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200423__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500424{
425 /* please define platform specific board_lmb_reserve() */
426}
Mike Frysingera16028d2009-11-03 11:35:59 -0500427
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200428__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500429{
430 /* please define platform specific arch_lmb_reserve() */
431}