xref: /linux/mm/memblock.c (revision 8a770c2a83eaf4c3d493ca4056abd6d6ddce6f18)
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 
135e1720feeSMike Rapoport 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  */
26487029ee9SGrygorii Strashko 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 
86166b16edfSTang Chen 	for (i = start_rgn; i < end_rgn; i++)
8624308ce17STony Luck 		if (set)
8634308ce17STony Luck 			memblock_set_region_flags(&type->regions[i], flag);
8644308ce17STony Luck 		else
8654308ce17STony Luck 			memblock_clear_region_flags(&type->regions[i], flag);
86666b16edfSTang Chen 
86766b16edfSTang Chen 	memblock_merge_regions(type);
86866b16edfSTang Chen 	return 0;
86966b16edfSTang Chen }
87066b16edfSTang Chen 
87166b16edfSTang Chen /**
8724308ce17STony Luck  * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
8734308ce17STony Luck  * @base: the base phys addr of the region
8744308ce17STony Luck  * @size: the size of the region
8754308ce17STony Luck  *
87647cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
8774308ce17STony Luck  */
8784308ce17STony Luck int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
8794308ce17STony Luck {
8804308ce17STony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
8814308ce17STony Luck }
8824308ce17STony Luck 
8834308ce17STony Luck /**
88466b16edfSTang Chen  * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
88566b16edfSTang Chen  * @base: the base phys addr of the region
88666b16edfSTang Chen  * @size: the size of the region
88766b16edfSTang Chen  *
88847cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
88966b16edfSTang Chen  */
89066b16edfSTang Chen int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
89166b16edfSTang Chen {
8924308ce17STony Luck 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
89366b16edfSTang Chen }
89466b16edfSTang Chen 
89566b16edfSTang Chen /**
896a3f5bafcSTony Luck  * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
897a3f5bafcSTony Luck  * @base: the base phys addr of the region
898a3f5bafcSTony Luck  * @size: the size of the region
899a3f5bafcSTony Luck  *
90047cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
901a3f5bafcSTony Luck  */
902a3f5bafcSTony Luck int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
903a3f5bafcSTony Luck {
904a3f5bafcSTony Luck 	system_has_some_mirror = true;
905a3f5bafcSTony Luck 
906a3f5bafcSTony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
907a3f5bafcSTony Luck }
908a3f5bafcSTony Luck 
909bf3d3cc5SArd Biesheuvel /**
910bf3d3cc5SArd Biesheuvel  * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
911bf3d3cc5SArd Biesheuvel  * @base: the base phys addr of the region
912bf3d3cc5SArd Biesheuvel  * @size: the size of the region
913bf3d3cc5SArd Biesheuvel  *
91447cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
915bf3d3cc5SArd Biesheuvel  */
916bf3d3cc5SArd Biesheuvel int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
917bf3d3cc5SArd Biesheuvel {
918bf3d3cc5SArd Biesheuvel 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
919bf3d3cc5SArd Biesheuvel }
920a3f5bafcSTony Luck 
921a3f5bafcSTony Luck /**
9224c546b8aSAKASHI Takahiro  * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
9234c546b8aSAKASHI Takahiro  * @base: the base phys addr of the region
9244c546b8aSAKASHI Takahiro  * @size: the size of the region
9254c546b8aSAKASHI Takahiro  *
92647cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
9274c546b8aSAKASHI Takahiro  */
9284c546b8aSAKASHI Takahiro int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
9294c546b8aSAKASHI Takahiro {
9304c546b8aSAKASHI Takahiro 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
9314c546b8aSAKASHI Takahiro }
9324c546b8aSAKASHI Takahiro 
9334c546b8aSAKASHI Takahiro /**
9348e7a7f86SRobin Holt  * __next_reserved_mem_region - next function for for_each_reserved_region()
9358e7a7f86SRobin Holt  * @idx: pointer to u64 loop variable
9368e7a7f86SRobin Holt  * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
9378e7a7f86SRobin Holt  * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
9388e7a7f86SRobin Holt  *
9398e7a7f86SRobin Holt  * Iterate over all reserved memory regions.
9408e7a7f86SRobin Holt  */
9418e7a7f86SRobin Holt void __init_memblock __next_reserved_mem_region(u64 *idx,
9428e7a7f86SRobin Holt 					   phys_addr_t *out_start,
9438e7a7f86SRobin Holt 					   phys_addr_t *out_end)
9448e7a7f86SRobin Holt {
945567d117bSAlexander Kuleshov 	struct memblock_type *type = &memblock.reserved;
9468e7a7f86SRobin Holt 
947cd33a76bSRichard Leitner 	if (*idx < type->cnt) {
948567d117bSAlexander Kuleshov 		struct memblock_region *r = &type->regions[*idx];
9498e7a7f86SRobin Holt 		phys_addr_t base = r->base;
9508e7a7f86SRobin Holt 		phys_addr_t size = r->size;
9518e7a7f86SRobin Holt 
9528e7a7f86SRobin Holt 		if (out_start)
9538e7a7f86SRobin Holt 			*out_start = base;
9548e7a7f86SRobin Holt 		if (out_end)
9558e7a7f86SRobin Holt 			*out_end = base + size - 1;
9568e7a7f86SRobin Holt 
9578e7a7f86SRobin Holt 		*idx += 1;
9588e7a7f86SRobin Holt 		return;
9598e7a7f86SRobin Holt 	}
9608e7a7f86SRobin Holt 
9618e7a7f86SRobin Holt 	/* signal end of iteration */
9628e7a7f86SRobin Holt 	*idx = ULLONG_MAX;
9638e7a7f86SRobin Holt }
9648e7a7f86SRobin Holt 
9658e7a7f86SRobin Holt /**
966f1af9d3aSPhilipp Hachtmann  * __next__mem_range - next function for for_each_free_mem_range() etc.
96735fd0808STejun Heo  * @idx: pointer to u64 loop variable
968b1154233SGrygorii Strashko  * @nid: node selector, %NUMA_NO_NODE for all nodes
969fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
970f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
971f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
972dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
973dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
974dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
97535fd0808STejun Heo  *
976f1af9d3aSPhilipp Hachtmann  * Find the first area from *@idx which matches @nid, fill the out
97735fd0808STejun Heo  * parameters, and update *@idx for the next iteration.  The lower 32bit of
978f1af9d3aSPhilipp Hachtmann  * *@idx contains index into type_a and the upper 32bit indexes the
979f1af9d3aSPhilipp Hachtmann  * areas before each region in type_b.	For example, if type_b regions
98035fd0808STejun Heo  * look like the following,
98135fd0808STejun Heo  *
98235fd0808STejun Heo  *	0:[0-16), 1:[32-48), 2:[128-130)
98335fd0808STejun Heo  *
98435fd0808STejun Heo  * The upper 32bit indexes the following regions.
98535fd0808STejun Heo  *
98635fd0808STejun Heo  *	0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
98735fd0808STejun Heo  *
98835fd0808STejun Heo  * As both region arrays are sorted, the function advances the two indices
98935fd0808STejun Heo  * in lockstep and returns each intersection.
99035fd0808STejun Heo  */
991e1720feeSMike Rapoport void __init_memblock __next_mem_range(u64 *idx, int nid,
992e1720feeSMike Rapoport 				      enum memblock_flags flags,
993f1af9d3aSPhilipp Hachtmann 				      struct memblock_type *type_a,
994f1af9d3aSPhilipp Hachtmann 				      struct memblock_type *type_b,
99535fd0808STejun Heo 				      phys_addr_t *out_start,
99635fd0808STejun Heo 				      phys_addr_t *out_end, int *out_nid)
99735fd0808STejun Heo {
998f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
999f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1000b1154233SGrygorii Strashko 
1001f1af9d3aSPhilipp Hachtmann 	if (WARN_ONCE(nid == MAX_NUMNODES,
1002f1af9d3aSPhilipp Hachtmann 	"Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1003560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
100435fd0808STejun Heo 
1005f1af9d3aSPhilipp Hachtmann 	for (; idx_a < type_a->cnt; idx_a++) {
1006f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1007f1af9d3aSPhilipp Hachtmann 
100835fd0808STejun Heo 		phys_addr_t m_start = m->base;
100935fd0808STejun Heo 		phys_addr_t m_end = m->base + m->size;
1010f1af9d3aSPhilipp Hachtmann 		int	    m_nid = memblock_get_region_node(m);
101135fd0808STejun Heo 
101235fd0808STejun Heo 		/* only memory regions are associated with nodes, check it */
1013f1af9d3aSPhilipp Hachtmann 		if (nid != NUMA_NO_NODE && nid != m_nid)
101435fd0808STejun Heo 			continue;
101535fd0808STejun Heo 
10160a313a99SXishi Qiu 		/* skip hotpluggable memory regions if needed */
10170a313a99SXishi Qiu 		if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
10180a313a99SXishi Qiu 			continue;
10190a313a99SXishi Qiu 
1020a3f5bafcSTony Luck 		/* if we want mirror memory skip non-mirror memory regions */
1021a3f5bafcSTony Luck 		if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1022a3f5bafcSTony Luck 			continue;
1023a3f5bafcSTony Luck 
1024bf3d3cc5SArd Biesheuvel 		/* skip nomap memory unless we were asked for it explicitly */
1025bf3d3cc5SArd Biesheuvel 		if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1026bf3d3cc5SArd Biesheuvel 			continue;
1027bf3d3cc5SArd Biesheuvel 
1028f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1029f1af9d3aSPhilipp Hachtmann 			if (out_start)
1030f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1031f1af9d3aSPhilipp Hachtmann 			if (out_end)
1032f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1033f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1034f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1035f1af9d3aSPhilipp Hachtmann 			idx_a++;
1036f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1037f1af9d3aSPhilipp Hachtmann 			return;
1038f1af9d3aSPhilipp Hachtmann 		}
103935fd0808STejun Heo 
1040f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1041f1af9d3aSPhilipp Hachtmann 		for (; idx_b < type_b->cnt + 1; idx_b++) {
1042f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1043f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1044f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1045f1af9d3aSPhilipp Hachtmann 
1046f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1047f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1048f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
10491c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1050f1af9d3aSPhilipp Hachtmann 
1051f1af9d3aSPhilipp Hachtmann 			/*
1052f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1053f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1054f1af9d3aSPhilipp Hachtmann 			 */
105535fd0808STejun Heo 			if (r_start >= m_end)
105635fd0808STejun Heo 				break;
105735fd0808STejun Heo 			/* if the two regions intersect, we're done */
105835fd0808STejun Heo 			if (m_start < r_end) {
105935fd0808STejun Heo 				if (out_start)
1060f1af9d3aSPhilipp Hachtmann 					*out_start =
1061f1af9d3aSPhilipp Hachtmann 						max(m_start, r_start);
106235fd0808STejun Heo 				if (out_end)
106335fd0808STejun Heo 					*out_end = min(m_end, r_end);
106435fd0808STejun Heo 				if (out_nid)
1065f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
106635fd0808STejun Heo 				/*
1067f1af9d3aSPhilipp Hachtmann 				 * The region which ends first is
1068f1af9d3aSPhilipp Hachtmann 				 * advanced for the next iteration.
106935fd0808STejun Heo 				 */
107035fd0808STejun Heo 				if (m_end <= r_end)
1071f1af9d3aSPhilipp Hachtmann 					idx_a++;
107235fd0808STejun Heo 				else
1073f1af9d3aSPhilipp Hachtmann 					idx_b++;
1074f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
107535fd0808STejun Heo 				return;
107635fd0808STejun Heo 			}
107735fd0808STejun Heo 		}
107835fd0808STejun Heo 	}
107935fd0808STejun Heo 
108035fd0808STejun Heo 	/* signal end of iteration */
108135fd0808STejun Heo 	*idx = ULLONG_MAX;
108235fd0808STejun Heo }
108335fd0808STejun Heo 
10847bd0b0f0STejun Heo /**
1085f1af9d3aSPhilipp Hachtmann  * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1086f1af9d3aSPhilipp Hachtmann  *
10877bd0b0f0STejun Heo  * @idx: pointer to u64 loop variable
1088ad5ea8cdSAlexander Kuleshov  * @nid: node selector, %NUMA_NO_NODE for all nodes
1089fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
1090f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
1091f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
1092dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1093dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1094dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
10957bd0b0f0STejun Heo  *
109647cec443SMike Rapoport  * Finds the next range from type_a which is not marked as unsuitable
109747cec443SMike Rapoport  * in type_b.
109847cec443SMike Rapoport  *
1099f1af9d3aSPhilipp Hachtmann  * Reverse of __next_mem_range().
11007bd0b0f0STejun Heo  */
1101e1720feeSMike Rapoport void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1102e1720feeSMike Rapoport 					  enum memblock_flags flags,
1103f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_a,
1104f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_b,
11057bd0b0f0STejun Heo 					  phys_addr_t *out_start,
11067bd0b0f0STejun Heo 					  phys_addr_t *out_end, int *out_nid)
11077bd0b0f0STejun Heo {
1108f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1109f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1110b1154233SGrygorii Strashko 
1111560dca27SGrygorii Strashko 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1112560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
11137bd0b0f0STejun Heo 
11147bd0b0f0STejun Heo 	if (*idx == (u64)ULLONG_MAX) {
1115f1af9d3aSPhilipp Hachtmann 		idx_a = type_a->cnt - 1;
1116e47608abSzijun_hu 		if (type_b != NULL)
1117f1af9d3aSPhilipp Hachtmann 			idx_b = type_b->cnt;
1118e47608abSzijun_hu 		else
1119e47608abSzijun_hu 			idx_b = 0;
11207bd0b0f0STejun Heo 	}
11217bd0b0f0STejun Heo 
1122f1af9d3aSPhilipp Hachtmann 	for (; idx_a >= 0; idx_a--) {
1123f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1124f1af9d3aSPhilipp Hachtmann 
11257bd0b0f0STejun Heo 		phys_addr_t m_start = m->base;
11267bd0b0f0STejun Heo 		phys_addr_t m_end = m->base + m->size;
1127f1af9d3aSPhilipp Hachtmann 		int m_nid = memblock_get_region_node(m);
11287bd0b0f0STejun Heo 
11297bd0b0f0STejun Heo 		/* only memory regions are associated with nodes, check it */
1130f1af9d3aSPhilipp Hachtmann 		if (nid != NUMA_NO_NODE && nid != m_nid)
11317bd0b0f0STejun Heo 			continue;
11327bd0b0f0STejun Heo 
113355ac590cSTang Chen 		/* skip hotpluggable memory regions if needed */
113455ac590cSTang Chen 		if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
113555ac590cSTang Chen 			continue;
113655ac590cSTang Chen 
1137a3f5bafcSTony Luck 		/* if we want mirror memory skip non-mirror memory regions */
1138a3f5bafcSTony Luck 		if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1139a3f5bafcSTony Luck 			continue;
1140a3f5bafcSTony Luck 
1141bf3d3cc5SArd Biesheuvel 		/* skip nomap memory unless we were asked for it explicitly */
1142bf3d3cc5SArd Biesheuvel 		if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
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 
12582bfc2862SAkinobu Mita static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
12592bfc2862SAkinobu Mita 					phys_addr_t align, phys_addr_t start,
1260e1720feeSMike Rapoport 					phys_addr_t end, int nid,
1261e1720feeSMike Rapoport 					enum memblock_flags flags)
126295f72d1eSYinghai Lu {
12636ed311b2SBenjamin Herrenschmidt 	phys_addr_t found;
126495f72d1eSYinghai Lu 
12652f770806SMike Rapoport 	if (!align) {
12662f770806SMike Rapoport 		/* Can't use WARNs this early in boot on powerpc */
12672f770806SMike Rapoport 		dump_stack();
12682f770806SMike Rapoport 		align = SMP_CACHE_BYTES;
12692f770806SMike Rapoport 	}
12702f770806SMike Rapoport 
1271fc6daaf9STony Luck 	found = memblock_find_in_range_node(size, align, start, end, nid,
1272fc6daaf9STony Luck 					    flags);
1273aedf95eaSCatalin Marinas 	if (found && !memblock_reserve(found, size)) {
1274aedf95eaSCatalin Marinas 		/*
1275aedf95eaSCatalin Marinas 		 * The min_count is set to 0 so that memblock allocations are
1276aedf95eaSCatalin Marinas 		 * never reported as leaks.
1277aedf95eaSCatalin Marinas 		 */
12789099daedSCatalin Marinas 		kmemleak_alloc_phys(found, size, 0, 0);
12796ed311b2SBenjamin Herrenschmidt 		return found;
1280aedf95eaSCatalin Marinas 	}
12816ed311b2SBenjamin Herrenschmidt 	return 0;
128295f72d1eSYinghai Lu }
128395f72d1eSYinghai Lu 
1284*8a770c2aSMike Rapoport phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
1285*8a770c2aSMike Rapoport 					     phys_addr_t align,
1286*8a770c2aSMike Rapoport 					     phys_addr_t start,
1287*8a770c2aSMike Rapoport 					     phys_addr_t end)
12882bfc2862SAkinobu Mita {
1289fc6daaf9STony Luck 	return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1290*8a770c2aSMike Rapoport 					MEMBLOCK_NONE);
12912bfc2862SAkinobu Mita }
12922bfc2862SAkinobu Mita 
12939a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
12947bd0b0f0STejun Heo {
1295e1720feeSMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
1296a3f5bafcSTony Luck 	phys_addr_t ret;
1297a3f5bafcSTony Luck 
1298a3f5bafcSTony Luck again:
129953d818d2SMike Rapoport 	ret = memblock_alloc_range_nid(size, align, 0,
130053d818d2SMike Rapoport 				       MEMBLOCK_ALLOC_ACCESSIBLE, nid, flags);
1301a3f5bafcSTony Luck 
1302a3f5bafcSTony Luck 	if (!ret && (flags & MEMBLOCK_MIRROR)) {
1303a3f5bafcSTony Luck 		flags &= ~MEMBLOCK_MIRROR;
1304a3f5bafcSTony Luck 		goto again;
1305a3f5bafcSTony Luck 	}
1306a3f5bafcSTony Luck 	return ret;
13077bd0b0f0STejun Heo }
13087bd0b0f0STejun Heo 
13097bd0b0f0STejun Heo phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
13107bd0b0f0STejun Heo {
131153d818d2SMike Rapoport 	return memblock_alloc_range_nid(size, align, 0, max_addr, NUMA_NO_NODE,
1312fc6daaf9STony Luck 					MEMBLOCK_NONE);
13137bd0b0f0STejun Heo }
13147bd0b0f0STejun Heo 
13156ed311b2SBenjamin Herrenschmidt phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
131695f72d1eSYinghai Lu {
13176ed311b2SBenjamin Herrenschmidt 	phys_addr_t alloc;
13186ed311b2SBenjamin Herrenschmidt 
13196ed311b2SBenjamin Herrenschmidt 	alloc = __memblock_alloc_base(size, align, max_addr);
13206ed311b2SBenjamin Herrenschmidt 
13216ed311b2SBenjamin Herrenschmidt 	if (alloc == 0)
13225d63f81cSMiles Chen 		panic("ERROR: Failed to allocate %pa bytes below %pa.\n",
13235d63f81cSMiles Chen 		      &size, &max_addr);
13246ed311b2SBenjamin Herrenschmidt 
13256ed311b2SBenjamin Herrenschmidt 	return alloc;
132695f72d1eSYinghai Lu }
132795f72d1eSYinghai Lu 
13289a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc(phys_addr_t size, phys_addr_t align)
132995f72d1eSYinghai Lu {
13306ed311b2SBenjamin Herrenschmidt 	return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
133195f72d1eSYinghai Lu }
133295f72d1eSYinghai Lu 
13339a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
13349d1e2492SBenjamin Herrenschmidt {
13359a8dd708SMike Rapoport 	phys_addr_t res = memblock_phys_alloc_nid(size, align, nid);
13369d1e2492SBenjamin Herrenschmidt 
13379d1e2492SBenjamin Herrenschmidt 	if (res)
13389d1e2492SBenjamin Herrenschmidt 		return res;
133915fb0972STejun Heo 	return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
134095f72d1eSYinghai Lu }
134195f72d1eSYinghai Lu 
134226f09e9bSSantosh Shilimkar /**
1343eb31d559SMike Rapoport  * memblock_alloc_internal - allocate boot memory block
134426f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
134526f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
134626f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region to allocate (phys address)
134726f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region to allocate (phys address)
134826f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
134926f09e9bSSantosh Shilimkar  *
135026f09e9bSSantosh Shilimkar  * The @min_addr limit is dropped if it can not be satisfied and the allocation
135126f09e9bSSantosh Shilimkar  * will fall back to memory below @min_addr. Also, allocation may fall back
135226f09e9bSSantosh Shilimkar  * to any node in the system if the specified node can not
135326f09e9bSSantosh Shilimkar  * hold the requested memory.
135426f09e9bSSantosh Shilimkar  *
135526f09e9bSSantosh Shilimkar  * The allocation is performed from memory region limited by
135697ad1087SMike Rapoport  * memblock.current_limit if @max_addr == %MEMBLOCK_ALLOC_ACCESSIBLE.
135726f09e9bSSantosh Shilimkar  *
135826f09e9bSSantosh Shilimkar  * The phys address of allocated boot memory block is converted to virtual and
135926f09e9bSSantosh Shilimkar  * allocated memory is reset to 0.
136026f09e9bSSantosh Shilimkar  *
136126f09e9bSSantosh Shilimkar  * In addition, function sets the min_count to 0 using kmemleak_alloc for
136226f09e9bSSantosh Shilimkar  * allocated boot memory block, so that it is never reported as leaks.
136326f09e9bSSantosh Shilimkar  *
136447cec443SMike Rapoport  * Return:
136526f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
136626f09e9bSSantosh Shilimkar  */
1367eb31d559SMike Rapoport static void * __init memblock_alloc_internal(
136826f09e9bSSantosh Shilimkar 				phys_addr_t size, phys_addr_t align,
136926f09e9bSSantosh Shilimkar 				phys_addr_t min_addr, phys_addr_t max_addr,
137026f09e9bSSantosh Shilimkar 				int nid)
137126f09e9bSSantosh Shilimkar {
137226f09e9bSSantosh Shilimkar 	phys_addr_t alloc;
137326f09e9bSSantosh Shilimkar 	void *ptr;
1374e1720feeSMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
137526f09e9bSSantosh Shilimkar 
1376560dca27SGrygorii Strashko 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1377560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
137826f09e9bSSantosh Shilimkar 
137926f09e9bSSantosh Shilimkar 	/*
138026f09e9bSSantosh Shilimkar 	 * Detect any accidental use of these APIs after slab is ready, as at
138126f09e9bSSantosh Shilimkar 	 * this moment memblock may be deinitialized already and its
1382c6ffc5caSMike Rapoport 	 * internal data may be destroyed (after execution of memblock_free_all)
138326f09e9bSSantosh Shilimkar 	 */
138426f09e9bSSantosh Shilimkar 	if (WARN_ON_ONCE(slab_is_available()))
138526f09e9bSSantosh Shilimkar 		return kzalloc_node(size, GFP_NOWAIT, nid);
138626f09e9bSSantosh Shilimkar 
13872f770806SMike Rapoport 	if (!align) {
13882f770806SMike Rapoport 		dump_stack();
13892f770806SMike Rapoport 		align = SMP_CACHE_BYTES;
13902f770806SMike Rapoport 	}
13912f770806SMike Rapoport 
1392f544e14fSYinghai Lu 	if (max_addr > memblock.current_limit)
1393f544e14fSYinghai Lu 		max_addr = memblock.current_limit;
139426f09e9bSSantosh Shilimkar again:
139526f09e9bSSantosh Shilimkar 	alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
1396a3f5bafcSTony Luck 					    nid, flags);
13977d41c03eSWei Yang 	if (alloc && !memblock_reserve(alloc, size))
139826f09e9bSSantosh Shilimkar 		goto done;
139926f09e9bSSantosh Shilimkar 
140026f09e9bSSantosh Shilimkar 	if (nid != NUMA_NO_NODE) {
140126f09e9bSSantosh Shilimkar 		alloc = memblock_find_in_range_node(size, align, min_addr,
1402fc6daaf9STony Luck 						    max_addr, NUMA_NO_NODE,
1403a3f5bafcSTony Luck 						    flags);
14047d41c03eSWei Yang 		if (alloc && !memblock_reserve(alloc, size))
140526f09e9bSSantosh Shilimkar 			goto done;
140626f09e9bSSantosh Shilimkar 	}
140726f09e9bSSantosh Shilimkar 
140826f09e9bSSantosh Shilimkar 	if (min_addr) {
140926f09e9bSSantosh Shilimkar 		min_addr = 0;
141026f09e9bSSantosh Shilimkar 		goto again;
141126f09e9bSSantosh Shilimkar 	}
141226f09e9bSSantosh Shilimkar 
1413a3f5bafcSTony Luck 	if (flags & MEMBLOCK_MIRROR) {
1414a3f5bafcSTony Luck 		flags &= ~MEMBLOCK_MIRROR;
1415a3f5bafcSTony Luck 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1416a3f5bafcSTony Luck 			&size);
1417a3f5bafcSTony Luck 		goto again;
1418a3f5bafcSTony Luck 	}
1419a3f5bafcSTony Luck 
1420a3f5bafcSTony Luck 	return NULL;
142126f09e9bSSantosh Shilimkar done:
142226f09e9bSSantosh Shilimkar 	ptr = phys_to_virt(alloc);
142326f09e9bSSantosh Shilimkar 
1424fed84c78SQian Cai 	/* Skip kmemleak for kasan_init() due to high volume. */
1425fed84c78SQian Cai 	if (max_addr != MEMBLOCK_ALLOC_KASAN)
142626f09e9bSSantosh Shilimkar 		/*
1427fed84c78SQian Cai 		 * The min_count is set to 0 so that bootmem allocated
1428fed84c78SQian Cai 		 * blocks are never reported as leaks. This is because many
1429fed84c78SQian Cai 		 * of these blocks are only referred via the physical
1430fed84c78SQian Cai 		 * address which is not looked up by kmemleak.
143126f09e9bSSantosh Shilimkar 		 */
143226f09e9bSSantosh Shilimkar 		kmemleak_alloc(ptr, size, 0, 0);
143326f09e9bSSantosh Shilimkar 
143426f09e9bSSantosh Shilimkar 	return ptr;
143526f09e9bSSantosh Shilimkar }
143626f09e9bSSantosh Shilimkar 
143726f09e9bSSantosh Shilimkar /**
1438eb31d559SMike Rapoport  * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
1439ea1f5f37SPavel Tatashin  * memory and without panicking
1440ea1f5f37SPavel Tatashin  * @size: size of memory block to be allocated in bytes
1441ea1f5f37SPavel Tatashin  * @align: alignment of the region and block's size
1442ea1f5f37SPavel Tatashin  * @min_addr: the lower bound of the memory region from where the allocation
1443ea1f5f37SPavel Tatashin  *	  is preferred (phys address)
1444ea1f5f37SPavel Tatashin  * @max_addr: the upper bound of the memory region from where the allocation
144597ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1446ea1f5f37SPavel Tatashin  *	      allocate only from memory limited by memblock.current_limit value
1447ea1f5f37SPavel Tatashin  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1448ea1f5f37SPavel Tatashin  *
1449ea1f5f37SPavel Tatashin  * Public function, provides additional debug information (including caller
1450ea1f5f37SPavel Tatashin  * info), if enabled. Does not zero allocated memory, does not panic if request
1451ea1f5f37SPavel Tatashin  * cannot be satisfied.
1452ea1f5f37SPavel Tatashin  *
145347cec443SMike Rapoport  * Return:
1454ea1f5f37SPavel Tatashin  * Virtual address of allocated memory block on success, NULL on failure.
1455ea1f5f37SPavel Tatashin  */
1456eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_raw(
1457ea1f5f37SPavel Tatashin 			phys_addr_t size, phys_addr_t align,
1458ea1f5f37SPavel Tatashin 			phys_addr_t min_addr, phys_addr_t max_addr,
1459ea1f5f37SPavel Tatashin 			int nid)
1460ea1f5f37SPavel Tatashin {
1461ea1f5f37SPavel Tatashin 	void *ptr;
1462ea1f5f37SPavel Tatashin 
1463a36aab89SMike Rapoport 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1464a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1465a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1466ea1f5f37SPavel Tatashin 
1467eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
1468ea1f5f37SPavel Tatashin 					   min_addr, max_addr, nid);
1469ea1f5f37SPavel Tatashin 	if (ptr && size > 0)
1470f682a97aSAlexander Duyck 		page_init_poison(ptr, size);
1471f682a97aSAlexander Duyck 
1472ea1f5f37SPavel Tatashin 	return ptr;
1473ea1f5f37SPavel Tatashin }
1474ea1f5f37SPavel Tatashin 
1475ea1f5f37SPavel Tatashin /**
1476eb31d559SMike Rapoport  * memblock_alloc_try_nid_nopanic - allocate boot memory block
147726f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
147826f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
147926f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region from where the allocation
148026f09e9bSSantosh Shilimkar  *	  is preferred (phys address)
148126f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region from where the allocation
148297ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
148326f09e9bSSantosh Shilimkar  *	      allocate only from memory limited by memblock.current_limit value
148426f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
148526f09e9bSSantosh Shilimkar  *
1486ea1f5f37SPavel Tatashin  * Public function, provides additional debug information (including caller
1487ea1f5f37SPavel Tatashin  * info), if enabled. This function zeroes the allocated memory.
148826f09e9bSSantosh Shilimkar  *
148947cec443SMike Rapoport  * Return:
149026f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
149126f09e9bSSantosh Shilimkar  */
1492eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_nopanic(
149326f09e9bSSantosh Shilimkar 				phys_addr_t size, phys_addr_t align,
149426f09e9bSSantosh Shilimkar 				phys_addr_t min_addr, phys_addr_t max_addr,
149526f09e9bSSantosh Shilimkar 				int nid)
149626f09e9bSSantosh Shilimkar {
1497ea1f5f37SPavel Tatashin 	void *ptr;
1498ea1f5f37SPavel Tatashin 
1499a36aab89SMike Rapoport 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1500a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1501a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1502ea1f5f37SPavel Tatashin 
1503eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
1504ea1f5f37SPavel Tatashin 					   min_addr, max_addr, nid);
1505ea1f5f37SPavel Tatashin 	if (ptr)
1506ea1f5f37SPavel Tatashin 		memset(ptr, 0, size);
1507ea1f5f37SPavel Tatashin 	return ptr;
150826f09e9bSSantosh Shilimkar }
150926f09e9bSSantosh Shilimkar 
151026f09e9bSSantosh Shilimkar /**
1511eb31d559SMike Rapoport  * memblock_alloc_try_nid - allocate boot memory block with panicking
151226f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
151326f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
151426f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region from where the allocation
151526f09e9bSSantosh Shilimkar  *	  is preferred (phys address)
151626f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region from where the allocation
151797ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
151826f09e9bSSantosh Shilimkar  *	      allocate only from memory limited by memblock.current_limit value
151926f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
152026f09e9bSSantosh Shilimkar  *
1521eb31d559SMike Rapoport  * Public panicking version of memblock_alloc_try_nid_nopanic()
152226f09e9bSSantosh Shilimkar  * which provides debug information (including caller info), if enabled,
152326f09e9bSSantosh Shilimkar  * and panics if the request can not be satisfied.
152426f09e9bSSantosh Shilimkar  *
152547cec443SMike Rapoport  * Return:
152626f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
152726f09e9bSSantosh Shilimkar  */
1528eb31d559SMike Rapoport void * __init memblock_alloc_try_nid(
152926f09e9bSSantosh Shilimkar 			phys_addr_t size, phys_addr_t align,
153026f09e9bSSantosh Shilimkar 			phys_addr_t min_addr, phys_addr_t max_addr,
153126f09e9bSSantosh Shilimkar 			int nid)
153226f09e9bSSantosh Shilimkar {
153326f09e9bSSantosh Shilimkar 	void *ptr;
153426f09e9bSSantosh Shilimkar 
1535a36aab89SMike Rapoport 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1536a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1537a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1538eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
153926f09e9bSSantosh Shilimkar 					   min_addr, max_addr, nid);
1540ea1f5f37SPavel Tatashin 	if (ptr) {
1541ea1f5f37SPavel Tatashin 		memset(ptr, 0, size);
154226f09e9bSSantosh Shilimkar 		return ptr;
1543ea1f5f37SPavel Tatashin 	}
154426f09e9bSSantosh Shilimkar 
1545a36aab89SMike Rapoport 	panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa\n",
1546a36aab89SMike Rapoport 	      __func__, (u64)size, (u64)align, nid, &min_addr, &max_addr);
154726f09e9bSSantosh Shilimkar 	return NULL;
154826f09e9bSSantosh Shilimkar }
154926f09e9bSSantosh Shilimkar 
155026f09e9bSSantosh Shilimkar /**
155126f09e9bSSantosh Shilimkar  * __memblock_free_late - free bootmem block pages directly to buddy allocator
155248a833ccSMike Rapoport  * @base: phys starting address of the  boot memory block
155326f09e9bSSantosh Shilimkar  * @size: size of the boot memory block in bytes
155426f09e9bSSantosh Shilimkar  *
155526f09e9bSSantosh Shilimkar  * This is only useful when the bootmem allocator has already been torn
155626f09e9bSSantosh Shilimkar  * down, but we are still initializing the system.  Pages are released directly
155726f09e9bSSantosh Shilimkar  * to the buddy allocator, no bootmem metadata is updated because it is gone.
155826f09e9bSSantosh Shilimkar  */
155926f09e9bSSantosh Shilimkar void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
156026f09e9bSSantosh Shilimkar {
1561a36aab89SMike Rapoport 	phys_addr_t cursor, end;
156226f09e9bSSantosh Shilimkar 
1563a36aab89SMike Rapoport 	end = base + size - 1;
1564a36aab89SMike Rapoport 	memblock_dbg("%s: [%pa-%pa] %pF\n",
1565a36aab89SMike Rapoport 		     __func__, &base, &end, (void *)_RET_IP_);
15669099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
156726f09e9bSSantosh Shilimkar 	cursor = PFN_UP(base);
156826f09e9bSSantosh Shilimkar 	end = PFN_DOWN(base + size);
156926f09e9bSSantosh Shilimkar 
157026f09e9bSSantosh Shilimkar 	for (; cursor < end; cursor++) {
15717c2ee349SMike Rapoport 		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
1572ca79b0c2SArun KS 		totalram_pages_inc();
157326f09e9bSSantosh Shilimkar 	}
157426f09e9bSSantosh Shilimkar }
15759d1e2492SBenjamin Herrenschmidt 
15769d1e2492SBenjamin Herrenschmidt /*
15779d1e2492SBenjamin Herrenschmidt  * Remaining API functions
15789d1e2492SBenjamin Herrenschmidt  */
15799d1e2492SBenjamin Herrenschmidt 
15801f1ffb8aSDavid Gibson phys_addr_t __init_memblock memblock_phys_mem_size(void)
158195f72d1eSYinghai Lu {
15821440c4e2STejun Heo 	return memblock.memory.total_size;
158395f72d1eSYinghai Lu }
158495f72d1eSYinghai Lu 
15858907de5dSSrikar Dronamraju phys_addr_t __init_memblock memblock_reserved_size(void)
15868907de5dSSrikar Dronamraju {
15878907de5dSSrikar Dronamraju 	return memblock.reserved.total_size;
15888907de5dSSrikar Dronamraju }
15898907de5dSSrikar Dronamraju 
1590595ad9afSYinghai Lu phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1591595ad9afSYinghai Lu {
1592595ad9afSYinghai Lu 	unsigned long pages = 0;
1593595ad9afSYinghai Lu 	struct memblock_region *r;
1594595ad9afSYinghai Lu 	unsigned long start_pfn, end_pfn;
1595595ad9afSYinghai Lu 
1596595ad9afSYinghai Lu 	for_each_memblock(memory, r) {
1597595ad9afSYinghai Lu 		start_pfn = memblock_region_memory_base_pfn(r);
1598595ad9afSYinghai Lu 		end_pfn = memblock_region_memory_end_pfn(r);
1599595ad9afSYinghai Lu 		start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1600595ad9afSYinghai Lu 		end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1601595ad9afSYinghai Lu 		pages += end_pfn - start_pfn;
1602595ad9afSYinghai Lu 	}
1603595ad9afSYinghai Lu 
160416763230SFabian Frederick 	return PFN_PHYS(pages);
1605595ad9afSYinghai Lu }
1606595ad9afSYinghai Lu 
16070a93ebefSSam Ravnborg /* lowest address */
16080a93ebefSSam Ravnborg phys_addr_t __init_memblock memblock_start_of_DRAM(void)
16090a93ebefSSam Ravnborg {
16100a93ebefSSam Ravnborg 	return memblock.memory.regions[0].base;
16110a93ebefSSam Ravnborg }
16120a93ebefSSam Ravnborg 
161310d06439SYinghai Lu phys_addr_t __init_memblock memblock_end_of_DRAM(void)
161495f72d1eSYinghai Lu {
161595f72d1eSYinghai Lu 	int idx = memblock.memory.cnt - 1;
161695f72d1eSYinghai Lu 
1617e3239ff9SBenjamin Herrenschmidt 	return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
161895f72d1eSYinghai Lu }
161995f72d1eSYinghai Lu 
1620a571d4ebSDennis Chen static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
162195f72d1eSYinghai Lu {
16221c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1623136199f0SEmil Medve 	struct memblock_region *r;
162495f72d1eSYinghai Lu 
1625a571d4ebSDennis Chen 	/*
1626a571d4ebSDennis Chen 	 * translate the memory @limit size into the max address within one of
1627a571d4ebSDennis Chen 	 * the memory memblock regions, if the @limit exceeds the total size
16281c4bc43dSStefan Agner 	 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
1629a571d4ebSDennis Chen 	 */
1630136199f0SEmil Medve 	for_each_memblock(memory, r) {
1631c0ce8fefSTejun Heo 		if (limit <= r->size) {
1632c0ce8fefSTejun Heo 			max_addr = r->base + limit;
163395f72d1eSYinghai Lu 			break;
163495f72d1eSYinghai Lu 		}
1635c0ce8fefSTejun Heo 		limit -= r->size;
163695f72d1eSYinghai Lu 	}
1637c0ce8fefSTejun Heo 
1638a571d4ebSDennis Chen 	return max_addr;
1639a571d4ebSDennis Chen }
1640a571d4ebSDennis Chen 
1641a571d4ebSDennis Chen void __init memblock_enforce_memory_limit(phys_addr_t limit)
1642a571d4ebSDennis Chen {
16431c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1644a571d4ebSDennis Chen 
1645a571d4ebSDennis Chen 	if (!limit)
1646a571d4ebSDennis Chen 		return;
1647a571d4ebSDennis Chen 
1648a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1649a571d4ebSDennis Chen 
1650a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
16511c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1652a571d4ebSDennis Chen 		return;
1653a571d4ebSDennis Chen 
1654c0ce8fefSTejun Heo 	/* truncate both memory and reserved regions */
1655f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.memory, max_addr,
16561c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
1657f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.reserved, max_addr,
16581c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
165995f72d1eSYinghai Lu }
166095f72d1eSYinghai Lu 
1661c9ca9b4eSAKASHI Takahiro void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1662c9ca9b4eSAKASHI Takahiro {
1663c9ca9b4eSAKASHI Takahiro 	int start_rgn, end_rgn;
1664c9ca9b4eSAKASHI Takahiro 	int i, ret;
1665c9ca9b4eSAKASHI Takahiro 
1666c9ca9b4eSAKASHI Takahiro 	if (!size)
1667c9ca9b4eSAKASHI Takahiro 		return;
1668c9ca9b4eSAKASHI Takahiro 
1669c9ca9b4eSAKASHI Takahiro 	ret = memblock_isolate_range(&memblock.memory, base, size,
1670c9ca9b4eSAKASHI Takahiro 						&start_rgn, &end_rgn);
1671c9ca9b4eSAKASHI Takahiro 	if (ret)
1672c9ca9b4eSAKASHI Takahiro 		return;
1673c9ca9b4eSAKASHI Takahiro 
1674c9ca9b4eSAKASHI Takahiro 	/* remove all the MAP regions */
1675c9ca9b4eSAKASHI Takahiro 	for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1676c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1677c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1678c9ca9b4eSAKASHI Takahiro 
1679c9ca9b4eSAKASHI Takahiro 	for (i = start_rgn - 1; i >= 0; i--)
1680c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1681c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1682c9ca9b4eSAKASHI Takahiro 
1683c9ca9b4eSAKASHI Takahiro 	/* truncate the reserved regions */
1684c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved, 0, base);
1685c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved,
16861c4bc43dSStefan Agner 			base + size, PHYS_ADDR_MAX);
1687c9ca9b4eSAKASHI Takahiro }
1688c9ca9b4eSAKASHI Takahiro 
1689a571d4ebSDennis Chen void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1690a571d4ebSDennis Chen {
1691a571d4ebSDennis Chen 	phys_addr_t max_addr;
1692a571d4ebSDennis Chen 
1693a571d4ebSDennis Chen 	if (!limit)
1694a571d4ebSDennis Chen 		return;
1695a571d4ebSDennis Chen 
1696a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1697a571d4ebSDennis Chen 
1698a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
16991c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1700a571d4ebSDennis Chen 		return;
1701a571d4ebSDennis Chen 
1702c9ca9b4eSAKASHI Takahiro 	memblock_cap_memory_range(0, max_addr);
1703a571d4ebSDennis Chen }
1704a571d4ebSDennis Chen 
1705cd79481dSYinghai Lu static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
170672d4b0b4SBenjamin Herrenschmidt {
170772d4b0b4SBenjamin Herrenschmidt 	unsigned int left = 0, right = type->cnt;
170872d4b0b4SBenjamin Herrenschmidt 
170972d4b0b4SBenjamin Herrenschmidt 	do {
171072d4b0b4SBenjamin Herrenschmidt 		unsigned int mid = (right + left) / 2;
171172d4b0b4SBenjamin Herrenschmidt 
171272d4b0b4SBenjamin Herrenschmidt 		if (addr < type->regions[mid].base)
171372d4b0b4SBenjamin Herrenschmidt 			right = mid;
171472d4b0b4SBenjamin Herrenschmidt 		else if (addr >= (type->regions[mid].base +
171572d4b0b4SBenjamin Herrenschmidt 				  type->regions[mid].size))
171672d4b0b4SBenjamin Herrenschmidt 			left = mid + 1;
171772d4b0b4SBenjamin Herrenschmidt 		else
171872d4b0b4SBenjamin Herrenschmidt 			return mid;
171972d4b0b4SBenjamin Herrenschmidt 	} while (left < right);
172072d4b0b4SBenjamin Herrenschmidt 	return -1;
172172d4b0b4SBenjamin Herrenschmidt }
172272d4b0b4SBenjamin Herrenschmidt 
1723f5a222dcSYueyi Li bool __init_memblock memblock_is_reserved(phys_addr_t addr)
172495f72d1eSYinghai Lu {
172572d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.reserved, addr) != -1;
172695f72d1eSYinghai Lu }
172772d4b0b4SBenjamin Herrenschmidt 
1728b4ad0c7eSYaowei Bai bool __init_memblock memblock_is_memory(phys_addr_t addr)
172972d4b0b4SBenjamin Herrenschmidt {
173072d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.memory, addr) != -1;
173172d4b0b4SBenjamin Herrenschmidt }
173272d4b0b4SBenjamin Herrenschmidt 
1733937f0c26SYaowei Bai bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
1734bf3d3cc5SArd Biesheuvel {
1735bf3d3cc5SArd Biesheuvel 	int i = memblock_search(&memblock.memory, addr);
1736bf3d3cc5SArd Biesheuvel 
1737bf3d3cc5SArd Biesheuvel 	if (i == -1)
1738bf3d3cc5SArd Biesheuvel 		return false;
1739bf3d3cc5SArd Biesheuvel 	return !memblock_is_nomap(&memblock.memory.regions[i]);
1740bf3d3cc5SArd Biesheuvel }
1741bf3d3cc5SArd Biesheuvel 
1742e76b63f8SYinghai Lu #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1743e76b63f8SYinghai Lu int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1744e76b63f8SYinghai Lu 			 unsigned long *start_pfn, unsigned long *end_pfn)
1745e76b63f8SYinghai Lu {
1746e76b63f8SYinghai Lu 	struct memblock_type *type = &memblock.memory;
174716763230SFabian Frederick 	int mid = memblock_search(type, PFN_PHYS(pfn));
1748e76b63f8SYinghai Lu 
1749e76b63f8SYinghai Lu 	if (mid == -1)
1750e76b63f8SYinghai Lu 		return -1;
1751e76b63f8SYinghai Lu 
1752f7e2f7e8SFabian Frederick 	*start_pfn = PFN_DOWN(type->regions[mid].base);
1753f7e2f7e8SFabian Frederick 	*end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
1754e76b63f8SYinghai Lu 
1755e76b63f8SYinghai Lu 	return type->regions[mid].nid;
1756e76b63f8SYinghai Lu }
1757e76b63f8SYinghai Lu #endif
1758e76b63f8SYinghai Lu 
1759eab30949SStephen Boyd /**
1760eab30949SStephen Boyd  * memblock_is_region_memory - check if a region is a subset of memory
1761eab30949SStephen Boyd  * @base: base of region to check
1762eab30949SStephen Boyd  * @size: size of region to check
1763eab30949SStephen Boyd  *
1764eab30949SStephen Boyd  * Check if the region [@base, @base + @size) is a subset of a memory block.
1765eab30949SStephen Boyd  *
176647cec443SMike Rapoport  * Return:
1767eab30949SStephen Boyd  * 0 if false, non-zero if true
1768eab30949SStephen Boyd  */
1769937f0c26SYaowei Bai bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
177072d4b0b4SBenjamin Herrenschmidt {
1771abb65272STomi Valkeinen 	int idx = memblock_search(&memblock.memory, base);
1772eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
177372d4b0b4SBenjamin Herrenschmidt 
177472d4b0b4SBenjamin Herrenschmidt 	if (idx == -1)
1775937f0c26SYaowei Bai 		return false;
1776ef415ef4SWei Yang 	return (memblock.memory.regions[idx].base +
1777eb18f1b5STejun Heo 		 memblock.memory.regions[idx].size) >= end;
177895f72d1eSYinghai Lu }
177995f72d1eSYinghai Lu 
1780eab30949SStephen Boyd /**
1781eab30949SStephen Boyd  * memblock_is_region_reserved - check if a region intersects reserved memory
1782eab30949SStephen Boyd  * @base: base of region to check
1783eab30949SStephen Boyd  * @size: size of region to check
1784eab30949SStephen Boyd  *
178547cec443SMike Rapoport  * Check if the region [@base, @base + @size) intersects a reserved
178647cec443SMike Rapoport  * memory block.
1787eab30949SStephen Boyd  *
178847cec443SMike Rapoport  * Return:
1789c5c5c9d1STang Chen  * True if they intersect, false if not.
1790eab30949SStephen Boyd  */
1791c5c5c9d1STang Chen bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
179295f72d1eSYinghai Lu {
1793eb18f1b5STejun Heo 	memblock_cap_size(base, &size);
1794c5c5c9d1STang Chen 	return memblock_overlaps_region(&memblock.reserved, base, size);
179595f72d1eSYinghai Lu }
179695f72d1eSYinghai Lu 
17976ede1fd3SYinghai Lu void __init_memblock memblock_trim_memory(phys_addr_t align)
17986ede1fd3SYinghai Lu {
17996ede1fd3SYinghai Lu 	phys_addr_t start, end, orig_start, orig_end;
1800136199f0SEmil Medve 	struct memblock_region *r;
18016ede1fd3SYinghai Lu 
1802136199f0SEmil Medve 	for_each_memblock(memory, r) {
1803136199f0SEmil Medve 		orig_start = r->base;
1804136199f0SEmil Medve 		orig_end = r->base + r->size;
18056ede1fd3SYinghai Lu 		start = round_up(orig_start, align);
18066ede1fd3SYinghai Lu 		end = round_down(orig_end, align);
18076ede1fd3SYinghai Lu 
18086ede1fd3SYinghai Lu 		if (start == orig_start && end == orig_end)
18096ede1fd3SYinghai Lu 			continue;
18106ede1fd3SYinghai Lu 
18116ede1fd3SYinghai Lu 		if (start < end) {
1812136199f0SEmil Medve 			r->base = start;
1813136199f0SEmil Medve 			r->size = end - start;
18146ede1fd3SYinghai Lu 		} else {
1815136199f0SEmil Medve 			memblock_remove_region(&memblock.memory,
1816136199f0SEmil Medve 					       r - memblock.memory.regions);
1817136199f0SEmil Medve 			r--;
18186ede1fd3SYinghai Lu 		}
18196ede1fd3SYinghai Lu 	}
18206ede1fd3SYinghai Lu }
1821e63075a3SBenjamin Herrenschmidt 
18223661ca66SYinghai Lu void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1823e63075a3SBenjamin Herrenschmidt {
1824e63075a3SBenjamin Herrenschmidt 	memblock.current_limit = limit;
1825e63075a3SBenjamin Herrenschmidt }
1826e63075a3SBenjamin Herrenschmidt 
1827fec51014SLaura Abbott phys_addr_t __init_memblock memblock_get_current_limit(void)
1828fec51014SLaura Abbott {
1829fec51014SLaura Abbott 	return memblock.current_limit;
1830fec51014SLaura Abbott }
1831fec51014SLaura Abbott 
18320262d9c8SHeiko Carstens static void __init_memblock memblock_dump(struct memblock_type *type)
18336ed311b2SBenjamin Herrenschmidt {
18345d63f81cSMiles Chen 	phys_addr_t base, end, size;
1835e1720feeSMike Rapoport 	enum memblock_flags flags;
18368c9c1701SAlexander Kuleshov 	int idx;
18378c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
18386ed311b2SBenjamin Herrenschmidt 
18390262d9c8SHeiko Carstens 	pr_info(" %s.cnt  = 0x%lx\n", type->name, type->cnt);
18406ed311b2SBenjamin Herrenschmidt 
184166e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
18427c0caeb8STejun Heo 		char nid_buf[32] = "";
18436ed311b2SBenjamin Herrenschmidt 
18447c0caeb8STejun Heo 		base = rgn->base;
18457c0caeb8STejun Heo 		size = rgn->size;
18465d63f81cSMiles Chen 		end = base + size - 1;
184766a20757STang Chen 		flags = rgn->flags;
18487c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
18497c0caeb8STejun Heo 		if (memblock_get_region_node(rgn) != MAX_NUMNODES)
18507c0caeb8STejun Heo 			snprintf(nid_buf, sizeof(nid_buf), " on node %d",
18517c0caeb8STejun Heo 				 memblock_get_region_node(rgn));
18527c0caeb8STejun Heo #endif
1853e1720feeSMike Rapoport 		pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
18540262d9c8SHeiko Carstens 			type->name, idx, &base, &end, &size, nid_buf, flags);
18556ed311b2SBenjamin Herrenschmidt 	}
18566ed311b2SBenjamin Herrenschmidt }
18576ed311b2SBenjamin Herrenschmidt 
18584ff7b82fSTejun Heo void __init_memblock __memblock_dump_all(void)
18596ed311b2SBenjamin Herrenschmidt {
18606ed311b2SBenjamin Herrenschmidt 	pr_info("MEMBLOCK configuration:\n");
18615d63f81cSMiles Chen 	pr_info(" memory size = %pa reserved size = %pa\n",
18625d63f81cSMiles Chen 		&memblock.memory.total_size,
18635d63f81cSMiles Chen 		&memblock.reserved.total_size);
18646ed311b2SBenjamin Herrenschmidt 
18650262d9c8SHeiko Carstens 	memblock_dump(&memblock.memory);
18660262d9c8SHeiko Carstens 	memblock_dump(&memblock.reserved);
1867409efd4cSHeiko Carstens #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
18680262d9c8SHeiko Carstens 	memblock_dump(&memblock.physmem);
1869409efd4cSHeiko Carstens #endif
18706ed311b2SBenjamin Herrenschmidt }
18716ed311b2SBenjamin Herrenschmidt 
18721aadc056STejun Heo void __init memblock_allow_resize(void)
18736ed311b2SBenjamin Herrenschmidt {
1874142b45a7SBenjamin Herrenschmidt 	memblock_can_resize = 1;
18756ed311b2SBenjamin Herrenschmidt }
18766ed311b2SBenjamin Herrenschmidt 
18776ed311b2SBenjamin Herrenschmidt static int __init early_memblock(char *p)
18786ed311b2SBenjamin Herrenschmidt {
18796ed311b2SBenjamin Herrenschmidt 	if (p && strstr(p, "debug"))
18806ed311b2SBenjamin Herrenschmidt 		memblock_debug = 1;
18816ed311b2SBenjamin Herrenschmidt 	return 0;
18826ed311b2SBenjamin Herrenschmidt }
18836ed311b2SBenjamin Herrenschmidt early_param("memblock", early_memblock);
18846ed311b2SBenjamin Herrenschmidt 
1885bda49a81SMike Rapoport static void __init __free_pages_memory(unsigned long start, unsigned long end)
1886bda49a81SMike Rapoport {
1887bda49a81SMike Rapoport 	int order;
1888bda49a81SMike Rapoport 
1889bda49a81SMike Rapoport 	while (start < end) {
1890bda49a81SMike Rapoport 		order = min(MAX_ORDER - 1UL, __ffs(start));
1891bda49a81SMike Rapoport 
1892bda49a81SMike Rapoport 		while (start + (1UL << order) > end)
1893bda49a81SMike Rapoport 			order--;
1894bda49a81SMike Rapoport 
1895bda49a81SMike Rapoport 		memblock_free_pages(pfn_to_page(start), start, order);
1896bda49a81SMike Rapoport 
1897bda49a81SMike Rapoport 		start += (1UL << order);
1898bda49a81SMike Rapoport 	}
1899bda49a81SMike Rapoport }
1900bda49a81SMike Rapoport 
1901bda49a81SMike Rapoport static unsigned long __init __free_memory_core(phys_addr_t start,
1902bda49a81SMike Rapoport 				 phys_addr_t end)
1903bda49a81SMike Rapoport {
1904bda49a81SMike Rapoport 	unsigned long start_pfn = PFN_UP(start);
1905bda49a81SMike Rapoport 	unsigned long end_pfn = min_t(unsigned long,
1906bda49a81SMike Rapoport 				      PFN_DOWN(end), max_low_pfn);
1907bda49a81SMike Rapoport 
1908bda49a81SMike Rapoport 	if (start_pfn >= end_pfn)
1909bda49a81SMike Rapoport 		return 0;
1910bda49a81SMike Rapoport 
1911bda49a81SMike Rapoport 	__free_pages_memory(start_pfn, end_pfn);
1912bda49a81SMike Rapoport 
1913bda49a81SMike Rapoport 	return end_pfn - start_pfn;
1914bda49a81SMike Rapoport }
1915bda49a81SMike Rapoport 
1916bda49a81SMike Rapoport static unsigned long __init free_low_memory_core_early(void)
1917bda49a81SMike Rapoport {
1918bda49a81SMike Rapoport 	unsigned long count = 0;
1919bda49a81SMike Rapoport 	phys_addr_t start, end;
1920bda49a81SMike Rapoport 	u64 i;
1921bda49a81SMike Rapoport 
1922bda49a81SMike Rapoport 	memblock_clear_hotplug(0, -1);
1923bda49a81SMike Rapoport 
1924bda49a81SMike Rapoport 	for_each_reserved_mem_region(i, &start, &end)
1925bda49a81SMike Rapoport 		reserve_bootmem_region(start, end);
1926bda49a81SMike Rapoport 
1927bda49a81SMike Rapoport 	/*
1928bda49a81SMike Rapoport 	 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
1929bda49a81SMike Rapoport 	 *  because in some case like Node0 doesn't have RAM installed
1930bda49a81SMike Rapoport 	 *  low ram will be on Node1
1931bda49a81SMike Rapoport 	 */
1932bda49a81SMike Rapoport 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
1933bda49a81SMike Rapoport 				NULL)
1934bda49a81SMike Rapoport 		count += __free_memory_core(start, end);
1935bda49a81SMike Rapoport 
1936bda49a81SMike Rapoport 	return count;
1937bda49a81SMike Rapoport }
1938bda49a81SMike Rapoport 
1939bda49a81SMike Rapoport static int reset_managed_pages_done __initdata;
1940bda49a81SMike Rapoport 
1941bda49a81SMike Rapoport void reset_node_managed_pages(pg_data_t *pgdat)
1942bda49a81SMike Rapoport {
1943bda49a81SMike Rapoport 	struct zone *z;
1944bda49a81SMike Rapoport 
1945bda49a81SMike Rapoport 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
19469705bea5SArun KS 		atomic_long_set(&z->managed_pages, 0);
1947bda49a81SMike Rapoport }
1948bda49a81SMike Rapoport 
1949bda49a81SMike Rapoport void __init reset_all_zones_managed_pages(void)
1950bda49a81SMike Rapoport {
1951bda49a81SMike Rapoport 	struct pglist_data *pgdat;
1952bda49a81SMike Rapoport 
1953bda49a81SMike Rapoport 	if (reset_managed_pages_done)
1954bda49a81SMike Rapoport 		return;
1955bda49a81SMike Rapoport 
1956bda49a81SMike Rapoport 	for_each_online_pgdat(pgdat)
1957bda49a81SMike Rapoport 		reset_node_managed_pages(pgdat);
1958bda49a81SMike Rapoport 
1959bda49a81SMike Rapoport 	reset_managed_pages_done = 1;
1960bda49a81SMike Rapoport }
1961bda49a81SMike Rapoport 
1962bda49a81SMike Rapoport /**
1963bda49a81SMike Rapoport  * memblock_free_all - release free pages to the buddy allocator
1964bda49a81SMike Rapoport  *
1965bda49a81SMike Rapoport  * Return: the number of pages actually released.
1966bda49a81SMike Rapoport  */
1967bda49a81SMike Rapoport unsigned long __init memblock_free_all(void)
1968bda49a81SMike Rapoport {
1969bda49a81SMike Rapoport 	unsigned long pages;
1970bda49a81SMike Rapoport 
1971bda49a81SMike Rapoport 	reset_all_zones_managed_pages();
1972bda49a81SMike Rapoport 
1973bda49a81SMike Rapoport 	pages = free_low_memory_core_early();
1974ca79b0c2SArun KS 	totalram_pages_add(pages);
1975bda49a81SMike Rapoport 
1976bda49a81SMike Rapoport 	return pages;
1977bda49a81SMike Rapoport }
1978bda49a81SMike Rapoport 
1979c378ddd5STejun Heo #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
19806d03b885SBenjamin Herrenschmidt 
19816d03b885SBenjamin Herrenschmidt static int memblock_debug_show(struct seq_file *m, void *private)
19826d03b885SBenjamin Herrenschmidt {
19836d03b885SBenjamin Herrenschmidt 	struct memblock_type *type = m->private;
19846d03b885SBenjamin Herrenschmidt 	struct memblock_region *reg;
19856d03b885SBenjamin Herrenschmidt 	int i;
19865d63f81cSMiles Chen 	phys_addr_t end;
19876d03b885SBenjamin Herrenschmidt 
19886d03b885SBenjamin Herrenschmidt 	for (i = 0; i < type->cnt; i++) {
19896d03b885SBenjamin Herrenschmidt 		reg = &type->regions[i];
19905d63f81cSMiles Chen 		end = reg->base + reg->size - 1;
19916d03b885SBenjamin Herrenschmidt 
19925d63f81cSMiles Chen 		seq_printf(m, "%4d: ", i);
19935d63f81cSMiles Chen 		seq_printf(m, "%pa..%pa\n", &reg->base, &end);
19946d03b885SBenjamin Herrenschmidt 	}
19956d03b885SBenjamin Herrenschmidt 	return 0;
19966d03b885SBenjamin Herrenschmidt }
19975ad35093SAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(memblock_debug);
19986d03b885SBenjamin Herrenschmidt 
19996d03b885SBenjamin Herrenschmidt static int __init memblock_init_debugfs(void)
20006d03b885SBenjamin Herrenschmidt {
20016d03b885SBenjamin Herrenschmidt 	struct dentry *root = debugfs_create_dir("memblock", NULL);
2002d9f7979cSGreg Kroah-Hartman 
20030825a6f9SJoe Perches 	debugfs_create_file("memory", 0444, root,
20040825a6f9SJoe Perches 			    &memblock.memory, &memblock_debug_fops);
20050825a6f9SJoe Perches 	debugfs_create_file("reserved", 0444, root,
20060825a6f9SJoe Perches 			    &memblock.reserved, &memblock_debug_fops);
200770210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
20080825a6f9SJoe Perches 	debugfs_create_file("physmem", 0444, root,
20090825a6f9SJoe Perches 			    &memblock.physmem, &memblock_debug_fops);
201070210ed9SPhilipp Hachtmann #endif
20116d03b885SBenjamin Herrenschmidt 
20126d03b885SBenjamin Herrenschmidt 	return 0;
20136d03b885SBenjamin Herrenschmidt }
20146d03b885SBenjamin Herrenschmidt __initcall(memblock_init_debugfs);
20156d03b885SBenjamin Herrenschmidt 
20166d03b885SBenjamin Herrenschmidt #endif /* CONFIG_DEBUG_FS */
2017