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/poison.h>
17 #include <linux/memblock.h>
19 struct memblock memblock;
21 static int memblock_debug;
22 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1];
23 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1];
25 #define MEMBLOCK_ERROR (~(phys_addr_t)0)
28 * Address comparison utilities
31 static phys_addr_t memblock_align_down(phys_addr_t addr, phys_addr_t size)
33 return addr & ~(size - 1);
36 static phys_addr_t memblock_align_up(phys_addr_t addr, phys_addr_t size)
38 return (addr + (size - 1)) & ~(size - 1);
41 static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
42 phys_addr_t base2, phys_addr_t size2)
44 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
47 static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1,
48 phys_addr_t base2, phys_addr_t size2)
50 if (base2 == base1 + size1)
52 else if (base1 == base2 + size2)
58 static long memblock_regions_adjacent(struct memblock_type *type,
59 unsigned long r1, unsigned long r2)
61 phys_addr_t base1 = type->regions[r1].base;
62 phys_addr_t size1 = type->regions[r1].size;
63 phys_addr_t base2 = type->regions[r2].base;
64 phys_addr_t size2 = type->regions[r2].size;
66 return memblock_addrs_adjacent(base1, size1, base2, size2);
69 long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
73 for (i = 0; i < type->cnt; i++) {
74 phys_addr_t rgnbase = type->regions[i].base;
75 phys_addr_t rgnsize = type->regions[i].size;
76 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
80 return (i < type->cnt) ? i : -1;
84 * Find, allocate, deallocate or reserve unreserved regions. All allocations
88 static phys_addr_t __init memblock_find_region(phys_addr_t start, phys_addr_t end,
89 phys_addr_t size, phys_addr_t align)
91 phys_addr_t base, res_base;
94 base = memblock_align_down((end - size), align);
95 while (start <= base) {
96 j = memblock_overlaps_region(&memblock.reserved, base, size);
99 res_base = memblock.reserved.regions[j].base;
102 base = memblock_align_down(res_base - size, align);
105 return MEMBLOCK_ERROR;
108 static phys_addr_t __init memblock_find_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
111 phys_addr_t base = 0;
112 phys_addr_t res_base;
116 size = memblock_align_up(size, align);
118 /* Pump up max_addr */
119 if (max_addr == MEMBLOCK_ALLOC_ACCESSIBLE)
120 max_addr = memblock.current_limit;
122 /* We do a top-down search, this tends to limit memory
123 * fragmentation by keeping early boot allocs near the
126 for (i = memblock.memory.cnt - 1; i >= 0; i--) {
127 phys_addr_t memblockbase = memblock.memory.regions[i].base;
128 phys_addr_t memblocksize = memblock.memory.regions[i].size;
130 if (memblocksize < size)
132 base = min(memblockbase + memblocksize, max_addr);
133 res_base = memblock_find_region(memblockbase, base, size, align);
134 if (res_base != MEMBLOCK_ERROR)
137 return MEMBLOCK_ERROR;
140 static void memblock_remove_region(struct memblock_type *type, unsigned long r)
144 for (i = r; i < type->cnt - 1; i++) {
145 type->regions[i].base = type->regions[i + 1].base;
146 type->regions[i].size = type->regions[i + 1].size;
151 /* Assumption: base addr of region 1 < base addr of region 2 */
152 static void memblock_coalesce_regions(struct memblock_type *type,
153 unsigned long r1, unsigned long r2)
155 type->regions[r1].size += type->regions[r2].size;
156 memblock_remove_region(type, r2);
159 static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
161 unsigned long coalesced = 0;
164 if ((type->cnt == 1) && (type->regions[0].size == 0)) {
165 type->regions[0].base = base;
166 type->regions[0].size = size;
170 /* First try and coalesce this MEMBLOCK with another. */
171 for (i = 0; i < type->cnt; i++) {
172 phys_addr_t rgnbase = type->regions[i].base;
173 phys_addr_t rgnsize = type->regions[i].size;
175 if ((rgnbase == base) && (rgnsize == size))
176 /* Already have this region, so we're done */
179 adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize);
181 type->regions[i].base -= size;
182 type->regions[i].size += size;
185 } else if (adjacent < 0) {
186 type->regions[i].size += size;
192 if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1)) {
193 memblock_coalesce_regions(type, i, i+1);
199 if (type->cnt >= type->max)
202 /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
203 for (i = type->cnt - 1; i >= 0; i--) {
204 if (base < type->regions[i].base) {
205 type->regions[i+1].base = type->regions[i].base;
206 type->regions[i+1].size = type->regions[i].size;
208 type->regions[i+1].base = base;
209 type->regions[i+1].size = size;
214 if (base < type->regions[0].base) {
215 type->regions[0].base = base;
216 type->regions[0].size = size;
223 long memblock_add(phys_addr_t base, phys_addr_t size)
225 return memblock_add_region(&memblock.memory, base, size);
229 static long __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
231 phys_addr_t rgnbegin, rgnend;
232 phys_addr_t end = base + size;
235 rgnbegin = rgnend = 0; /* supress gcc warnings */
237 /* Find the region where (base, size) belongs to */
238 for (i=0; i < type->cnt; i++) {
239 rgnbegin = type->regions[i].base;
240 rgnend = rgnbegin + type->regions[i].size;
242 if ((rgnbegin <= base) && (end <= rgnend))
246 /* Didn't find the region */
250 /* Check to see if we are removing entire region */
251 if ((rgnbegin == base) && (rgnend == end)) {
252 memblock_remove_region(type, i);
256 /* Check to see if region is matching at the front */
257 if (rgnbegin == base) {
258 type->regions[i].base = end;
259 type->regions[i].size -= size;
263 /* Check to see if the region is matching at the end */
265 type->regions[i].size -= size;
270 * We need to split the entry - adjust the current one to the
271 * beginging of the hole and add the region after hole.
273 type->regions[i].size = base - type->regions[i].base;
274 return memblock_add_region(type, end, rgnend - end);
277 long memblock_remove(phys_addr_t base, phys_addr_t size)
279 return __memblock_remove(&memblock.memory, base, size);
282 long __init memblock_free(phys_addr_t base, phys_addr_t size)
284 return __memblock_remove(&memblock.reserved, base, size);
287 long __init memblock_reserve(phys_addr_t base, phys_addr_t size)
289 struct memblock_type *_rgn = &memblock.reserved;
293 return memblock_add_region(_rgn, base, size);
296 phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
300 /* We align the size to limit fragmentation. Without this, a lot of
301 * small allocs quickly eat up the whole reserve array on sparc
303 size = memblock_align_up(size, align);
305 found = memblock_find_base(size, align, max_addr);
306 if (found != MEMBLOCK_ERROR &&
307 memblock_add_region(&memblock.reserved, found, size) >= 0)
313 phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
317 alloc = __memblock_alloc_base(size, align, max_addr);
320 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
321 (unsigned long long) size, (unsigned long long) max_addr);
326 phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
328 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
333 * Additional node-local allocators. Search for node memory is bottom up
334 * and walks memblock regions within that node bottom-up as well, but allocation
335 * within an memblock region is top-down.
338 phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
345 static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
347 phys_addr_t align, int nid)
349 phys_addr_t start, end;
352 end = start + mp->size;
354 start = memblock_align_up(start, align);
355 while (start < end) {
356 phys_addr_t this_end;
359 this_end = memblock_nid_range(start, end, &this_nid);
360 if (this_nid == nid) {
361 phys_addr_t ret = memblock_find_region(start, this_end, size, align);
362 if (ret != MEMBLOCK_ERROR &&
363 memblock_add_region(&memblock.reserved, ret, size) >= 0)
369 return MEMBLOCK_ERROR;
372 phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
374 struct memblock_type *mem = &memblock.memory;
379 /* We align the size to limit fragmentation. Without this, a lot of
380 * small allocs quickly eat up the whole reserve array on sparc
382 size = memblock_align_up(size, align);
384 /* We do a bottom-up search for a region with the right
385 * nid since that's easier considering how memblock_nid_range()
388 for (i = 0; i < mem->cnt; i++) {
389 phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
391 if (ret != MEMBLOCK_ERROR)
395 return memblock_alloc(size, align);
398 /* You must call memblock_analyze() before this. */
399 phys_addr_t __init memblock_phys_mem_size(void)
401 return memblock.memory_size;
404 phys_addr_t memblock_end_of_DRAM(void)
406 int idx = memblock.memory.cnt - 1;
408 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
411 /* You must call memblock_analyze() after this. */
412 void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
416 struct memblock_region *p;
421 /* Truncate the memblock regions to satisfy the memory limit. */
422 limit = memory_limit;
423 for (i = 0; i < memblock.memory.cnt; i++) {
424 if (limit > memblock.memory.regions[i].size) {
425 limit -= memblock.memory.regions[i].size;
429 memblock.memory.regions[i].size = limit;
430 memblock.memory.cnt = i + 1;
434 memory_limit = memblock_end_of_DRAM();
436 /* And truncate any reserves above the limit also. */
437 for (i = 0; i < memblock.reserved.cnt; i++) {
438 p = &memblock.reserved.regions[i];
440 if (p->base > memory_limit)
442 else if ((p->base + p->size) > memory_limit)
443 p->size = memory_limit - p->base;
446 memblock_remove_region(&memblock.reserved, i);
452 static int memblock_search(struct memblock_type *type, phys_addr_t addr)
454 unsigned int left = 0, right = type->cnt;
457 unsigned int mid = (right + left) / 2;
459 if (addr < type->regions[mid].base)
461 else if (addr >= (type->regions[mid].base +
462 type->regions[mid].size))
466 } while (left < right);
470 int __init memblock_is_reserved(phys_addr_t addr)
472 return memblock_search(&memblock.reserved, addr) != -1;
475 int memblock_is_memory(phys_addr_t addr)
477 return memblock_search(&memblock.memory, addr) != -1;
480 int memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
482 int idx = memblock_search(&memblock.reserved, base);
486 return memblock.reserved.regions[idx].base <= base &&
487 (memblock.reserved.regions[idx].base +
488 memblock.reserved.regions[idx].size) >= (base + size);
491 int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
493 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
497 void __init memblock_set_current_limit(phys_addr_t limit)
499 memblock.current_limit = limit;
502 static void memblock_dump(struct memblock_type *region, char *name)
504 unsigned long long base, size;
507 pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
509 for (i = 0; i < region->cnt; i++) {
510 base = region->regions[i].base;
511 size = region->regions[i].size;
513 pr_info(" %s[0x%x]\t0x%016llx - 0x%016llx, 0x%llx bytes\n",
514 name, i, base, base + size - 1, size);
518 void memblock_dump_all(void)
523 pr_info("MEMBLOCK configuration:\n");
524 pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
526 memblock_dump(&memblock.memory, "memory");
527 memblock_dump(&memblock.reserved, "reserved");
530 void __init memblock_analyze(void)
534 /* Check marker in the unused last array entry */
535 WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
536 != (phys_addr_t)RED_INACTIVE);
537 WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
538 != (phys_addr_t)RED_INACTIVE);
540 memblock.memory_size = 0;
542 for (i = 0; i < memblock.memory.cnt; i++)
543 memblock.memory_size += memblock.memory.regions[i].size;
546 void __init memblock_init(void)
548 /* Hookup the initial arrays */
549 memblock.memory.regions = memblock_memory_init_regions;
550 memblock.memory.max = INIT_MEMBLOCK_REGIONS;
551 memblock.reserved.regions = memblock_reserved_init_regions;
552 memblock.reserved.max = INIT_MEMBLOCK_REGIONS;
554 /* Write a marker in the unused last array entry */
555 memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
556 memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
558 /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
559 * This simplifies the memblock_add() code below...
561 memblock.memory.regions[0].base = 0;
562 memblock.memory.regions[0].size = 0;
563 memblock.memory.cnt = 1;
566 memblock.reserved.regions[0].base = 0;
567 memblock.reserved.regions[0].size = 0;
568 memblock.reserved.cnt = 1;
570 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
573 static int __init early_memblock(char *p)
575 if (p && strstr(p, "debug"))
579 early_param("memblock", early_memblock);