2 * Procedures for maintaining information about logical memory blocks.
4 * Peter Bergner, IBM Corp. June 2001.
5 * Copyright (C) 2001 Peter Bergner.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
16 #define LMB_ALLOC_ANYWHERE 0
18 void lmb_dump_all(struct lmb *lmb)
23 debug("lmb_dump_all:\n");
24 debug(" memory.cnt = 0x%lx\n", lmb->memory.cnt);
25 debug(" memory.size = 0x%llx\n",
26 (unsigned long long)lmb->memory.size);
27 for (i=0; i < lmb->memory.cnt ;i++) {
28 debug(" memory.reg[0x%x].base = 0x%llx\n", i,
29 lmb->memory.region[i].base);
30 debug(" .size = 0x%llx\n",
31 lmb->memory.region[i].size);
34 debug("\n reserved.cnt = 0x%lx\n", lmb->reserved.cnt);
35 debug(" reserved.size = 0x%llx\n", lmb->reserved.size);
36 for (i=0; i < lmb->reserved.cnt ;i++) {
37 debug(" reserved.reg[0x%x].base = 0x%llx\n", i,
38 lmb->reserved.region[i].base);
39 debug(" .size = 0x%llx\n",
40 lmb->reserved.region[i].size);
45 static long lmb_addrs_overlap(phys_addr_t base1,
46 phys_size_t size1, phys_addr_t base2, phys_size_t size2)
48 return ((base1 < (base2+size2)) && (base2 < (base1+size1)));
51 static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
52 phys_addr_t base2, phys_size_t size2)
54 if (base2 == base1 + size1)
56 else if (base1 == base2 + size2)
62 static long lmb_regions_adjacent(struct lmb_region *rgn,
63 unsigned long r1, unsigned long r2)
65 phys_addr_t base1 = rgn->region[r1].base;
66 phys_size_t size1 = rgn->region[r1].size;
67 phys_addr_t base2 = rgn->region[r2].base;
68 phys_size_t size2 = rgn->region[r2].size;
70 return lmb_addrs_adjacent(base1, size1, base2, size2);
73 static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
77 for (i = r; i < rgn->cnt - 1; i++) {
78 rgn->region[i].base = rgn->region[i + 1].base;
79 rgn->region[i].size = rgn->region[i + 1].size;
84 /* Assumption: base addr of region 1 < base addr of region 2 */
85 static void lmb_coalesce_regions(struct lmb_region *rgn,
86 unsigned long r1, unsigned long r2)
88 rgn->region[r1].size += rgn->region[r2].size;
89 lmb_remove_region(rgn, r2);
92 void lmb_init(struct lmb *lmb)
94 /* Create a dummy zero size LMB which will get coalesced away later.
95 * This simplifies the lmb_add() code below...
97 lmb->memory.region[0].base = 0;
98 lmb->memory.region[0].size = 0;
100 lmb->memory.size = 0;
103 lmb->reserved.region[0].base = 0;
104 lmb->reserved.region[0].size = 0;
105 lmb->reserved.cnt = 1;
106 lmb->reserved.size = 0;
109 /* This routine called with relocation disabled. */
110 static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t size)
112 unsigned long coalesced = 0;
115 if ((rgn->cnt == 1) && (rgn->region[0].size == 0)) {
116 rgn->region[0].base = base;
117 rgn->region[0].size = size;
121 /* First try and coalesce this LMB with another. */
122 for (i=0; i < rgn->cnt; i++) {
123 phys_addr_t rgnbase = rgn->region[i].base;
124 phys_size_t rgnsize = rgn->region[i].size;
126 if ((rgnbase == base) && (rgnsize == size))
127 /* Already have this region, so we're done */
130 adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize);
131 if ( adjacent > 0 ) {
132 rgn->region[i].base -= size;
133 rgn->region[i].size += size;
137 else if ( adjacent < 0 ) {
138 rgn->region[i].size += size;
144 if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) {
145 lmb_coalesce_regions(rgn, i, i+1);
151 if (rgn->cnt >= MAX_LMB_REGIONS)
154 /* Couldn't coalesce the LMB, so add it to the sorted table. */
155 for (i = rgn->cnt-1; i >= 0; i--) {
156 if (base < rgn->region[i].base) {
157 rgn->region[i+1].base = rgn->region[i].base;
158 rgn->region[i+1].size = rgn->region[i].size;
160 rgn->region[i+1].base = base;
161 rgn->region[i+1].size = size;
166 if (base < rgn->region[0].base) {
167 rgn->region[0].base = base;
168 rgn->region[0].size = size;
176 /* This routine may be called with relocation disabled. */
177 long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
179 struct lmb_region *_rgn = &(lmb->memory);
181 return lmb_add_region(_rgn, base, size);
184 long lmb_free(struct lmb *lmb, u64 base, u64 size)
186 struct lmb_region *rgn = &(lmb->reserved);
187 u64 rgnbegin, rgnend;
188 u64 end = base + size;
191 rgnbegin = rgnend = 0; /* supress gcc warnings */
193 /* Find the region where (base, size) belongs to */
194 for (i=0; i < rgn->cnt; i++) {
195 rgnbegin = rgn->region[i].base;
196 rgnend = rgnbegin + rgn->region[i].size;
198 if ((rgnbegin <= base) && (end <= rgnend))
202 /* Didn't find the region */
206 /* Check to see if we are removing entire region */
207 if ((rgnbegin == base) && (rgnend == end)) {
208 lmb_remove_region(rgn, i);
212 /* Check to see if region is matching at the front */
213 if (rgnbegin == base) {
214 rgn->region[i].base = end;
215 rgn->region[i].size -= size;
219 /* Check to see if the region is matching at the end */
221 rgn->region[i].size -= size;
226 * We need to split the entry - adjust the current one to the
227 * beginging of the hole and add the region after hole.
229 rgn->region[i].size = base - rgn->region[i].base;
230 return lmb_add_region(rgn, end, rgnend - end);
233 long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
235 struct lmb_region *_rgn = &(lmb->reserved);
237 return lmb_add_region(_rgn, base, size);
240 long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
245 for (i=0; i < rgn->cnt; i++) {
246 phys_addr_t rgnbase = rgn->region[i].base;
247 phys_size_t rgnsize = rgn->region[i].size;
248 if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) {
253 return (i < rgn->cnt) ? i : -1;
256 phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align)
258 return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE);
261 phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
265 alloc = __lmb_alloc_base(lmb, size, align, max_addr);
268 printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
274 static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
276 return addr & ~(size - 1);
279 static phys_addr_t lmb_align_up(phys_addr_t addr, ulong size)
281 return (addr + (size - 1)) & ~(size - 1);
284 phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr)
287 phys_addr_t base = 0;
288 phys_addr_t res_base;
290 for (i = lmb->memory.cnt-1; i >= 0; i--) {
291 phys_addr_t lmbbase = lmb->memory.region[i].base;
292 phys_size_t lmbsize = lmb->memory.region[i].size;
296 if (max_addr == LMB_ALLOC_ANYWHERE)
297 base = lmb_align_down(lmbbase + lmbsize - size, align);
298 else if (lmbbase < max_addr) {
299 base = min(lmbbase + lmbsize, max_addr);
300 base = lmb_align_down(base - size, align);
304 while (base && lmbbase <= base) {
305 j = lmb_overlaps_region(&lmb->reserved, base, size);
307 /* This area isn't reserved, take it */
308 if (lmb_add_region(&lmb->reserved, base,
314 res_base = lmb->reserved.region[j].base;
317 base = lmb_align_down(res_base - size, align);
323 int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr)
327 for (i = 0; i < lmb->reserved.cnt; i++) {
328 phys_addr_t upper = lmb->reserved.region[i].base +
329 lmb->reserved.region[i].size - 1;
330 if ((addr >= lmb->reserved.region[i].base) && (addr <= upper))