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.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/bitops.h>
16 #include <linux/lmb.h>
18 #define LMB_ALLOC_ANYWHERE 0
24 static int __init early_lmb(char *p)
26 if (p && strstr(p, "debug"))
30 early_param("lmb", early_lmb);
32 static void lmb_dump(struct lmb_region *region, char *name)
34 unsigned long long base, size;
37 pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
39 for (i = 0; i < region->cnt; i++) {
40 base = region->region[i].base;
41 size = region->region[i].size;
43 pr_info(" %s[0x%x]\t0x%016llx - 0x%016llx, 0x%llx bytes\n",
44 name, i, base, base + size - 1, size);
48 void lmb_dump_all(void)
53 pr_info("LMB configuration:\n");
54 pr_info(" rmo_size = 0x%llx\n", (unsigned long long)lmb.rmo_size);
55 pr_info(" memory.size = 0x%llx\n", (unsigned long long)lmb.memory.size);
57 lmb_dump(&lmb.memory, "memory");
58 lmb_dump(&lmb.reserved, "reserved");
61 static unsigned long lmb_addrs_overlap(u64 base1, u64 size1, u64 base2,
64 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
67 static long lmb_addrs_adjacent(u64 base1, u64 size1, u64 base2, u64 size2)
69 if (base2 == base1 + size1)
71 else if (base1 == base2 + size2)
77 static long lmb_regions_adjacent(struct lmb_region *rgn,
78 unsigned long r1, unsigned long r2)
80 u64 base1 = rgn->region[r1].base;
81 u64 size1 = rgn->region[r1].size;
82 u64 base2 = rgn->region[r2].base;
83 u64 size2 = rgn->region[r2].size;
85 return lmb_addrs_adjacent(base1, size1, base2, size2);
88 static void lmb_remove_region(struct lmb_region *rgn, unsigned long r)
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;
99 /* Assumption: base addr of region 1 < base addr of region 2 */
100 static void lmb_coalesce_regions(struct lmb_region *rgn,
101 unsigned long r1, unsigned long r2)
103 rgn->region[r1].size += rgn->region[r2].size;
104 lmb_remove_region(rgn, r2);
107 void __init lmb_init(void)
109 /* Create a dummy zero size LMB which will get coalesced away later.
110 * This simplifies the lmb_add() code below...
112 lmb.memory.region[0].base = 0;
113 lmb.memory.region[0].size = 0;
117 lmb.reserved.region[0].base = 0;
118 lmb.reserved.region[0].size = 0;
119 lmb.reserved.cnt = 1;
122 void __init lmb_analyze(void)
128 for (i = 0; i < lmb.memory.cnt; i++)
129 lmb.memory.size += lmb.memory.region[i].size;
132 static long lmb_add_region(struct lmb_region *rgn, u64 base, u64 size)
134 unsigned long coalesced = 0;
137 if ((rgn->cnt == 1) && (rgn->region[0].size == 0)) {
138 rgn->region[0].base = base;
139 rgn->region[0].size = size;
143 /* First try and coalesce this LMB with another. */
144 for (i = 0; i < rgn->cnt; i++) {
145 u64 rgnbase = rgn->region[i].base;
146 u64 rgnsize = rgn->region[i].size;
148 if ((rgnbase == base) && (rgnsize == size))
149 /* Already have this region, so we're done */
152 adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
154 rgn->region[i].base -= size;
155 rgn->region[i].size += size;
158 } else if (adjacent < 0) {
159 rgn->region[i].size += size;
165 if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i+1)) {
166 lmb_coalesce_regions(rgn, i, i+1);
172 if (rgn->cnt >= MAX_LMB_REGIONS)
175 /* Couldn't coalesce the LMB, so add it to the sorted table. */
176 for (i = rgn->cnt - 1; i >= 0; i--) {
177 if (base < rgn->region[i].base) {
178 rgn->region[i+1].base = rgn->region[i].base;
179 rgn->region[i+1].size = rgn->region[i].size;
181 rgn->region[i+1].base = base;
182 rgn->region[i+1].size = size;
187 if (base < rgn->region[0].base) {
188 rgn->region[0].base = base;
189 rgn->region[0].size = size;
196 long lmb_add(u64 base, u64 size)
198 struct lmb_region *_rgn = &lmb.memory;
200 /* On pSeries LPAR systems, the first LMB is our RMO region. */
204 return lmb_add_region(_rgn, base, size);
208 long lmb_remove(u64 base, u64 size)
210 struct lmb_region *rgn = &(lmb.memory);
211 u64 rgnbegin, rgnend;
212 u64 end = base + size;
215 rgnbegin = rgnend = 0; /* supress gcc warnings */
217 /* Find the region where (base, size) belongs to */
218 for (i=0; i < rgn->cnt; i++) {
219 rgnbegin = rgn->region[i].base;
220 rgnend = rgnbegin + rgn->region[i].size;
222 if ((rgnbegin <= base) && (end <= rgnend))
226 /* Didn't find the region */
230 /* Check to see if we are removing entire region */
231 if ((rgnbegin == base) && (rgnend == end)) {
232 lmb_remove_region(rgn, i);
236 /* Check to see if region is matching at the front */
237 if (rgnbegin == base) {
238 rgn->region[i].base = end;
239 rgn->region[i].size -= size;
243 /* Check to see if the region is matching at the end */
245 rgn->region[i].size -= size;
250 * We need to split the entry - adjust the current one to the
251 * beginging of the hole and add the region after hole.
253 rgn->region[i].size = base - rgn->region[i].base;
254 return lmb_add_region(rgn, end, rgnend - end);
257 long __init lmb_reserve(u64 base, u64 size)
259 struct lmb_region *_rgn = &lmb.reserved;
263 return lmb_add_region(_rgn, base, size);
266 long __init lmb_overlaps_region(struct lmb_region *rgn, u64 base, u64 size)
270 for (i = 0; i < rgn->cnt; i++) {
271 u64 rgnbase = rgn->region[i].base;
272 u64 rgnsize = rgn->region[i].size;
273 if (lmb_addrs_overlap(base, size, rgnbase, rgnsize))
277 return (i < rgn->cnt) ? i : -1;
280 static u64 lmb_align_down(u64 addr, u64 size)
282 return addr & ~(size - 1);
285 static u64 lmb_align_up(u64 addr, u64 size)
287 return (addr + (size - 1)) & ~(size - 1);
290 static u64 __init lmb_alloc_nid_unreserved(u64 start, u64 end,
296 base = lmb_align_down((end - size), align);
297 while (start <= base) {
298 j = lmb_overlaps_region(&lmb.reserved, base, size);
300 /* this area isn't reserved, take it */
301 if (lmb_add_region(&lmb.reserved, base, size) < 0)
305 res_base = lmb.reserved.region[j].base;
308 base = lmb_align_down(res_base - size, align);
314 static u64 __init lmb_alloc_nid_region(struct lmb_property *mp,
315 u64 (*nid_range)(u64, u64, int *),
316 u64 size, u64 align, int nid)
321 end = start + mp->size;
323 start = lmb_align_up(start, align);
324 while (start < end) {
328 this_end = nid_range(start, end, &this_nid);
329 if (this_nid == nid) {
330 u64 ret = lmb_alloc_nid_unreserved(start, this_end,
341 u64 __init lmb_alloc_nid(u64 size, u64 align, int nid,
342 u64 (*nid_range)(u64 start, u64 end, int *nid))
344 struct lmb_region *mem = &lmb.memory;
349 size = lmb_align_up(size, align);
351 for (i = 0; i < mem->cnt; i++) {
352 u64 ret = lmb_alloc_nid_region(&mem->region[i],
359 return lmb_alloc(size, align);
362 u64 __init lmb_alloc(u64 size, u64 align)
364 return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE);
367 u64 __init lmb_alloc_base(u64 size, u64 align, u64 max_addr)
371 alloc = __lmb_alloc_base(size, align, max_addr);
374 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
375 (unsigned long long) size, (unsigned long long) max_addr);
380 u64 __init __lmb_alloc_base(u64 size, u64 align, u64 max_addr)
388 size = lmb_align_up(size, align);
390 /* On some platforms, make sure we allocate lowmem */
391 /* Note that LMB_REAL_LIMIT may be LMB_ALLOC_ANYWHERE */
392 if (max_addr == LMB_ALLOC_ANYWHERE)
393 max_addr = LMB_REAL_LIMIT;
395 for (i = lmb.memory.cnt - 1; i >= 0; i--) {
396 u64 lmbbase = lmb.memory.region[i].base;
397 u64 lmbsize = lmb.memory.region[i].size;
401 if (max_addr == LMB_ALLOC_ANYWHERE)
402 base = lmb_align_down(lmbbase + lmbsize - size, align);
403 else if (lmbbase < max_addr) {
404 base = min(lmbbase + lmbsize, max_addr);
405 base = lmb_align_down(base - size, align);
409 while (base && lmbbase <= base) {
410 j = lmb_overlaps_region(&lmb.reserved, base, size);
412 /* this area isn't reserved, take it */
413 if (lmb_add_region(&lmb.reserved, base, size) < 0)
417 res_base = lmb.reserved.region[j].base;
420 base = lmb_align_down(res_base - size, align);
426 /* You must call lmb_analyze() before this. */
427 u64 __init lmb_phys_mem_size(void)
429 return lmb.memory.size;
432 u64 lmb_end_of_DRAM(void)
434 int idx = lmb.memory.cnt - 1;
436 return (lmb.memory.region[idx].base + lmb.memory.region[idx].size);
439 /* You must call lmb_analyze() after this. */
440 void __init lmb_enforce_memory_limit(u64 memory_limit)
444 struct lmb_property *p;
449 /* Truncate the lmb regions to satisfy the memory limit. */
450 limit = memory_limit;
451 for (i = 0; i < lmb.memory.cnt; i++) {
452 if (limit > lmb.memory.region[i].size) {
453 limit -= lmb.memory.region[i].size;
457 lmb.memory.region[i].size = limit;
458 lmb.memory.cnt = i + 1;
462 if (lmb.memory.region[0].size < lmb.rmo_size)
463 lmb.rmo_size = lmb.memory.region[0].size;
465 memory_limit = lmb_end_of_DRAM();
467 /* And truncate any reserves above the limit also. */
468 for (i = 0; i < lmb.reserved.cnt; i++) {
469 p = &lmb.reserved.region[i];
471 if (p->base > memory_limit)
473 else if ((p->base + p->size) > memory_limit)
474 p->size = memory_limit - p->base;
477 lmb_remove_region(&lmb.reserved, i);
483 int __init lmb_is_reserved(u64 addr)
487 for (i = 0; i < lmb.reserved.cnt; i++) {
488 u64 upper = lmb.reserved.region[i].base +
489 lmb.reserved.region[i].size - 1;
490 if ((addr >= lmb.reserved.region[i].base) && (addr <= upper))
497 * Given a <base, len>, find which memory regions belong to this range.
498 * Adjust the request and return a contiguous chunk.
500 int lmb_find(struct lmb_property *res)
506 rend = rstart + res->size - 1;
508 for (i = 0; i < lmb.memory.cnt; i++) {
509 u64 start = lmb.memory.region[i].base;
510 u64 end = start + lmb.memory.region[i].size - 1;
515 if ((end >= rstart) && (start < rend)) {
516 /* adjust the request */
522 res->size = rend - rstart + 1;