13 #include <linux/kernel.h>
14 #include <linux/slab.h>
16 #include <linux/bitops.h>
17 #include <linux/poison.h>
23 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
24 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
26 struct memblock memblock __initdata_memblock = {
27 .memory.regions = memblock_memory_init_regions,
29 .memory.max = INIT_MEMBLOCK_REGIONS,
31 .reserved.regions = memblock_reserved_init_regions,
33 .reserved.max = INIT_MEMBLOCK_REGIONS,
35 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
38 int memblock_debug __initdata_memblock;
39 static int memblock_can_resize __initdata_memblock;
40 static int memblock_memory_in_slab __initdata_memblock = 0;
41 static int memblock_reserved_in_slab __initdata_memblock = 0;
44 static __init_memblock
const char *
45 memblock_type_name(
struct memblock_type *
type)
47 if (type == &memblock.memory)
49 else if (type == &memblock.reserved)
67 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
70 static long __init_memblock memblock_overlaps_region(
struct memblock_type *type,
75 for (i = 0; i < type->cnt; i++) {
78 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
82 return (i < type->
cnt) ? i : -1;
106 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
107 end = memblock.current_limit;
111 end =
max(start, end);
113 for_each_free_mem_range_reverse(i, nid, &this_start, &this_end,
NULL) {
114 this_start =
clamp(this_start, start, end);
115 this_end =
clamp(this_end, start, end);
121 if (cand >= this_start)
147 static void __init_memblock memblock_remove_region(
struct memblock_type *type,
unsigned long r)
149 type->total_size -= type->regions[
r].size;
150 memmove(&type->regions[r], &type->regions[r + 1],
151 (type->cnt - (r + 1)) *
sizeof(type->regions[r]));
155 if (type->cnt == 0) {
156 WARN_ON(type->total_size != 0);
158 type->regions[0].base = 0;
159 type->regions[0].size = 0;
160 memblock_set_region_node(&type->regions[0],
MAX_NUMNODES);
167 if (memblock.reserved.regions == memblock_reserved_init_regions)
170 *addr =
__pa(memblock.reserved.regions);
172 return PAGE_ALIGN(
sizeof(
struct memblock_region) *
173 memblock.reserved.max);
191 static int __init_memblock memblock_double_array(
struct memblock_type *type,
195 struct memblock_region *new_array, *old_array;
204 if (!memblock_can_resize)
208 old_size = type->max *
sizeof(
struct memblock_region);
209 new_size = old_size << 1;
218 if (type == &memblock.memory)
219 in_slab = &memblock_memory_in_slab;
221 in_slab = &memblock_reserved_in_slab;
236 addr = new_array ?
__pa(new_array) : 0;
239 if (type != &memblock.reserved)
240 new_area_start = new_area_size = 0;
243 memblock.current_limit,
245 if (!addr && new_area_size)
247 min(new_area_start, memblock.current_limit),
250 new_array = addr ?
__va(addr) :
NULL;
253 pr_err(
"memblock: Failed to double %s array from %ld to %ld entries !\n",
254 memblock_type_name(type), type->max, type->max * 2);
258 memblock_dbg(
"memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
259 memblock_type_name(type), type->max * 2, (
u64)addr,
260 (
u64)addr + new_size - 1);
267 memcpy(new_array, type->regions, old_size);
268 memset(new_array + type->max, 0, old_size);
269 old_array = type->regions;
270 type->regions = new_array;
276 else if (old_array != memblock_memory_init_regions &&
277 old_array != memblock_reserved_init_regions)
299 static void __init_memblock memblock_merge_regions(
struct memblock_type *type)
304 while (i < type->
cnt - 1) {
305 struct memblock_region *
this = &type->regions[
i];
306 struct memblock_region *
next = &type->regions[i + 1];
308 if (this->base + this->size != next->base ||
309 memblock_get_region_node(
this) !=
310 memblock_get_region_node(next)) {
311 BUG_ON(this->base + this->size > next->base);
316 this->size += next->size;
317 memmove(next, next + 1, (type->cnt - (i + 1)) *
sizeof(*next));
332 static void __init_memblock memblock_insert_region(
struct memblock_type *type,
336 struct memblock_region *rgn = &type->regions[
idx];
338 BUG_ON(type->cnt >= type->max);
339 memmove(rgn + 1, rgn, (type->cnt - idx) *
sizeof(*rgn));
342 memblock_set_region_node(rgn, nid);
344 type->total_size +=
size;
362 static int __init_memblock memblock_add_region(
struct memblock_type *type,
374 if (type->regions[0].size == 0) {
375 WARN_ON(type->cnt != 1 || type->total_size);
376 type->regions[0].base = base;
377 type->regions[0].size =
size;
378 memblock_set_region_node(&type->regions[0], nid);
379 type->total_size =
size;
391 for (i = 0; i < type->cnt; i++) {
392 struct memblock_region *rgn = &type->regions[
i];
407 memblock_insert_region(type, i++, base,
411 base =
min(rend, end);
418 memblock_insert_region(type, i, base, end - base, nid);
426 while (type->cnt + nr_new > type->max)
427 if (memblock_double_array(type, obase, size) < 0)
432 memblock_merge_regions(type);
440 return memblock_add_region(&memblock.memory, base, size, nid);
445 return memblock_add_region(&memblock.memory, base, size,
MAX_NUMNODES);
464 static int __init_memblock memblock_isolate_range(
struct memblock_type *type,
466 int *start_rgn,
int *end_rgn)
468 phys_addr_t end = base + memblock_cap_size(base, &size);
471 *start_rgn = *end_rgn = 0;
477 while (type->cnt + 2 > type->max)
478 if (memblock_double_array(type, base, size) < 0)
481 for (i = 0; i < type->cnt; i++) {
482 struct memblock_region *rgn = &type->regions[
i];
497 rgn->size -= base - rbase;
498 type->total_size -= base - rbase;
499 memblock_insert_region(type, i, rbase, base - rbase,
500 memblock_get_region_node(rgn));
501 }
else if (rend > end) {
507 rgn->size -= end - rbase;
508 type->total_size -= end - rbase;
509 memblock_insert_region(type, i--, rbase, end - rbase,
510 memblock_get_region_node(rgn));
522 static int __init_memblock __memblock_remove(
struct memblock_type *type,
525 int start_rgn, end_rgn;
528 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
532 for (i = end_rgn - 1; i >= start_rgn; i--)
533 memblock_remove_region(type, i);
539 return __memblock_remove(&memblock.memory, base, size);
544 memblock_dbg(
" memblock_free: [%#016llx-%#016llx] %pF\n",
545 (
unsigned long long)base,
546 (
unsigned long long)base + size,
549 return __memblock_remove(&memblock.reserved, base, size);
554 struct memblock_type *_rgn = &memblock.reserved;
556 memblock_dbg(
"memblock_reserve: [%#016llx-%#016llx] %pF\n",
557 (
unsigned long long)base,
558 (
unsigned long long)base + size,
561 return memblock_add_region(_rgn, base, size,
MAX_NUMNODES);
591 struct memblock_type *
mem = &memblock.memory;
592 struct memblock_type *
rsv = &memblock.reserved;
593 int mi = *idx & 0xffffffff;
596 for ( ; mi < mem->cnt; mi++) {
597 struct memblock_region *
m = &mem->regions[mi];
602 if (nid !=
MAX_NUMNODES && nid != memblock_get_region_node(m))
606 for ( ; ri < rsv->cnt + 1; ri++) {
607 struct memblock_region *r = &rsv->regions[ri];
608 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
612 if (r_start >= m_end)
615 if (m_start < r_end) {
617 *out_start =
max(m_start, r_start);
619 *out_end =
min(m_end, r_end);
621 *out_nid = memblock_get_region_node(m);
630 *idx = (
u32)mi | (
u64)ri << 32;
654 struct memblock_type *
mem = &memblock.memory;
655 struct memblock_type *
rsv = &memblock.reserved;
656 int mi = *idx & 0xffffffff;
664 for ( ; mi >= 0; mi--) {
665 struct memblock_region *
m = &mem->regions[mi];
670 if (nid !=
MAX_NUMNODES && nid != memblock_get_region_node(m))
674 for ( ; ri >= 0; ri--) {
675 struct memblock_region *r = &rsv->regions[ri];
676 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
680 if (r_end <= m_start)
683 if (m_end > r_start) {
685 *out_start =
max(m_start, r_start);
687 *out_end =
min(m_end, r_end);
689 *out_nid = memblock_get_region_node(m);
691 if (m_start >= r_start)
695 *idx = (
u32)mi | (
u64)ri << 32;
704 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
708 void __init_memblock __next_mem_pfn_range(
int *idx,
int nid,
709 unsigned long *out_start_pfn,
710 unsigned long *out_end_pfn,
int *out_nid)
712 struct memblock_type *type = &memblock.memory;
713 struct memblock_region *
r;
715 while (++*idx < type->
cnt) {
716 r = &type->regions[*
idx];
723 if (*idx >= type->cnt) {
729 *out_start_pfn =
PFN_UP(r->base);
731 *out_end_pfn =
PFN_DOWN(r->base + r->size);
751 struct memblock_type *type = &memblock.memory;
752 int start_rgn, end_rgn;
755 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
759 for (i = start_rgn; i < end_rgn; i++)
760 memblock_set_region_node(&type->regions[i], nid);
762 memblock_merge_regions(type);
785 return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
790 return memblock_alloc_base_nid(size, align, max_addr,
MAX_NUMNODES);
800 panic(
"ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
801 (
unsigned long long) size, (
unsigned long long) max_addr);
827 return memblock.memory.total_size;
833 return memblock.memory.regions[0].base;
838 int idx = memblock.memory.cnt - 1;
840 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
852 for (i = 0; i < memblock.memory.cnt; i++) {
853 struct memblock_region *r = &memblock.memory.regions[
i];
855 if (limit <= r->size) {
856 max_addr = r->base +
limit;
864 __memblock_remove(&memblock.reserved, max_addr, (
phys_addr_t)ULLONG_MAX);
867 static int __init_memblock memblock_search(
struct memblock_type *type,
phys_addr_t addr)
869 unsigned int left = 0,
right = type->cnt;
874 if (addr < type->regions[mid].base)
876 else if (addr >= (type->regions[mid].base +
877 type->regions[mid].size))
881 }
while (left <
right);
887 return memblock_search(&memblock.reserved, addr) != -1;
892 return memblock_search(&memblock.memory, addr) != -1;
907 int idx = memblock_search(&memblock.memory, base);
908 phys_addr_t end = base + memblock_cap_size(base, &size);
912 return memblock.memory.regions[
idx].base <= base &&
913 (memblock.memory.regions[
idx].base +
914 memblock.memory.regions[
idx].size) >= end;
929 memblock_cap_size(base, &size);
930 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
937 struct memblock_type *
mem = &memblock.memory;
939 for (i = 0; i < mem->cnt; i++) {
940 orig_start = mem->regions[
i].base;
941 orig_end = mem->regions[
i].base + mem->regions[
i].
size;
942 start =
round_up(orig_start, align);
945 if (start == orig_start && end == orig_end)
949 mem->regions[
i].base =
start;
950 mem->regions[
i].size = end -
start;
952 memblock_remove_region(mem, i);
960 memblock.current_limit =
limit;
963 static void __init_memblock memblock_dump(
struct memblock_type *type,
char *
name)
965 unsigned long long base,
size;
968 pr_info(
" %s.cnt = 0x%lx\n", name, type->cnt);
970 for (i = 0; i < type->cnt; i++) {
971 struct memblock_region *rgn = &type->regions[
i];
972 char nid_buf[32] =
"";
976 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
978 snprintf(nid_buf,
sizeof(nid_buf),
" on node %d",
979 memblock_get_region_node(rgn));
981 pr_info(
" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
982 name, i, base, base + size - 1, size, nid_buf);
988 pr_info(
"MEMBLOCK configuration:\n");
989 pr_info(
" memory size = %#llx reserved size = %#llx\n",
990 (
unsigned long long)memblock.memory.total_size,
991 (
unsigned long long)memblock.reserved.total_size);
993 memblock_dump(&memblock.memory,
"memory");
994 memblock_dump(&memblock.reserved,
"reserved");
999 memblock_can_resize = 1;
1002 static int __init early_memblock(
char *
p)
1004 if (p &&
strstr(p,
"debug"))
1010 #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
1012 static int memblock_debug_show(
struct seq_file *
m,
void *
private)
1014 struct memblock_type *type = m->
private;
1015 struct memblock_region *
reg;
1018 for (i = 0; i < type->cnt; i++) {
1019 reg = &type->regions[
i];
1023 (
unsigned long)reg->base,
1024 (
unsigned long)(reg->base + reg->size - 1));
1027 (
unsigned long long)reg->base,
1028 (
unsigned long long)(reg->base + reg->size - 1));
1040 .
open = memblock_debug_open,
1046 static int __init memblock_init_debugfs(
void)