1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Procedures for maintaining information about logical memory blocks.
5 * Peter Bergner, IBM Corp. June 2001.
6 * Copyright (C) 2001 Peter Bergner.
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/init.h>
12 #include <linux/bitops.h>
13 #include <linux/poison.h>
14 #include <linux/pfn.h>
15 #include <linux/debugfs.h>
16 #include <linux/kmemleak.h>
17 #include <linux/seq_file.h>
18 #include <linux/memblock.h>
20 #include <asm/sections.h>
25 #define INIT_MEMBLOCK_REGIONS 128
26 #define INIT_PHYSMEM_REGIONS 4
28 #ifndef INIT_MEMBLOCK_RESERVED_REGIONS
29 # define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
33 * DOC: memblock overview
35 * Memblock is a method of managing memory regions during the early
36 * boot period when the usual kernel memory allocators are not up and
39 * Memblock views the system memory as collections of contiguous
40 * regions. There are several types of these collections:
42 * * ``memory`` - describes the physical memory available to the
43 * kernel; this may differ from the actual physical memory installed
44 * in the system, for instance when the memory is restricted with
45 * ``mem=`` command line parameter
46 * * ``reserved`` - describes the regions that were allocated
47 * * ``physmap`` - describes the actual physical memory regardless of
48 * the possible restrictions; the ``physmap`` type is only available
49 * on some architectures.
51 * Each region is represented by :c:type:`struct memblock_region` that
52 * defines the region extents, its attributes and NUMA node id on NUMA
53 * systems. Every memory type is described by the :c:type:`struct
54 * memblock_type` which contains an array of memory regions along with
55 * the allocator metadata. The memory types are nicely wrapped with
56 * :c:type:`struct memblock`. This structure is statically initialzed
57 * at build time. The region arrays for the "memory" and "reserved"
58 * types are initially sized to %INIT_MEMBLOCK_REGIONS and for the
59 * "physmap" type to %INIT_PHYSMEM_REGIONS.
60 * The :c:func:`memblock_allow_resize` enables automatic resizing of
61 * the region arrays during addition of new regions. This feature
62 * should be used with care so that memory allocated for the region
63 * array will not overlap with areas that should be reserved, for
66 * The early architecture setup should tell memblock what the physical
67 * memory layout is by using :c:func:`memblock_add` or
68 * :c:func:`memblock_add_node` functions. The first function does not
69 * assign the region to a NUMA node and it is appropriate for UMA
70 * systems. Yet, it is possible to use it on NUMA systems as well and
71 * assign the region to a NUMA node later in the setup process using
72 * :c:func:`memblock_set_node`. The :c:func:`memblock_add_node`
73 * performs such an assignment directly.
75 * Once memblock is setup the memory can be allocated using one of the
78 * * :c:func:`memblock_phys_alloc*` - these functions return the
79 * **physical** address of the allocated memory
80 * * :c:func:`memblock_alloc*` - these functions return the **virtual**
81 * address of the allocated memory.
83 * Note, that both API variants use implict assumptions about allowed
84 * memory ranges and the fallback methods. Consult the documentation
85 * of :c:func:`memblock_alloc_internal` and
86 * :c:func:`memblock_alloc_range_nid` functions for more elaboarte
89 * As the system boot progresses, the architecture specific
90 * :c:func:`mem_init` function frees all the memory to the buddy page
93 * Unless an architecure enables %CONFIG_ARCH_KEEP_MEMBLOCK, the
94 * memblock data structures will be discarded after the system
95 * initialization compltes.
98 #ifndef CONFIG_NEED_MULTIPLE_NODES
99 struct pglist_data __refdata contig_page_data;
100 EXPORT_SYMBOL(contig_page_data);
103 unsigned long max_low_pfn;
104 unsigned long min_low_pfn;
105 unsigned long max_pfn;
106 unsigned long long max_possible_pfn;
108 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
109 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
110 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
111 static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
114 struct memblock memblock __initdata_memblock = {
115 .memory.regions = memblock_memory_init_regions,
116 .memory.cnt = 1, /* empty dummy entry */
117 .memory.max = INIT_MEMBLOCK_REGIONS,
118 .memory.name = "memory",
120 .reserved.regions = memblock_reserved_init_regions,
121 .reserved.cnt = 1, /* empty dummy entry */
122 .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS,
123 .reserved.name = "reserved",
125 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
126 .physmem.regions = memblock_physmem_init_regions,
127 .physmem.cnt = 1, /* empty dummy entry */
128 .physmem.max = INIT_PHYSMEM_REGIONS,
129 .physmem.name = "physmem",
133 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
136 int memblock_debug __initdata_memblock;
137 static bool system_has_some_mirror __initdata_memblock = false;
138 static int memblock_can_resize __initdata_memblock;
139 static int memblock_memory_in_slab __initdata_memblock = 0;
140 static int memblock_reserved_in_slab __initdata_memblock = 0;
142 static enum memblock_flags __init_memblock choose_memblock_flags(void)
144 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
147 /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
148 static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
150 return *size = min(*size, PHYS_ADDR_MAX - base);
154 * Address comparison utilities
156 static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
157 phys_addr_t base2, phys_addr_t size2)
159 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
162 bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
163 phys_addr_t base, phys_addr_t size)
167 memblock_cap_size(base, &size);
169 for (i = 0; i < type->cnt; i++)
170 if (memblock_addrs_overlap(base, size, type->regions[i].base,
171 type->regions[i].size))
173 return i < type->cnt;
177 * __memblock_find_range_bottom_up - find free area utility in bottom-up
178 * @start: start of candidate range
179 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
180 * %MEMBLOCK_ALLOC_ACCESSIBLE
181 * @size: size of free area to find
182 * @align: alignment of free area to find
183 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
184 * @flags: pick from blocks based on memory attributes
186 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
189 * Found address on success, 0 on failure.
191 static phys_addr_t __init_memblock
192 __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
193 phys_addr_t size, phys_addr_t align, int nid,
194 enum memblock_flags flags)
196 phys_addr_t this_start, this_end, cand;
199 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
200 this_start = clamp(this_start, start, end);
201 this_end = clamp(this_end, start, end);
203 cand = round_up(this_start, align);
204 if (cand < this_end && this_end - cand >= size)
212 * __memblock_find_range_top_down - find free area utility, in top-down
213 * @start: start of candidate range
214 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
215 * %MEMBLOCK_ALLOC_ACCESSIBLE
216 * @size: size of free area to find
217 * @align: alignment of free area to find
218 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
219 * @flags: pick from blocks based on memory attributes
221 * Utility called from memblock_find_in_range_node(), find free area top-down.
224 * Found address on success, 0 on failure.
226 static phys_addr_t __init_memblock
227 __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
228 phys_addr_t size, phys_addr_t align, int nid,
229 enum memblock_flags flags)
231 phys_addr_t this_start, this_end, cand;
234 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
236 this_start = clamp(this_start, start, end);
237 this_end = clamp(this_end, start, end);
242 cand = round_down(this_end - size, align);
243 if (cand >= this_start)
251 * memblock_find_in_range_node - find free area in given range and node
252 * @size: size of free area to find
253 * @align: alignment of free area to find
254 * @start: start of candidate range
255 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
256 * %MEMBLOCK_ALLOC_ACCESSIBLE
257 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
258 * @flags: pick from blocks based on memory attributes
260 * Find @size free area aligned to @align in the specified range and node.
263 * Found address on success, 0 on failure.
265 static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
266 phys_addr_t align, phys_addr_t start,
267 phys_addr_t end, int nid,
268 enum memblock_flags flags)
271 if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
272 end == MEMBLOCK_ALLOC_KASAN)
273 end = memblock.current_limit;
275 /* avoid allocating the first page */
276 start = max_t(phys_addr_t, start, PAGE_SIZE);
277 end = max(start, end);
279 if (memblock_bottom_up())
280 return __memblock_find_range_bottom_up(start, end, size, align,
283 return __memblock_find_range_top_down(start, end, size, align,
288 * memblock_find_in_range - find free area in given range
289 * @start: start of candidate range
290 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
291 * %MEMBLOCK_ALLOC_ACCESSIBLE
292 * @size: size of free area to find
293 * @align: alignment of free area to find
295 * Find @size free area aligned to @align in the specified range.
298 * Found address on success, 0 on failure.
300 phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
301 phys_addr_t end, phys_addr_t size,
305 enum memblock_flags flags = choose_memblock_flags();
308 ret = memblock_find_in_range_node(size, align, start, end,
309 NUMA_NO_NODE, flags);
311 if (!ret && (flags & MEMBLOCK_MIRROR)) {
312 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
314 flags &= ~MEMBLOCK_MIRROR;
321 static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
323 type->total_size -= type->regions[r].size;
324 memmove(&type->regions[r], &type->regions[r + 1],
325 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
328 /* Special case for empty arrays */
329 if (type->cnt == 0) {
330 WARN_ON(type->total_size != 0);
332 type->regions[0].base = 0;
333 type->regions[0].size = 0;
334 type->regions[0].flags = 0;
335 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
339 #ifndef CONFIG_ARCH_KEEP_MEMBLOCK
341 * memblock_discard - discard memory and reserved arrays if they were allocated
343 void __init memblock_discard(void)
345 phys_addr_t addr, size;
347 if (memblock.reserved.regions != memblock_reserved_init_regions) {
348 addr = __pa(memblock.reserved.regions);
349 size = PAGE_ALIGN(sizeof(struct memblock_region) *
350 memblock.reserved.max);
351 if (memblock_reserved_in_slab)
352 kfree(memblock.reserved.regions);
354 __memblock_free_late(addr, size);
357 if (memblock.memory.regions != memblock_memory_init_regions) {
358 addr = __pa(memblock.memory.regions);
359 size = PAGE_ALIGN(sizeof(struct memblock_region) *
360 memblock.memory.max);
361 if (memblock_memory_in_slab)
362 kfree(memblock.memory.regions);
364 __memblock_free_late(addr, size);
370 * memblock_double_array - double the size of the memblock regions array
371 * @type: memblock type of the regions array being doubled
372 * @new_area_start: starting address of memory range to avoid overlap with
373 * @new_area_size: size of memory range to avoid overlap with
375 * Double the size of the @type regions array. If memblock is being used to
376 * allocate memory for a new reserved regions array and there is a previously
377 * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
378 * waiting to be reserved, ensure the memory used by the new array does
382 * 0 on success, -1 on failure.
384 static int __init_memblock memblock_double_array(struct memblock_type *type,
385 phys_addr_t new_area_start,
386 phys_addr_t new_area_size)
388 struct memblock_region *new_array, *old_array;
389 phys_addr_t old_alloc_size, new_alloc_size;
390 phys_addr_t old_size, new_size, addr, new_end;
391 int use_slab = slab_is_available();
394 /* We don't allow resizing until we know about the reserved regions
395 * of memory that aren't suitable for allocation
397 if (!memblock_can_resize)
400 /* Calculate new doubled size */
401 old_size = type->max * sizeof(struct memblock_region);
402 new_size = old_size << 1;
404 * We need to allocated new one align to PAGE_SIZE,
405 * so we can free them completely later.
407 old_alloc_size = PAGE_ALIGN(old_size);
408 new_alloc_size = PAGE_ALIGN(new_size);
410 /* Retrieve the slab flag */
411 if (type == &memblock.memory)
412 in_slab = &memblock_memory_in_slab;
414 in_slab = &memblock_reserved_in_slab;
416 /* Try to find some space for it */
418 new_array = kmalloc(new_size, GFP_KERNEL);
419 addr = new_array ? __pa(new_array) : 0;
421 /* only exclude range when trying to double reserved.regions */
422 if (type != &memblock.reserved)
423 new_area_start = new_area_size = 0;
425 addr = memblock_find_in_range(new_area_start + new_area_size,
426 memblock.current_limit,
427 new_alloc_size, PAGE_SIZE);
428 if (!addr && new_area_size)
429 addr = memblock_find_in_range(0,
430 min(new_area_start, memblock.current_limit),
431 new_alloc_size, PAGE_SIZE);
433 new_array = addr ? __va(addr) : NULL;
436 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
437 type->name, type->max, type->max * 2);
441 new_end = addr + new_size - 1;
442 memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
443 type->name, type->max * 2, &addr, &new_end);
446 * Found space, we now need to move the array over before we add the
447 * reserved region since it may be our reserved array itself that is
450 memcpy(new_array, type->regions, old_size);
451 memset(new_array + type->max, 0, old_size);
452 old_array = type->regions;
453 type->regions = new_array;
456 /* Free old array. We needn't free it if the array is the static one */
459 else if (old_array != memblock_memory_init_regions &&
460 old_array != memblock_reserved_init_regions)
461 memblock_free(__pa(old_array), old_alloc_size);
464 * Reserve the new array if that comes from the memblock. Otherwise, we
468 BUG_ON(memblock_reserve(addr, new_alloc_size));
470 /* Update slab flag */
477 * memblock_merge_regions - merge neighboring compatible regions
478 * @type: memblock type to scan
480 * Scan @type and merge neighboring compatible regions.
482 static void __init_memblock memblock_merge_regions(struct memblock_type *type)
486 /* cnt never goes below 1 */
487 while (i < type->cnt - 1) {
488 struct memblock_region *this = &type->regions[i];
489 struct memblock_region *next = &type->regions[i + 1];
491 if (this->base + this->size != next->base ||
492 memblock_get_region_node(this) !=
493 memblock_get_region_node(next) ||
494 this->flags != next->flags) {
495 BUG_ON(this->base + this->size > next->base);
500 this->size += next->size;
501 /* move forward from next + 1, index of which is i + 2 */
502 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
508 * memblock_insert_region - insert new memblock region
509 * @type: memblock type to insert into
510 * @idx: index for the insertion point
511 * @base: base address of the new region
512 * @size: size of the new region
513 * @nid: node id of the new region
514 * @flags: flags of the new region
516 * Insert new memblock region [@base, @base + @size) into @type at @idx.
517 * @type must already have extra room to accommodate the new region.
519 static void __init_memblock memblock_insert_region(struct memblock_type *type,
520 int idx, phys_addr_t base,
523 enum memblock_flags flags)
525 struct memblock_region *rgn = &type->regions[idx];
527 BUG_ON(type->cnt >= type->max);
528 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
532 memblock_set_region_node(rgn, nid);
534 type->total_size += size;
538 * memblock_add_range - add new memblock region
539 * @type: memblock type to add new region into
540 * @base: base address of the new region
541 * @size: size of the new region
542 * @nid: nid of the new region
543 * @flags: flags of the new region
545 * Add new memblock region [@base, @base + @size) into @type. The new region
546 * is allowed to overlap with existing ones - overlaps don't affect already
547 * existing regions. @type is guaranteed to be minimal (all neighbouring
548 * compatible regions are merged) after the addition.
551 * 0 on success, -errno on failure.
553 int __init_memblock memblock_add_range(struct memblock_type *type,
554 phys_addr_t base, phys_addr_t size,
555 int nid, enum memblock_flags flags)
558 phys_addr_t obase = base;
559 phys_addr_t end = base + memblock_cap_size(base, &size);
561 struct memblock_region *rgn;
566 /* special case for empty array */
567 if (type->regions[0].size == 0) {
568 WARN_ON(type->cnt != 1 || type->total_size);
569 type->regions[0].base = base;
570 type->regions[0].size = size;
571 type->regions[0].flags = flags;
572 memblock_set_region_node(&type->regions[0], nid);
573 type->total_size = size;
578 * The following is executed twice. Once with %false @insert and
579 * then with %true. The first counts the number of regions needed
580 * to accommodate the new area. The second actually inserts them.
585 for_each_memblock_type(idx, type, rgn) {
586 phys_addr_t rbase = rgn->base;
587 phys_addr_t rend = rbase + rgn->size;
594 * @rgn overlaps. If it separates the lower part of new
595 * area, insert that portion.
598 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
599 WARN_ON(nid != memblock_get_region_node(rgn));
601 WARN_ON(flags != rgn->flags);
604 memblock_insert_region(type, idx++, base,
608 /* area below @rend is dealt with, forget about it */
609 base = min(rend, end);
612 /* insert the remaining portion */
616 memblock_insert_region(type, idx, base, end - base,
624 * If this was the first round, resize array and repeat for actual
625 * insertions; otherwise, merge and return.
628 while (type->cnt + nr_new > type->max)
629 if (memblock_double_array(type, obase, size) < 0)
634 memblock_merge_regions(type);
640 * memblock_add_node - add new memblock region within a NUMA node
641 * @base: base address of the new region
642 * @size: size of the new region
643 * @nid: nid of the new region
645 * Add new memblock region [@base, @base + @size) to the "memory"
646 * type. See memblock_add_range() description for mode details
649 * 0 on success, -errno on failure.
651 int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
654 return memblock_add_range(&memblock.memory, base, size, nid, 0);
658 * memblock_add - add new memblock region
659 * @base: base address of the new region
660 * @size: size of the new region
662 * Add new memblock region [@base, @base + @size) to the "memory"
663 * type. See memblock_add_range() description for mode details
666 * 0 on success, -errno on failure.
668 int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
670 phys_addr_t end = base + size - 1;
672 memblock_dbg("memblock_add: [%pa-%pa] %pS\n",
673 &base, &end, (void *)_RET_IP_);
675 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
679 * memblock_isolate_range - isolate given range into disjoint memblocks
680 * @type: memblock type to isolate range for
681 * @base: base of range to isolate
682 * @size: size of range to isolate
683 * @start_rgn: out parameter for the start of isolated region
684 * @end_rgn: out parameter for the end of isolated region
686 * Walk @type and ensure that regions don't cross the boundaries defined by
687 * [@base, @base + @size). Crossing regions are split at the boundaries,
688 * which may create at most two more regions. The index of the first
689 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
692 * 0 on success, -errno on failure.
694 static int __init_memblock memblock_isolate_range(struct memblock_type *type,
695 phys_addr_t base, phys_addr_t size,
696 int *start_rgn, int *end_rgn)
698 phys_addr_t end = base + memblock_cap_size(base, &size);
700 struct memblock_region *rgn;
702 *start_rgn = *end_rgn = 0;
707 /* we'll create at most two more regions */
708 while (type->cnt + 2 > type->max)
709 if (memblock_double_array(type, base, size) < 0)
712 for_each_memblock_type(idx, type, rgn) {
713 phys_addr_t rbase = rgn->base;
714 phys_addr_t rend = rbase + rgn->size;
723 * @rgn intersects from below. Split and continue
724 * to process the next region - the new top half.
727 rgn->size -= base - rbase;
728 type->total_size -= base - rbase;
729 memblock_insert_region(type, idx, rbase, base - rbase,
730 memblock_get_region_node(rgn),
732 } else if (rend > end) {
734 * @rgn intersects from above. Split and redo the
735 * current region - the new bottom half.
738 rgn->size -= end - rbase;
739 type->total_size -= end - rbase;
740 memblock_insert_region(type, idx--, rbase, end - rbase,
741 memblock_get_region_node(rgn),
744 /* @rgn is fully contained, record it */
754 static int __init_memblock memblock_remove_range(struct memblock_type *type,
755 phys_addr_t base, phys_addr_t size)
757 int start_rgn, end_rgn;
760 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
764 for (i = end_rgn - 1; i >= start_rgn; i--)
765 memblock_remove_region(type, i);
769 int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
771 phys_addr_t end = base + size - 1;
773 memblock_dbg("memblock_remove: [%pa-%pa] %pS\n",
774 &base, &end, (void *)_RET_IP_);
776 return memblock_remove_range(&memblock.memory, base, size);
780 * memblock_free - free boot memory block
781 * @base: phys starting address of the boot memory block
782 * @size: size of the boot memory block in bytes
784 * Free boot memory block previously allocated by memblock_alloc_xx() API.
785 * The freeing memory will not be released to the buddy allocator.
787 int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
789 phys_addr_t end = base + size - 1;
791 memblock_dbg(" memblock_free: [%pa-%pa] %pS\n",
792 &base, &end, (void *)_RET_IP_);
794 kmemleak_free_part_phys(base, size);
795 return memblock_remove_range(&memblock.reserved, base, size);
798 int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
800 phys_addr_t end = base + size - 1;
802 memblock_dbg("memblock_reserve: [%pa-%pa] %pS\n",
803 &base, &end, (void *)_RET_IP_);
805 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
809 * memblock_setclr_flag - set or clear flag for a memory region
810 * @base: base address of the region
811 * @size: size of the region
812 * @set: set or clear the flag
813 * @flag: the flag to udpate
815 * This function isolates region [@base, @base + @size), and sets/clears flag
817 * Return: 0 on success, -errno on failure.
819 static int __init_memblock memblock_setclr_flag(phys_addr_t base,
820 phys_addr_t size, int set, int flag)
822 struct memblock_type *type = &memblock.memory;
823 int i, ret, start_rgn, end_rgn;
825 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
829 for (i = start_rgn; i < end_rgn; i++) {
830 struct memblock_region *r = &type->regions[i];
838 memblock_merge_regions(type);
843 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
844 * @base: the base phys addr of the region
845 * @size: the size of the region
847 * Return: 0 on success, -errno on failure.
849 int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
851 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
855 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
856 * @base: the base phys addr of the region
857 * @size: the size of the region
859 * Return: 0 on success, -errno on failure.
861 int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
863 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
867 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
868 * @base: the base phys addr of the region
869 * @size: the size of the region
871 * Return: 0 on success, -errno on failure.
873 int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
875 system_has_some_mirror = true;
877 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
881 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
882 * @base: the base phys addr of the region
883 * @size: the size of the region
885 * Return: 0 on success, -errno on failure.
887 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
889 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
893 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
894 * @base: the base phys addr of the region
895 * @size: the size of the region
897 * Return: 0 on success, -errno on failure.
899 int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
901 return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
905 * __next_reserved_mem_region - next function for for_each_reserved_region()
906 * @idx: pointer to u64 loop variable
907 * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
908 * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
910 * Iterate over all reserved memory regions.
912 void __init_memblock __next_reserved_mem_region(u64 *idx,
913 phys_addr_t *out_start,
914 phys_addr_t *out_end)
916 struct memblock_type *type = &memblock.reserved;
918 if (*idx < type->cnt) {
919 struct memblock_region *r = &type->regions[*idx];
920 phys_addr_t base = r->base;
921 phys_addr_t size = r->size;
926 *out_end = base + size - 1;
932 /* signal end of iteration */
936 static bool should_skip_region(struct memblock_region *m, int nid, int flags)
938 int m_nid = memblock_get_region_node(m);
940 /* only memory regions are associated with nodes, check it */
941 if (nid != NUMA_NO_NODE && nid != m_nid)
944 /* skip hotpluggable memory regions if needed */
945 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
948 /* if we want mirror memory skip non-mirror memory regions */
949 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
952 /* skip nomap memory unless we were asked for it explicitly */
953 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
960 * __next_mem_range - next function for for_each_free_mem_range() etc.
961 * @idx: pointer to u64 loop variable
962 * @nid: node selector, %NUMA_NO_NODE for all nodes
963 * @flags: pick from blocks based on memory attributes
964 * @type_a: pointer to memblock_type from where the range is taken
965 * @type_b: pointer to memblock_type which excludes memory from being taken
966 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
967 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
968 * @out_nid: ptr to int for nid of the range, can be %NULL
970 * Find the first area from *@idx which matches @nid, fill the out
971 * parameters, and update *@idx for the next iteration. The lower 32bit of
972 * *@idx contains index into type_a and the upper 32bit indexes the
973 * areas before each region in type_b. For example, if type_b regions
974 * look like the following,
976 * 0:[0-16), 1:[32-48), 2:[128-130)
978 * The upper 32bit indexes the following regions.
980 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
982 * As both region arrays are sorted, the function advances the two indices
983 * in lockstep and returns each intersection.
985 void __init_memblock __next_mem_range(u64 *idx, int nid,
986 enum memblock_flags flags,
987 struct memblock_type *type_a,
988 struct memblock_type *type_b,
989 phys_addr_t *out_start,
990 phys_addr_t *out_end, int *out_nid)
992 int idx_a = *idx & 0xffffffff;
993 int idx_b = *idx >> 32;
995 if (WARN_ONCE(nid == MAX_NUMNODES,
996 "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
999 for (; idx_a < type_a->cnt; idx_a++) {
1000 struct memblock_region *m = &type_a->regions[idx_a];
1002 phys_addr_t m_start = m->base;
1003 phys_addr_t m_end = m->base + m->size;
1004 int m_nid = memblock_get_region_node(m);
1006 if (should_skip_region(m, nid, flags))
1011 *out_start = m_start;
1017 *idx = (u32)idx_a | (u64)idx_b << 32;
1021 /* scan areas before each reservation */
1022 for (; idx_b < type_b->cnt + 1; idx_b++) {
1023 struct memblock_region *r;
1024 phys_addr_t r_start;
1027 r = &type_b->regions[idx_b];
1028 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1029 r_end = idx_b < type_b->cnt ?
1030 r->base : PHYS_ADDR_MAX;
1033 * if idx_b advanced past idx_a,
1034 * break out to advance idx_a
1036 if (r_start >= m_end)
1038 /* if the two regions intersect, we're done */
1039 if (m_start < r_end) {
1042 max(m_start, r_start);
1044 *out_end = min(m_end, r_end);
1048 * The region which ends first is
1049 * advanced for the next iteration.
1055 *idx = (u32)idx_a | (u64)idx_b << 32;
1061 /* signal end of iteration */
1066 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1068 * @idx: pointer to u64 loop variable
1069 * @nid: node selector, %NUMA_NO_NODE for all nodes
1070 * @flags: pick from blocks based on memory attributes
1071 * @type_a: pointer to memblock_type from where the range is taken
1072 * @type_b: pointer to memblock_type which excludes memory from being taken
1073 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1074 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1075 * @out_nid: ptr to int for nid of the range, can be %NULL
1077 * Finds the next range from type_a which is not marked as unsuitable
1080 * Reverse of __next_mem_range().
1082 void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1083 enum memblock_flags flags,
1084 struct memblock_type *type_a,
1085 struct memblock_type *type_b,
1086 phys_addr_t *out_start,
1087 phys_addr_t *out_end, int *out_nid)
1089 int idx_a = *idx & 0xffffffff;
1090 int idx_b = *idx >> 32;
1092 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1095 if (*idx == (u64)ULLONG_MAX) {
1096 idx_a = type_a->cnt - 1;
1098 idx_b = type_b->cnt;
1103 for (; idx_a >= 0; idx_a--) {
1104 struct memblock_region *m = &type_a->regions[idx_a];
1106 phys_addr_t m_start = m->base;
1107 phys_addr_t m_end = m->base + m->size;
1108 int m_nid = memblock_get_region_node(m);
1110 if (should_skip_region(m, nid, flags))
1115 *out_start = m_start;
1121 *idx = (u32)idx_a | (u64)idx_b << 32;
1125 /* scan areas before each reservation */
1126 for (; idx_b >= 0; idx_b--) {
1127 struct memblock_region *r;
1128 phys_addr_t r_start;
1131 r = &type_b->regions[idx_b];
1132 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1133 r_end = idx_b < type_b->cnt ?
1134 r->base : PHYS_ADDR_MAX;
1136 * if idx_b advanced past idx_a,
1137 * break out to advance idx_a
1140 if (r_end <= m_start)
1142 /* if the two regions intersect, we're done */
1143 if (m_end > r_start) {
1145 *out_start = max(m_start, r_start);
1147 *out_end = min(m_end, r_end);
1150 if (m_start >= r_start)
1154 *idx = (u32)idx_a | (u64)idx_b << 32;
1159 /* signal end of iteration */
1163 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1165 * Common iterator interface used to define for_each_mem_pfn_range().
1167 void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1168 unsigned long *out_start_pfn,
1169 unsigned long *out_end_pfn, int *out_nid)
1171 struct memblock_type *type = &memblock.memory;
1172 struct memblock_region *r;
1174 while (++*idx < type->cnt) {
1175 r = &type->regions[*idx];
1177 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1179 if (nid == MAX_NUMNODES || nid == r->nid)
1182 if (*idx >= type->cnt) {
1188 *out_start_pfn = PFN_UP(r->base);
1190 *out_end_pfn = PFN_DOWN(r->base + r->size);
1196 * memblock_set_node - set node ID on memblock regions
1197 * @base: base of area to set node ID for
1198 * @size: size of area to set node ID for
1199 * @type: memblock type to set node ID for
1200 * @nid: node ID to set
1202 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
1203 * Regions which cross the area boundaries are split as necessary.
1206 * 0 on success, -errno on failure.
1208 int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1209 struct memblock_type *type, int nid)
1211 int start_rgn, end_rgn;
1214 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1218 for (i = start_rgn; i < end_rgn; i++)
1219 memblock_set_region_node(&type->regions[i], nid);
1221 memblock_merge_regions(type);
1224 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1225 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1227 * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
1229 * @idx: pointer to u64 loop variable
1230 * @zone: zone in which all of the memory blocks reside
1231 * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
1232 * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
1234 * This function is meant to be a zone/pfn specific wrapper for the
1235 * for_each_mem_range type iterators. Specifically they are used in the
1236 * deferred memory init routines and as such we were duplicating much of
1237 * this logic throughout the code. So instead of having it in multiple
1238 * locations it seemed like it would make more sense to centralize this to
1239 * one new iterator that does everything they need.
1241 void __init_memblock
1242 __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
1243 unsigned long *out_spfn, unsigned long *out_epfn)
1245 int zone_nid = zone_to_nid(zone);
1246 phys_addr_t spa, epa;
1249 __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1250 &memblock.memory, &memblock.reserved,
1253 while (*idx != U64_MAX) {
1254 unsigned long epfn = PFN_DOWN(epa);
1255 unsigned long spfn = PFN_UP(spa);
1258 * Verify the end is at least past the start of the zone and
1259 * that we have at least one PFN to initialize.
1261 if (zone->zone_start_pfn < epfn && spfn < epfn) {
1262 /* if we went too far just stop searching */
1263 if (zone_end_pfn(zone) <= spfn) {
1269 *out_spfn = max(zone->zone_start_pfn, spfn);
1271 *out_epfn = min(zone_end_pfn(zone), epfn);
1276 __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1277 &memblock.memory, &memblock.reserved,
1281 /* signal end of iteration */
1283 *out_spfn = ULONG_MAX;
1288 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
1291 * memblock_alloc_range_nid - allocate boot memory block
1292 * @size: size of memory block to be allocated in bytes
1293 * @align: alignment of the region and block's size
1294 * @start: the lower bound of the memory region to allocate (phys address)
1295 * @end: the upper bound of the memory region to allocate (phys address)
1296 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1298 * The allocation is performed from memory region limited by
1299 * memblock.current_limit if @max_addr == %MEMBLOCK_ALLOC_ACCESSIBLE.
1301 * If the specified node can not hold the requested memory the
1302 * allocation falls back to any node in the system
1304 * For systems with memory mirroring, the allocation is attempted first
1305 * from the regions with mirroring enabled and then retried from any
1308 * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for
1309 * allocated boot memory block, so that it is never reported as leaks.
1312 * Physical address of allocated memory block on success, %0 on failure.
1314 static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1315 phys_addr_t align, phys_addr_t start,
1316 phys_addr_t end, int nid)
1318 enum memblock_flags flags = choose_memblock_flags();
1321 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1325 /* Can't use WARNs this early in boot on powerpc */
1327 align = SMP_CACHE_BYTES;
1331 found = memblock_find_in_range_node(size, align, start, end, nid,
1333 if (found && !memblock_reserve(found, size))
1336 if (nid != NUMA_NO_NODE) {
1337 found = memblock_find_in_range_node(size, align, start,
1340 if (found && !memblock_reserve(found, size))
1344 if (flags & MEMBLOCK_MIRROR) {
1345 flags &= ~MEMBLOCK_MIRROR;
1346 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1354 /* Skip kmemleak for kasan_init() due to high volume. */
1355 if (end != MEMBLOCK_ALLOC_KASAN)
1357 * The min_count is set to 0 so that memblock allocated
1358 * blocks are never reported as leaks. This is because many
1359 * of these blocks are only referred via the physical
1360 * address which is not looked up by kmemleak.
1362 kmemleak_alloc_phys(found, size, 0, 0);
1368 * memblock_phys_alloc_range - allocate a memory block inside specified range
1369 * @size: size of memory block to be allocated in bytes
1370 * @align: alignment of the region and block's size
1371 * @start: the lower bound of the memory region to allocate (physical address)
1372 * @end: the upper bound of the memory region to allocate (physical address)
1374 * Allocate @size bytes in the between @start and @end.
1376 * Return: physical address of the allocated memory block on success,
1379 phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
1384 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE);
1388 * memblock_phys_alloc_try_nid - allocate a memory block from specified MUMA node
1389 * @size: size of memory block to be allocated in bytes
1390 * @align: alignment of the region and block's size
1391 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1393 * Allocates memory block from the specified NUMA node. If the node
1394 * has no available memory, attempts to allocated from any node in the
1397 * Return: physical address of the allocated memory block on success,
1400 phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1402 return memblock_alloc_range_nid(size, align, 0,
1403 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
1407 * memblock_alloc_internal - allocate boot memory block
1408 * @size: size of memory block to be allocated in bytes
1409 * @align: alignment of the region and block's size
1410 * @min_addr: the lower bound of the memory region to allocate (phys address)
1411 * @max_addr: the upper bound of the memory region to allocate (phys address)
1412 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1414 * Allocates memory block using memblock_alloc_range_nid() and
1415 * converts the returned physical address to virtual.
1417 * The @min_addr limit is dropped if it can not be satisfied and the allocation
1418 * will fall back to memory below @min_addr. Other constraints, such
1419 * as node and mirrored memory will be handled again in
1420 * memblock_alloc_range_nid().
1423 * Virtual address of allocated memory block on success, NULL on failure.
1425 static void * __init memblock_alloc_internal(
1426 phys_addr_t size, phys_addr_t align,
1427 phys_addr_t min_addr, phys_addr_t max_addr,
1433 * Detect any accidental use of these APIs after slab is ready, as at
1434 * this moment memblock may be deinitialized already and its
1435 * internal data may be destroyed (after execution of memblock_free_all)
1437 if (WARN_ON_ONCE(slab_is_available()))
1438 return kzalloc_node(size, GFP_NOWAIT, nid);
1440 if (max_addr > memblock.current_limit)
1441 max_addr = memblock.current_limit;
1443 alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid);
1445 /* retry allocation without lower limit */
1446 if (!alloc && min_addr)
1447 alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid);
1452 return phys_to_virt(alloc);
1456 * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
1457 * memory and without panicking
1458 * @size: size of memory block to be allocated in bytes
1459 * @align: alignment of the region and block's size
1460 * @min_addr: the lower bound of the memory region from where the allocation
1461 * is preferred (phys address)
1462 * @max_addr: the upper bound of the memory region from where the allocation
1463 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1464 * allocate only from memory limited by memblock.current_limit value
1465 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1467 * Public function, provides additional debug information (including caller
1468 * info), if enabled. Does not zero allocated memory, does not panic if request
1469 * cannot be satisfied.
1472 * Virtual address of allocated memory block on success, NULL on failure.
1474 void * __init memblock_alloc_try_nid_raw(
1475 phys_addr_t size, phys_addr_t align,
1476 phys_addr_t min_addr, phys_addr_t max_addr,
1481 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1482 __func__, (u64)size, (u64)align, nid, &min_addr,
1483 &max_addr, (void *)_RET_IP_);
1485 ptr = memblock_alloc_internal(size, align,
1486 min_addr, max_addr, nid);
1487 if (ptr && size > 0)
1488 page_init_poison(ptr, size);
1494 * memblock_alloc_try_nid - allocate boot memory block
1495 * @size: size of memory block to be allocated in bytes
1496 * @align: alignment of the region and block's size
1497 * @min_addr: the lower bound of the memory region from where the allocation
1498 * is preferred (phys address)
1499 * @max_addr: the upper bound of the memory region from where the allocation
1500 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1501 * allocate only from memory limited by memblock.current_limit value
1502 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1504 * Public function, provides additional debug information (including caller
1505 * info), if enabled. This function zeroes the allocated memory.
1508 * Virtual address of allocated memory block on success, NULL on failure.
1510 void * __init memblock_alloc_try_nid(
1511 phys_addr_t size, phys_addr_t align,
1512 phys_addr_t min_addr, phys_addr_t max_addr,
1517 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1518 __func__, (u64)size, (u64)align, nid, &min_addr,
1519 &max_addr, (void *)_RET_IP_);
1520 ptr = memblock_alloc_internal(size, align,
1521 min_addr, max_addr, nid);
1523 memset(ptr, 0, size);
1529 * __memblock_free_late - free pages directly to buddy allocator
1530 * @base: phys starting address of the boot memory block
1531 * @size: size of the boot memory block in bytes
1533 * This is only useful when the memblock allocator has already been torn
1534 * down, but we are still initializing the system. Pages are released directly
1535 * to the buddy allocator.
1537 void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1539 phys_addr_t cursor, end;
1541 end = base + size - 1;
1542 memblock_dbg("%s: [%pa-%pa] %pS\n",
1543 __func__, &base, &end, (void *)_RET_IP_);
1544 kmemleak_free_part_phys(base, size);
1545 cursor = PFN_UP(base);
1546 end = PFN_DOWN(base + size);
1548 for (; cursor < end; cursor++) {
1549 memblock_free_pages(pfn_to_page(cursor), cursor, 0);
1550 totalram_pages_inc();
1555 * Remaining API functions
1558 phys_addr_t __init_memblock memblock_phys_mem_size(void)
1560 return memblock.memory.total_size;
1563 phys_addr_t __init_memblock memblock_reserved_size(void)
1565 return memblock.reserved.total_size;
1568 phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1570 unsigned long pages = 0;
1571 struct memblock_region *r;
1572 unsigned long start_pfn, end_pfn;
1574 for_each_memblock(memory, r) {
1575 start_pfn = memblock_region_memory_base_pfn(r);
1576 end_pfn = memblock_region_memory_end_pfn(r);
1577 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1578 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1579 pages += end_pfn - start_pfn;
1582 return PFN_PHYS(pages);
1585 /* lowest address */
1586 phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1588 return memblock.memory.regions[0].base;
1591 phys_addr_t __init_memblock memblock_end_of_DRAM(void)
1593 int idx = memblock.memory.cnt - 1;
1595 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
1598 static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
1600 phys_addr_t max_addr = PHYS_ADDR_MAX;
1601 struct memblock_region *r;
1604 * translate the memory @limit size into the max address within one of
1605 * the memory memblock regions, if the @limit exceeds the total size
1606 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
1608 for_each_memblock(memory, r) {
1609 if (limit <= r->size) {
1610 max_addr = r->base + limit;
1619 void __init memblock_enforce_memory_limit(phys_addr_t limit)
1621 phys_addr_t max_addr = PHYS_ADDR_MAX;
1626 max_addr = __find_max_addr(limit);
1628 /* @limit exceeds the total size of the memory, do nothing */
1629 if (max_addr == PHYS_ADDR_MAX)
1632 /* truncate both memory and reserved regions */
1633 memblock_remove_range(&memblock.memory, max_addr,
1635 memblock_remove_range(&memblock.reserved, max_addr,
1639 void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1641 int start_rgn, end_rgn;
1647 ret = memblock_isolate_range(&memblock.memory, base, size,
1648 &start_rgn, &end_rgn);
1652 /* remove all the MAP regions */
1653 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1654 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1655 memblock_remove_region(&memblock.memory, i);
1657 for (i = start_rgn - 1; i >= 0; i--)
1658 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1659 memblock_remove_region(&memblock.memory, i);
1661 /* truncate the reserved regions */
1662 memblock_remove_range(&memblock.reserved, 0, base);
1663 memblock_remove_range(&memblock.reserved,
1664 base + size, PHYS_ADDR_MAX);
1667 void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1669 phys_addr_t max_addr;
1674 max_addr = __find_max_addr(limit);
1676 /* @limit exceeds the total size of the memory, do nothing */
1677 if (max_addr == PHYS_ADDR_MAX)
1680 memblock_cap_memory_range(0, max_addr);
1683 static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
1685 unsigned int left = 0, right = type->cnt;
1688 unsigned int mid = (right + left) / 2;
1690 if (addr < type->regions[mid].base)
1692 else if (addr >= (type->regions[mid].base +
1693 type->regions[mid].size))
1697 } while (left < right);
1701 bool __init_memblock memblock_is_reserved(phys_addr_t addr)
1703 return memblock_search(&memblock.reserved, addr) != -1;
1706 bool __init_memblock memblock_is_memory(phys_addr_t addr)
1708 return memblock_search(&memblock.memory, addr) != -1;
1711 bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
1713 int i = memblock_search(&memblock.memory, addr);
1717 return !memblock_is_nomap(&memblock.memory.regions[i]);
1720 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1721 int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1722 unsigned long *start_pfn, unsigned long *end_pfn)
1724 struct memblock_type *type = &memblock.memory;
1725 int mid = memblock_search(type, PFN_PHYS(pfn));
1730 *start_pfn = PFN_DOWN(type->regions[mid].base);
1731 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
1733 return type->regions[mid].nid;
1738 * memblock_is_region_memory - check if a region is a subset of memory
1739 * @base: base of region to check
1740 * @size: size of region to check
1742 * Check if the region [@base, @base + @size) is a subset of a memory block.
1745 * 0 if false, non-zero if true
1747 bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
1749 int idx = memblock_search(&memblock.memory, base);
1750 phys_addr_t end = base + memblock_cap_size(base, &size);
1754 return (memblock.memory.regions[idx].base +
1755 memblock.memory.regions[idx].size) >= end;
1759 * memblock_is_region_reserved - check if a region intersects reserved memory
1760 * @base: base of region to check
1761 * @size: size of region to check
1763 * Check if the region [@base, @base + @size) intersects a reserved
1767 * True if they intersect, false if not.
1769 bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
1771 return memblock_overlaps_region(&memblock.reserved, base, size);
1774 void __init_memblock memblock_trim_memory(phys_addr_t align)
1776 phys_addr_t start, end, orig_start, orig_end;
1777 struct memblock_region *r;
1779 for_each_memblock(memory, r) {
1780 orig_start = r->base;
1781 orig_end = r->base + r->size;
1782 start = round_up(orig_start, align);
1783 end = round_down(orig_end, align);
1785 if (start == orig_start && end == orig_end)
1790 r->size = end - start;
1792 memblock_remove_region(&memblock.memory,
1793 r - memblock.memory.regions);
1799 void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1801 memblock.current_limit = limit;
1804 phys_addr_t __init_memblock memblock_get_current_limit(void)
1806 return memblock.current_limit;
1809 static void __init_memblock memblock_dump(struct memblock_type *type)
1811 phys_addr_t base, end, size;
1812 enum memblock_flags flags;
1814 struct memblock_region *rgn;
1816 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt);
1818 for_each_memblock_type(idx, type, rgn) {
1819 char nid_buf[32] = "";
1823 end = base + size - 1;
1825 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1826 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1827 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1828 memblock_get_region_node(rgn));
1830 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
1831 type->name, idx, &base, &end, &size, nid_buf, flags);
1835 void __init_memblock __memblock_dump_all(void)
1837 pr_info("MEMBLOCK configuration:\n");
1838 pr_info(" memory size = %pa reserved size = %pa\n",
1839 &memblock.memory.total_size,
1840 &memblock.reserved.total_size);
1842 memblock_dump(&memblock.memory);
1843 memblock_dump(&memblock.reserved);
1844 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
1845 memblock_dump(&memblock.physmem);
1849 void __init memblock_allow_resize(void)
1851 memblock_can_resize = 1;
1854 static int __init early_memblock(char *p)
1856 if (p && strstr(p, "debug"))
1860 early_param("memblock", early_memblock);
1862 static void __init __free_pages_memory(unsigned long start, unsigned long end)
1866 while (start < end) {
1867 order = min(MAX_ORDER - 1UL, __ffs(start));
1869 while (start + (1UL << order) > end)
1872 memblock_free_pages(pfn_to_page(start), start, order);
1874 start += (1UL << order);
1878 static unsigned long __init __free_memory_core(phys_addr_t start,
1881 unsigned long start_pfn = PFN_UP(start);
1882 unsigned long end_pfn = min_t(unsigned long,
1883 PFN_DOWN(end), max_low_pfn);
1885 if (start_pfn >= end_pfn)
1888 __free_pages_memory(start_pfn, end_pfn);
1890 return end_pfn - start_pfn;
1893 static unsigned long __init free_low_memory_core_early(void)
1895 unsigned long count = 0;
1896 phys_addr_t start, end;
1899 memblock_clear_hotplug(0, -1);
1901 for_each_reserved_mem_region(i, &start, &end)
1902 reserve_bootmem_region(start, end);
1905 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
1906 * because in some case like Node0 doesn't have RAM installed
1907 * low ram will be on Node1
1909 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
1911 count += __free_memory_core(start, end);
1916 static int reset_managed_pages_done __initdata;
1918 void reset_node_managed_pages(pg_data_t *pgdat)
1922 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1923 atomic_long_set(&z->managed_pages, 0);
1926 void __init reset_all_zones_managed_pages(void)
1928 struct pglist_data *pgdat;
1930 if (reset_managed_pages_done)
1933 for_each_online_pgdat(pgdat)
1934 reset_node_managed_pages(pgdat);
1936 reset_managed_pages_done = 1;
1940 * memblock_free_all - release free pages to the buddy allocator
1942 * Return: the number of pages actually released.
1944 unsigned long __init memblock_free_all(void)
1946 unsigned long pages;
1948 reset_all_zones_managed_pages();
1950 pages = free_low_memory_core_early();
1951 totalram_pages_add(pages);
1956 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
1958 static int memblock_debug_show(struct seq_file *m, void *private)
1960 struct memblock_type *type = m->private;
1961 struct memblock_region *reg;
1965 for (i = 0; i < type->cnt; i++) {
1966 reg = &type->regions[i];
1967 end = reg->base + reg->size - 1;
1969 seq_printf(m, "%4d: ", i);
1970 seq_printf(m, "%pa..%pa\n", ®->base, &end);
1974 DEFINE_SHOW_ATTRIBUTE(memblock_debug);
1976 static int __init memblock_init_debugfs(void)
1978 struct dentry *root = debugfs_create_dir("memblock", NULL);
1980 debugfs_create_file("memory", 0444, root,
1981 &memblock.memory, &memblock_debug_fops);
1982 debugfs_create_file("reserved", 0444, root,
1983 &memblock.reserved, &memblock_debug_fops);
1984 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
1985 debugfs_create_file("physmem", 0444, root,
1986 &memblock.physmem, &memblock_debug_fops);
1991 __initcall(memblock_init_debugfs);
1993 #endif /* CONFIG_DEBUG_FS */