blob: 2f0e495edf280bf1cde57ded13cc0e373d3102c8 [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 Glass336d4612020-02-03 07:36:16 -070012#include <malloc.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060013
14#define LMB_ALLOC_ANYWHERE 0
15
16void lmb_dump_all(struct lmb *lmb)
17{
18#ifdef DEBUG
19 unsigned long i;
20
21 debug("lmb_dump_all:\n");
22 debug(" memory.cnt = 0x%lx\n", lmb->memory.cnt);
Becky Bruce391fd932008-06-09 20:37:18 -050023 debug(" memory.size = 0x%llx\n",
24 (unsigned long long)lmb->memory.size);
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010025 for (i = 0; i < lmb->memory.cnt; i++) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +020026 debug(" memory.reg[0x%lx].base = 0x%llx\n", i,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010027 (unsigned long long)lmb->memory.region[i].base);
Becky Bruce391fd932008-06-09 20:37:18 -050028 debug(" .size = 0x%llx\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010029 (unsigned long long)lmb->memory.region[i].size);
Kumar Gala4ed65522008-02-27 21:51:47 -060030 }
31
Wolfgang Denk9b55a252008-07-11 01:16:00 +020032 debug("\n reserved.cnt = 0x%lx\n",
33 lmb->reserved.cnt);
34 debug(" reserved.size = 0x%llx\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010035 (unsigned long long)lmb->reserved.size);
36 for (i = 0; i < lmb->reserved.cnt; i++) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +020037 debug(" reserved.reg[0x%lx].base = 0x%llx\n", i,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010038 (unsigned long long)lmb->reserved.region[i].base);
Becky Bruce391fd932008-06-09 20:37:18 -050039 debug(" .size = 0x%llx\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010040 (unsigned long long)lmb->reserved.region[i].size);
Kumar Gala4ed65522008-02-27 21:51:47 -060041 }
42#endif /* DEBUG */
43}
44
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010045static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
46 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -060047{
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +010048 const phys_addr_t base1_end = base1 + size1 - 1;
49 const phys_addr_t base2_end = base2 + size2 - 1;
50
51 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala4ed65522008-02-27 21:51:47 -060052}
53
Becky Bruce391fd932008-06-09 20:37:18 -050054static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010055 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-02-27 21:51:47 -060056{
57 if (base2 == base1 + size1)
58 return 1;
59 else if (base1 == base2 + size2)
60 return -1;
61
62 return 0;
63}
64
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010065static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
66 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -060067{
Becky Bruce391fd932008-06-09 20:37:18 -050068 phys_addr_t base1 = rgn->region[r1].base;
69 phys_size_t size1 = rgn->region[r1].size;
70 phys_addr_t base2 = rgn->region[r2].base;
71 phys_size_t size2 = rgn->region[r2].size;
Kumar Gala4ed65522008-02-27 21:51:47 -060072
73 return lmb_addrs_adjacent(base1, size1, base2, size2);
74}
75
76static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
77{
78 unsigned long i;
79
80 for (i = r; i < rgn->cnt - 1; i++) {
81 rgn->region[i].base = rgn->region[i + 1].base;
82 rgn->region[i].size = rgn->region[i + 1].size;
83 }
84 rgn->cnt--;
85}
86
87/* Assumption: base addr of region 1 < base addr of region 2 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010088static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
89 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -060090{
91 rgn->region[r1].size += rgn->region[r2].size;
92 lmb_remove_region(rgn, r2);
93}
94
95void lmb_init(struct lmb *lmb)
96{
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +010097 lmb->memory.cnt = 0;
Kumar Gala4ed65522008-02-27 21:51:47 -060098 lmb->memory.size = 0;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +010099 lmb->reserved.cnt = 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600100 lmb->reserved.size = 0;
101}
102
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100103static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100104{
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100105 arch_lmb_reserve(lmb);
106 board_lmb_reserve(lmb);
107
108 if (IMAGE_ENABLE_OF_LIBFDT && fdt_blob)
109 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
110}
111
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100112/* Initialize the struct, add memory and call arch/board reserve functions */
113void lmb_init_and_reserve(struct lmb *lmb, bd_t *bd, void *fdt_blob)
114{
115#ifdef CONFIG_NR_DRAM_BANKS
116 int i;
117#endif
118
119 lmb_init(lmb);
120#ifdef CONFIG_NR_DRAM_BANKS
121 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
122 if (bd->bi_dram[i].size) {
123 lmb_add(lmb, bd->bi_dram[i].start,
124 bd->bi_dram[i].size);
125 }
126 }
127#else
128 if (bd->bi_memsize)
129 lmb_add(lmb, bd->bi_memstart, bd->bi_memsize);
130#endif
131 lmb_reserve_common(lmb, fdt_blob);
132}
133
134/* Initialize the struct, add memory and call arch/board reserve functions */
135void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
136 phys_size_t size, void *fdt_blob)
137{
138 lmb_init(lmb);
139 lmb_add(lmb, base, size);
140 lmb_reserve_common(lmb, fdt_blob);
141}
142
Kumar Gala4ed65522008-02-27 21:51:47 -0600143/* This routine called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500144static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600145{
146 unsigned long coalesced = 0;
147 long adjacent, i;
148
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100149 if (rgn->cnt == 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600150 rgn->region[0].base = base;
151 rgn->region[0].size = size;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100152 rgn->cnt = 1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600153 return 0;
154 }
155
156 /* First try and coalesce this LMB with another. */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100157 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500158 phys_addr_t rgnbase = rgn->region[i].base;
159 phys_size_t rgnsize = rgn->region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600160
161 if ((rgnbase == base) && (rgnsize == size))
162 /* Already have this region, so we're done */
163 return 0;
164
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100165 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
166 if (adjacent > 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600167 rgn->region[i].base -= size;
168 rgn->region[i].size += size;
169 coalesced++;
170 break;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100171 } else if (adjacent < 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600172 rgn->region[i].size += size;
173 coalesced++;
174 break;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100175 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
176 /* regions overlap */
177 return -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600178 }
179 }
180
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100181 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
182 lmb_coalesce_regions(rgn, i, i + 1);
Kumar Gala4ed65522008-02-27 21:51:47 -0600183 coalesced++;
184 }
185
186 if (coalesced)
187 return coalesced;
188 if (rgn->cnt >= MAX_LMB_REGIONS)
189 return -1;
190
191 /* Couldn't coalesce the LMB, so add it to the sorted table. */
192 for (i = rgn->cnt-1; i >= 0; i--) {
193 if (base < rgn->region[i].base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100194 rgn->region[i + 1].base = rgn->region[i].base;
195 rgn->region[i + 1].size = rgn->region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600196 } else {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100197 rgn->region[i + 1].base = base;
198 rgn->region[i + 1].size = size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600199 break;
200 }
201 }
202
203 if (base < rgn->region[0].base) {
204 rgn->region[0].base = base;
205 rgn->region[0].size = size;
206 }
207
208 rgn->cnt++;
209
210 return 0;
211}
212
213/* This routine may be called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500214long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600215{
216 struct lmb_region *_rgn = &(lmb->memory);
217
218 return lmb_add_region(_rgn, base, size);
219}
220
Andy Fleming98874ff2008-07-07 14:24:39 -0500221long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming63796c42008-06-16 13:58:54 -0500222{
223 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming98874ff2008-07-07 14:24:39 -0500224 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100225 phys_addr_t end = base + size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500226 int i;
227
228 rgnbegin = rgnend = 0; /* supress gcc warnings */
229
230 /* Find the region where (base, size) belongs to */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100231 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming63796c42008-06-16 13:58:54 -0500232 rgnbegin = rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100233 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500234
235 if ((rgnbegin <= base) && (end <= rgnend))
236 break;
237 }
238
239 /* Didn't find the region */
240 if (i == rgn->cnt)
241 return -1;
242
243 /* Check to see if we are removing entire region */
244 if ((rgnbegin == base) && (rgnend == end)) {
245 lmb_remove_region(rgn, i);
246 return 0;
247 }
248
249 /* Check to see if region is matching at the front */
250 if (rgnbegin == base) {
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100251 rgn->region[i].base = end + 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500252 rgn->region[i].size -= size;
253 return 0;
254 }
255
256 /* Check to see if the region is matching at the end */
257 if (rgnend == end) {
258 rgn->region[i].size -= size;
259 return 0;
260 }
261
262 /*
263 * We need to split the entry - adjust the current one to the
264 * beginging of the hole and add the region after hole.
265 */
266 rgn->region[i].size = base - rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100267 return lmb_add_region(rgn, end + 1, rgnend - end);
Andy Fleming63796c42008-06-16 13:58:54 -0500268}
269
Becky Bruce391fd932008-06-09 20:37:18 -0500270long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600271{
272 struct lmb_region *_rgn = &(lmb->reserved);
273
274 return lmb_add_region(_rgn, base, size);
275}
276
Jeroen Hofstee750a6ff2014-10-08 22:57:39 +0200277static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruce391fd932008-06-09 20:37:18 -0500278 phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600279{
280 unsigned long i;
281
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100282 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500283 phys_addr_t rgnbase = rgn->region[i].base;
284 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100285 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala4ed65522008-02-27 21:51:47 -0600286 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600287 }
288
289 return (i < rgn->cnt) ? i : -1;
290}
291
Becky Bruce391fd932008-06-09 20:37:18 -0500292phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala4ed65522008-02-27 21:51:47 -0600293{
294 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
295}
296
Becky Bruce391fd932008-06-09 20:37:18 -0500297phys_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 -0600298{
Becky Bruce391fd932008-06-09 20:37:18 -0500299 phys_addr_t alloc;
Kumar Gala4ed65522008-02-27 21:51:47 -0600300
301 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
302
303 if (alloc == 0)
304 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100305 (ulong)size, (ulong)max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600306
307 return alloc;
308}
309
Becky Bruce391fd932008-06-09 20:37:18 -0500310static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600311{
312 return addr & ~(size - 1);
313}
314
Becky Bruce391fd932008-06-09 20:37:18 -0500315phys_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 -0600316{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100317 long i, rgn;
Becky Bruce391fd932008-06-09 20:37:18 -0500318 phys_addr_t base = 0;
Andy Fleming7570a992008-06-16 13:58:55 -0500319 phys_addr_t res_base;
Kumar Gala4ed65522008-02-27 21:51:47 -0600320
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100321 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruce391fd932008-06-09 20:37:18 -0500322 phys_addr_t lmbbase = lmb->memory.region[i].base;
323 phys_size_t lmbsize = lmb->memory.region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600324
Andy Fleming7570a992008-06-16 13:58:55 -0500325 if (lmbsize < size)
326 continue;
Kumar Gala4ed65522008-02-27 21:51:47 -0600327 if (max_addr == LMB_ALLOC_ANYWHERE)
328 base = lmb_align_down(lmbbase + lmbsize - size, align);
329 else if (lmbbase < max_addr) {
Stephen Warrenad3fda52014-07-31 13:40:07 -0600330 base = lmbbase + lmbsize;
331 if (base < lmbbase)
332 base = -1;
333 base = min(base, max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600334 base = lmb_align_down(base - size, align);
335 } else
336 continue;
337
Andy Fleming7570a992008-06-16 13:58:55 -0500338 while (base && lmbbase <= base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100339 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
340 if (rgn < 0) {
Andy Fleming7570a992008-06-16 13:58:55 -0500341 /* This area isn't reserved, take it */
342 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100343 size) < 0)
Andy Fleming7570a992008-06-16 13:58:55 -0500344 return 0;
345 return base;
346 }
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100347 res_base = lmb->reserved.region[rgn].base;
Andy Fleming7570a992008-06-16 13:58:55 -0500348 if (res_base < size)
349 break;
350 base = lmb_align_down(res_base - size, align);
351 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600352 }
Andy Fleming7570a992008-06-16 13:58:55 -0500353 return 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600354}
355
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100356/*
357 * Try to allocate a specific address range: must be in defined memory but not
358 * reserved
359 */
360phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
361{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100362 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100363
364 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100365 rgn = lmb_overlaps_region(&lmb->memory, base, size);
366 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100367 /*
368 * Check if the requested end address is in the same memory
369 * region we found.
370 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100371 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
372 lmb->memory.region[rgn].size,
373 base + size - 1, 1)) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100374 /* ok, reserve the memory */
375 if (lmb_reserve(lmb, base, size) >= 0)
376 return base;
377 }
378 }
379 return 0;
380}
381
382/* Return number of bytes from a given address that are free */
Simon Goldschmidt65304aa2019-01-21 20:29:55 +0100383phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100384{
385 int i;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100386 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100387
388 /* check if the requested address is in the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100389 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
390 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100391 for (i = 0; i < lmb->reserved.cnt; i++) {
392 if (addr < lmb->reserved.region[i].base) {
393 /* first reserved range > requested address */
394 return lmb->reserved.region[i].base - addr;
395 }
396 if (lmb->reserved.region[i].base +
397 lmb->reserved.region[i].size > addr) {
398 /* requested addr is in this reserved range */
399 return 0;
400 }
401 }
402 /* if we come here: no reserved ranges above requested addr */
403 return lmb->memory.region[lmb->memory.cnt - 1].base +
404 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
405 }
406 return 0;
407}
408
Becky Bruce391fd932008-06-09 20:37:18 -0500409int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
Kumar Gala4ed65522008-02-27 21:51:47 -0600410{
411 int i;
412
413 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500414 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala4ed65522008-02-27 21:51:47 -0600415 lmb->reserved.region[i].size - 1;
416 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
417 return 1;
418 }
419 return 0;
420}
Mike Frysingera16028d2009-11-03 11:35:59 -0500421
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200422__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500423{
424 /* please define platform specific board_lmb_reserve() */
425}
Mike Frysingera16028d2009-11-03 11:35:59 -0500426
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200427__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500428{
429 /* please define platform specific arch_lmb_reserve() */
430}