blob: 9fbc56619b42732f510f588a9eda14a0933b6bdc [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
Tero Kristo9996cea2020-07-20 11:10:45 +030017void lmb_dump_all_force(struct lmb *lmb)
18{
19 unsigned long i;
20
21 printf("lmb_dump_all:\n");
22 printf(" memory.cnt = 0x%lx\n", lmb->memory.cnt);
Tero Kristo9996cea2020-07-20 11:10:45 +030023 for (i = 0; i < lmb->memory.cnt; i++) {
24 printf(" memory.reg[0x%lx].base = 0x%llx\n", i,
25 (unsigned long long)lmb->memory.region[i].base);
26 printf(" .size = 0x%llx\n",
27 (unsigned long long)lmb->memory.region[i].size);
28 }
29
30 printf("\n reserved.cnt = 0x%lx\n", lmb->reserved.cnt);
Tero Kristo9996cea2020-07-20 11:10:45 +030031 for (i = 0; i < lmb->reserved.cnt; i++) {
32 printf(" reserved.reg[0x%lx].base = 0x%llx\n", i,
33 (unsigned long long)lmb->reserved.region[i].base);
34 printf(" .size = 0x%llx\n",
35 (unsigned long long)lmb->reserved.region[i].size);
36 }
37}
38
Kumar Gala4ed65522008-02-27 21:51:47 -060039void lmb_dump_all(struct lmb *lmb)
40{
41#ifdef DEBUG
Tero Kristo9996cea2020-07-20 11:10:45 +030042 lmb_dump_all_force(lmb);
43#endif
Kumar Gala4ed65522008-02-27 21:51:47 -060044}
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{
Patrick Delaunay4fa01502021-03-10 10:16:28 +010098 lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
99 lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
Patrick Delaunay00fd8da2021-03-10 10:16:27 +0100100
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100101 lmb->memory.cnt = 0;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100102 lmb->reserved.cnt = 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600103}
104
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100105static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100106{
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100107 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
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100114/* Initialize the struct, add memory and call arch/board reserve functions */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900115void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100116{
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100117 int i;
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100118
119 lmb_init(lmb);
Stefan Roesedfaf6a52020-08-12 11:55:46 +0200120
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100121 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 }
Stefan Roesedfaf6a52020-08-12 11:55:46 +0200127
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100128 lmb_reserve_common(lmb, fdt_blob);
129}
130
131/* Initialize the struct, add memory and call arch/board reserve functions */
132void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
133 phys_size_t size, void *fdt_blob)
134{
135 lmb_init(lmb);
136 lmb_add(lmb, base, size);
137 lmb_reserve_common(lmb, fdt_blob);
138}
139
Kumar Gala4ed65522008-02-27 21:51:47 -0600140/* This routine called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500141static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600142{
143 unsigned long coalesced = 0;
144 long adjacent, i;
145
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100146 if (rgn->cnt == 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600147 rgn->region[0].base = base;
148 rgn->region[0].size = size;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100149 rgn->cnt = 1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600150 return 0;
151 }
152
153 /* First try and coalesce this LMB with another. */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100154 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500155 phys_addr_t rgnbase = rgn->region[i].base;
156 phys_size_t rgnsize = rgn->region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600157
158 if ((rgnbase == base) && (rgnsize == size))
159 /* Already have this region, so we're done */
160 return 0;
161
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100162 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
163 if (adjacent > 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600164 rgn->region[i].base -= size;
165 rgn->region[i].size += size;
166 coalesced++;
167 break;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100168 } else if (adjacent < 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600169 rgn->region[i].size += size;
170 coalesced++;
171 break;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100172 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
173 /* regions overlap */
174 return -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600175 }
176 }
177
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100178 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
179 lmb_coalesce_regions(rgn, i, i + 1);
Kumar Gala4ed65522008-02-27 21:51:47 -0600180 coalesced++;
181 }
182
183 if (coalesced)
184 return coalesced;
Patrick Delaunay00fd8da2021-03-10 10:16:27 +0100185 if (rgn->cnt >= rgn->max)
Kumar Gala4ed65522008-02-27 21:51:47 -0600186 return -1;
187
188 /* Couldn't coalesce the LMB, so add it to the sorted table. */
189 for (i = rgn->cnt-1; i >= 0; i--) {
190 if (base < rgn->region[i].base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100191 rgn->region[i + 1].base = rgn->region[i].base;
192 rgn->region[i + 1].size = rgn->region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600193 } else {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100194 rgn->region[i + 1].base = base;
195 rgn->region[i + 1].size = size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600196 break;
197 }
198 }
199
200 if (base < rgn->region[0].base) {
201 rgn->region[0].base = base;
202 rgn->region[0].size = size;
203 }
204
205 rgn->cnt++;
206
207 return 0;
208}
209
210/* This routine may be called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500211long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600212{
213 struct lmb_region *_rgn = &(lmb->memory);
214
215 return lmb_add_region(_rgn, base, size);
216}
217
Andy Fleming98874ff2008-07-07 14:24:39 -0500218long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming63796c42008-06-16 13:58:54 -0500219{
220 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming98874ff2008-07-07 14:24:39 -0500221 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100222 phys_addr_t end = base + size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500223 int i;
224
225 rgnbegin = rgnend = 0; /* supress gcc warnings */
226
227 /* Find the region where (base, size) belongs to */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100228 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming63796c42008-06-16 13:58:54 -0500229 rgnbegin = rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100230 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500231
232 if ((rgnbegin <= base) && (end <= rgnend))
233 break;
234 }
235
236 /* Didn't find the region */
237 if (i == rgn->cnt)
238 return -1;
239
240 /* Check to see if we are removing entire region */
241 if ((rgnbegin == base) && (rgnend == end)) {
242 lmb_remove_region(rgn, i);
243 return 0;
244 }
245
246 /* Check to see if region is matching at the front */
247 if (rgnbegin == base) {
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100248 rgn->region[i].base = end + 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500249 rgn->region[i].size -= size;
250 return 0;
251 }
252
253 /* Check to see if the region is matching at the end */
254 if (rgnend == end) {
255 rgn->region[i].size -= size;
256 return 0;
257 }
258
259 /*
260 * We need to split the entry - adjust the current one to the
261 * beginging of the hole and add the region after hole.
262 */
263 rgn->region[i].size = base - rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100264 return lmb_add_region(rgn, end + 1, rgnend - end);
Andy Fleming63796c42008-06-16 13:58:54 -0500265}
266
Becky Bruce391fd932008-06-09 20:37:18 -0500267long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600268{
269 struct lmb_region *_rgn = &(lmb->reserved);
270
271 return lmb_add_region(_rgn, base, size);
272}
273
Jeroen Hofstee750a6ff2014-10-08 22:57:39 +0200274static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruce391fd932008-06-09 20:37:18 -0500275 phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600276{
277 unsigned long i;
278
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100279 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500280 phys_addr_t rgnbase = rgn->region[i].base;
281 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100282 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala4ed65522008-02-27 21:51:47 -0600283 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600284 }
285
286 return (i < rgn->cnt) ? i : -1;
287}
288
Becky Bruce391fd932008-06-09 20:37:18 -0500289phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala4ed65522008-02-27 21:51:47 -0600290{
291 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
292}
293
Becky Bruce391fd932008-06-09 20:37:18 -0500294phys_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 -0600295{
Becky Bruce391fd932008-06-09 20:37:18 -0500296 phys_addr_t alloc;
Kumar Gala4ed65522008-02-27 21:51:47 -0600297
298 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
299
300 if (alloc == 0)
301 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100302 (ulong)size, (ulong)max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600303
304 return alloc;
305}
306
Becky Bruce391fd932008-06-09 20:37:18 -0500307static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600308{
309 return addr & ~(size - 1);
310}
311
Becky Bruce391fd932008-06-09 20:37:18 -0500312phys_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 -0600313{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100314 long i, rgn;
Becky Bruce391fd932008-06-09 20:37:18 -0500315 phys_addr_t base = 0;
Andy Fleming7570a992008-06-16 13:58:55 -0500316 phys_addr_t res_base;
Kumar Gala4ed65522008-02-27 21:51:47 -0600317
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100318 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruce391fd932008-06-09 20:37:18 -0500319 phys_addr_t lmbbase = lmb->memory.region[i].base;
320 phys_size_t lmbsize = lmb->memory.region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600321
Andy Fleming7570a992008-06-16 13:58:55 -0500322 if (lmbsize < size)
323 continue;
Kumar Gala4ed65522008-02-27 21:51:47 -0600324 if (max_addr == LMB_ALLOC_ANYWHERE)
325 base = lmb_align_down(lmbbase + lmbsize - size, align);
326 else if (lmbbase < max_addr) {
Stephen Warrenad3fda52014-07-31 13:40:07 -0600327 base = lmbbase + lmbsize;
328 if (base < lmbbase)
329 base = -1;
330 base = min(base, max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600331 base = lmb_align_down(base - size, align);
332 } else
333 continue;
334
Andy Fleming7570a992008-06-16 13:58:55 -0500335 while (base && lmbbase <= base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100336 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
337 if (rgn < 0) {
Andy Fleming7570a992008-06-16 13:58:55 -0500338 /* This area isn't reserved, take it */
339 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100340 size) < 0)
Andy Fleming7570a992008-06-16 13:58:55 -0500341 return 0;
342 return base;
343 }
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100344 res_base = lmb->reserved.region[rgn].base;
Andy Fleming7570a992008-06-16 13:58:55 -0500345 if (res_base < size)
346 break;
347 base = lmb_align_down(res_base - size, align);
348 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600349 }
Andy Fleming7570a992008-06-16 13:58:55 -0500350 return 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600351}
352
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100353/*
354 * Try to allocate a specific address range: must be in defined memory but not
355 * reserved
356 */
357phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
358{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100359 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100360
361 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100362 rgn = lmb_overlaps_region(&lmb->memory, base, size);
363 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100364 /*
365 * Check if the requested end address is in the same memory
366 * region we found.
367 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100368 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
369 lmb->memory.region[rgn].size,
370 base + size - 1, 1)) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100371 /* ok, reserve the memory */
372 if (lmb_reserve(lmb, base, size) >= 0)
373 return base;
374 }
375 }
376 return 0;
377}
378
379/* Return number of bytes from a given address that are free */
Simon Goldschmidt65304aa2019-01-21 20:29:55 +0100380phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100381{
382 int i;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100383 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100384
385 /* check if the requested address is in the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100386 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
387 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100388 for (i = 0; i < lmb->reserved.cnt; i++) {
389 if (addr < lmb->reserved.region[i].base) {
390 /* first reserved range > requested address */
391 return lmb->reserved.region[i].base - addr;
392 }
393 if (lmb->reserved.region[i].base +
394 lmb->reserved.region[i].size > addr) {
395 /* requested addr is in this reserved range */
396 return 0;
397 }
398 }
399 /* if we come here: no reserved ranges above requested addr */
400 return lmb->memory.region[lmb->memory.cnt - 1].base +
401 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
402 }
403 return 0;
404}
405
Becky Bruce391fd932008-06-09 20:37:18 -0500406int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
Kumar Gala4ed65522008-02-27 21:51:47 -0600407{
408 int i;
409
410 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500411 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala4ed65522008-02-27 21:51:47 -0600412 lmb->reserved.region[i].size - 1;
413 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
414 return 1;
415 }
416 return 0;
417}
Mike Frysingera16028d2009-11-03 11:35:59 -0500418
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200419__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500420{
421 /* please define platform specific board_lmb_reserve() */
422}
Mike Frysingera16028d2009-11-03 11:35:59 -0500423
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200424__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500425{
426 /* please define platform specific arch_lmb_reserve() */
427}