xref: /linux/mm/memblock.c (revision c9a688a3e918c4eb4f3916ff99a6dae8995af41b)
195f72d1eSYinghai Lu /*
295f72d1eSYinghai Lu  * Procedures for maintaining information about logical memory blocks.
395f72d1eSYinghai Lu  *
495f72d1eSYinghai Lu  * Peter Bergner, IBM Corp.	June 2001.
595f72d1eSYinghai Lu  * Copyright (C) 2001 Peter Bergner.
695f72d1eSYinghai Lu  *
795f72d1eSYinghai Lu  *      This program is free software; you can redistribute it and/or
895f72d1eSYinghai Lu  *      modify it under the terms of the GNU General Public License
995f72d1eSYinghai Lu  *      as published by the Free Software Foundation; either version
1095f72d1eSYinghai Lu  *      2 of the License, or (at your option) any later version.
1195f72d1eSYinghai Lu  */
1295f72d1eSYinghai Lu 
1395f72d1eSYinghai Lu #include <linux/kernel.h>
14142b45a7SBenjamin Herrenschmidt #include <linux/slab.h>
1595f72d1eSYinghai Lu #include <linux/init.h>
1695f72d1eSYinghai Lu #include <linux/bitops.h>
17449e8df3SBenjamin Herrenschmidt #include <linux/poison.h>
18c196f76fSBenjamin Herrenschmidt #include <linux/pfn.h>
196d03b885SBenjamin Herrenschmidt #include <linux/debugfs.h>
20514c6032SRandy Dunlap #include <linux/kmemleak.h>
216d03b885SBenjamin Herrenschmidt #include <linux/seq_file.h>
2295f72d1eSYinghai Lu #include <linux/memblock.h>
2395f72d1eSYinghai Lu 
24c4c5ad6bSChristoph Hellwig #include <asm/sections.h>
2526f09e9bSSantosh Shilimkar #include <linux/io.h>
2626f09e9bSSantosh Shilimkar 
2726f09e9bSSantosh Shilimkar #include "internal.h"
2879442ed1STang Chen 
298a5b403dSArd Biesheuvel #define INIT_MEMBLOCK_REGIONS			128
308a5b403dSArd Biesheuvel #define INIT_PHYSMEM_REGIONS			4
318a5b403dSArd Biesheuvel 
328a5b403dSArd Biesheuvel #ifndef INIT_MEMBLOCK_RESERVED_REGIONS
338a5b403dSArd Biesheuvel # define INIT_MEMBLOCK_RESERVED_REGIONS		INIT_MEMBLOCK_REGIONS
348a5b403dSArd Biesheuvel #endif
358a5b403dSArd Biesheuvel 
363e039c5cSMike Rapoport /**
373e039c5cSMike Rapoport  * DOC: memblock overview
383e039c5cSMike Rapoport  *
393e039c5cSMike Rapoport  * Memblock is a method of managing memory regions during the early
403e039c5cSMike Rapoport  * boot period when the usual kernel memory allocators are not up and
413e039c5cSMike Rapoport  * running.
423e039c5cSMike Rapoport  *
433e039c5cSMike Rapoport  * Memblock views the system memory as collections of contiguous
443e039c5cSMike Rapoport  * regions. There are several types of these collections:
453e039c5cSMike Rapoport  *
463e039c5cSMike Rapoport  * * ``memory`` - describes the physical memory available to the
473e039c5cSMike Rapoport  *   kernel; this may differ from the actual physical memory installed
483e039c5cSMike Rapoport  *   in the system, for instance when the memory is restricted with
493e039c5cSMike Rapoport  *   ``mem=`` command line parameter
503e039c5cSMike Rapoport  * * ``reserved`` - describes the regions that were allocated
513e039c5cSMike Rapoport  * * ``physmap`` - describes the actual physical memory regardless of
523e039c5cSMike Rapoport  *   the possible restrictions; the ``physmap`` type is only available
533e039c5cSMike Rapoport  *   on some architectures.
543e039c5cSMike Rapoport  *
553e039c5cSMike Rapoport  * Each region is represented by :c:type:`struct memblock_region` that
563e039c5cSMike Rapoport  * defines the region extents, its attributes and NUMA node id on NUMA
573e039c5cSMike Rapoport  * systems. Every memory type is described by the :c:type:`struct
583e039c5cSMike Rapoport  * memblock_type` which contains an array of memory regions along with
593e039c5cSMike Rapoport  * the allocator metadata. The memory types are nicely wrapped with
603e039c5cSMike Rapoport  * :c:type:`struct memblock`. This structure is statically initialzed
613e039c5cSMike Rapoport  * at build time. The region arrays for the "memory" and "reserved"
623e039c5cSMike Rapoport  * types are initially sized to %INIT_MEMBLOCK_REGIONS and for the
633e039c5cSMike Rapoport  * "physmap" type to %INIT_PHYSMEM_REGIONS.
643e039c5cSMike Rapoport  * The :c:func:`memblock_allow_resize` enables automatic resizing of
653e039c5cSMike Rapoport  * the region arrays during addition of new regions. This feature
663e039c5cSMike Rapoport  * should be used with care so that memory allocated for the region
673e039c5cSMike Rapoport  * array will not overlap with areas that should be reserved, for
683e039c5cSMike Rapoport  * example initrd.
693e039c5cSMike Rapoport  *
703e039c5cSMike Rapoport  * The early architecture setup should tell memblock what the physical
713e039c5cSMike Rapoport  * memory layout is by using :c:func:`memblock_add` or
723e039c5cSMike Rapoport  * :c:func:`memblock_add_node` functions. The first function does not
733e039c5cSMike Rapoport  * assign the region to a NUMA node and it is appropriate for UMA
743e039c5cSMike Rapoport  * systems. Yet, it is possible to use it on NUMA systems as well and
753e039c5cSMike Rapoport  * assign the region to a NUMA node later in the setup process using
763e039c5cSMike Rapoport  * :c:func:`memblock_set_node`. The :c:func:`memblock_add_node`
773e039c5cSMike Rapoport  * performs such an assignment directly.
783e039c5cSMike Rapoport  *
793e039c5cSMike Rapoport  * Once memblock is setup the memory can be allocated using either
803e039c5cSMike Rapoport  * memblock or bootmem APIs.
813e039c5cSMike Rapoport  *
823e039c5cSMike Rapoport  * As the system boot progresses, the architecture specific
833e039c5cSMike Rapoport  * :c:func:`mem_init` function frees all the memory to the buddy page
843e039c5cSMike Rapoport  * allocator.
853e039c5cSMike Rapoport  *
863e039c5cSMike Rapoport  * If an architecure enables %CONFIG_ARCH_DISCARD_MEMBLOCK, the
873e039c5cSMike Rapoport  * memblock data structures will be discarded after the system
883e039c5cSMike Rapoport  * initialization compltes.
893e039c5cSMike Rapoport  */
903e039c5cSMike Rapoport 
91bda49a81SMike Rapoport #ifndef CONFIG_NEED_MULTIPLE_NODES
92bda49a81SMike Rapoport struct pglist_data __refdata contig_page_data;
93bda49a81SMike Rapoport EXPORT_SYMBOL(contig_page_data);
94bda49a81SMike Rapoport #endif
95bda49a81SMike Rapoport 
96bda49a81SMike Rapoport unsigned long max_low_pfn;
97bda49a81SMike Rapoport unsigned long min_low_pfn;
98bda49a81SMike Rapoport unsigned long max_pfn;
99bda49a81SMike Rapoport unsigned long long max_possible_pfn;
100bda49a81SMike Rapoport 
101fe091c20STejun Heo static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
1028a5b403dSArd Biesheuvel static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
10370210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
10470210ed9SPhilipp Hachtmann static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
10570210ed9SPhilipp Hachtmann #endif
106fe091c20STejun Heo 
107fe091c20STejun Heo struct memblock memblock __initdata_memblock = {
108fe091c20STejun Heo 	.memory.regions		= memblock_memory_init_regions,
109fe091c20STejun Heo 	.memory.cnt		= 1,	/* empty dummy entry */
110fe091c20STejun Heo 	.memory.max		= INIT_MEMBLOCK_REGIONS,
1110262d9c8SHeiko Carstens 	.memory.name		= "memory",
112fe091c20STejun Heo 
113fe091c20STejun Heo 	.reserved.regions	= memblock_reserved_init_regions,
114fe091c20STejun Heo 	.reserved.cnt		= 1,	/* empty dummy entry */
1158a5b403dSArd Biesheuvel 	.reserved.max		= INIT_MEMBLOCK_RESERVED_REGIONS,
1160262d9c8SHeiko Carstens 	.reserved.name		= "reserved",
117fe091c20STejun Heo 
11870210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
11970210ed9SPhilipp Hachtmann 	.physmem.regions	= memblock_physmem_init_regions,
12070210ed9SPhilipp Hachtmann 	.physmem.cnt		= 1,	/* empty dummy entry */
12170210ed9SPhilipp Hachtmann 	.physmem.max		= INIT_PHYSMEM_REGIONS,
1220262d9c8SHeiko Carstens 	.physmem.name		= "physmem",
12370210ed9SPhilipp Hachtmann #endif
12470210ed9SPhilipp Hachtmann 
12579442ed1STang Chen 	.bottom_up		= false,
126fe091c20STejun Heo 	.current_limit		= MEMBLOCK_ALLOC_ANYWHERE,
127fe091c20STejun Heo };
12895f72d1eSYinghai Lu 
12910d06439SYinghai Lu int memblock_debug __initdata_memblock;
130a3f5bafcSTony Luck static bool system_has_some_mirror __initdata_memblock = false;
1311aadc056STejun Heo static int memblock_can_resize __initdata_memblock;
132181eb394SGavin Shan static int memblock_memory_in_slab __initdata_memblock = 0;
133181eb394SGavin Shan static int memblock_reserved_in_slab __initdata_memblock = 0;
13495f72d1eSYinghai Lu 
135c366ea89SMike Rapoport static enum memblock_flags __init_memblock choose_memblock_flags(void)
136a3f5bafcSTony Luck {
137a3f5bafcSTony Luck 	return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
138a3f5bafcSTony Luck }
139a3f5bafcSTony Luck 
140eb18f1b5STejun Heo /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
141eb18f1b5STejun Heo static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
142eb18f1b5STejun Heo {
1431c4bc43dSStefan Agner 	return *size = min(*size, PHYS_ADDR_MAX - base);
144eb18f1b5STejun Heo }
145eb18f1b5STejun Heo 
1466ed311b2SBenjamin Herrenschmidt /*
1476ed311b2SBenjamin Herrenschmidt  * Address comparison utilities
1486ed311b2SBenjamin Herrenschmidt  */
14910d06439SYinghai Lu static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
1502898cc4cSBenjamin Herrenschmidt 				       phys_addr_t base2, phys_addr_t size2)
15195f72d1eSYinghai Lu {
15295f72d1eSYinghai Lu 	return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
15395f72d1eSYinghai Lu }
15495f72d1eSYinghai Lu 
15595cf82ecSTang Chen bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
1562d7d3eb2SH Hartley Sweeten 					phys_addr_t base, phys_addr_t size)
1576ed311b2SBenjamin Herrenschmidt {
1586ed311b2SBenjamin Herrenschmidt 	unsigned long i;
1596ed311b2SBenjamin Herrenschmidt 
160f14516fbSAlexander Kuleshov 	for (i = 0; i < type->cnt; i++)
161f14516fbSAlexander Kuleshov 		if (memblock_addrs_overlap(base, size, type->regions[i].base,
162f14516fbSAlexander Kuleshov 					   type->regions[i].size))
1636ed311b2SBenjamin Herrenschmidt 			break;
164c5c5c9d1STang Chen 	return i < type->cnt;
1656ed311b2SBenjamin Herrenschmidt }
1666ed311b2SBenjamin Herrenschmidt 
16747cec443SMike Rapoport /**
16879442ed1STang Chen  * __memblock_find_range_bottom_up - find free area utility in bottom-up
16979442ed1STang Chen  * @start: start of candidate range
17047cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
17147cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
17279442ed1STang Chen  * @size: size of free area to find
17379442ed1STang Chen  * @align: alignment of free area to find
174b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
175fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
17679442ed1STang Chen  *
17779442ed1STang Chen  * Utility called from memblock_find_in_range_node(), find free area bottom-up.
17879442ed1STang Chen  *
17947cec443SMike Rapoport  * Return:
18079442ed1STang Chen  * Found address on success, 0 on failure.
18179442ed1STang Chen  */
18279442ed1STang Chen static phys_addr_t __init_memblock
18379442ed1STang Chen __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
184fc6daaf9STony Luck 				phys_addr_t size, phys_addr_t align, int nid,
185e1720feeSMike Rapoport 				enum memblock_flags flags)
18679442ed1STang Chen {
18779442ed1STang Chen 	phys_addr_t this_start, this_end, cand;
18879442ed1STang Chen 	u64 i;
18979442ed1STang Chen 
190fc6daaf9STony Luck 	for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
19179442ed1STang Chen 		this_start = clamp(this_start, start, end);
19279442ed1STang Chen 		this_end = clamp(this_end, start, end);
19379442ed1STang Chen 
19479442ed1STang Chen 		cand = round_up(this_start, align);
19579442ed1STang Chen 		if (cand < this_end && this_end - cand >= size)
19679442ed1STang Chen 			return cand;
19779442ed1STang Chen 	}
19879442ed1STang Chen 
19979442ed1STang Chen 	return 0;
20079442ed1STang Chen }
20179442ed1STang Chen 
2027bd0b0f0STejun Heo /**
2031402899eSTang Chen  * __memblock_find_range_top_down - find free area utility, in top-down
2041402899eSTang Chen  * @start: start of candidate range
20547cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
20647cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
2071402899eSTang Chen  * @size: size of free area to find
2081402899eSTang Chen  * @align: alignment of free area to find
209b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
210fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2111402899eSTang Chen  *
2121402899eSTang Chen  * Utility called from memblock_find_in_range_node(), find free area top-down.
2131402899eSTang Chen  *
21447cec443SMike Rapoport  * Return:
21579442ed1STang Chen  * Found address on success, 0 on failure.
2161402899eSTang Chen  */
2171402899eSTang Chen static phys_addr_t __init_memblock
2181402899eSTang Chen __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
219fc6daaf9STony Luck 			       phys_addr_t size, phys_addr_t align, int nid,
220e1720feeSMike Rapoport 			       enum memblock_flags flags)
2211402899eSTang Chen {
2221402899eSTang Chen 	phys_addr_t this_start, this_end, cand;
2231402899eSTang Chen 	u64 i;
2241402899eSTang Chen 
225fc6daaf9STony Luck 	for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
226fc6daaf9STony Luck 					NULL) {
2271402899eSTang Chen 		this_start = clamp(this_start, start, end);
2281402899eSTang Chen 		this_end = clamp(this_end, start, end);
2291402899eSTang Chen 
2301402899eSTang Chen 		if (this_end < size)
2311402899eSTang Chen 			continue;
2321402899eSTang Chen 
2331402899eSTang Chen 		cand = round_down(this_end - size, align);
2341402899eSTang Chen 		if (cand >= this_start)
2351402899eSTang Chen 			return cand;
2361402899eSTang Chen 	}
2371402899eSTang Chen 
2381402899eSTang Chen 	return 0;
2391402899eSTang Chen }
2401402899eSTang Chen 
2411402899eSTang Chen /**
2427bd0b0f0STejun Heo  * memblock_find_in_range_node - find free area in given range and node
2437bd0b0f0STejun Heo  * @size: size of free area to find
2447bd0b0f0STejun Heo  * @align: alignment of free area to find
24587029ee9SGrygorii Strashko  * @start: start of candidate range
24647cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
24747cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
248b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
249fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2507bd0b0f0STejun Heo  *
2517bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range and node.
2527bd0b0f0STejun Heo  *
25379442ed1STang Chen  * When allocation direction is bottom-up, the @start should be greater
25479442ed1STang Chen  * than the end of the kernel image. Otherwise, it will be trimmed. The
25579442ed1STang Chen  * reason is that we want the bottom-up allocation just near the kernel
25679442ed1STang Chen  * image so it is highly likely that the allocated memory and the kernel
25779442ed1STang Chen  * will reside in the same node.
25879442ed1STang Chen  *
25979442ed1STang Chen  * If bottom-up allocation failed, will try to allocate memory top-down.
26079442ed1STang Chen  *
26147cec443SMike Rapoport  * Return:
26279442ed1STang Chen  * Found address on success, 0 on failure.
2636ed311b2SBenjamin Herrenschmidt  */
264c366ea89SMike Rapoport static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
26587029ee9SGrygorii Strashko 					phys_addr_t align, phys_addr_t start,
266e1720feeSMike Rapoport 					phys_addr_t end, int nid,
267e1720feeSMike Rapoport 					enum memblock_flags flags)
268f7210e6cSTang Chen {
2690cfb8f0cSTang Chen 	phys_addr_t kernel_end, ret;
27079442ed1STang Chen 
271f7210e6cSTang Chen 	/* pump up @end */
272fed84c78SQian Cai 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
273fed84c78SQian Cai 	    end == MEMBLOCK_ALLOC_KASAN)
274f7210e6cSTang Chen 		end = memblock.current_limit;
275f7210e6cSTang Chen 
276f7210e6cSTang Chen 	/* avoid allocating the first page */
277f7210e6cSTang Chen 	start = max_t(phys_addr_t, start, PAGE_SIZE);
278f7210e6cSTang Chen 	end = max(start, end);
27979442ed1STang Chen 	kernel_end = __pa_symbol(_end);
28079442ed1STang Chen 
28179442ed1STang Chen 	/*
28279442ed1STang Chen 	 * try bottom-up allocation only when bottom-up mode
28379442ed1STang Chen 	 * is set and @end is above the kernel image.
28479442ed1STang Chen 	 */
28579442ed1STang Chen 	if (memblock_bottom_up() && end > kernel_end) {
28679442ed1STang Chen 		phys_addr_t bottom_up_start;
28779442ed1STang Chen 
28879442ed1STang Chen 		/* make sure we will allocate above the kernel */
28979442ed1STang Chen 		bottom_up_start = max(start, kernel_end);
29079442ed1STang Chen 
29179442ed1STang Chen 		/* ok, try bottom-up allocation first */
29279442ed1STang Chen 		ret = __memblock_find_range_bottom_up(bottom_up_start, end,
293fc6daaf9STony Luck 						      size, align, nid, flags);
29479442ed1STang Chen 		if (ret)
29579442ed1STang Chen 			return ret;
29679442ed1STang Chen 
29779442ed1STang Chen 		/*
29879442ed1STang Chen 		 * we always limit bottom-up allocation above the kernel,
29979442ed1STang Chen 		 * but top-down allocation doesn't have the limit, so
30079442ed1STang Chen 		 * retrying top-down allocation may succeed when bottom-up
30179442ed1STang Chen 		 * allocation failed.
30279442ed1STang Chen 		 *
30379442ed1STang Chen 		 * bottom-up allocation is expected to be fail very rarely,
30479442ed1STang Chen 		 * so we use WARN_ONCE() here to see the stack trace if
30579442ed1STang Chen 		 * fail happens.
30679442ed1STang Chen 		 */
307e3d301caSMichal Hocko 		WARN_ONCE(IS_ENABLED(CONFIG_MEMORY_HOTREMOVE),
308e3d301caSMichal Hocko 			  "memblock: bottom-up allocation failed, memory hotremove may be affected\n");
30979442ed1STang Chen 	}
310f7210e6cSTang Chen 
311fc6daaf9STony Luck 	return __memblock_find_range_top_down(start, end, size, align, nid,
312fc6daaf9STony Luck 					      flags);
313f7210e6cSTang Chen }
3146ed311b2SBenjamin Herrenschmidt 
3157bd0b0f0STejun Heo /**
3167bd0b0f0STejun Heo  * memblock_find_in_range - find free area in given range
3177bd0b0f0STejun Heo  * @start: start of candidate range
31847cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
31947cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
3207bd0b0f0STejun Heo  * @size: size of free area to find
3217bd0b0f0STejun Heo  * @align: alignment of free area to find
3227bd0b0f0STejun Heo  *
3237bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range.
3247bd0b0f0STejun Heo  *
32547cec443SMike Rapoport  * Return:
32679442ed1STang Chen  * Found address on success, 0 on failure.
3277bd0b0f0STejun Heo  */
3287bd0b0f0STejun Heo phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
3297bd0b0f0STejun Heo 					phys_addr_t end, phys_addr_t size,
3307bd0b0f0STejun Heo 					phys_addr_t align)
3317bd0b0f0STejun Heo {
332a3f5bafcSTony Luck 	phys_addr_t ret;
333e1720feeSMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
334a3f5bafcSTony Luck 
335a3f5bafcSTony Luck again:
336a3f5bafcSTony Luck 	ret = memblock_find_in_range_node(size, align, start, end,
337a3f5bafcSTony Luck 					    NUMA_NO_NODE, flags);
338a3f5bafcSTony Luck 
339a3f5bafcSTony Luck 	if (!ret && (flags & MEMBLOCK_MIRROR)) {
340a3f5bafcSTony Luck 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
341a3f5bafcSTony Luck 			&size);
342a3f5bafcSTony Luck 		flags &= ~MEMBLOCK_MIRROR;
343a3f5bafcSTony Luck 		goto again;
344a3f5bafcSTony Luck 	}
345a3f5bafcSTony Luck 
346a3f5bafcSTony Luck 	return ret;
3477bd0b0f0STejun Heo }
3487bd0b0f0STejun Heo 
34910d06439SYinghai Lu static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
35095f72d1eSYinghai Lu {
3511440c4e2STejun Heo 	type->total_size -= type->regions[r].size;
3527c0caeb8STejun Heo 	memmove(&type->regions[r], &type->regions[r + 1],
3537c0caeb8STejun Heo 		(type->cnt - (r + 1)) * sizeof(type->regions[r]));
354e3239ff9SBenjamin Herrenschmidt 	type->cnt--;
35595f72d1eSYinghai Lu 
3568f7a6605SBenjamin Herrenschmidt 	/* Special case for empty arrays */
3578f7a6605SBenjamin Herrenschmidt 	if (type->cnt == 0) {
3581440c4e2STejun Heo 		WARN_ON(type->total_size != 0);
3598f7a6605SBenjamin Herrenschmidt 		type->cnt = 1;
3608f7a6605SBenjamin Herrenschmidt 		type->regions[0].base = 0;
3618f7a6605SBenjamin Herrenschmidt 		type->regions[0].size = 0;
36266a20757STang Chen 		type->regions[0].flags = 0;
3637c0caeb8STejun Heo 		memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
3648f7a6605SBenjamin Herrenschmidt 	}
36595f72d1eSYinghai Lu }
36695f72d1eSYinghai Lu 
367354f17e1SPhilipp Hachtmann #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
3683010f876SPavel Tatashin /**
36947cec443SMike Rapoport  * memblock_discard - discard memory and reserved arrays if they were allocated
3703010f876SPavel Tatashin  */
3713010f876SPavel Tatashin void __init memblock_discard(void)
37229f67386SYinghai Lu {
3733010f876SPavel Tatashin 	phys_addr_t addr, size;
37429f67386SYinghai Lu 
3753010f876SPavel Tatashin 	if (memblock.reserved.regions != memblock_reserved_init_regions) {
3763010f876SPavel Tatashin 		addr = __pa(memblock.reserved.regions);
3773010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
37829f67386SYinghai Lu 				  memblock.reserved.max);
3793010f876SPavel Tatashin 		__memblock_free_late(addr, size);
38029f67386SYinghai Lu 	}
38129f67386SYinghai Lu 
38291b540f9SPavel Tatashin 	if (memblock.memory.regions != memblock_memory_init_regions) {
3833010f876SPavel Tatashin 		addr = __pa(memblock.memory.regions);
3843010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
3855e270e25SPhilipp Hachtmann 				  memblock.memory.max);
3863010f876SPavel Tatashin 		__memblock_free_late(addr, size);
3875e270e25SPhilipp Hachtmann 	}
3883010f876SPavel Tatashin }
3895e270e25SPhilipp Hachtmann #endif
3905e270e25SPhilipp Hachtmann 
39148c3b583SGreg Pearson /**
39248c3b583SGreg Pearson  * memblock_double_array - double the size of the memblock regions array
39348c3b583SGreg Pearson  * @type: memblock type of the regions array being doubled
39448c3b583SGreg Pearson  * @new_area_start: starting address of memory range to avoid overlap with
39548c3b583SGreg Pearson  * @new_area_size: size of memory range to avoid overlap with
39648c3b583SGreg Pearson  *
39748c3b583SGreg Pearson  * Double the size of the @type regions array. If memblock is being used to
39848c3b583SGreg Pearson  * allocate memory for a new reserved regions array and there is a previously
39948c3b583SGreg Pearson  * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
40048c3b583SGreg Pearson  * waiting to be reserved, ensure the memory used by the new array does
40148c3b583SGreg Pearson  * not overlap.
40248c3b583SGreg Pearson  *
40347cec443SMike Rapoport  * Return:
40448c3b583SGreg Pearson  * 0 on success, -1 on failure.
40548c3b583SGreg Pearson  */
40648c3b583SGreg Pearson static int __init_memblock memblock_double_array(struct memblock_type *type,
40748c3b583SGreg Pearson 						phys_addr_t new_area_start,
40848c3b583SGreg Pearson 						phys_addr_t new_area_size)
409142b45a7SBenjamin Herrenschmidt {
410142b45a7SBenjamin Herrenschmidt 	struct memblock_region *new_array, *old_array;
41129f67386SYinghai Lu 	phys_addr_t old_alloc_size, new_alloc_size;
412a36aab89SMike Rapoport 	phys_addr_t old_size, new_size, addr, new_end;
413142b45a7SBenjamin Herrenschmidt 	int use_slab = slab_is_available();
414181eb394SGavin Shan 	int *in_slab;
415142b45a7SBenjamin Herrenschmidt 
416142b45a7SBenjamin Herrenschmidt 	/* We don't allow resizing until we know about the reserved regions
417142b45a7SBenjamin Herrenschmidt 	 * of memory that aren't suitable for allocation
418142b45a7SBenjamin Herrenschmidt 	 */
419142b45a7SBenjamin Herrenschmidt 	if (!memblock_can_resize)
420142b45a7SBenjamin Herrenschmidt 		return -1;
421142b45a7SBenjamin Herrenschmidt 
422142b45a7SBenjamin Herrenschmidt 	/* Calculate new doubled size */
423142b45a7SBenjamin Herrenschmidt 	old_size = type->max * sizeof(struct memblock_region);
424142b45a7SBenjamin Herrenschmidt 	new_size = old_size << 1;
42529f67386SYinghai Lu 	/*
42629f67386SYinghai Lu 	 * We need to allocated new one align to PAGE_SIZE,
42729f67386SYinghai Lu 	 *   so we can free them completely later.
42829f67386SYinghai Lu 	 */
42929f67386SYinghai Lu 	old_alloc_size = PAGE_ALIGN(old_size);
43029f67386SYinghai Lu 	new_alloc_size = PAGE_ALIGN(new_size);
431142b45a7SBenjamin Herrenschmidt 
432181eb394SGavin Shan 	/* Retrieve the slab flag */
433181eb394SGavin Shan 	if (type == &memblock.memory)
434181eb394SGavin Shan 		in_slab = &memblock_memory_in_slab;
435181eb394SGavin Shan 	else
436181eb394SGavin Shan 		in_slab = &memblock_reserved_in_slab;
437181eb394SGavin Shan 
438142b45a7SBenjamin Herrenschmidt 	/* Try to find some space for it.
439142b45a7SBenjamin Herrenschmidt 	 *
440142b45a7SBenjamin Herrenschmidt 	 * WARNING: We assume that either slab_is_available() and we use it or
441fd07383bSAndrew Morton 	 * we use MEMBLOCK for allocations. That means that this is unsafe to
442fd07383bSAndrew Morton 	 * use when bootmem is currently active (unless bootmem itself is
443fd07383bSAndrew Morton 	 * implemented on top of MEMBLOCK which isn't the case yet)
444142b45a7SBenjamin Herrenschmidt 	 *
445142b45a7SBenjamin Herrenschmidt 	 * This should however not be an issue for now, as we currently only
446fd07383bSAndrew Morton 	 * call into MEMBLOCK while it's still active, or much later when slab
447fd07383bSAndrew Morton 	 * is active for memory hotplug operations
448142b45a7SBenjamin Herrenschmidt 	 */
449142b45a7SBenjamin Herrenschmidt 	if (use_slab) {
450142b45a7SBenjamin Herrenschmidt 		new_array = kmalloc(new_size, GFP_KERNEL);
4511f5026a7STejun Heo 		addr = new_array ? __pa(new_array) : 0;
4524e2f0775SGavin Shan 	} else {
45348c3b583SGreg Pearson 		/* only exclude range when trying to double reserved.regions */
45448c3b583SGreg Pearson 		if (type != &memblock.reserved)
45548c3b583SGreg Pearson 			new_area_start = new_area_size = 0;
45648c3b583SGreg Pearson 
45748c3b583SGreg Pearson 		addr = memblock_find_in_range(new_area_start + new_area_size,
45848c3b583SGreg Pearson 						memblock.current_limit,
45929f67386SYinghai Lu 						new_alloc_size, PAGE_SIZE);
46048c3b583SGreg Pearson 		if (!addr && new_area_size)
46148c3b583SGreg Pearson 			addr = memblock_find_in_range(0,
46248c3b583SGreg Pearson 				min(new_area_start, memblock.current_limit),
46329f67386SYinghai Lu 				new_alloc_size, PAGE_SIZE);
46448c3b583SGreg Pearson 
46515674868SSachin Kamat 		new_array = addr ? __va(addr) : NULL;
4664e2f0775SGavin Shan 	}
4671f5026a7STejun Heo 	if (!addr) {
468142b45a7SBenjamin Herrenschmidt 		pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
4690262d9c8SHeiko Carstens 		       type->name, type->max, type->max * 2);
470142b45a7SBenjamin Herrenschmidt 		return -1;
471142b45a7SBenjamin Herrenschmidt 	}
472142b45a7SBenjamin Herrenschmidt 
473a36aab89SMike Rapoport 	new_end = addr + new_size - 1;
474a36aab89SMike Rapoport 	memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
475a36aab89SMike Rapoport 			type->name, type->max * 2, &addr, &new_end);
476ea9e4376SYinghai Lu 
477fd07383bSAndrew Morton 	/*
478fd07383bSAndrew Morton 	 * Found space, we now need to move the array over before we add the
479fd07383bSAndrew Morton 	 * reserved region since it may be our reserved array itself that is
480fd07383bSAndrew Morton 	 * full.
481142b45a7SBenjamin Herrenschmidt 	 */
482142b45a7SBenjamin Herrenschmidt 	memcpy(new_array, type->regions, old_size);
483142b45a7SBenjamin Herrenschmidt 	memset(new_array + type->max, 0, old_size);
484142b45a7SBenjamin Herrenschmidt 	old_array = type->regions;
485142b45a7SBenjamin Herrenschmidt 	type->regions = new_array;
486142b45a7SBenjamin Herrenschmidt 	type->max <<= 1;
487142b45a7SBenjamin Herrenschmidt 
488fd07383bSAndrew Morton 	/* Free old array. We needn't free it if the array is the static one */
489181eb394SGavin Shan 	if (*in_slab)
490181eb394SGavin Shan 		kfree(old_array);
491181eb394SGavin Shan 	else if (old_array != memblock_memory_init_regions &&
492142b45a7SBenjamin Herrenschmidt 		 old_array != memblock_reserved_init_regions)
49329f67386SYinghai Lu 		memblock_free(__pa(old_array), old_alloc_size);
494142b45a7SBenjamin Herrenschmidt 
495fd07383bSAndrew Morton 	/*
496fd07383bSAndrew Morton 	 * Reserve the new array if that comes from the memblock.  Otherwise, we
497fd07383bSAndrew Morton 	 * needn't do it
498181eb394SGavin Shan 	 */
499181eb394SGavin Shan 	if (!use_slab)
50029f67386SYinghai Lu 		BUG_ON(memblock_reserve(addr, new_alloc_size));
501181eb394SGavin Shan 
502181eb394SGavin Shan 	/* Update slab flag */
503181eb394SGavin Shan 	*in_slab = use_slab;
504181eb394SGavin Shan 
505142b45a7SBenjamin Herrenschmidt 	return 0;
506142b45a7SBenjamin Herrenschmidt }
507142b45a7SBenjamin Herrenschmidt 
508784656f9STejun Heo /**
509784656f9STejun Heo  * memblock_merge_regions - merge neighboring compatible regions
510784656f9STejun Heo  * @type: memblock type to scan
511784656f9STejun Heo  *
512784656f9STejun Heo  * Scan @type and merge neighboring compatible regions.
513784656f9STejun Heo  */
514784656f9STejun Heo static void __init_memblock memblock_merge_regions(struct memblock_type *type)
515784656f9STejun Heo {
516784656f9STejun Heo 	int i = 0;
517784656f9STejun Heo 
518784656f9STejun Heo 	/* cnt never goes below 1 */
519784656f9STejun Heo 	while (i < type->cnt - 1) {
520784656f9STejun Heo 		struct memblock_region *this = &type->regions[i];
521784656f9STejun Heo 		struct memblock_region *next = &type->regions[i + 1];
522784656f9STejun Heo 
5237c0caeb8STejun Heo 		if (this->base + this->size != next->base ||
5247c0caeb8STejun Heo 		    memblock_get_region_node(this) !=
52566a20757STang Chen 		    memblock_get_region_node(next) ||
52666a20757STang Chen 		    this->flags != next->flags) {
527784656f9STejun Heo 			BUG_ON(this->base + this->size > next->base);
528784656f9STejun Heo 			i++;
529784656f9STejun Heo 			continue;
530784656f9STejun Heo 		}
531784656f9STejun Heo 
532784656f9STejun Heo 		this->size += next->size;
533c0232ae8SLin Feng 		/* move forward from next + 1, index of which is i + 2 */
534c0232ae8SLin Feng 		memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
535784656f9STejun Heo 		type->cnt--;
536784656f9STejun Heo 	}
537784656f9STejun Heo }
538784656f9STejun Heo 
539784656f9STejun Heo /**
540784656f9STejun Heo  * memblock_insert_region - insert new memblock region
541784656f9STejun Heo  * @type:	memblock type to insert into
542784656f9STejun Heo  * @idx:	index for the insertion point
543784656f9STejun Heo  * @base:	base address of the new region
544784656f9STejun Heo  * @size:	size of the new region
545209ff86dSTang Chen  * @nid:	node id of the new region
54666a20757STang Chen  * @flags:	flags of the new region
547784656f9STejun Heo  *
548784656f9STejun Heo  * Insert new memblock region [@base, @base + @size) into @type at @idx.
549412d0008SAlexander Kuleshov  * @type must already have extra room to accommodate the new region.
550784656f9STejun Heo  */
551784656f9STejun Heo static void __init_memblock memblock_insert_region(struct memblock_type *type,
552784656f9STejun Heo 						   int idx, phys_addr_t base,
55366a20757STang Chen 						   phys_addr_t size,
554e1720feeSMike Rapoport 						   int nid,
555e1720feeSMike Rapoport 						   enum memblock_flags flags)
556784656f9STejun Heo {
557784656f9STejun Heo 	struct memblock_region *rgn = &type->regions[idx];
558784656f9STejun Heo 
559784656f9STejun Heo 	BUG_ON(type->cnt >= type->max);
560784656f9STejun Heo 	memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
561784656f9STejun Heo 	rgn->base = base;
562784656f9STejun Heo 	rgn->size = size;
56366a20757STang Chen 	rgn->flags = flags;
5647c0caeb8STejun Heo 	memblock_set_region_node(rgn, nid);
565784656f9STejun Heo 	type->cnt++;
5661440c4e2STejun Heo 	type->total_size += size;
567784656f9STejun Heo }
568784656f9STejun Heo 
569784656f9STejun Heo /**
570f1af9d3aSPhilipp Hachtmann  * memblock_add_range - add new memblock region
571784656f9STejun Heo  * @type: memblock type to add new region into
572784656f9STejun Heo  * @base: base address of the new region
573784656f9STejun Heo  * @size: size of the new region
5747fb0bc3fSTejun Heo  * @nid: nid of the new region
57566a20757STang Chen  * @flags: flags of the new region
576784656f9STejun Heo  *
577784656f9STejun Heo  * Add new memblock region [@base, @base + @size) into @type.  The new region
578784656f9STejun Heo  * is allowed to overlap with existing ones - overlaps don't affect already
579784656f9STejun Heo  * existing regions.  @type is guaranteed to be minimal (all neighbouring
580784656f9STejun Heo  * compatible regions are merged) after the addition.
581784656f9STejun Heo  *
58247cec443SMike Rapoport  * Return:
583784656f9STejun Heo  * 0 on success, -errno on failure.
584784656f9STejun Heo  */
585f1af9d3aSPhilipp Hachtmann int __init_memblock memblock_add_range(struct memblock_type *type,
58666a20757STang Chen 				phys_addr_t base, phys_addr_t size,
587e1720feeSMike Rapoport 				int nid, enum memblock_flags flags)
58895f72d1eSYinghai Lu {
589784656f9STejun Heo 	bool insert = false;
590eb18f1b5STejun Heo 	phys_addr_t obase = base;
591eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
5928c9c1701SAlexander Kuleshov 	int idx, nr_new;
5938c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
59495f72d1eSYinghai Lu 
595b3dc627cSTejun Heo 	if (!size)
596b3dc627cSTejun Heo 		return 0;
597b3dc627cSTejun Heo 
598784656f9STejun Heo 	/* special case for empty array */
599784656f9STejun Heo 	if (type->regions[0].size == 0) {
6001440c4e2STejun Heo 		WARN_ON(type->cnt != 1 || type->total_size);
601784656f9STejun Heo 		type->regions[0].base = base;
602784656f9STejun Heo 		type->regions[0].size = size;
60366a20757STang Chen 		type->regions[0].flags = flags;
6047fb0bc3fSTejun Heo 		memblock_set_region_node(&type->regions[0], nid);
6051440c4e2STejun Heo 		type->total_size = size;
606784656f9STejun Heo 		return 0;
607784656f9STejun Heo 	}
608784656f9STejun Heo repeat:
609784656f9STejun Heo 	/*
610784656f9STejun Heo 	 * The following is executed twice.  Once with %false @insert and
611784656f9STejun Heo 	 * then with %true.  The first counts the number of regions needed
612412d0008SAlexander Kuleshov 	 * to accommodate the new area.  The second actually inserts them.
613784656f9STejun Heo 	 */
614784656f9STejun Heo 	base = obase;
615784656f9STejun Heo 	nr_new = 0;
616784656f9STejun Heo 
61766e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
618784656f9STejun Heo 		phys_addr_t rbase = rgn->base;
619784656f9STejun Heo 		phys_addr_t rend = rbase + rgn->size;
6208f7a6605SBenjamin Herrenschmidt 
621784656f9STejun Heo 		if (rbase >= end)
6228f7a6605SBenjamin Herrenschmidt 			break;
623784656f9STejun Heo 		if (rend <= base)
624784656f9STejun Heo 			continue;
625784656f9STejun Heo 		/*
626784656f9STejun Heo 		 * @rgn overlaps.  If it separates the lower part of new
627784656f9STejun Heo 		 * area, insert that portion.
6288f7a6605SBenjamin Herrenschmidt 		 */
629784656f9STejun Heo 		if (rbase > base) {
630c0a29498SWei Yang #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
631c0a29498SWei Yang 			WARN_ON(nid != memblock_get_region_node(rgn));
632c0a29498SWei Yang #endif
6334fcab5f4SWei Yang 			WARN_ON(flags != rgn->flags);
634784656f9STejun Heo 			nr_new++;
635784656f9STejun Heo 			if (insert)
6368c9c1701SAlexander Kuleshov 				memblock_insert_region(type, idx++, base,
63766a20757STang Chen 						       rbase - base, nid,
63866a20757STang Chen 						       flags);
639784656f9STejun Heo 		}
640784656f9STejun Heo 		/* area below @rend is dealt with, forget about it */
641784656f9STejun Heo 		base = min(rend, end);
6428f7a6605SBenjamin Herrenschmidt 	}
6438f7a6605SBenjamin Herrenschmidt 
644784656f9STejun Heo 	/* insert the remaining portion */
645784656f9STejun Heo 	if (base < end) {
646784656f9STejun Heo 		nr_new++;
647784656f9STejun Heo 		if (insert)
6488c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, base, end - base,
64966a20757STang Chen 					       nid, flags);
6508f7a6605SBenjamin Herrenschmidt 	}
6518f7a6605SBenjamin Herrenschmidt 
652ef3cc4dbSnimisolo 	if (!nr_new)
653ef3cc4dbSnimisolo 		return 0;
654ef3cc4dbSnimisolo 
655784656f9STejun Heo 	/*
656784656f9STejun Heo 	 * If this was the first round, resize array and repeat for actual
657784656f9STejun Heo 	 * insertions; otherwise, merge and return.
6588f7a6605SBenjamin Herrenschmidt 	 */
659784656f9STejun Heo 	if (!insert) {
660784656f9STejun Heo 		while (type->cnt + nr_new > type->max)
66148c3b583SGreg Pearson 			if (memblock_double_array(type, obase, size) < 0)
662784656f9STejun Heo 				return -ENOMEM;
663784656f9STejun Heo 		insert = true;
664784656f9STejun Heo 		goto repeat;
66595f72d1eSYinghai Lu 	} else {
666784656f9STejun Heo 		memblock_merge_regions(type);
66795f72d1eSYinghai Lu 		return 0;
66895f72d1eSYinghai Lu 	}
669784656f9STejun Heo }
67095f72d1eSYinghai Lu 
67148a833ccSMike Rapoport /**
67248a833ccSMike Rapoport  * memblock_add_node - add new memblock region within a NUMA node
67348a833ccSMike Rapoport  * @base: base address of the new region
67448a833ccSMike Rapoport  * @size: size of the new region
67548a833ccSMike Rapoport  * @nid: nid of the new region
67648a833ccSMike Rapoport  *
67748a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
67848a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
67948a833ccSMike Rapoport  *
68048a833ccSMike Rapoport  * Return:
68148a833ccSMike Rapoport  * 0 on success, -errno on failure.
68248a833ccSMike Rapoport  */
6837fb0bc3fSTejun Heo int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
6847fb0bc3fSTejun Heo 				       int nid)
6857fb0bc3fSTejun Heo {
686f1af9d3aSPhilipp Hachtmann 	return memblock_add_range(&memblock.memory, base, size, nid, 0);
6877fb0bc3fSTejun Heo }
6887fb0bc3fSTejun Heo 
68948a833ccSMike Rapoport /**
69048a833ccSMike Rapoport  * memblock_add - add new memblock region
69148a833ccSMike Rapoport  * @base: base address of the new region
69248a833ccSMike Rapoport  * @size: size of the new region
69348a833ccSMike Rapoport  *
69448a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
69548a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
69648a833ccSMike Rapoport  *
69748a833ccSMike Rapoport  * Return:
69848a833ccSMike Rapoport  * 0 on success, -errno on failure.
69948a833ccSMike Rapoport  */
700f705ac4bSAlexander Kuleshov int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
7016a4055bcSAlexander Kuleshov {
7025d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
7035d63f81cSMiles Chen 
7045d63f81cSMiles Chen 	memblock_dbg("memblock_add: [%pa-%pa] %pF\n",
7055d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
7066a4055bcSAlexander Kuleshov 
707f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
70895f72d1eSYinghai Lu }
70995f72d1eSYinghai Lu 
7106a9ceb31STejun Heo /**
7116a9ceb31STejun Heo  * memblock_isolate_range - isolate given range into disjoint memblocks
7126a9ceb31STejun Heo  * @type: memblock type to isolate range for
7136a9ceb31STejun Heo  * @base: base of range to isolate
7146a9ceb31STejun Heo  * @size: size of range to isolate
7156a9ceb31STejun Heo  * @start_rgn: out parameter for the start of isolated region
7166a9ceb31STejun Heo  * @end_rgn: out parameter for the end of isolated region
7176a9ceb31STejun Heo  *
7186a9ceb31STejun Heo  * Walk @type and ensure that regions don't cross the boundaries defined by
7196a9ceb31STejun Heo  * [@base, @base + @size).  Crossing regions are split at the boundaries,
7206a9ceb31STejun Heo  * which may create at most two more regions.  The index of the first
7216a9ceb31STejun Heo  * region inside the range is returned in *@start_rgn and end in *@end_rgn.
7226a9ceb31STejun Heo  *
72347cec443SMike Rapoport  * Return:
7246a9ceb31STejun Heo  * 0 on success, -errno on failure.
7256a9ceb31STejun Heo  */
7266a9ceb31STejun Heo static int __init_memblock memblock_isolate_range(struct memblock_type *type,
7276a9ceb31STejun Heo 					phys_addr_t base, phys_addr_t size,
7286a9ceb31STejun Heo 					int *start_rgn, int *end_rgn)
7296a9ceb31STejun Heo {
730eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
7318c9c1701SAlexander Kuleshov 	int idx;
7328c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
7336a9ceb31STejun Heo 
7346a9ceb31STejun Heo 	*start_rgn = *end_rgn = 0;
7356a9ceb31STejun Heo 
736b3dc627cSTejun Heo 	if (!size)
737b3dc627cSTejun Heo 		return 0;
738b3dc627cSTejun Heo 
7396a9ceb31STejun Heo 	/* we'll create at most two more regions */
7406a9ceb31STejun Heo 	while (type->cnt + 2 > type->max)
74148c3b583SGreg Pearson 		if (memblock_double_array(type, base, size) < 0)
7426a9ceb31STejun Heo 			return -ENOMEM;
7436a9ceb31STejun Heo 
74466e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
7456a9ceb31STejun Heo 		phys_addr_t rbase = rgn->base;
7466a9ceb31STejun Heo 		phys_addr_t rend = rbase + rgn->size;
7476a9ceb31STejun Heo 
7486a9ceb31STejun Heo 		if (rbase >= end)
7496a9ceb31STejun Heo 			break;
7506a9ceb31STejun Heo 		if (rend <= base)
7516a9ceb31STejun Heo 			continue;
7526a9ceb31STejun Heo 
7536a9ceb31STejun Heo 		if (rbase < base) {
7546a9ceb31STejun Heo 			/*
7556a9ceb31STejun Heo 			 * @rgn intersects from below.  Split and continue
7566a9ceb31STejun Heo 			 * to process the next region - the new top half.
7576a9ceb31STejun Heo 			 */
7586a9ceb31STejun Heo 			rgn->base = base;
7591440c4e2STejun Heo 			rgn->size -= base - rbase;
7601440c4e2STejun Heo 			type->total_size -= base - rbase;
7618c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, rbase, base - rbase,
76266a20757STang Chen 					       memblock_get_region_node(rgn),
76366a20757STang Chen 					       rgn->flags);
7646a9ceb31STejun Heo 		} else if (rend > end) {
7656a9ceb31STejun Heo 			/*
7666a9ceb31STejun Heo 			 * @rgn intersects from above.  Split and redo the
7676a9ceb31STejun Heo 			 * current region - the new bottom half.
7686a9ceb31STejun Heo 			 */
7696a9ceb31STejun Heo 			rgn->base = end;
7701440c4e2STejun Heo 			rgn->size -= end - rbase;
7711440c4e2STejun Heo 			type->total_size -= end - rbase;
7728c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx--, rbase, end - rbase,
77366a20757STang Chen 					       memblock_get_region_node(rgn),
77466a20757STang Chen 					       rgn->flags);
7756a9ceb31STejun Heo 		} else {
7766a9ceb31STejun Heo 			/* @rgn is fully contained, record it */
7776a9ceb31STejun Heo 			if (!*end_rgn)
7788c9c1701SAlexander Kuleshov 				*start_rgn = idx;
7798c9c1701SAlexander Kuleshov 			*end_rgn = idx + 1;
7806a9ceb31STejun Heo 		}
7816a9ceb31STejun Heo 	}
7826a9ceb31STejun Heo 
7836a9ceb31STejun Heo 	return 0;
7846a9ceb31STejun Heo }
7856a9ceb31STejun Heo 
78635bd16a2SAlexander Kuleshov static int __init_memblock memblock_remove_range(struct memblock_type *type,
7878f7a6605SBenjamin Herrenschmidt 					  phys_addr_t base, phys_addr_t size)
78895f72d1eSYinghai Lu {
78971936180STejun Heo 	int start_rgn, end_rgn;
79071936180STejun Heo 	int i, ret;
79195f72d1eSYinghai Lu 
79271936180STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
79371936180STejun Heo 	if (ret)
79471936180STejun Heo 		return ret;
79595f72d1eSYinghai Lu 
79671936180STejun Heo 	for (i = end_rgn - 1; i >= start_rgn; i--)
79771936180STejun Heo 		memblock_remove_region(type, i);
79895f72d1eSYinghai Lu 	return 0;
79995f72d1eSYinghai Lu }
80095f72d1eSYinghai Lu 
801581adcbeSTejun Heo int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
80295f72d1eSYinghai Lu {
80325cf23d7SMinchan Kim 	phys_addr_t end = base + size - 1;
80425cf23d7SMinchan Kim 
80525cf23d7SMinchan Kim 	memblock_dbg("memblock_remove: [%pa-%pa] %pS\n",
80625cf23d7SMinchan Kim 		     &base, &end, (void *)_RET_IP_);
80725cf23d7SMinchan Kim 
808f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.memory, base, size);
80995f72d1eSYinghai Lu }
81095f72d1eSYinghai Lu 
8114d72868cSMike Rapoport /**
8124d72868cSMike Rapoport  * memblock_free - free boot memory block
8134d72868cSMike Rapoport  * @base: phys starting address of the  boot memory block
8144d72868cSMike Rapoport  * @size: size of the boot memory block in bytes
8154d72868cSMike Rapoport  *
8164d72868cSMike Rapoport  * Free boot memory block previously allocated by memblock_alloc_xx() API.
8174d72868cSMike Rapoport  * The freeing memory will not be released to the buddy allocator.
8184d72868cSMike Rapoport  */
819581adcbeSTejun Heo int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
82095f72d1eSYinghai Lu {
8215d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8225d63f81cSMiles Chen 
8235d63f81cSMiles Chen 	memblock_dbg("   memblock_free: [%pa-%pa] %pF\n",
8245d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
82524aa0788STejun Heo 
8269099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
827f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.reserved, base, size);
82895f72d1eSYinghai Lu }
82995f72d1eSYinghai Lu 
830f705ac4bSAlexander Kuleshov int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
83195f72d1eSYinghai Lu {
8325d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8335d63f81cSMiles Chen 
8345d63f81cSMiles Chen 	memblock_dbg("memblock_reserve: [%pa-%pa] %pF\n",
8355d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
83695f72d1eSYinghai Lu 
837f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
83895f72d1eSYinghai Lu }
83995f72d1eSYinghai Lu 
84035fd0808STejun Heo /**
84147cec443SMike Rapoport  * memblock_setclr_flag - set or clear flag for a memory region
84247cec443SMike Rapoport  * @base: base address of the region
84347cec443SMike Rapoport  * @size: size of the region
84447cec443SMike Rapoport  * @set: set or clear the flag
84547cec443SMike Rapoport  * @flag: the flag to udpate
84666b16edfSTang Chen  *
8474308ce17STony Luck  * This function isolates region [@base, @base + @size), and sets/clears flag
84866b16edfSTang Chen  *
84947cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
85066b16edfSTang Chen  */
8514308ce17STony Luck static int __init_memblock memblock_setclr_flag(phys_addr_t base,
8524308ce17STony Luck 				phys_addr_t size, int set, int flag)
85366b16edfSTang Chen {
85466b16edfSTang Chen 	struct memblock_type *type = &memblock.memory;
85566b16edfSTang Chen 	int i, ret, start_rgn, end_rgn;
85666b16edfSTang Chen 
85766b16edfSTang Chen 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
85866b16edfSTang Chen 	if (ret)
85966b16edfSTang Chen 		return ret;
86066b16edfSTang Chen 
861fe145124SMike Rapoport 	for (i = start_rgn; i < end_rgn; i++) {
862fe145124SMike Rapoport 		struct memblock_region *r = &type->regions[i];
863fe145124SMike Rapoport 
8644308ce17STony Luck 		if (set)
865fe145124SMike Rapoport 			r->flags |= flag;
8664308ce17STony Luck 		else
867fe145124SMike Rapoport 			r->flags &= ~flag;
868fe145124SMike Rapoport 	}
86966b16edfSTang Chen 
87066b16edfSTang Chen 	memblock_merge_regions(type);
87166b16edfSTang Chen 	return 0;
87266b16edfSTang Chen }
87366b16edfSTang Chen 
87466b16edfSTang Chen /**
8754308ce17STony Luck  * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
8764308ce17STony Luck  * @base: the base phys addr of the region
8774308ce17STony Luck  * @size: the size of the region
8784308ce17STony Luck  *
87947cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
8804308ce17STony Luck  */
8814308ce17STony Luck int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
8824308ce17STony Luck {
8834308ce17STony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
8844308ce17STony Luck }
8854308ce17STony Luck 
8864308ce17STony Luck /**
88766b16edfSTang Chen  * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
88866b16edfSTang Chen  * @base: the base phys addr of the region
88966b16edfSTang Chen  * @size: the size of the region
89066b16edfSTang Chen  *
89147cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
89266b16edfSTang Chen  */
89366b16edfSTang Chen int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
89466b16edfSTang Chen {
8954308ce17STony Luck 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
89666b16edfSTang Chen }
89766b16edfSTang Chen 
89866b16edfSTang Chen /**
899a3f5bafcSTony Luck  * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
900a3f5bafcSTony Luck  * @base: the base phys addr of the region
901a3f5bafcSTony Luck  * @size: the size of the region
902a3f5bafcSTony Luck  *
90347cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
904a3f5bafcSTony Luck  */
905a3f5bafcSTony Luck int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
906a3f5bafcSTony Luck {
907a3f5bafcSTony Luck 	system_has_some_mirror = true;
908a3f5bafcSTony Luck 
909a3f5bafcSTony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
910a3f5bafcSTony Luck }
911a3f5bafcSTony Luck 
912bf3d3cc5SArd Biesheuvel /**
913bf3d3cc5SArd Biesheuvel  * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
914bf3d3cc5SArd Biesheuvel  * @base: the base phys addr of the region
915bf3d3cc5SArd Biesheuvel  * @size: the size of the region
916bf3d3cc5SArd Biesheuvel  *
91747cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
918bf3d3cc5SArd Biesheuvel  */
919bf3d3cc5SArd Biesheuvel int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
920bf3d3cc5SArd Biesheuvel {
921bf3d3cc5SArd Biesheuvel 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
922bf3d3cc5SArd Biesheuvel }
923a3f5bafcSTony Luck 
924a3f5bafcSTony Luck /**
9254c546b8aSAKASHI Takahiro  * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
9264c546b8aSAKASHI Takahiro  * @base: the base phys addr of the region
9274c546b8aSAKASHI Takahiro  * @size: the size of the region
9284c546b8aSAKASHI Takahiro  *
92947cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
9304c546b8aSAKASHI Takahiro  */
9314c546b8aSAKASHI Takahiro int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
9324c546b8aSAKASHI Takahiro {
9334c546b8aSAKASHI Takahiro 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
9344c546b8aSAKASHI Takahiro }
9354c546b8aSAKASHI Takahiro 
9364c546b8aSAKASHI Takahiro /**
9378e7a7f86SRobin Holt  * __next_reserved_mem_region - next function for for_each_reserved_region()
9388e7a7f86SRobin Holt  * @idx: pointer to u64 loop variable
9398e7a7f86SRobin Holt  * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
9408e7a7f86SRobin Holt  * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
9418e7a7f86SRobin Holt  *
9428e7a7f86SRobin Holt  * Iterate over all reserved memory regions.
9438e7a7f86SRobin Holt  */
9448e7a7f86SRobin Holt void __init_memblock __next_reserved_mem_region(u64 *idx,
9458e7a7f86SRobin Holt 					   phys_addr_t *out_start,
9468e7a7f86SRobin Holt 					   phys_addr_t *out_end)
9478e7a7f86SRobin Holt {
948567d117bSAlexander Kuleshov 	struct memblock_type *type = &memblock.reserved;
9498e7a7f86SRobin Holt 
950cd33a76bSRichard Leitner 	if (*idx < type->cnt) {
951567d117bSAlexander Kuleshov 		struct memblock_region *r = &type->regions[*idx];
9528e7a7f86SRobin Holt 		phys_addr_t base = r->base;
9538e7a7f86SRobin Holt 		phys_addr_t size = r->size;
9548e7a7f86SRobin Holt 
9558e7a7f86SRobin Holt 		if (out_start)
9568e7a7f86SRobin Holt 			*out_start = base;
9578e7a7f86SRobin Holt 		if (out_end)
9588e7a7f86SRobin Holt 			*out_end = base + size - 1;
9598e7a7f86SRobin Holt 
9608e7a7f86SRobin Holt 		*idx += 1;
9618e7a7f86SRobin Holt 		return;
9628e7a7f86SRobin Holt 	}
9638e7a7f86SRobin Holt 
9648e7a7f86SRobin Holt 	/* signal end of iteration */
9658e7a7f86SRobin Holt 	*idx = ULLONG_MAX;
9668e7a7f86SRobin Holt }
9678e7a7f86SRobin Holt 
968*c9a688a3SMike Rapoport static bool should_skip_region(struct memblock_region *m, int nid, int flags)
969*c9a688a3SMike Rapoport {
970*c9a688a3SMike Rapoport 	int m_nid = memblock_get_region_node(m);
971*c9a688a3SMike Rapoport 
972*c9a688a3SMike Rapoport 	/* only memory regions are associated with nodes, check it */
973*c9a688a3SMike Rapoport 	if (nid != NUMA_NO_NODE && nid != m_nid)
974*c9a688a3SMike Rapoport 		return true;
975*c9a688a3SMike Rapoport 
976*c9a688a3SMike Rapoport 	/* skip hotpluggable memory regions if needed */
977*c9a688a3SMike Rapoport 	if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
978*c9a688a3SMike Rapoport 		return true;
979*c9a688a3SMike Rapoport 
980*c9a688a3SMike Rapoport 	/* if we want mirror memory skip non-mirror memory regions */
981*c9a688a3SMike Rapoport 	if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
982*c9a688a3SMike Rapoport 		return true;
983*c9a688a3SMike Rapoport 
984*c9a688a3SMike Rapoport 	/* skip nomap memory unless we were asked for it explicitly */
985*c9a688a3SMike Rapoport 	if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
986*c9a688a3SMike Rapoport 		return true;
987*c9a688a3SMike Rapoport 
988*c9a688a3SMike Rapoport 	return false;
989*c9a688a3SMike Rapoport }
990*c9a688a3SMike Rapoport 
9918e7a7f86SRobin Holt /**
992f1af9d3aSPhilipp Hachtmann  * __next__mem_range - next function for for_each_free_mem_range() etc.
99335fd0808STejun Heo  * @idx: pointer to u64 loop variable
994b1154233SGrygorii Strashko  * @nid: node selector, %NUMA_NO_NODE for all nodes
995fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
996f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
997f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
998dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
999dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1000dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
100135fd0808STejun Heo  *
1002f1af9d3aSPhilipp Hachtmann  * Find the first area from *@idx which matches @nid, fill the out
100335fd0808STejun Heo  * parameters, and update *@idx for the next iteration.  The lower 32bit of
1004f1af9d3aSPhilipp Hachtmann  * *@idx contains index into type_a and the upper 32bit indexes the
1005f1af9d3aSPhilipp Hachtmann  * areas before each region in type_b.	For example, if type_b regions
100635fd0808STejun Heo  * look like the following,
100735fd0808STejun Heo  *
100835fd0808STejun Heo  *	0:[0-16), 1:[32-48), 2:[128-130)
100935fd0808STejun Heo  *
101035fd0808STejun Heo  * The upper 32bit indexes the following regions.
101135fd0808STejun Heo  *
101235fd0808STejun Heo  *	0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
101335fd0808STejun Heo  *
101435fd0808STejun Heo  * As both region arrays are sorted, the function advances the two indices
101535fd0808STejun Heo  * in lockstep and returns each intersection.
101635fd0808STejun Heo  */
1017e1720feeSMike Rapoport void __init_memblock __next_mem_range(u64 *idx, int nid,
1018e1720feeSMike Rapoport 				      enum memblock_flags flags,
1019f1af9d3aSPhilipp Hachtmann 				      struct memblock_type *type_a,
1020f1af9d3aSPhilipp Hachtmann 				      struct memblock_type *type_b,
102135fd0808STejun Heo 				      phys_addr_t *out_start,
102235fd0808STejun Heo 				      phys_addr_t *out_end, int *out_nid)
102335fd0808STejun Heo {
1024f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1025f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1026b1154233SGrygorii Strashko 
1027f1af9d3aSPhilipp Hachtmann 	if (WARN_ONCE(nid == MAX_NUMNODES,
1028f1af9d3aSPhilipp Hachtmann 	"Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1029560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
103035fd0808STejun Heo 
1031f1af9d3aSPhilipp Hachtmann 	for (; idx_a < type_a->cnt; idx_a++) {
1032f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1033f1af9d3aSPhilipp Hachtmann 
103435fd0808STejun Heo 		phys_addr_t m_start = m->base;
103535fd0808STejun Heo 		phys_addr_t m_end = m->base + m->size;
1036f1af9d3aSPhilipp Hachtmann 		int	    m_nid = memblock_get_region_node(m);
103735fd0808STejun Heo 
1038*c9a688a3SMike Rapoport 		if (should_skip_region(m, nid, flags))
1039bf3d3cc5SArd Biesheuvel 			continue;
1040bf3d3cc5SArd Biesheuvel 
1041f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1042f1af9d3aSPhilipp Hachtmann 			if (out_start)
1043f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1044f1af9d3aSPhilipp Hachtmann 			if (out_end)
1045f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1046f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1047f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1048f1af9d3aSPhilipp Hachtmann 			idx_a++;
1049f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1050f1af9d3aSPhilipp Hachtmann 			return;
1051f1af9d3aSPhilipp Hachtmann 		}
105235fd0808STejun Heo 
1053f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1054f1af9d3aSPhilipp Hachtmann 		for (; idx_b < type_b->cnt + 1; idx_b++) {
1055f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1056f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1057f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1058f1af9d3aSPhilipp Hachtmann 
1059f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1060f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1061f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
10621c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1063f1af9d3aSPhilipp Hachtmann 
1064f1af9d3aSPhilipp Hachtmann 			/*
1065f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1066f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1067f1af9d3aSPhilipp Hachtmann 			 */
106835fd0808STejun Heo 			if (r_start >= m_end)
106935fd0808STejun Heo 				break;
107035fd0808STejun Heo 			/* if the two regions intersect, we're done */
107135fd0808STejun Heo 			if (m_start < r_end) {
107235fd0808STejun Heo 				if (out_start)
1073f1af9d3aSPhilipp Hachtmann 					*out_start =
1074f1af9d3aSPhilipp Hachtmann 						max(m_start, r_start);
107535fd0808STejun Heo 				if (out_end)
107635fd0808STejun Heo 					*out_end = min(m_end, r_end);
107735fd0808STejun Heo 				if (out_nid)
1078f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
107935fd0808STejun Heo 				/*
1080f1af9d3aSPhilipp Hachtmann 				 * The region which ends first is
1081f1af9d3aSPhilipp Hachtmann 				 * advanced for the next iteration.
108235fd0808STejun Heo 				 */
108335fd0808STejun Heo 				if (m_end <= r_end)
1084f1af9d3aSPhilipp Hachtmann 					idx_a++;
108535fd0808STejun Heo 				else
1086f1af9d3aSPhilipp Hachtmann 					idx_b++;
1087f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
108835fd0808STejun Heo 				return;
108935fd0808STejun Heo 			}
109035fd0808STejun Heo 		}
109135fd0808STejun Heo 	}
109235fd0808STejun Heo 
109335fd0808STejun Heo 	/* signal end of iteration */
109435fd0808STejun Heo 	*idx = ULLONG_MAX;
109535fd0808STejun Heo }
109635fd0808STejun Heo 
10977bd0b0f0STejun Heo /**
1098f1af9d3aSPhilipp Hachtmann  * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1099f1af9d3aSPhilipp Hachtmann  *
11007bd0b0f0STejun Heo  * @idx: pointer to u64 loop variable
1101ad5ea8cdSAlexander Kuleshov  * @nid: node selector, %NUMA_NO_NODE for all nodes
1102fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
1103f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
1104f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
1105dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1106dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1107dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
11087bd0b0f0STejun Heo  *
110947cec443SMike Rapoport  * Finds the next range from type_a which is not marked as unsuitable
111047cec443SMike Rapoport  * in type_b.
111147cec443SMike Rapoport  *
1112f1af9d3aSPhilipp Hachtmann  * Reverse of __next_mem_range().
11137bd0b0f0STejun Heo  */
1114e1720feeSMike Rapoport void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1115e1720feeSMike Rapoport 					  enum memblock_flags flags,
1116f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_a,
1117f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_b,
11187bd0b0f0STejun Heo 					  phys_addr_t *out_start,
11197bd0b0f0STejun Heo 					  phys_addr_t *out_end, int *out_nid)
11207bd0b0f0STejun Heo {
1121f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1122f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1123b1154233SGrygorii Strashko 
1124560dca27SGrygorii Strashko 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1125560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
11267bd0b0f0STejun Heo 
11277bd0b0f0STejun Heo 	if (*idx == (u64)ULLONG_MAX) {
1128f1af9d3aSPhilipp Hachtmann 		idx_a = type_a->cnt - 1;
1129e47608abSzijun_hu 		if (type_b != NULL)
1130f1af9d3aSPhilipp Hachtmann 			idx_b = type_b->cnt;
1131e47608abSzijun_hu 		else
1132e47608abSzijun_hu 			idx_b = 0;
11337bd0b0f0STejun Heo 	}
11347bd0b0f0STejun Heo 
1135f1af9d3aSPhilipp Hachtmann 	for (; idx_a >= 0; idx_a--) {
1136f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1137f1af9d3aSPhilipp Hachtmann 
11387bd0b0f0STejun Heo 		phys_addr_t m_start = m->base;
11397bd0b0f0STejun Heo 		phys_addr_t m_end = m->base + m->size;
1140f1af9d3aSPhilipp Hachtmann 		int m_nid = memblock_get_region_node(m);
11417bd0b0f0STejun Heo 
1142*c9a688a3SMike Rapoport 		if (should_skip_region(m, nid, flags))
1143bf3d3cc5SArd Biesheuvel 			continue;
1144bf3d3cc5SArd Biesheuvel 
1145f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1146f1af9d3aSPhilipp Hachtmann 			if (out_start)
1147f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1148f1af9d3aSPhilipp Hachtmann 			if (out_end)
1149f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1150f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1151f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1152fb399b48Szijun_hu 			idx_a--;
1153f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1154f1af9d3aSPhilipp Hachtmann 			return;
1155f1af9d3aSPhilipp Hachtmann 		}
11567bd0b0f0STejun Heo 
1157f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1158f1af9d3aSPhilipp Hachtmann 		for (; idx_b >= 0; idx_b--) {
1159f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1160f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1161f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1162f1af9d3aSPhilipp Hachtmann 
1163f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1164f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1165f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
11661c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1167f1af9d3aSPhilipp Hachtmann 			/*
1168f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1169f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1170f1af9d3aSPhilipp Hachtmann 			 */
1171f1af9d3aSPhilipp Hachtmann 
11727bd0b0f0STejun Heo 			if (r_end <= m_start)
11737bd0b0f0STejun Heo 				break;
11747bd0b0f0STejun Heo 			/* if the two regions intersect, we're done */
11757bd0b0f0STejun Heo 			if (m_end > r_start) {
11767bd0b0f0STejun Heo 				if (out_start)
11777bd0b0f0STejun Heo 					*out_start = max(m_start, r_start);
11787bd0b0f0STejun Heo 				if (out_end)
11797bd0b0f0STejun Heo 					*out_end = min(m_end, r_end);
11807bd0b0f0STejun Heo 				if (out_nid)
1181f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
11827bd0b0f0STejun Heo 				if (m_start >= r_start)
1183f1af9d3aSPhilipp Hachtmann 					idx_a--;
11847bd0b0f0STejun Heo 				else
1185f1af9d3aSPhilipp Hachtmann 					idx_b--;
1186f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
11877bd0b0f0STejun Heo 				return;
11887bd0b0f0STejun Heo 			}
11897bd0b0f0STejun Heo 		}
11907bd0b0f0STejun Heo 	}
1191f1af9d3aSPhilipp Hachtmann 	/* signal end of iteration */
11927bd0b0f0STejun Heo 	*idx = ULLONG_MAX;
11937bd0b0f0STejun Heo }
11947bd0b0f0STejun Heo 
11957c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
11967c0caeb8STejun Heo /*
119745e79815SChen Chang  * Common iterator interface used to define for_each_mem_pfn_range().
11987c0caeb8STejun Heo  */
11997c0caeb8STejun Heo void __init_memblock __next_mem_pfn_range(int *idx, int nid,
12007c0caeb8STejun Heo 				unsigned long *out_start_pfn,
12017c0caeb8STejun Heo 				unsigned long *out_end_pfn, int *out_nid)
12027c0caeb8STejun Heo {
12037c0caeb8STejun Heo 	struct memblock_type *type = &memblock.memory;
12047c0caeb8STejun Heo 	struct memblock_region *r;
12057c0caeb8STejun Heo 
12067c0caeb8STejun Heo 	while (++*idx < type->cnt) {
12077c0caeb8STejun Heo 		r = &type->regions[*idx];
12087c0caeb8STejun Heo 
12097c0caeb8STejun Heo 		if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
12107c0caeb8STejun Heo 			continue;
12117c0caeb8STejun Heo 		if (nid == MAX_NUMNODES || nid == r->nid)
12127c0caeb8STejun Heo 			break;
12137c0caeb8STejun Heo 	}
12147c0caeb8STejun Heo 	if (*idx >= type->cnt) {
12157c0caeb8STejun Heo 		*idx = -1;
12167c0caeb8STejun Heo 		return;
12177c0caeb8STejun Heo 	}
12187c0caeb8STejun Heo 
12197c0caeb8STejun Heo 	if (out_start_pfn)
12207c0caeb8STejun Heo 		*out_start_pfn = PFN_UP(r->base);
12217c0caeb8STejun Heo 	if (out_end_pfn)
12227c0caeb8STejun Heo 		*out_end_pfn = PFN_DOWN(r->base + r->size);
12237c0caeb8STejun Heo 	if (out_nid)
12247c0caeb8STejun Heo 		*out_nid = r->nid;
12257c0caeb8STejun Heo }
12267c0caeb8STejun Heo 
12277c0caeb8STejun Heo /**
12287c0caeb8STejun Heo  * memblock_set_node - set node ID on memblock regions
12297c0caeb8STejun Heo  * @base: base of area to set node ID for
12307c0caeb8STejun Heo  * @size: size of area to set node ID for
1231e7e8de59STang Chen  * @type: memblock type to set node ID for
12327c0caeb8STejun Heo  * @nid: node ID to set
12337c0caeb8STejun Heo  *
1234e7e8de59STang Chen  * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
12357c0caeb8STejun Heo  * Regions which cross the area boundaries are split as necessary.
12367c0caeb8STejun Heo  *
123747cec443SMike Rapoport  * Return:
12387c0caeb8STejun Heo  * 0 on success, -errno on failure.
12397c0caeb8STejun Heo  */
12407c0caeb8STejun Heo int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1241e7e8de59STang Chen 				      struct memblock_type *type, int nid)
12427c0caeb8STejun Heo {
12436a9ceb31STejun Heo 	int start_rgn, end_rgn;
12446a9ceb31STejun Heo 	int i, ret;
12457c0caeb8STejun Heo 
12466a9ceb31STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
12476a9ceb31STejun Heo 	if (ret)
12486a9ceb31STejun Heo 		return ret;
12497c0caeb8STejun Heo 
12506a9ceb31STejun Heo 	for (i = start_rgn; i < end_rgn; i++)
1251e9d24ad3SWanpeng Li 		memblock_set_region_node(&type->regions[i], nid);
12527c0caeb8STejun Heo 
12537c0caeb8STejun Heo 	memblock_merge_regions(type);
12547c0caeb8STejun Heo 	return 0;
12557c0caeb8STejun Heo }
12567c0caeb8STejun Heo #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
12577c0caeb8STejun Heo 
125892d12f95SMike Rapoport /**
125992d12f95SMike Rapoport  * memblock_alloc_range_nid - allocate boot memory block
126092d12f95SMike Rapoport  * @size: size of memory block to be allocated in bytes
126192d12f95SMike Rapoport  * @align: alignment of the region and block's size
126292d12f95SMike Rapoport  * @start: the lower bound of the memory region to allocate (phys address)
126392d12f95SMike Rapoport  * @end: the upper bound of the memory region to allocate (phys address)
126492d12f95SMike Rapoport  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
126592d12f95SMike Rapoport  *
126692d12f95SMike Rapoport  * The allocation is performed from memory region limited by
126792d12f95SMike Rapoport  * memblock.current_limit if @max_addr == %MEMBLOCK_ALLOC_ACCESSIBLE.
126892d12f95SMike Rapoport  *
126992d12f95SMike Rapoport  * If the specified node can not hold the requested memory the
127092d12f95SMike Rapoport  * allocation falls back to any node in the system
127192d12f95SMike Rapoport  *
127292d12f95SMike Rapoport  * For systems with memory mirroring, the allocation is attempted first
127392d12f95SMike Rapoport  * from the regions with mirroring enabled and then retried from any
127492d12f95SMike Rapoport  * memory region.
127592d12f95SMike Rapoport  *
127692d12f95SMike Rapoport  * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for
127792d12f95SMike Rapoport  * allocated boot memory block, so that it is never reported as leaks.
127892d12f95SMike Rapoport  *
127992d12f95SMike Rapoport  * Return:
128092d12f95SMike Rapoport  * Physical address of allocated memory block on success, %0 on failure.
128192d12f95SMike Rapoport  */
12822bfc2862SAkinobu Mita static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
12832bfc2862SAkinobu Mita 					phys_addr_t align, phys_addr_t start,
128492d12f95SMike Rapoport 					phys_addr_t end, int nid)
128595f72d1eSYinghai Lu {
128692d12f95SMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
12876ed311b2SBenjamin Herrenschmidt 	phys_addr_t found;
128895f72d1eSYinghai Lu 
128992d12f95SMike Rapoport 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
129092d12f95SMike Rapoport 		nid = NUMA_NO_NODE;
129192d12f95SMike Rapoport 
12922f770806SMike Rapoport 	if (!align) {
12932f770806SMike Rapoport 		/* Can't use WARNs this early in boot on powerpc */
12942f770806SMike Rapoport 		dump_stack();
12952f770806SMike Rapoport 		align = SMP_CACHE_BYTES;
12962f770806SMike Rapoport 	}
12972f770806SMike Rapoport 
129892d12f95SMike Rapoport 	if (end > memblock.current_limit)
129992d12f95SMike Rapoport 		end = memblock.current_limit;
130092d12f95SMike Rapoport 
130192d12f95SMike Rapoport again:
1302fc6daaf9STony Luck 	found = memblock_find_in_range_node(size, align, start, end, nid,
1303fc6daaf9STony Luck 					    flags);
130492d12f95SMike Rapoport 	if (found && !memblock_reserve(found, size))
130592d12f95SMike Rapoport 		goto done;
130692d12f95SMike Rapoport 
130792d12f95SMike Rapoport 	if (nid != NUMA_NO_NODE) {
130892d12f95SMike Rapoport 		found = memblock_find_in_range_node(size, align, start,
130992d12f95SMike Rapoport 						    end, NUMA_NO_NODE,
131092d12f95SMike Rapoport 						    flags);
131192d12f95SMike Rapoport 		if (found && !memblock_reserve(found, size))
131292d12f95SMike Rapoport 			goto done;
131392d12f95SMike Rapoport 	}
131492d12f95SMike Rapoport 
131592d12f95SMike Rapoport 	if (flags & MEMBLOCK_MIRROR) {
131692d12f95SMike Rapoport 		flags &= ~MEMBLOCK_MIRROR;
131792d12f95SMike Rapoport 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
131892d12f95SMike Rapoport 			&size);
131992d12f95SMike Rapoport 		goto again;
132092d12f95SMike Rapoport 	}
132192d12f95SMike Rapoport 
132292d12f95SMike Rapoport 	return 0;
132392d12f95SMike Rapoport 
132492d12f95SMike Rapoport done:
132592d12f95SMike Rapoport 	/* Skip kmemleak for kasan_init() due to high volume. */
132692d12f95SMike Rapoport 	if (end != MEMBLOCK_ALLOC_KASAN)
1327aedf95eaSCatalin Marinas 		/*
132892d12f95SMike Rapoport 		 * The min_count is set to 0 so that memblock allocated
132992d12f95SMike Rapoport 		 * blocks are never reported as leaks. This is because many
133092d12f95SMike Rapoport 		 * of these blocks are only referred via the physical
133192d12f95SMike Rapoport 		 * address which is not looked up by kmemleak.
1332aedf95eaSCatalin Marinas 		 */
13339099daedSCatalin Marinas 		kmemleak_alloc_phys(found, size, 0, 0);
133492d12f95SMike Rapoport 
13356ed311b2SBenjamin Herrenschmidt 	return found;
1336aedf95eaSCatalin Marinas }
133795f72d1eSYinghai Lu 
13388a770c2aSMike Rapoport phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
13398a770c2aSMike Rapoport 					     phys_addr_t align,
13408a770c2aSMike Rapoport 					     phys_addr_t start,
13418a770c2aSMike Rapoport 					     phys_addr_t end)
13422bfc2862SAkinobu Mita {
134392d12f95SMike Rapoport 	return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE);
13447bd0b0f0STejun Heo }
13457bd0b0f0STejun Heo 
13469a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
13479d1e2492SBenjamin Herrenschmidt {
134833755574SMike Rapoport 	return memblock_alloc_range_nid(size, align, 0,
134992d12f95SMike Rapoport 					MEMBLOCK_ALLOC_ACCESSIBLE, nid);
135095f72d1eSYinghai Lu }
135195f72d1eSYinghai Lu 
135226f09e9bSSantosh Shilimkar /**
1353eb31d559SMike Rapoport  * memblock_alloc_internal - allocate boot memory block
135426f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
135526f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
135626f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region to allocate (phys address)
135726f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region to allocate (phys address)
135826f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
135926f09e9bSSantosh Shilimkar  *
136092d12f95SMike Rapoport  * Allocates memory block using memblock_alloc_range_nid() and
136192d12f95SMike Rapoport  * converts the returned physical address to virtual.
136292d12f95SMike Rapoport  *
136326f09e9bSSantosh Shilimkar  * The @min_addr limit is dropped if it can not be satisfied and the allocation
136492d12f95SMike Rapoport  * will fall back to memory below @min_addr. Other constraints, such
136592d12f95SMike Rapoport  * as node and mirrored memory will be handled again in
136692d12f95SMike Rapoport  * memblock_alloc_range_nid().
136726f09e9bSSantosh Shilimkar  *
136847cec443SMike Rapoport  * Return:
136926f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
137026f09e9bSSantosh Shilimkar  */
1371eb31d559SMike Rapoport static void * __init memblock_alloc_internal(
137226f09e9bSSantosh Shilimkar 				phys_addr_t size, phys_addr_t align,
137326f09e9bSSantosh Shilimkar 				phys_addr_t min_addr, phys_addr_t max_addr,
137426f09e9bSSantosh Shilimkar 				int nid)
137526f09e9bSSantosh Shilimkar {
137626f09e9bSSantosh Shilimkar 	phys_addr_t alloc;
137726f09e9bSSantosh Shilimkar 
137826f09e9bSSantosh Shilimkar 	/*
137926f09e9bSSantosh Shilimkar 	 * Detect any accidental use of these APIs after slab is ready, as at
138026f09e9bSSantosh Shilimkar 	 * this moment memblock may be deinitialized already and its
1381c6ffc5caSMike Rapoport 	 * internal data may be destroyed (after execution of memblock_free_all)
138226f09e9bSSantosh Shilimkar 	 */
138326f09e9bSSantosh Shilimkar 	if (WARN_ON_ONCE(slab_is_available()))
138426f09e9bSSantosh Shilimkar 		return kzalloc_node(size, GFP_NOWAIT, nid);
138526f09e9bSSantosh Shilimkar 
138692d12f95SMike Rapoport 	alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid);
13872f770806SMike Rapoport 
138892d12f95SMike Rapoport 	/* retry allocation without lower limit */
138992d12f95SMike Rapoport 	if (!alloc && min_addr)
139092d12f95SMike Rapoport 		alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid);
139126f09e9bSSantosh Shilimkar 
139292d12f95SMike Rapoport 	if (!alloc)
1393a3f5bafcSTony Luck 		return NULL;
139426f09e9bSSantosh Shilimkar 
139592d12f95SMike Rapoport 	return phys_to_virt(alloc);
139626f09e9bSSantosh Shilimkar }
139726f09e9bSSantosh Shilimkar 
139826f09e9bSSantosh Shilimkar /**
1399eb31d559SMike Rapoport  * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
1400ea1f5f37SPavel Tatashin  * memory and without panicking
1401ea1f5f37SPavel Tatashin  * @size: size of memory block to be allocated in bytes
1402ea1f5f37SPavel Tatashin  * @align: alignment of the region and block's size
1403ea1f5f37SPavel Tatashin  * @min_addr: the lower bound of the memory region from where the allocation
1404ea1f5f37SPavel Tatashin  *	  is preferred (phys address)
1405ea1f5f37SPavel Tatashin  * @max_addr: the upper bound of the memory region from where the allocation
140697ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1407ea1f5f37SPavel Tatashin  *	      allocate only from memory limited by memblock.current_limit value
1408ea1f5f37SPavel Tatashin  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1409ea1f5f37SPavel Tatashin  *
1410ea1f5f37SPavel Tatashin  * Public function, provides additional debug information (including caller
1411ea1f5f37SPavel Tatashin  * info), if enabled. Does not zero allocated memory, does not panic if request
1412ea1f5f37SPavel Tatashin  * cannot be satisfied.
1413ea1f5f37SPavel Tatashin  *
141447cec443SMike Rapoport  * Return:
1415ea1f5f37SPavel Tatashin  * Virtual address of allocated memory block on success, NULL on failure.
1416ea1f5f37SPavel Tatashin  */
1417eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_raw(
1418ea1f5f37SPavel Tatashin 			phys_addr_t size, phys_addr_t align,
1419ea1f5f37SPavel Tatashin 			phys_addr_t min_addr, phys_addr_t max_addr,
1420ea1f5f37SPavel Tatashin 			int nid)
1421ea1f5f37SPavel Tatashin {
1422ea1f5f37SPavel Tatashin 	void *ptr;
1423ea1f5f37SPavel Tatashin 
1424a36aab89SMike Rapoport 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1425a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1426a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1427ea1f5f37SPavel Tatashin 
1428eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
1429ea1f5f37SPavel Tatashin 					   min_addr, max_addr, nid);
1430ea1f5f37SPavel Tatashin 	if (ptr && size > 0)
1431f682a97aSAlexander Duyck 		page_init_poison(ptr, size);
1432f682a97aSAlexander Duyck 
1433ea1f5f37SPavel Tatashin 	return ptr;
1434ea1f5f37SPavel Tatashin }
1435ea1f5f37SPavel Tatashin 
1436ea1f5f37SPavel Tatashin /**
1437c0dbe825SMike Rapoport  * memblock_alloc_try_nid - allocate boot memory block
143826f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
143926f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
144026f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region from where the allocation
144126f09e9bSSantosh Shilimkar  *	  is preferred (phys address)
144226f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region from where the allocation
144397ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
144426f09e9bSSantosh Shilimkar  *	      allocate only from memory limited by memblock.current_limit value
144526f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
144626f09e9bSSantosh Shilimkar  *
1447c0dbe825SMike Rapoport  * Public function, provides additional debug information (including caller
1448c0dbe825SMike Rapoport  * info), if enabled. This function zeroes the allocated memory.
144926f09e9bSSantosh Shilimkar  *
145047cec443SMike Rapoport  * Return:
145126f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
145226f09e9bSSantosh Shilimkar  */
1453eb31d559SMike Rapoport void * __init memblock_alloc_try_nid(
145426f09e9bSSantosh Shilimkar 			phys_addr_t size, phys_addr_t align,
145526f09e9bSSantosh Shilimkar 			phys_addr_t min_addr, phys_addr_t max_addr,
145626f09e9bSSantosh Shilimkar 			int nid)
145726f09e9bSSantosh Shilimkar {
145826f09e9bSSantosh Shilimkar 	void *ptr;
145926f09e9bSSantosh Shilimkar 
1460a36aab89SMike Rapoport 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1461a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1462a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1463eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
146426f09e9bSSantosh Shilimkar 					   min_addr, max_addr, nid);
1465c0dbe825SMike Rapoport 	if (ptr)
1466ea1f5f37SPavel Tatashin 		memset(ptr, 0, size);
146726f09e9bSSantosh Shilimkar 
1468c0dbe825SMike Rapoport 	return ptr;
146926f09e9bSSantosh Shilimkar }
147026f09e9bSSantosh Shilimkar 
147126f09e9bSSantosh Shilimkar /**
147226f09e9bSSantosh Shilimkar  * __memblock_free_late - free bootmem block pages directly to buddy allocator
147348a833ccSMike Rapoport  * @base: phys starting address of the  boot memory block
147426f09e9bSSantosh Shilimkar  * @size: size of the boot memory block in bytes
147526f09e9bSSantosh Shilimkar  *
147626f09e9bSSantosh Shilimkar  * This is only useful when the bootmem allocator has already been torn
147726f09e9bSSantosh Shilimkar  * down, but we are still initializing the system.  Pages are released directly
147826f09e9bSSantosh Shilimkar  * to the buddy allocator, no bootmem metadata is updated because it is gone.
147926f09e9bSSantosh Shilimkar  */
148026f09e9bSSantosh Shilimkar void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
148126f09e9bSSantosh Shilimkar {
1482a36aab89SMike Rapoport 	phys_addr_t cursor, end;
148326f09e9bSSantosh Shilimkar 
1484a36aab89SMike Rapoport 	end = base + size - 1;
1485a36aab89SMike Rapoport 	memblock_dbg("%s: [%pa-%pa] %pF\n",
1486a36aab89SMike Rapoport 		     __func__, &base, &end, (void *)_RET_IP_);
14879099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
148826f09e9bSSantosh Shilimkar 	cursor = PFN_UP(base);
148926f09e9bSSantosh Shilimkar 	end = PFN_DOWN(base + size);
149026f09e9bSSantosh Shilimkar 
149126f09e9bSSantosh Shilimkar 	for (; cursor < end; cursor++) {
14927c2ee349SMike Rapoport 		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
1493ca79b0c2SArun KS 		totalram_pages_inc();
149426f09e9bSSantosh Shilimkar 	}
149526f09e9bSSantosh Shilimkar }
14969d1e2492SBenjamin Herrenschmidt 
14979d1e2492SBenjamin Herrenschmidt /*
14989d1e2492SBenjamin Herrenschmidt  * Remaining API functions
14999d1e2492SBenjamin Herrenschmidt  */
15009d1e2492SBenjamin Herrenschmidt 
15011f1ffb8aSDavid Gibson phys_addr_t __init_memblock memblock_phys_mem_size(void)
150295f72d1eSYinghai Lu {
15031440c4e2STejun Heo 	return memblock.memory.total_size;
150495f72d1eSYinghai Lu }
150595f72d1eSYinghai Lu 
15068907de5dSSrikar Dronamraju phys_addr_t __init_memblock memblock_reserved_size(void)
15078907de5dSSrikar Dronamraju {
15088907de5dSSrikar Dronamraju 	return memblock.reserved.total_size;
15098907de5dSSrikar Dronamraju }
15108907de5dSSrikar Dronamraju 
1511595ad9afSYinghai Lu phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1512595ad9afSYinghai Lu {
1513595ad9afSYinghai Lu 	unsigned long pages = 0;
1514595ad9afSYinghai Lu 	struct memblock_region *r;
1515595ad9afSYinghai Lu 	unsigned long start_pfn, end_pfn;
1516595ad9afSYinghai Lu 
1517595ad9afSYinghai Lu 	for_each_memblock(memory, r) {
1518595ad9afSYinghai Lu 		start_pfn = memblock_region_memory_base_pfn(r);
1519595ad9afSYinghai Lu 		end_pfn = memblock_region_memory_end_pfn(r);
1520595ad9afSYinghai Lu 		start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1521595ad9afSYinghai Lu 		end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1522595ad9afSYinghai Lu 		pages += end_pfn - start_pfn;
1523595ad9afSYinghai Lu 	}
1524595ad9afSYinghai Lu 
152516763230SFabian Frederick 	return PFN_PHYS(pages);
1526595ad9afSYinghai Lu }
1527595ad9afSYinghai Lu 
15280a93ebefSSam Ravnborg /* lowest address */
15290a93ebefSSam Ravnborg phys_addr_t __init_memblock memblock_start_of_DRAM(void)
15300a93ebefSSam Ravnborg {
15310a93ebefSSam Ravnborg 	return memblock.memory.regions[0].base;
15320a93ebefSSam Ravnborg }
15330a93ebefSSam Ravnborg 
153410d06439SYinghai Lu phys_addr_t __init_memblock memblock_end_of_DRAM(void)
153595f72d1eSYinghai Lu {
153695f72d1eSYinghai Lu 	int idx = memblock.memory.cnt - 1;
153795f72d1eSYinghai Lu 
1538e3239ff9SBenjamin Herrenschmidt 	return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
153995f72d1eSYinghai Lu }
154095f72d1eSYinghai Lu 
1541a571d4ebSDennis Chen static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
154295f72d1eSYinghai Lu {
15431c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1544136199f0SEmil Medve 	struct memblock_region *r;
154595f72d1eSYinghai Lu 
1546a571d4ebSDennis Chen 	/*
1547a571d4ebSDennis Chen 	 * translate the memory @limit size into the max address within one of
1548a571d4ebSDennis Chen 	 * the memory memblock regions, if the @limit exceeds the total size
15491c4bc43dSStefan Agner 	 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
1550a571d4ebSDennis Chen 	 */
1551136199f0SEmil Medve 	for_each_memblock(memory, r) {
1552c0ce8fefSTejun Heo 		if (limit <= r->size) {
1553c0ce8fefSTejun Heo 			max_addr = r->base + limit;
155495f72d1eSYinghai Lu 			break;
155595f72d1eSYinghai Lu 		}
1556c0ce8fefSTejun Heo 		limit -= r->size;
155795f72d1eSYinghai Lu 	}
1558c0ce8fefSTejun Heo 
1559a571d4ebSDennis Chen 	return max_addr;
1560a571d4ebSDennis Chen }
1561a571d4ebSDennis Chen 
1562a571d4ebSDennis Chen void __init memblock_enforce_memory_limit(phys_addr_t limit)
1563a571d4ebSDennis Chen {
15641c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1565a571d4ebSDennis Chen 
1566a571d4ebSDennis Chen 	if (!limit)
1567a571d4ebSDennis Chen 		return;
1568a571d4ebSDennis Chen 
1569a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1570a571d4ebSDennis Chen 
1571a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
15721c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1573a571d4ebSDennis Chen 		return;
1574a571d4ebSDennis Chen 
1575c0ce8fefSTejun Heo 	/* truncate both memory and reserved regions */
1576f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.memory, max_addr,
15771c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
1578f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.reserved, max_addr,
15791c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
158095f72d1eSYinghai Lu }
158195f72d1eSYinghai Lu 
1582c9ca9b4eSAKASHI Takahiro void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1583c9ca9b4eSAKASHI Takahiro {
1584c9ca9b4eSAKASHI Takahiro 	int start_rgn, end_rgn;
1585c9ca9b4eSAKASHI Takahiro 	int i, ret;
1586c9ca9b4eSAKASHI Takahiro 
1587c9ca9b4eSAKASHI Takahiro 	if (!size)
1588c9ca9b4eSAKASHI Takahiro 		return;
1589c9ca9b4eSAKASHI Takahiro 
1590c9ca9b4eSAKASHI Takahiro 	ret = memblock_isolate_range(&memblock.memory, base, size,
1591c9ca9b4eSAKASHI Takahiro 						&start_rgn, &end_rgn);
1592c9ca9b4eSAKASHI Takahiro 	if (ret)
1593c9ca9b4eSAKASHI Takahiro 		return;
1594c9ca9b4eSAKASHI Takahiro 
1595c9ca9b4eSAKASHI Takahiro 	/* remove all the MAP regions */
1596c9ca9b4eSAKASHI Takahiro 	for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1597c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1598c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1599c9ca9b4eSAKASHI Takahiro 
1600c9ca9b4eSAKASHI Takahiro 	for (i = start_rgn - 1; i >= 0; i--)
1601c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1602c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1603c9ca9b4eSAKASHI Takahiro 
1604c9ca9b4eSAKASHI Takahiro 	/* truncate the reserved regions */
1605c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved, 0, base);
1606c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved,
16071c4bc43dSStefan Agner 			base + size, PHYS_ADDR_MAX);
1608c9ca9b4eSAKASHI Takahiro }
1609c9ca9b4eSAKASHI Takahiro 
1610a571d4ebSDennis Chen void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1611a571d4ebSDennis Chen {
1612a571d4ebSDennis Chen 	phys_addr_t max_addr;
1613a571d4ebSDennis Chen 
1614a571d4ebSDennis Chen 	if (!limit)
1615a571d4ebSDennis Chen 		return;
1616a571d4ebSDennis Chen 
1617a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1618a571d4ebSDennis Chen 
1619a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
16201c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1621a571d4ebSDennis Chen 		return;
1622a571d4ebSDennis Chen 
1623c9ca9b4eSAKASHI Takahiro 	memblock_cap_memory_range(0, max_addr);
1624a571d4ebSDennis Chen }
1625a571d4ebSDennis Chen 
1626cd79481dSYinghai Lu static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
162772d4b0b4SBenjamin Herrenschmidt {
162872d4b0b4SBenjamin Herrenschmidt 	unsigned int left = 0, right = type->cnt;
162972d4b0b4SBenjamin Herrenschmidt 
163072d4b0b4SBenjamin Herrenschmidt 	do {
163172d4b0b4SBenjamin Herrenschmidt 		unsigned int mid = (right + left) / 2;
163272d4b0b4SBenjamin Herrenschmidt 
163372d4b0b4SBenjamin Herrenschmidt 		if (addr < type->regions[mid].base)
163472d4b0b4SBenjamin Herrenschmidt 			right = mid;
163572d4b0b4SBenjamin Herrenschmidt 		else if (addr >= (type->regions[mid].base +
163672d4b0b4SBenjamin Herrenschmidt 				  type->regions[mid].size))
163772d4b0b4SBenjamin Herrenschmidt 			left = mid + 1;
163872d4b0b4SBenjamin Herrenschmidt 		else
163972d4b0b4SBenjamin Herrenschmidt 			return mid;
164072d4b0b4SBenjamin Herrenschmidt 	} while (left < right);
164172d4b0b4SBenjamin Herrenschmidt 	return -1;
164272d4b0b4SBenjamin Herrenschmidt }
164372d4b0b4SBenjamin Herrenschmidt 
1644f5a222dcSYueyi Li bool __init_memblock memblock_is_reserved(phys_addr_t addr)
164595f72d1eSYinghai Lu {
164672d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.reserved, addr) != -1;
164795f72d1eSYinghai Lu }
164872d4b0b4SBenjamin Herrenschmidt 
1649b4ad0c7eSYaowei Bai bool __init_memblock memblock_is_memory(phys_addr_t addr)
165072d4b0b4SBenjamin Herrenschmidt {
165172d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.memory, addr) != -1;
165272d4b0b4SBenjamin Herrenschmidt }
165372d4b0b4SBenjamin Herrenschmidt 
1654937f0c26SYaowei Bai bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
1655bf3d3cc5SArd Biesheuvel {
1656bf3d3cc5SArd Biesheuvel 	int i = memblock_search(&memblock.memory, addr);
1657bf3d3cc5SArd Biesheuvel 
1658bf3d3cc5SArd Biesheuvel 	if (i == -1)
1659bf3d3cc5SArd Biesheuvel 		return false;
1660bf3d3cc5SArd Biesheuvel 	return !memblock_is_nomap(&memblock.memory.regions[i]);
1661bf3d3cc5SArd Biesheuvel }
1662bf3d3cc5SArd Biesheuvel 
1663e76b63f8SYinghai Lu #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1664e76b63f8SYinghai Lu int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1665e76b63f8SYinghai Lu 			 unsigned long *start_pfn, unsigned long *end_pfn)
1666e76b63f8SYinghai Lu {
1667e76b63f8SYinghai Lu 	struct memblock_type *type = &memblock.memory;
166816763230SFabian Frederick 	int mid = memblock_search(type, PFN_PHYS(pfn));
1669e76b63f8SYinghai Lu 
1670e76b63f8SYinghai Lu 	if (mid == -1)
1671e76b63f8SYinghai Lu 		return -1;
1672e76b63f8SYinghai Lu 
1673f7e2f7e8SFabian Frederick 	*start_pfn = PFN_DOWN(type->regions[mid].base);
1674f7e2f7e8SFabian Frederick 	*end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
1675e76b63f8SYinghai Lu 
1676e76b63f8SYinghai Lu 	return type->regions[mid].nid;
1677e76b63f8SYinghai Lu }
1678e76b63f8SYinghai Lu #endif
1679e76b63f8SYinghai Lu 
1680eab30949SStephen Boyd /**
1681eab30949SStephen Boyd  * memblock_is_region_memory - check if a region is a subset of memory
1682eab30949SStephen Boyd  * @base: base of region to check
1683eab30949SStephen Boyd  * @size: size of region to check
1684eab30949SStephen Boyd  *
1685eab30949SStephen Boyd  * Check if the region [@base, @base + @size) is a subset of a memory block.
1686eab30949SStephen Boyd  *
168747cec443SMike Rapoport  * Return:
1688eab30949SStephen Boyd  * 0 if false, non-zero if true
1689eab30949SStephen Boyd  */
1690937f0c26SYaowei Bai bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
169172d4b0b4SBenjamin Herrenschmidt {
1692abb65272STomi Valkeinen 	int idx = memblock_search(&memblock.memory, base);
1693eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
169472d4b0b4SBenjamin Herrenschmidt 
169572d4b0b4SBenjamin Herrenschmidt 	if (idx == -1)
1696937f0c26SYaowei Bai 		return false;
1697ef415ef4SWei Yang 	return (memblock.memory.regions[idx].base +
1698eb18f1b5STejun Heo 		 memblock.memory.regions[idx].size) >= end;
169995f72d1eSYinghai Lu }
170095f72d1eSYinghai Lu 
1701eab30949SStephen Boyd /**
1702eab30949SStephen Boyd  * memblock_is_region_reserved - check if a region intersects reserved memory
1703eab30949SStephen Boyd  * @base: base of region to check
1704eab30949SStephen Boyd  * @size: size of region to check
1705eab30949SStephen Boyd  *
170647cec443SMike Rapoport  * Check if the region [@base, @base + @size) intersects a reserved
170747cec443SMike Rapoport  * memory block.
1708eab30949SStephen Boyd  *
170947cec443SMike Rapoport  * Return:
1710c5c5c9d1STang Chen  * True if they intersect, false if not.
1711eab30949SStephen Boyd  */
1712c5c5c9d1STang Chen bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
171395f72d1eSYinghai Lu {
1714eb18f1b5STejun Heo 	memblock_cap_size(base, &size);
1715c5c5c9d1STang Chen 	return memblock_overlaps_region(&memblock.reserved, base, size);
171695f72d1eSYinghai Lu }
171795f72d1eSYinghai Lu 
17186ede1fd3SYinghai Lu void __init_memblock memblock_trim_memory(phys_addr_t align)
17196ede1fd3SYinghai Lu {
17206ede1fd3SYinghai Lu 	phys_addr_t start, end, orig_start, orig_end;
1721136199f0SEmil Medve 	struct memblock_region *r;
17226ede1fd3SYinghai Lu 
1723136199f0SEmil Medve 	for_each_memblock(memory, r) {
1724136199f0SEmil Medve 		orig_start = r->base;
1725136199f0SEmil Medve 		orig_end = r->base + r->size;
17266ede1fd3SYinghai Lu 		start = round_up(orig_start, align);
17276ede1fd3SYinghai Lu 		end = round_down(orig_end, align);
17286ede1fd3SYinghai Lu 
17296ede1fd3SYinghai Lu 		if (start == orig_start && end == orig_end)
17306ede1fd3SYinghai Lu 			continue;
17316ede1fd3SYinghai Lu 
17326ede1fd3SYinghai Lu 		if (start < end) {
1733136199f0SEmil Medve 			r->base = start;
1734136199f0SEmil Medve 			r->size = end - start;
17356ede1fd3SYinghai Lu 		} else {
1736136199f0SEmil Medve 			memblock_remove_region(&memblock.memory,
1737136199f0SEmil Medve 					       r - memblock.memory.regions);
1738136199f0SEmil Medve 			r--;
17396ede1fd3SYinghai Lu 		}
17406ede1fd3SYinghai Lu 	}
17416ede1fd3SYinghai Lu }
1742e63075a3SBenjamin Herrenschmidt 
17433661ca66SYinghai Lu void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1744e63075a3SBenjamin Herrenschmidt {
1745e63075a3SBenjamin Herrenschmidt 	memblock.current_limit = limit;
1746e63075a3SBenjamin Herrenschmidt }
1747e63075a3SBenjamin Herrenschmidt 
1748fec51014SLaura Abbott phys_addr_t __init_memblock memblock_get_current_limit(void)
1749fec51014SLaura Abbott {
1750fec51014SLaura Abbott 	return memblock.current_limit;
1751fec51014SLaura Abbott }
1752fec51014SLaura Abbott 
17530262d9c8SHeiko Carstens static void __init_memblock memblock_dump(struct memblock_type *type)
17546ed311b2SBenjamin Herrenschmidt {
17555d63f81cSMiles Chen 	phys_addr_t base, end, size;
1756e1720feeSMike Rapoport 	enum memblock_flags flags;
17578c9c1701SAlexander Kuleshov 	int idx;
17588c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
17596ed311b2SBenjamin Herrenschmidt 
17600262d9c8SHeiko Carstens 	pr_info(" %s.cnt  = 0x%lx\n", type->name, type->cnt);
17616ed311b2SBenjamin Herrenschmidt 
176266e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
17637c0caeb8STejun Heo 		char nid_buf[32] = "";
17646ed311b2SBenjamin Herrenschmidt 
17657c0caeb8STejun Heo 		base = rgn->base;
17667c0caeb8STejun Heo 		size = rgn->size;
17675d63f81cSMiles Chen 		end = base + size - 1;
176866a20757STang Chen 		flags = rgn->flags;
17697c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
17707c0caeb8STejun Heo 		if (memblock_get_region_node(rgn) != MAX_NUMNODES)
17717c0caeb8STejun Heo 			snprintf(nid_buf, sizeof(nid_buf), " on node %d",
17727c0caeb8STejun Heo 				 memblock_get_region_node(rgn));
17737c0caeb8STejun Heo #endif
1774e1720feeSMike Rapoport 		pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
17750262d9c8SHeiko Carstens 			type->name, idx, &base, &end, &size, nid_buf, flags);
17766ed311b2SBenjamin Herrenschmidt 	}
17776ed311b2SBenjamin Herrenschmidt }
17786ed311b2SBenjamin Herrenschmidt 
17794ff7b82fSTejun Heo void __init_memblock __memblock_dump_all(void)
17806ed311b2SBenjamin Herrenschmidt {
17816ed311b2SBenjamin Herrenschmidt 	pr_info("MEMBLOCK configuration:\n");
17825d63f81cSMiles Chen 	pr_info(" memory size = %pa reserved size = %pa\n",
17835d63f81cSMiles Chen 		&memblock.memory.total_size,
17845d63f81cSMiles Chen 		&memblock.reserved.total_size);
17856ed311b2SBenjamin Herrenschmidt 
17860262d9c8SHeiko Carstens 	memblock_dump(&memblock.memory);
17870262d9c8SHeiko Carstens 	memblock_dump(&memblock.reserved);
1788409efd4cSHeiko Carstens #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
17890262d9c8SHeiko Carstens 	memblock_dump(&memblock.physmem);
1790409efd4cSHeiko Carstens #endif
17916ed311b2SBenjamin Herrenschmidt }
17926ed311b2SBenjamin Herrenschmidt 
17931aadc056STejun Heo void __init memblock_allow_resize(void)
17946ed311b2SBenjamin Herrenschmidt {
1795142b45a7SBenjamin Herrenschmidt 	memblock_can_resize = 1;
17966ed311b2SBenjamin Herrenschmidt }
17976ed311b2SBenjamin Herrenschmidt 
17986ed311b2SBenjamin Herrenschmidt static int __init early_memblock(char *p)
17996ed311b2SBenjamin Herrenschmidt {
18006ed311b2SBenjamin Herrenschmidt 	if (p && strstr(p, "debug"))
18016ed311b2SBenjamin Herrenschmidt 		memblock_debug = 1;
18026ed311b2SBenjamin Herrenschmidt 	return 0;
18036ed311b2SBenjamin Herrenschmidt }
18046ed311b2SBenjamin Herrenschmidt early_param("memblock", early_memblock);
18056ed311b2SBenjamin Herrenschmidt 
1806bda49a81SMike Rapoport static void __init __free_pages_memory(unsigned long start, unsigned long end)
1807bda49a81SMike Rapoport {
1808bda49a81SMike Rapoport 	int order;
1809bda49a81SMike Rapoport 
1810bda49a81SMike Rapoport 	while (start < end) {
1811bda49a81SMike Rapoport 		order = min(MAX_ORDER - 1UL, __ffs(start));
1812bda49a81SMike Rapoport 
1813bda49a81SMike Rapoport 		while (start + (1UL << order) > end)
1814bda49a81SMike Rapoport 			order--;
1815bda49a81SMike Rapoport 
1816bda49a81SMike Rapoport 		memblock_free_pages(pfn_to_page(start), start, order);
1817bda49a81SMike Rapoport 
1818bda49a81SMike Rapoport 		start += (1UL << order);
1819bda49a81SMike Rapoport 	}
1820bda49a81SMike Rapoport }
1821bda49a81SMike Rapoport 
1822bda49a81SMike Rapoport static unsigned long __init __free_memory_core(phys_addr_t start,
1823bda49a81SMike Rapoport 				 phys_addr_t end)
1824bda49a81SMike Rapoport {
1825bda49a81SMike Rapoport 	unsigned long start_pfn = PFN_UP(start);
1826bda49a81SMike Rapoport 	unsigned long end_pfn = min_t(unsigned long,
1827bda49a81SMike Rapoport 				      PFN_DOWN(end), max_low_pfn);
1828bda49a81SMike Rapoport 
1829bda49a81SMike Rapoport 	if (start_pfn >= end_pfn)
1830bda49a81SMike Rapoport 		return 0;
1831bda49a81SMike Rapoport 
1832bda49a81SMike Rapoport 	__free_pages_memory(start_pfn, end_pfn);
1833bda49a81SMike Rapoport 
1834bda49a81SMike Rapoport 	return end_pfn - start_pfn;
1835bda49a81SMike Rapoport }
1836bda49a81SMike Rapoport 
1837bda49a81SMike Rapoport static unsigned long __init free_low_memory_core_early(void)
1838bda49a81SMike Rapoport {
1839bda49a81SMike Rapoport 	unsigned long count = 0;
1840bda49a81SMike Rapoport 	phys_addr_t start, end;
1841bda49a81SMike Rapoport 	u64 i;
1842bda49a81SMike Rapoport 
1843bda49a81SMike Rapoport 	memblock_clear_hotplug(0, -1);
1844bda49a81SMike Rapoport 
1845bda49a81SMike Rapoport 	for_each_reserved_mem_region(i, &start, &end)
1846bda49a81SMike Rapoport 		reserve_bootmem_region(start, end);
1847bda49a81SMike Rapoport 
1848bda49a81SMike Rapoport 	/*
1849bda49a81SMike Rapoport 	 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
1850bda49a81SMike Rapoport 	 *  because in some case like Node0 doesn't have RAM installed
1851bda49a81SMike Rapoport 	 *  low ram will be on Node1
1852bda49a81SMike Rapoport 	 */
1853bda49a81SMike Rapoport 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
1854bda49a81SMike Rapoport 				NULL)
1855bda49a81SMike Rapoport 		count += __free_memory_core(start, end);
1856bda49a81SMike Rapoport 
1857bda49a81SMike Rapoport 	return count;
1858bda49a81SMike Rapoport }
1859bda49a81SMike Rapoport 
1860bda49a81SMike Rapoport static int reset_managed_pages_done __initdata;
1861bda49a81SMike Rapoport 
1862bda49a81SMike Rapoport void reset_node_managed_pages(pg_data_t *pgdat)
1863bda49a81SMike Rapoport {
1864bda49a81SMike Rapoport 	struct zone *z;
1865bda49a81SMike Rapoport 
1866bda49a81SMike Rapoport 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
18679705bea5SArun KS 		atomic_long_set(&z->managed_pages, 0);
1868bda49a81SMike Rapoport }
1869bda49a81SMike Rapoport 
1870bda49a81SMike Rapoport void __init reset_all_zones_managed_pages(void)
1871bda49a81SMike Rapoport {
1872bda49a81SMike Rapoport 	struct pglist_data *pgdat;
1873bda49a81SMike Rapoport 
1874bda49a81SMike Rapoport 	if (reset_managed_pages_done)
1875bda49a81SMike Rapoport 		return;
1876bda49a81SMike Rapoport 
1877bda49a81SMike Rapoport 	for_each_online_pgdat(pgdat)
1878bda49a81SMike Rapoport 		reset_node_managed_pages(pgdat);
1879bda49a81SMike Rapoport 
1880bda49a81SMike Rapoport 	reset_managed_pages_done = 1;
1881bda49a81SMike Rapoport }
1882bda49a81SMike Rapoport 
1883bda49a81SMike Rapoport /**
1884bda49a81SMike Rapoport  * memblock_free_all - release free pages to the buddy allocator
1885bda49a81SMike Rapoport  *
1886bda49a81SMike Rapoport  * Return: the number of pages actually released.
1887bda49a81SMike Rapoport  */
1888bda49a81SMike Rapoport unsigned long __init memblock_free_all(void)
1889bda49a81SMike Rapoport {
1890bda49a81SMike Rapoport 	unsigned long pages;
1891bda49a81SMike Rapoport 
1892bda49a81SMike Rapoport 	reset_all_zones_managed_pages();
1893bda49a81SMike Rapoport 
1894bda49a81SMike Rapoport 	pages = free_low_memory_core_early();
1895ca79b0c2SArun KS 	totalram_pages_add(pages);
1896bda49a81SMike Rapoport 
1897bda49a81SMike Rapoport 	return pages;
1898bda49a81SMike Rapoport }
1899bda49a81SMike Rapoport 
1900c378ddd5STejun Heo #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
19016d03b885SBenjamin Herrenschmidt 
19026d03b885SBenjamin Herrenschmidt static int memblock_debug_show(struct seq_file *m, void *private)
19036d03b885SBenjamin Herrenschmidt {
19046d03b885SBenjamin Herrenschmidt 	struct memblock_type *type = m->private;
19056d03b885SBenjamin Herrenschmidt 	struct memblock_region *reg;
19066d03b885SBenjamin Herrenschmidt 	int i;
19075d63f81cSMiles Chen 	phys_addr_t end;
19086d03b885SBenjamin Herrenschmidt 
19096d03b885SBenjamin Herrenschmidt 	for (i = 0; i < type->cnt; i++) {
19106d03b885SBenjamin Herrenschmidt 		reg = &type->regions[i];
19115d63f81cSMiles Chen 		end = reg->base + reg->size - 1;
19126d03b885SBenjamin Herrenschmidt 
19135d63f81cSMiles Chen 		seq_printf(m, "%4d: ", i);
19145d63f81cSMiles Chen 		seq_printf(m, "%pa..%pa\n", &reg->base, &end);
19156d03b885SBenjamin Herrenschmidt 	}
19166d03b885SBenjamin Herrenschmidt 	return 0;
19176d03b885SBenjamin Herrenschmidt }
19185ad35093SAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(memblock_debug);
19196d03b885SBenjamin Herrenschmidt 
19206d03b885SBenjamin Herrenschmidt static int __init memblock_init_debugfs(void)
19216d03b885SBenjamin Herrenschmidt {
19226d03b885SBenjamin Herrenschmidt 	struct dentry *root = debugfs_create_dir("memblock", NULL);
1923d9f7979cSGreg Kroah-Hartman 
19240825a6f9SJoe Perches 	debugfs_create_file("memory", 0444, root,
19250825a6f9SJoe Perches 			    &memblock.memory, &memblock_debug_fops);
19260825a6f9SJoe Perches 	debugfs_create_file("reserved", 0444, root,
19270825a6f9SJoe Perches 			    &memblock.reserved, &memblock_debug_fops);
192870210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
19290825a6f9SJoe Perches 	debugfs_create_file("physmem", 0444, root,
19300825a6f9SJoe Perches 			    &memblock.physmem, &memblock_debug_fops);
193170210ed9SPhilipp Hachtmann #endif
19326d03b885SBenjamin Herrenschmidt 
19336d03b885SBenjamin Herrenschmidt 	return 0;
19346d03b885SBenjamin Herrenschmidt }
19356d03b885SBenjamin Herrenschmidt __initcall(memblock_init_debugfs);
19366d03b885SBenjamin Herrenschmidt 
19376d03b885SBenjamin Herrenschmidt #endif /* CONFIG_DEBUG_FS */
1938