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/slab.h>
15 #include <linux/init.h>
16 #include <linux/bitops.h>
17 #include <linux/poison.h>
18 #include <linux/memblock.h>
20 struct memblock memblock;
22 static int memblock_debug, memblock_can_resize;
23 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1];
24 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1];
26 #define MEMBLOCK_ERROR (~(phys_addr_t)0)
28 /* inline so we don't get a warning when pr_debug is compiled out */
29 static inline const char *memblock_type_name(struct memblock_type *type)
31 if (type == &memblock.memory)
33 else if (type == &memblock.reserved)
40 * Address comparison utilities
43 static phys_addr_t memblock_align_down(phys_addr_t addr, phys_addr_t size)
45 return addr & ~(size - 1);
48 static phys_addr_t memblock_align_up(phys_addr_t addr, phys_addr_t size)
50 return (addr + (size - 1)) & ~(size - 1);
53 static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
54 phys_addr_t base2, phys_addr_t size2)
56 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
59 static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1,
60 phys_addr_t base2, phys_addr_t size2)
62 if (base2 == base1 + size1)
64 else if (base1 == base2 + size2)
70 static long memblock_regions_adjacent(struct memblock_type *type,
71 unsigned long r1, unsigned long r2)
73 phys_addr_t base1 = type->regions[r1].base;
74 phys_addr_t size1 = type->regions[r1].size;
75 phys_addr_t base2 = type->regions[r2].base;
76 phys_addr_t size2 = type->regions[r2].size;
78 return memblock_addrs_adjacent(base1, size1, base2, size2);
81 long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
85 for (i = 0; i < type->cnt; i++) {
86 phys_addr_t rgnbase = type->regions[i].base;
87 phys_addr_t rgnsize = type->regions[i].size;
88 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
92 return (i < type->cnt) ? i : -1;
96 * Find, allocate, deallocate or reserve unreserved regions. All allocations
100 static phys_addr_t __init memblock_find_region(phys_addr_t start, phys_addr_t end,
101 phys_addr_t size, phys_addr_t align)
103 phys_addr_t base, res_base;
106 base = memblock_align_down((end - size), align);
107 while (start <= base) {
108 j = memblock_overlaps_region(&memblock.reserved, base, size);
111 res_base = memblock.reserved.regions[j].base;
114 base = memblock_align_down(res_base - size, align);
117 return MEMBLOCK_ERROR;
120 static phys_addr_t __init memblock_find_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
123 phys_addr_t base = 0;
124 phys_addr_t res_base;
128 size = memblock_align_up(size, align);
130 /* Pump up max_addr */
131 if (max_addr == MEMBLOCK_ALLOC_ACCESSIBLE)
132 max_addr = memblock.current_limit;
134 /* We do a top-down search, this tends to limit memory
135 * fragmentation by keeping early boot allocs near the
138 for (i = memblock.memory.cnt - 1; i >= 0; i--) {
139 phys_addr_t memblockbase = memblock.memory.regions[i].base;
140 phys_addr_t memblocksize = memblock.memory.regions[i].size;
142 if (memblocksize < size)
144 base = min(memblockbase + memblocksize, max_addr);
145 res_base = memblock_find_region(memblockbase, base, size, align);
146 if (res_base != MEMBLOCK_ERROR)
149 return MEMBLOCK_ERROR;
152 static void memblock_remove_region(struct memblock_type *type, unsigned long r)
156 for (i = r; i < type->cnt - 1; i++) {
157 type->regions[i].base = type->regions[i + 1].base;
158 type->regions[i].size = type->regions[i + 1].size;
163 /* Assumption: base addr of region 1 < base addr of region 2 */
164 static void memblock_coalesce_regions(struct memblock_type *type,
165 unsigned long r1, unsigned long r2)
167 type->regions[r1].size += type->regions[r2].size;
168 memblock_remove_region(type, r2);
171 /* Defined below but needed now */
172 static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size);
174 static int memblock_double_array(struct memblock_type *type)
176 struct memblock_region *new_array, *old_array;
177 phys_addr_t old_size, new_size, addr;
178 int use_slab = slab_is_available();
180 /* We don't allow resizing until we know about the reserved regions
181 * of memory that aren't suitable for allocation
183 if (!memblock_can_resize)
186 pr_debug("memblock: %s array full, doubling...", memblock_type_name(type));
188 /* Calculate new doubled size */
189 old_size = type->max * sizeof(struct memblock_region);
190 new_size = old_size << 1;
192 /* Try to find some space for it.
194 * WARNING: We assume that either slab_is_available() and we use it or
195 * we use MEMBLOCK for allocations. That means that this is unsafe to use
196 * when bootmem is currently active (unless bootmem itself is implemented
197 * on top of MEMBLOCK which isn't the case yet)
199 * This should however not be an issue for now, as we currently only
200 * call into MEMBLOCK while it's still active, or much later when slab is
201 * active for memory hotplug operations
204 new_array = kmalloc(new_size, GFP_KERNEL);
205 addr = new_array == NULL ? MEMBLOCK_ERROR : __pa(new_array);
207 addr = memblock_find_base(new_size, sizeof(phys_addr_t), MEMBLOCK_ALLOC_ACCESSIBLE);
208 if (addr == MEMBLOCK_ERROR) {
209 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
210 memblock_type_name(type), type->max, type->max * 2);
213 new_array = __va(addr);
215 /* Found space, we now need to move the array over before
216 * we add the reserved region since it may be our reserved
217 * array itself that is full.
219 memcpy(new_array, type->regions, old_size);
220 memset(new_array + type->max, 0, old_size);
221 old_array = type->regions;
222 type->regions = new_array;
225 /* If we use SLAB that's it, we are done */
229 /* Add the new reserved region now. Should not fail ! */
230 BUG_ON(memblock_add_region(&memblock.reserved, addr, new_size) < 0);
232 /* If the array wasn't our static init one, then free it. We only do
233 * that before SLAB is available as later on, we don't know whether
234 * to use kfree or free_bootmem_pages(). Shouldn't be a big deal
237 if (old_array != memblock_memory_init_regions &&
238 old_array != memblock_reserved_init_regions)
239 memblock_free(__pa(old_array), old_size);
244 static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
246 unsigned long coalesced = 0;
249 if ((type->cnt == 1) && (type->regions[0].size == 0)) {
250 type->regions[0].base = base;
251 type->regions[0].size = size;
255 /* First try and coalesce this MEMBLOCK with another. */
256 for (i = 0; i < type->cnt; i++) {
257 phys_addr_t rgnbase = type->regions[i].base;
258 phys_addr_t rgnsize = type->regions[i].size;
260 if ((rgnbase == base) && (rgnsize == size))
261 /* Already have this region, so we're done */
264 adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize);
266 type->regions[i].base -= size;
267 type->regions[i].size += size;
270 } else if (adjacent < 0) {
271 type->regions[i].size += size;
277 if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1)) {
278 memblock_coalesce_regions(type, i, i+1);
285 /* If we are out of space, we fail. It's too late to resize the array
286 * but then this shouldn't have happened in the first place.
288 if (WARN_ON(type->cnt >= type->max))
291 /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
292 for (i = type->cnt - 1; i >= 0; i--) {
293 if (base < type->regions[i].base) {
294 type->regions[i+1].base = type->regions[i].base;
295 type->regions[i+1].size = type->regions[i].size;
297 type->regions[i+1].base = base;
298 type->regions[i+1].size = size;
303 if (base < type->regions[0].base) {
304 type->regions[0].base = base;
305 type->regions[0].size = size;
309 /* The array is full ? Try to resize it. If that fails, we undo
310 * our allocation and return an error
312 if (type->cnt == type->max && memblock_double_array(type)) {
320 long memblock_add(phys_addr_t base, phys_addr_t size)
322 return memblock_add_region(&memblock.memory, base, size);
326 static long __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
328 phys_addr_t rgnbegin, rgnend;
329 phys_addr_t end = base + size;
332 rgnbegin = rgnend = 0; /* supress gcc warnings */
334 /* Find the region where (base, size) belongs to */
335 for (i=0; i < type->cnt; i++) {
336 rgnbegin = type->regions[i].base;
337 rgnend = rgnbegin + type->regions[i].size;
339 if ((rgnbegin <= base) && (end <= rgnend))
343 /* Didn't find the region */
347 /* Check to see if we are removing entire region */
348 if ((rgnbegin == base) && (rgnend == end)) {
349 memblock_remove_region(type, i);
353 /* Check to see if region is matching at the front */
354 if (rgnbegin == base) {
355 type->regions[i].base = end;
356 type->regions[i].size -= size;
360 /* Check to see if the region is matching at the end */
362 type->regions[i].size -= size;
367 * We need to split the entry - adjust the current one to the
368 * beginging of the hole and add the region after hole.
370 type->regions[i].size = base - type->regions[i].base;
371 return memblock_add_region(type, end, rgnend - end);
374 long memblock_remove(phys_addr_t base, phys_addr_t size)
376 return __memblock_remove(&memblock.memory, base, size);
379 long __init memblock_free(phys_addr_t base, phys_addr_t size)
381 return __memblock_remove(&memblock.reserved, base, size);
384 long __init memblock_reserve(phys_addr_t base, phys_addr_t size)
386 struct memblock_type *_rgn = &memblock.reserved;
390 return memblock_add_region(_rgn, base, size);
393 phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
397 /* We align the size to limit fragmentation. Without this, a lot of
398 * small allocs quickly eat up the whole reserve array on sparc
400 size = memblock_align_up(size, align);
402 found = memblock_find_base(size, align, max_addr);
403 if (found != MEMBLOCK_ERROR &&
404 memblock_add_region(&memblock.reserved, found, size) >= 0)
410 phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
414 alloc = __memblock_alloc_base(size, align, max_addr);
417 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
418 (unsigned long long) size, (unsigned long long) max_addr);
423 phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
425 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
430 * Additional node-local allocators. Search for node memory is bottom up
431 * and walks memblock regions within that node bottom-up as well, but allocation
432 * within an memblock region is top-down.
435 phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
442 static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
444 phys_addr_t align, int nid)
446 phys_addr_t start, end;
449 end = start + mp->size;
451 start = memblock_align_up(start, align);
452 while (start < end) {
453 phys_addr_t this_end;
456 this_end = memblock_nid_range(start, end, &this_nid);
457 if (this_nid == nid) {
458 phys_addr_t ret = memblock_find_region(start, this_end, size, align);
459 if (ret != MEMBLOCK_ERROR &&
460 memblock_add_region(&memblock.reserved, ret, size) >= 0)
466 return MEMBLOCK_ERROR;
469 phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
471 struct memblock_type *mem = &memblock.memory;
476 /* We align the size to limit fragmentation. Without this, a lot of
477 * small allocs quickly eat up the whole reserve array on sparc
479 size = memblock_align_up(size, align);
481 /* We do a bottom-up search for a region with the right
482 * nid since that's easier considering how memblock_nid_range()
485 for (i = 0; i < mem->cnt; i++) {
486 phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
488 if (ret != MEMBLOCK_ERROR)
492 return memblock_alloc(size, align);
495 /* You must call memblock_analyze() before this. */
496 phys_addr_t __init memblock_phys_mem_size(void)
498 return memblock.memory_size;
501 phys_addr_t memblock_end_of_DRAM(void)
503 int idx = memblock.memory.cnt - 1;
505 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
508 /* You must call memblock_analyze() after this. */
509 void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
513 struct memblock_region *p;
518 /* Truncate the memblock regions to satisfy the memory limit. */
519 limit = memory_limit;
520 for (i = 0; i < memblock.memory.cnt; i++) {
521 if (limit > memblock.memory.regions[i].size) {
522 limit -= memblock.memory.regions[i].size;
526 memblock.memory.regions[i].size = limit;
527 memblock.memory.cnt = i + 1;
531 memory_limit = memblock_end_of_DRAM();
533 /* And truncate any reserves above the limit also. */
534 for (i = 0; i < memblock.reserved.cnt; i++) {
535 p = &memblock.reserved.regions[i];
537 if (p->base > memory_limit)
539 else if ((p->base + p->size) > memory_limit)
540 p->size = memory_limit - p->base;
543 memblock_remove_region(&memblock.reserved, i);
549 static int memblock_search(struct memblock_type *type, phys_addr_t addr)
551 unsigned int left = 0, right = type->cnt;
554 unsigned int mid = (right + left) / 2;
556 if (addr < type->regions[mid].base)
558 else if (addr >= (type->regions[mid].base +
559 type->regions[mid].size))
563 } while (left < right);
567 int __init memblock_is_reserved(phys_addr_t addr)
569 return memblock_search(&memblock.reserved, addr) != -1;
572 int memblock_is_memory(phys_addr_t addr)
574 return memblock_search(&memblock.memory, addr) != -1;
577 int memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
579 int idx = memblock_search(&memblock.reserved, base);
583 return memblock.reserved.regions[idx].base <= base &&
584 (memblock.reserved.regions[idx].base +
585 memblock.reserved.regions[idx].size) >= (base + size);
588 int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
590 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
594 void __init memblock_set_current_limit(phys_addr_t limit)
596 memblock.current_limit = limit;
599 static void memblock_dump(struct memblock_type *region, char *name)
601 unsigned long long base, size;
604 pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
606 for (i = 0; i < region->cnt; i++) {
607 base = region->regions[i].base;
608 size = region->regions[i].size;
610 pr_info(" %s[0x%x]\t0x%016llx - 0x%016llx, 0x%llx bytes\n",
611 name, i, base, base + size - 1, size);
615 void memblock_dump_all(void)
620 pr_info("MEMBLOCK configuration:\n");
621 pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
623 memblock_dump(&memblock.memory, "memory");
624 memblock_dump(&memblock.reserved, "reserved");
627 void __init memblock_analyze(void)
631 /* Check marker in the unused last array entry */
632 WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
633 != (phys_addr_t)RED_INACTIVE);
634 WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
635 != (phys_addr_t)RED_INACTIVE);
637 memblock.memory_size = 0;
639 for (i = 0; i < memblock.memory.cnt; i++)
640 memblock.memory_size += memblock.memory.regions[i].size;
642 /* We allow resizing from there */
643 memblock_can_resize = 1;
646 void __init memblock_init(void)
648 /* Hookup the initial arrays */
649 memblock.memory.regions = memblock_memory_init_regions;
650 memblock.memory.max = INIT_MEMBLOCK_REGIONS;
651 memblock.reserved.regions = memblock_reserved_init_regions;
652 memblock.reserved.max = INIT_MEMBLOCK_REGIONS;
654 /* Write a marker in the unused last array entry */
655 memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
656 memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
658 /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
659 * This simplifies the memblock_add() code below...
661 memblock.memory.regions[0].base = 0;
662 memblock.memory.regions[0].size = 0;
663 memblock.memory.cnt = 1;
666 memblock.reserved.regions[0].base = 0;
667 memblock.reserved.regions[0].size = 0;
668 memblock.reserved.cnt = 1;
670 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
673 static int __init early_memblock(char *p)
675 if (p && strstr(p, "debug"))
679 early_param("memblock", early_memblock);