blob: da924c6789acd4211faa7af07b6375e74beb7665 [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>
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +010010#include <efi_loader.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060011#include <image.h>
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +010012#include <mapmem.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060013#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <malloc.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060016
Marek Vasut12746982021-09-10 22:47:09 +020017#include <asm/global_data.h>
Marek Vasutbd994c02021-11-13 18:34:37 +010018#include <asm/sections.h>
Marek Vasut12746982021-09-10 22:47:09 +020019
20DECLARE_GLOBAL_DATA_PTR;
21
Kumar Gala4ed65522008-02-27 21:51:47 -060022#define LMB_ALLOC_ANYWHERE 0
23
Patrick Delaunay358c7782021-05-07 14:50:31 +020024static void lmb_dump_region(struct lmb_region *rgn, char *name)
25{
26 unsigned long long base, size, end;
27 enum lmb_flags flags;
28 int i;
29
Patrick Delaunayc9eb37a2023-03-22 19:12:26 +010030 printf(" %s.cnt = 0x%lx / max = 0x%lx\n", name, rgn->cnt, rgn->max);
Patrick Delaunay358c7782021-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;
37
38 printf(" %s[%d]\t[0x%llx-0x%llx], 0x%08llx bytes flags: %x\n",
39 name, i, base, end, size, flags);
40 }
41}
42
Tero Kristo9996cea2020-07-20 11:10:45 +030043void lmb_dump_all_force(struct lmb *lmb)
44{
Tero Kristo9996cea2020-07-20 11:10:45 +030045 printf("lmb_dump_all:\n");
Patrick Delaunay358c7782021-05-07 14:50:31 +020046 lmb_dump_region(&lmb->memory, "memory");
47 lmb_dump_region(&lmb->reserved, "reserved");
Tero Kristo9996cea2020-07-20 11:10:45 +030048}
49
Kumar Gala4ed65522008-02-27 21:51:47 -060050void lmb_dump_all(struct lmb *lmb)
51{
52#ifdef DEBUG
Tero Kristo9996cea2020-07-20 11:10:45 +030053 lmb_dump_all_force(lmb);
54#endif
Kumar Gala4ed65522008-02-27 21:51:47 -060055}
56
Simon Goldschmidte35d2a72019-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 Gala4ed65522008-02-27 21:51:47 -060059{
Simon Goldschmidtd67f33c2019-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 Gala4ed65522008-02-27 21:51:47 -060064}
65
Becky Bruce391fd932008-06-09 20:37:18 -050066static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010067 phys_addr_t base2, phys_size_t size2)
Kumar Gala4ed65522008-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
Udit Kumaredb58242023-09-26 16:54:42 +053077static long lmb_regions_overlap(struct lmb_region *rgn, unsigned long r1,
78 unsigned long r2)
79{
80 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;
84
85 return lmb_addrs_overlap(base1, size1, base2, size2);
86}
Simon Goldschmidte35d2a72019-01-21 20:29:56 +010087static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
88 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -060089{
Becky Bruce391fd932008-06-09 20:37:18 -050090 phys_addr_t base1 = rgn->region[r1].base;
91 phys_size_t size1 = rgn->region[r1].size;
92 phys_addr_t base2 = rgn->region[r2].base;
93 phys_size_t size2 = rgn->region[r2].size;
Kumar Gala4ed65522008-02-27 21:51:47 -060094 return lmb_addrs_adjacent(base1, size1, base2, size2);
95}
96
97static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
98{
99 unsigned long i;
100
101 for (i = r; i < rgn->cnt - 1; i++) {
102 rgn->region[i].base = rgn->region[i + 1].base;
103 rgn->region[i].size = rgn->region[i + 1].size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200104 rgn->region[i].flags = rgn->region[i + 1].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600105 }
106 rgn->cnt--;
107}
108
109/* Assumption: base addr of region 1 < base addr of region 2 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100110static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
111 unsigned long r2)
Kumar Gala4ed65522008-02-27 21:51:47 -0600112{
113 rgn->region[r1].size += rgn->region[r2].size;
114 lmb_remove_region(rgn, r2);
115}
116
Udit Kumaredb58242023-09-26 16:54:42 +0530117/*Assumption : base addr of region 1 < base addr of region 2*/
118static void lmb_fix_over_lap_regions(struct lmb_region *rgn, unsigned long r1,
119 unsigned long r2)
120{
121 phys_addr_t base1 = rgn->region[r1].base;
122 phys_size_t size1 = rgn->region[r1].size;
123 phys_addr_t base2 = rgn->region[r2].base;
124 phys_size_t size2 = rgn->region[r2].size;
125
126 if (base1 + size1 > base2 + size2) {
127 printf("This will not be a case any time\n");
128 return;
129 }
130 rgn->region[r1].size = base2 + size2 - base1;
131 lmb_remove_region(rgn, r2);
132}
133
Kumar Gala4ed65522008-02-27 21:51:47 -0600134void lmb_init(struct lmb *lmb)
135{
Patrick Delaunay6d665022021-03-10 10:16:31 +0100136#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
Patrick Delaunay4fa01502021-03-10 10:16:28 +0100137 lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
138 lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
Patrick Delaunay94c8da22023-03-22 19:12:25 +0100139#else
Patrick Delaunay6d665022021-03-10 10:16:31 +0100140 lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS;
141 lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS;
142 lmb->memory.region = lmb->memory_regions;
143 lmb->reserved.region = lmb->reserved_regions;
144#endif
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100145 lmb->memory.cnt = 0;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100146 lmb->reserved.cnt = 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600147}
148
Marek Vasut12746982021-09-10 22:47:09 +0200149void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align)
150{
151 ulong bank_end;
152 int bank;
153
154 /*
155 * Reserve memory from aligned address below the bottom of U-Boot stack
156 * until end of U-Boot area using LMB to prevent U-Boot from overwriting
157 * that memory.
158 */
159 debug("## Current stack ends at 0x%08lx ", sp);
160
161 /* adjust sp by 4K to be safe */
162 sp -= align;
163 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
164 if (!gd->bd->bi_dram[bank].size ||
165 sp < gd->bd->bi_dram[bank].start)
166 continue;
167 /* Watch out for RAM at end of address space! */
168 bank_end = gd->bd->bi_dram[bank].start +
169 gd->bd->bi_dram[bank].size - 1;
170 if (sp > bank_end)
171 continue;
172 if (bank_end > end)
173 bank_end = end - 1;
174
175 lmb_reserve(lmb, sp, bank_end - sp + 1);
Marek Vasutbd994c02021-11-13 18:34:37 +0100176
177 if (gd->flags & GD_FLG_SKIP_RELOC)
178 lmb_reserve(lmb, (phys_addr_t)(uintptr_t)_start, gd->mon_len);
179
Marek Vasut12746982021-09-10 22:47:09 +0200180 break;
181 }
182}
183
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +0100184/**
185 * efi_lmb_reserve() - add reservations for EFI memory
186 *
187 * Add reservations for all EFI memory areas that are not
188 * EFI_CONVENTIONAL_MEMORY.
189 *
190 * @lmb: lmb environment
191 * Return: 0 on success, 1 on failure
192 */
193static __maybe_unused int efi_lmb_reserve(struct lmb *lmb)
194{
195 struct efi_mem_desc *memmap = NULL, *map;
196 efi_uintn_t i, map_size = 0;
197 efi_status_t ret;
198
199 ret = efi_get_memory_map_alloc(&map_size, &memmap);
200 if (ret != EFI_SUCCESS)
201 return 1;
202
203 for (i = 0, map = memmap; i < map_size / sizeof(*map); ++map, ++i) {
Sjoerd Simonsc5279ea2023-01-19 09:38:18 +0100204 if (map->type != EFI_CONVENTIONAL_MEMORY) {
205 lmb_reserve_flags(lmb,
206 map_to_sysmem((void *)(uintptr_t)
207 map->physical_start),
208 map->num_pages * EFI_PAGE_SIZE,
209 map->type == EFI_RESERVED_MEMORY_TYPE
210 ? LMB_NOMAP : LMB_NONE);
211 }
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +0100212 }
213 efi_free_pool(memmap);
214
215 return 0;
216}
217
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100218static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100219{
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100220 arch_lmb_reserve(lmb);
221 board_lmb_reserve(lmb);
222
Simon Glass0c303f92021-09-25 19:43:21 -0600223 if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100224 boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
Heinrich Schuchardt06d514d2023-01-04 01:36:14 +0100225
226 if (CONFIG_IS_ENABLED(EFI_LOADER))
227 efi_lmb_reserve(lmb);
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100228}
229
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100230/* Initialize the struct, add memory and call arch/board reserve functions */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900231void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob)
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100232{
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100233 int i;
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100234
235 lmb_init(lmb);
Stefan Roesedfaf6a52020-08-12 11:55:46 +0200236
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100237 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
238 if (bd->bi_dram[i].size) {
239 lmb_add(lmb, bd->bi_dram[i].start,
240 bd->bi_dram[i].size);
241 }
242 }
Stefan Roesedfaf6a52020-08-12 11:55:46 +0200243
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100244 lmb_reserve_common(lmb, fdt_blob);
245}
246
247/* Initialize the struct, add memory and call arch/board reserve functions */
248void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base,
249 phys_size_t size, void *fdt_blob)
250{
251 lmb_init(lmb);
252 lmb_add(lmb, base, size);
253 lmb_reserve_common(lmb, fdt_blob);
254}
255
Kumar Gala4ed65522008-02-27 21:51:47 -0600256/* This routine called with relocation disabled. */
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200257static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
258 phys_size_t size, enum lmb_flags flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600259{
260 unsigned long coalesced = 0;
261 long adjacent, i;
262
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100263 if (rgn->cnt == 0) {
Kumar Gala4ed65522008-02-27 21:51:47 -0600264 rgn->region[0].base = base;
265 rgn->region[0].size = size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200266 rgn->region[0].flags = flags;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100267 rgn->cnt = 1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600268 return 0;
269 }
270
271 /* First try and coalesce this LMB with another. */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100272 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500273 phys_addr_t rgnbase = rgn->region[i].base;
274 phys_size_t rgnsize = rgn->region[i].size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200275 phys_size_t rgnflags = rgn->region[i].flags;
Sjoerd Simons0d91c882023-02-12 16:07:05 +0100276 phys_addr_t end = base + size - 1;
277 phys_addr_t rgnend = rgnbase + rgnsize - 1;
Sjoerd Simons0d91c882023-02-12 16:07:05 +0100278 if (rgnbase <= base && end <= rgnend) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200279 if (flags == rgnflags)
280 /* Already have this region, so we're done */
281 return 0;
282 else
283 return -1; /* regions with new flags */
284 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600285
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100286 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
287 if (adjacent > 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200288 if (flags != rgnflags)
289 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600290 rgn->region[i].base -= size;
291 rgn->region[i].size += size;
292 coalesced++;
293 break;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100294 } else if (adjacent < 0) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200295 if (flags != rgnflags)
296 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600297 rgn->region[i].size += size;
298 coalesced++;
299 break;
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100300 } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
301 /* regions overlap */
302 return -1;
Kumar Gala4ed65522008-02-27 21:51:47 -0600303 }
304 }
305
Udit Kumaredb58242023-09-26 16:54:42 +0530306 if (i < rgn->cnt - 1 && rgn->region[i].flags == rgn->region[i + 1].flags) {
307 if (lmb_regions_adjacent(rgn, i, i + 1)) {
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200308 lmb_coalesce_regions(rgn, i, i + 1);
309 coalesced++;
Udit Kumaredb58242023-09-26 16:54:42 +0530310 } else if (lmb_regions_overlap(rgn, i, i + 1)) {
311 /* fix overlapping area */
312 lmb_fix_over_lap_regions(rgn, i, i + 1);
313 coalesced++;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200314 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600315 }
316
317 if (coalesced)
318 return coalesced;
Patrick Delaunay00fd8da2021-03-10 10:16:27 +0100319 if (rgn->cnt >= rgn->max)
Kumar Gala4ed65522008-02-27 21:51:47 -0600320 return -1;
321
322 /* Couldn't coalesce the LMB, so add it to the sorted table. */
323 for (i = rgn->cnt-1; i >= 0; i--) {
324 if (base < rgn->region[i].base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100325 rgn->region[i + 1].base = rgn->region[i].base;
326 rgn->region[i + 1].size = rgn->region[i].size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200327 rgn->region[i + 1].flags = rgn->region[i].flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600328 } else {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100329 rgn->region[i + 1].base = base;
330 rgn->region[i + 1].size = size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200331 rgn->region[i + 1].flags = flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600332 break;
333 }
334 }
335
336 if (base < rgn->region[0].base) {
337 rgn->region[0].base = base;
338 rgn->region[0].size = size;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200339 rgn->region[0].flags = flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600340 }
341
342 rgn->cnt++;
343
344 return 0;
345}
346
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200347static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base,
348 phys_size_t size)
349{
350 return lmb_add_region_flags(rgn, base, size, LMB_NONE);
351}
352
Kumar Gala4ed65522008-02-27 21:51:47 -0600353/* This routine may be called with relocation disabled. */
Becky Bruce391fd932008-06-09 20:37:18 -0500354long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600355{
356 struct lmb_region *_rgn = &(lmb->memory);
357
358 return lmb_add_region(_rgn, base, size);
359}
360
Andy Fleming98874ff2008-07-07 14:24:39 -0500361long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Andy Fleming63796c42008-06-16 13:58:54 -0500362{
363 struct lmb_region *rgn = &(lmb->reserved);
Andy Fleming98874ff2008-07-07 14:24:39 -0500364 phys_addr_t rgnbegin, rgnend;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100365 phys_addr_t end = base + size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500366 int i;
367
368 rgnbegin = rgnend = 0; /* supress gcc warnings */
369
370 /* Find the region where (base, size) belongs to */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100371 for (i = 0; i < rgn->cnt; i++) {
Andy Fleming63796c42008-06-16 13:58:54 -0500372 rgnbegin = rgn->region[i].base;
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100373 rgnend = rgnbegin + rgn->region[i].size - 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500374
375 if ((rgnbegin <= base) && (end <= rgnend))
376 break;
377 }
378
379 /* Didn't find the region */
380 if (i == rgn->cnt)
381 return -1;
382
383 /* Check to see if we are removing entire region */
384 if ((rgnbegin == base) && (rgnend == end)) {
385 lmb_remove_region(rgn, i);
386 return 0;
387 }
388
389 /* Check to see if region is matching at the front */
390 if (rgnbegin == base) {
Simon Goldschmidtd67f33c2019-01-14 22:38:15 +0100391 rgn->region[i].base = end + 1;
Andy Fleming63796c42008-06-16 13:58:54 -0500392 rgn->region[i].size -= size;
393 return 0;
394 }
395
396 /* Check to see if the region is matching at the end */
397 if (rgnend == end) {
398 rgn->region[i].size -= size;
399 return 0;
400 }
401
402 /*
403 * We need to split the entry - adjust the current one to the
404 * beginging of the hole and add the region after hole.
405 */
406 rgn->region[i].size = base - rgn->region[i].base;
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200407 return lmb_add_region_flags(rgn, end + 1, rgnend - end,
408 rgn->region[i].flags);
409}
410
411long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size,
412 enum lmb_flags flags)
413{
414 struct lmb_region *_rgn = &(lmb->reserved);
415
416 return lmb_add_region_flags(_rgn, base, size, flags);
Andy Fleming63796c42008-06-16 13:58:54 -0500417}
418
Becky Bruce391fd932008-06-09 20:37:18 -0500419long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600420{
Patrick Delaunay59c0ea52021-05-07 14:50:29 +0200421 return lmb_reserve_flags(lmb, base, size, LMB_NONE);
Kumar Gala4ed65522008-02-27 21:51:47 -0600422}
423
Jeroen Hofstee750a6ff2014-10-08 22:57:39 +0200424static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
Becky Bruce391fd932008-06-09 20:37:18 -0500425 phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600426{
427 unsigned long i;
428
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100429 for (i = 0; i < rgn->cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500430 phys_addr_t rgnbase = rgn->region[i].base;
431 phys_size_t rgnsize = rgn->region[i].size;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100432 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
Kumar Gala4ed65522008-02-27 21:51:47 -0600433 break;
Kumar Gala4ed65522008-02-27 21:51:47 -0600434 }
435
436 return (i < rgn->cnt) ? i : -1;
437}
438
Becky Bruce391fd932008-06-09 20:37:18 -0500439phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
Kumar Gala4ed65522008-02-27 21:51:47 -0600440{
441 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
442}
443
Becky Bruce391fd932008-06-09 20:37:18 -0500444phys_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 -0600445{
Becky Bruce391fd932008-06-09 20:37:18 -0500446 phys_addr_t alloc;
Kumar Gala4ed65522008-02-27 21:51:47 -0600447
448 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
449
450 if (alloc == 0)
451 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100452 (ulong)size, (ulong)max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600453
454 return alloc;
455}
456
Becky Bruce391fd932008-06-09 20:37:18 -0500457static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
Kumar Gala4ed65522008-02-27 21:51:47 -0600458{
459 return addr & ~(size - 1);
460}
461
Becky Bruce391fd932008-06-09 20:37:18 -0500462phys_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 -0600463{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100464 long i, rgn;
Becky Bruce391fd932008-06-09 20:37:18 -0500465 phys_addr_t base = 0;
Andy Fleming7570a992008-06-16 13:58:55 -0500466 phys_addr_t res_base;
Kumar Gala4ed65522008-02-27 21:51:47 -0600467
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100468 for (i = lmb->memory.cnt - 1; i >= 0; i--) {
Becky Bruce391fd932008-06-09 20:37:18 -0500469 phys_addr_t lmbbase = lmb->memory.region[i].base;
470 phys_size_t lmbsize = lmb->memory.region[i].size;
Kumar Gala4ed65522008-02-27 21:51:47 -0600471
Andy Fleming7570a992008-06-16 13:58:55 -0500472 if (lmbsize < size)
473 continue;
Kumar Gala4ed65522008-02-27 21:51:47 -0600474 if (max_addr == LMB_ALLOC_ANYWHERE)
475 base = lmb_align_down(lmbbase + lmbsize - size, align);
476 else if (lmbbase < max_addr) {
Stephen Warrenad3fda52014-07-31 13:40:07 -0600477 base = lmbbase + lmbsize;
478 if (base < lmbbase)
479 base = -1;
480 base = min(base, max_addr);
Kumar Gala4ed65522008-02-27 21:51:47 -0600481 base = lmb_align_down(base - size, align);
482 } else
483 continue;
484
Andy Fleming7570a992008-06-16 13:58:55 -0500485 while (base && lmbbase <= base) {
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100486 rgn = lmb_overlaps_region(&lmb->reserved, base, size);
487 if (rgn < 0) {
Andy Fleming7570a992008-06-16 13:58:55 -0500488 /* This area isn't reserved, take it */
489 if (lmb_add_region(&lmb->reserved, base,
Simon Goldschmidt0f7c51a2019-01-14 22:38:16 +0100490 size) < 0)
Andy Fleming7570a992008-06-16 13:58:55 -0500491 return 0;
492 return base;
493 }
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100494 res_base = lmb->reserved.region[rgn].base;
Andy Fleming7570a992008-06-16 13:58:55 -0500495 if (res_base < size)
496 break;
497 base = lmb_align_down(res_base - size, align);
498 }
Kumar Gala4ed65522008-02-27 21:51:47 -0600499 }
Andy Fleming7570a992008-06-16 13:58:55 -0500500 return 0;
Kumar Gala4ed65522008-02-27 21:51:47 -0600501}
502
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100503/*
504 * Try to allocate a specific address range: must be in defined memory but not
505 * reserved
506 */
507phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size)
508{
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100509 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100510
511 /* Check if the requested address is in one of the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100512 rgn = lmb_overlaps_region(&lmb->memory, base, size);
513 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100514 /*
515 * Check if the requested end address is in the same memory
516 * region we found.
517 */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100518 if (lmb_addrs_overlap(lmb->memory.region[rgn].base,
519 lmb->memory.region[rgn].size,
520 base + size - 1, 1)) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100521 /* ok, reserve the memory */
522 if (lmb_reserve(lmb, base, size) >= 0)
523 return base;
524 }
525 }
526 return 0;
527}
528
529/* Return number of bytes from a given address that are free */
Simon Goldschmidt65304aa2019-01-21 20:29:55 +0100530phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr)
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100531{
532 int i;
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100533 long rgn;
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100534
535 /* check if the requested address is in the memory regions */
Simon Goldschmidte35d2a72019-01-21 20:29:56 +0100536 rgn = lmb_overlaps_region(&lmb->memory, addr, 1);
537 if (rgn >= 0) {
Simon Goldschmidt4cc8af82019-01-14 22:38:18 +0100538 for (i = 0; i < lmb->reserved.cnt; i++) {
539 if (addr < lmb->reserved.region[i].base) {
540 /* first reserved range > requested address */
541 return lmb->reserved.region[i].base - addr;
542 }
543 if (lmb->reserved.region[i].base +
544 lmb->reserved.region[i].size > addr) {
545 /* requested addr is in this reserved range */
546 return 0;
547 }
548 }
549 /* if we come here: no reserved ranges above requested addr */
550 return lmb->memory.region[lmb->memory.cnt - 1].base +
551 lmb->memory.region[lmb->memory.cnt - 1].size - addr;
552 }
553 return 0;
554}
555
Patrick Delaunaye359a4a2021-05-07 14:50:30 +0200556int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags)
Kumar Gala4ed65522008-02-27 21:51:47 -0600557{
558 int i;
559
560 for (i = 0; i < lmb->reserved.cnt; i++) {
Becky Bruce391fd932008-06-09 20:37:18 -0500561 phys_addr_t upper = lmb->reserved.region[i].base +
Kumar Gala4ed65522008-02-27 21:51:47 -0600562 lmb->reserved.region[i].size - 1;
563 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))
Patrick Delaunaye359a4a2021-05-07 14:50:30 +0200564 return (lmb->reserved.region[i].flags & flags) == flags;
Kumar Gala4ed65522008-02-27 21:51:47 -0600565 }
566 return 0;
567}
Mike Frysingera16028d2009-11-03 11:35:59 -0500568
Patrick Delaunaye359a4a2021-05-07 14:50:30 +0200569int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
570{
571 return lmb_is_reserved_flags(lmb, addr, LMB_NONE);
572}
573
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200574__weak void board_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500575{
576 /* please define platform specific board_lmb_reserve() */
577}
Mike Frysingera16028d2009-11-03 11:35:59 -0500578
Jeroen Hofstee2c34f3f2014-06-26 20:04:37 +0200579__weak void arch_lmb_reserve(struct lmb *lmb)
Mike Frysingera16028d2009-11-03 11:35:59 -0500580{
581 /* please define platform specific arch_lmb_reserve() */
582}