blob: b2c233edb64ee459a503eca5ce37f87376d63ecc [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>
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +010010#include <efi_loader.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060011#include <image.h>
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +010012#include <mapmem.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060013#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <malloc.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060016
Marek Vasuta2eec022021-09-10 22:47:09 +020017#include <asm/global_data.h>
Marek Vasut0fcae7f2021-11-13 18:34:37 +010018#include <asm/sections.h>
Marek Vasuta2eec022021-09-10 22:47:09 +020019
20DECLARE_GLOBAL_DATA_PTR;
21
Kumar Gala6d7bfa82008-02-27 21:51:47 -060022#define LMB_ALLOC_ANYWHERE 0
23
Patrick Delaunay48120022021-05-07 14:50:31 +020024static void lmb_dump_region(struct lmb_region *rgn, char *name)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060025{
Patrick Delaunay48120022021-05-07 14:50:31 +020026 unsigned long long base, size, end;
27 enum lmb_flags flags;
28 int i;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060029
Patrick Delaunay6c230522023-03-22 19:12:26 +010030 printf(" %s.cnt = 0x%lx / max = 0x%lx\n", name, rgn->cnt, rgn->max);
Patrick Delaunay48120022021-05-07 14:50:31 +020031
32 for (i = 0; i < rgn->cnt; i++) {
33 base = rgn->region[i].base;
34 size = rgn->region[i].size;
35 end = base + size - 1;
36 flags = rgn->region[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060037
Patrick Delaunay48120022021-05-07 14:50:31 +020038 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
39 name, i, base, end, size, flags);
Kumar Gala6d7bfa82008-02-27 21:51:47 -060040 }
Tero Kristo97c418b2020-07-20 11:10:45 +030041}
42
Patrick Delaunay48120022021-05-07 14:50:31 +020043void lmb_dump_all_force(struct lmb *lmb)
44{
45 printf("lmb_dump_all:\n");
46 lmb_dump_region(&lmb->memory, "memory");
47 lmb_dump_region(&lmb->reserved, "reserved");
48}
49
Tero Kristo97c418b2020-07-20 11:10:45 +030050void lmb_dump_all(struct lmb *lmb)
51{
52#ifdef DEBUG
53 lmb_dump_all_force(lmb);
54#endif
Kumar Gala6d7bfa82008-02-27 21:51:47 -060055}
56
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010057static long lmb_addrs_overlap(phys_addr_t base1, phys_size_t size1,
58 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060059{
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +010060 const phys_addr_t base1_end = base1 + size1 - 1;
61 const phys_addr_t base2_end = base2 + size2 - 1;
62
63 return ((base1 <= base2_end) && (base2 <= base1_end));
Kumar Gala6d7bfa82008-02-27 21:51:47 -060064}
65
Becky Bruced26d67c2008-06-09 20:37:18 -050066static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010067 phys_addr_t base2, phys_size_t size2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060068{
69 if (base2 == base1 + size1)
70 return 1;
71 else if (base1 == base2 + size2)
72 return -1;
73
74 return 0;
75}
76
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +010077static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
78 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -060079{
Becky Bruced26d67c2008-06-09 20:37:18 -050080 phys_addr_t base1 = rgn->region[r1].base;
81 phys_size_t size1 = rgn->region[r1].size;
82 phys_addr_t base2 = rgn->region[r2].base;
83 phys_size_t size2 = rgn->region[r2].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060084
85 return lmb_addrs_adjacent(base1, size1, base2, size2);
86}
87
88static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
89{
90 unsigned long i;
91
92 for (i = r; i < rgn->cnt - 1; i++) {
93 rgn->region[i].base = rgn->region[i + 1].base;
94 rgn->region[i].size = rgn->region[i + 1].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +020095 rgn->region[i].flags = rgn->region[i + 1].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -060096 }
97 rgn->cnt--;
98}
99
100/* Assumption: base addr of region 1 < base addr of region 2 */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100101static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
102 unsigned long r2)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600103{
104 rgn->region[r1].size += rgn->region[r2].size;
105 lmb_remove_region(rgn, r2);
106}
107
108void lmb_init(struct lmb *lmb)
109{
Patrick Delaunay71cc9c52021-03-10 10:16:31 +0100110#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
Patrick Delaunaycd831af2021-03-10 10:16:28 +0100111 lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
112 lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
Patrick Delaunay479de712023-03-22 19:12:25 +0100113#else
Patrick Delaunay71cc9c52021-03-10 10:16:31 +0100114 lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS;
115 lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS;
116 lmb->memory.region = lmb->memory_regions;
117 lmb->reserved.region = lmb->reserved_regions;
118#endif
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100119 lmb->memory.cnt = 0;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100120 lmb->reserved.cnt = 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600121}
122
Marek Vasuta2eec022021-09-10 22:47:09 +0200123void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align)
124{
125 ulong bank_end;
126 int bank;
127
128 /*
129 * Reserve memory from aligned address below the bottom of U-Boot stack
130 * until end of U-Boot area using LMB to prevent U-Boot from overwriting
131 * that memory.
132 */
133 debug("## Current stack ends at 0x%08lx ", sp);
134
135 /* adjust sp by 4K to be safe */
136 sp -= align;
137 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
138 if (!gd->bd->bi_dram[bank].size ||
139 sp < gd->bd->bi_dram[bank].start)
140 continue;
141 /* Watch out for RAM at end of address space! */
142 bank_end = gd->bd->bi_dram[bank].start +
143 gd->bd->bi_dram[bank].size - 1;
144 if (sp > bank_end)
145 continue;
146 if (bank_end > end)
147 bank_end = end - 1;
148
149 lmb_reserve(lmb, sp, bank_end - sp + 1);
Marek Vasut0fcae7f2021-11-13 18:34:37 +0100150
151 if (gd->flags & GD_FLG_SKIP_RELOC)
152 lmb_reserve(lmb, (phys_addr_t)(uintptr_t)_start, gd->mon_len);
153
Marek Vasuta2eec022021-09-10 22:47:09 +0200154 break;
155 }
156}
157
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100158/**
159 * efi_lmb_reserve() - add reservations for EFI memory
160 *
161 * Add reservations for all EFI memory areas that are not
162 * EFI_CONVENTIONAL_MEMORY.
163 *
164 * @lmb: lmb environment
165 * Return: 0 on success, 1 on failure
166 */
167static __maybe_unused int efi_lmb_reserve(struct lmb *lmb)
168{
169 struct efi_mem_desc *memmap = NULL, *map;
170 efi_uintn_t i, map_size = 0;
171 efi_status_t ret;
172
173 ret = efi_get_memory_map_alloc(&map_size, &memmap);
174 if (ret != EFI_SUCCESS)
175 return 1;
176
177 for (i = 0, map = memmap; i < map_size / sizeof(*map); ++map, ++i) {
Sjoerd Simons47a5a842023-01-19 09:38:18 +0100178 if (map->type != EFI_CONVENTIONAL_MEMORY) {
179 lmb_reserve_flags(lmb,
180 map_to_sysmem((void *)(uintptr_t)
181 map->physical_start),
182 map->num_pages * EFI_PAGE_SIZE,
183 map->type == EFI_RESERVED_MEMORY_TYPE
184 ? LMB_NOMAP : LMB_NONE);
185 }
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100186 }
187 efi_free_pool(memmap);
188
189 return 0;
190}
191
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100192static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100193{
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100194 arch_lmb_reserve(lmb);
195 board_lmb_reserve(lmb);
196
Simon Glass85c057e2021-09-25 19:43:21 -0600197 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100198 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
Heinrich Schuchardtc9bb2eb2023-01-04 01:36:14 +0100199
200 if (CONFIG_IS_ENABLED(EFI_LOADER))
201 efi_lmb_reserve(lmb);
Simon Goldschmidt5b2c6872019-01-14 22:38:19 +0100202}
203
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100204/* Initialize the struct, add memory and call arch/board reserve functions */
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900205void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100206{
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100207 int i;
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100208
209 lmb_init(lmb);
Stefan Roesebbc88462020-08-12 11:55:46 +0200210
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100211 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
212 if (bd->bi_dram[i].size) {
213 lmb_add(lmb, bd->bi_dram[i].start,
214 bd->bi_dram[i].size);
215 }
216 }
Stefan Roesebbc88462020-08-12 11:55:46 +0200217
Simon Goldschmidt8890e7d2019-01-26 22:13:04 +0100218 lmb_reserve_common(lmb, fdt_blob);
219}
220
221/* Initialize the struct, add memory and call arch/board reserve functions */
222void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
223 phys_size_t size, void *fdt_blob)
224{
225 lmb_init(lmb);
226 lmb_add(lmb, base, size);
227 lmb_reserve_common(lmb, fdt_blob);
228}
229
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600230/* This routine called with relocation disabled. */
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200231static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
232 phys_size_t size, enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600233{
234 unsigned long coalesced = 0;
235 long adjacent, i;
236
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100237 if (rgn->cnt == 0) {
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600238 rgn->region[0].base = base;
239 rgn->region[0].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200240 rgn->region[0].flags = flags;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100241 rgn->cnt = 1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600242 return 0;
243 }
244
245 /* First try and coalesce this LMB with another. */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100246 for (i = 0; i < rgn->cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500247 phys_addr_t rgnbase = rgn->region[i].base;
248 phys_size_t rgnsize = rgn->region[i].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200249 phys_size_t rgnflags = rgn->region[i].flags;
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100250 phys_addr_t end = base + size - 1;
251 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600252
Sjoerd Simons8cce4912023-02-12 16:07:05 +0100253 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200254 if (flags == rgnflags)
255 /* Already have this region, so we're done */
256 return 0;
257 else
258 return -1; /* regions with new flags */
259 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600260
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100261 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
262 if (adjacent > 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200263 if (flags != rgnflags)
264 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600265 rgn->region[i].base -= size;
266 rgn->region[i].size += size;
267 coalesced++;
268 break;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100269 } else if (adjacent < 0) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200270 if (flags != rgnflags)
271 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600272 rgn->region[i].size += size;
273 coalesced++;
274 break;
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100275 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
276 /* regions overlap */
277 return -1;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600278 }
279 }
280
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100281 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200282 if (rgn->region[i].flags == rgn->region[i + 1].flags) {
283 lmb_coalesce_regions(rgn, i, i + 1);
284 coalesced++;
285 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600286 }
287
288 if (coalesced)
289 return coalesced;
Patrick Delaunay8c69fc22021-03-10 10:16:27 +0100290 if (rgn->cnt >= rgn->max)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600291 return -1;
292
293 /* Couldn't coalesce the LMB, so add it to the sorted table. */
294 for (i = rgn->cnt-1; i >= 0; i--) {
295 if (base < rgn->region[i].base) {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100296 rgn->region[i + 1].base = rgn->region[i].base;
297 rgn->region[i + 1].size = rgn->region[i].size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200298 rgn->region[i + 1].flags = rgn->region[i].flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600299 } else {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100300 rgn->region[i + 1].base = base;
301 rgn->region[i + 1].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200302 rgn->region[i + 1].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600303 break;
304 }
305 }
306
307 if (base < rgn->region[0].base) {
308 rgn->region[0].base = base;
309 rgn->region[0].size = size;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200310 rgn->region[0].flags = flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600311 }
312
313 rgn->cnt++;
314
315 return 0;
316}
317
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200318static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base,
319 phys_size_t size)
320{
321 return lmb_add_region_flags(rgn, base, size, LMB_NONE);
322}
323
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600324/* This routine may be called with relocation disabled. */
Becky Bruced26d67c2008-06-09 20:37:18 -0500325long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600326{
327 struct lmb_region *_rgn = &(lmb->memory);
328
329 return lmb_add_region(_rgn, base, size);
330}
331
Andy Fleming09d2a712008-07-07 14:24:39 -0500332long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming1ae346c2008-06-16 13:58:54 -0500333{
334 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming09d2a712008-07-07 14:24:39 -0500335 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100336 phys_addr_t end = base + size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500337 int i;
338
339 rgnbegin = rgnend = 0; /* supress gcc warnings */
340
341 /* Find the region where (base, size) belongs to */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100342 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming1ae346c2008-06-16 13:58:54 -0500343 rgnbegin = rgn->region[i].base;
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100344 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500345
346 if ((rgnbegin <= base) && (end <= rgnend))
347 break;
348 }
349
350 /* Didn't find the region */
351 if (i == rgn->cnt)
352 return -1;
353
354 /* Check to see if we are removing entire region */
355 if ((rgnbegin == base) && (rgnend == end)) {
356 lmb_remove_region(rgn, i);
357 return 0;
358 }
359
360 /* Check to see if region is matching at the front */
361 if (rgnbegin == base) {
Simon Goldschmidt6402d9b2019-01-14 22:38:15 +0100362 rgn->region[i].base = end + 1;
Andy Fleming1ae346c2008-06-16 13:58:54 -0500363 rgn->region[i].size -= size;
364 return 0;
365 }
366
367 /* Check to see if the region is matching at the end */
368 if (rgnend == end) {
369 rgn->region[i].size -= size;
370 return 0;
371 }
372
373 /*
374 * We need to split the entry - adjust the current one to the
375 * beginging of the hole and add the region after hole.
376 */
377 rgn->region[i].size = base - rgn->region[i].base;
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200378 return lmb_add_region_flags(rgn, end + 1, rgnend - end,
379 rgn->region[i].flags);
Andy Fleming1ae346c2008-06-16 13:58:54 -0500380}
381
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200382long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size,
383 enum lmb_flags flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600384{
385 struct lmb_region *_rgn = &(lmb->reserved);
386
Patrick Delaunaye11c9082021-05-07 14:50:29 +0200387 return lmb_add_region_flags(_rgn, base, size, flags);
388}
389
390long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
391{
392 return lmb_reserve_flags(lmb, base, size, LMB_NONE);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600393}
394
Jeroen Hofsteea0209722014-10-08 22:57:39 +0200395static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruced26d67c2008-06-09 20:37:18 -0500396 phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600397{
398 unsigned long i;
399
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100400 for (i = 0; i < rgn->cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500401 phys_addr_t rgnbase = rgn->region[i].base;
402 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100403 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600404 break;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600405 }
406
407 return (i < rgn->cnt) ? i : -1;
408}
409
Becky Bruced26d67c2008-06-09 20:37:18 -0500410phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600411{
412 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
413}
414
Becky Bruced26d67c2008-06-09 20:37:18 -0500415phys_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 -0600416{
Becky Bruced26d67c2008-06-09 20:37:18 -0500417 phys_addr_t alloc;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600418
419 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
420
421 if (alloc == 0)
422 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100423 (ulong)size, (ulong)max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600424
425 return alloc;
426}
427
Becky Bruced26d67c2008-06-09 20:37:18 -0500428static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600429{
430 return addr & ~(size - 1);
431}
432
Becky Bruced26d67c2008-06-09 20:37:18 -0500433phys_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 -0600434{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100435 long i, rgn;
Becky Bruced26d67c2008-06-09 20:37:18 -0500436 phys_addr_t base = 0;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500437 phys_addr_t res_base;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600438
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100439 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500440 phys_addr_t lmbbase = lmb->memory.region[i].base;
441 phys_size_t lmbsize = lmb->memory.region[i].size;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600442
Andy Fleming78bd5a72008-06-16 13:58:55 -0500443 if (lmbsize < size)
444 continue;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600445 if (max_addr == LMB_ALLOC_ANYWHERE)
446 base = lmb_align_down(lmbbase + lmbsize - size, align);
447 else if (lmbbase < max_addr) {
Stephen Warrenb6a010b2014-07-31 13:40:07 -0600448 base = lmbbase + lmbsize;
449 if (base < lmbbase)
450 base = -1;
451 base = min(base, max_addr);
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600452 base = lmb_align_down(base - size, align);
453 } else
454 continue;
455
Andy Fleming78bd5a72008-06-16 13:58:55 -0500456 while (base && lmbbase <= base) {
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100457 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
458 if (rgn < 0) {
Andy Fleming78bd5a72008-06-16 13:58:55 -0500459 /* This area isn't reserved, take it */
460 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidtcb57d132019-01-14 22:38:16 +0100461 size) < 0)
Andy Fleming78bd5a72008-06-16 13:58:55 -0500462 return 0;
463 return base;
464 }
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100465 res_base = lmb->reserved.region[rgn].base;
Andy Fleming78bd5a72008-06-16 13:58:55 -0500466 if (res_base < size)
467 break;
468 base = lmb_align_down(res_base - size, align);
469 }
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600470 }
Andy Fleming78bd5a72008-06-16 13:58:55 -0500471 return 0;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600472}
473
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100474/*
475 * Try to allocate a specific address range: must be in defined memory but not
476 * reserved
477 */
478phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
479{
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100480 long rgn;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100481
482 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100483 rgn = lmb_overlaps_region(&lmb->memory, base, size);
484 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100485 /*
486 * Check if the requested end address is in the same memory
487 * region we found.
488 */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100489 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
490 lmb->memory.region[rgn].size,
491 base + size - 1, 1)) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100492 /* ok, reserve the memory */
493 if (lmb_reserve(lmb, base, size) >= 0)
494 return base;
495 }
496 }
497 return 0;
498}
499
500/* Return number of bytes from a given address that are free */
Simon Goldschmidt7510a562019-01-21 20:29:55 +0100501phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100502{
503 int i;
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100504 long rgn;
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100505
506 /* check if the requested address is in the memory regions */
Simon Goldschmidtf41f7d62019-01-21 20:29:56 +0100507 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
508 if (rgn >= 0) {
Simon Goldschmidt7a6ee462019-01-14 22:38:18 +0100509 for (i = 0; i < lmb->reserved.cnt; i++) {
510 if (addr < lmb->reserved.region[i].base) {
511 /* first reserved range > requested address */
512 return lmb->reserved.region[i].base - addr;
513 }
514 if (lmb->reserved.region[i].base +
515 lmb->reserved.region[i].size > addr) {
516 /* requested addr is in this reserved range */
517 return 0;
518 }
519 }
520 /* if we come here: no reserved ranges above requested addr */
521 return lmb->memory.region[lmb->memory.cnt - 1].base +
522 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
523 }
524 return 0;
525}
526
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200527int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags)
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600528{
529 int i;
530
531 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500532 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600533 lmb->reserved.region[i].size - 1;
534 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200535 return (lmb->reserved.region[i].flags & flags) == flags;
Kumar Gala6d7bfa82008-02-27 21:51:47 -0600536 }
537 return 0;
538}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500539
Patrick Delaunaydb2c9aa2021-05-07 14:50:30 +0200540int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
541{
542 return lmb_is_reserved_flags(lmb, addr, LMB_NONE);
543}
544
Jeroen Hofstee0461dda2014-06-26 20:04:37 +0200545__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera0dadf82009-11-03 11:35:59 -0500546{
547 /* please define platform specific board_lmb_reserve() */
548}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500549
Jeroen Hofstee0461dda2014-06-26 20:04:37 +0200550__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera0dadf82009-11-03 11:35:59 -0500551{
552 /* please define platform specific arch_lmb_reserve() */
553}