xref: /linux/mm/memblock.c (revision 02634a44b8aba2d4f16ea09d3c17400d9320327e)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
295f72d1eSYinghai Lu /*
395f72d1eSYinghai Lu  * Procedures for maintaining information about logical memory blocks.
495f72d1eSYinghai Lu  *
595f72d1eSYinghai Lu  * Peter Bergner, IBM Corp.	June 2001.
695f72d1eSYinghai Lu  * Copyright (C) 2001 Peter Bergner.
795f72d1eSYinghai Lu  */
895f72d1eSYinghai Lu 
995f72d1eSYinghai Lu #include <linux/kernel.h>
10142b45a7SBenjamin Herrenschmidt #include <linux/slab.h>
1195f72d1eSYinghai Lu #include <linux/init.h>
1295f72d1eSYinghai Lu #include <linux/bitops.h>
13449e8df3SBenjamin Herrenschmidt #include <linux/poison.h>
14c196f76fSBenjamin Herrenschmidt #include <linux/pfn.h>
156d03b885SBenjamin Herrenschmidt #include <linux/debugfs.h>
16514c6032SRandy Dunlap #include <linux/kmemleak.h>
176d03b885SBenjamin Herrenschmidt #include <linux/seq_file.h>
1895f72d1eSYinghai Lu #include <linux/memblock.h>
1995f72d1eSYinghai Lu 
20c4c5ad6bSChristoph Hellwig #include <asm/sections.h>
2126f09e9bSSantosh Shilimkar #include <linux/io.h>
2226f09e9bSSantosh Shilimkar 
2326f09e9bSSantosh Shilimkar #include "internal.h"
2479442ed1STang Chen 
258a5b403dSArd Biesheuvel #define INIT_MEMBLOCK_REGIONS			128
268a5b403dSArd Biesheuvel #define INIT_PHYSMEM_REGIONS			4
278a5b403dSArd Biesheuvel 
288a5b403dSArd Biesheuvel #ifndef INIT_MEMBLOCK_RESERVED_REGIONS
298a5b403dSArd Biesheuvel # define INIT_MEMBLOCK_RESERVED_REGIONS		INIT_MEMBLOCK_REGIONS
308a5b403dSArd Biesheuvel #endif
318a5b403dSArd Biesheuvel 
323e039c5cSMike Rapoport /**
333e039c5cSMike Rapoport  * DOC: memblock overview
343e039c5cSMike Rapoport  *
353e039c5cSMike Rapoport  * Memblock is a method of managing memory regions during the early
363e039c5cSMike Rapoport  * boot period when the usual kernel memory allocators are not up and
373e039c5cSMike Rapoport  * running.
383e039c5cSMike Rapoport  *
393e039c5cSMike Rapoport  * Memblock views the system memory as collections of contiguous
403e039c5cSMike Rapoport  * regions. There are several types of these collections:
413e039c5cSMike Rapoport  *
423e039c5cSMike Rapoport  * * ``memory`` - describes the physical memory available to the
433e039c5cSMike Rapoport  *   kernel; this may differ from the actual physical memory installed
443e039c5cSMike Rapoport  *   in the system, for instance when the memory is restricted with
453e039c5cSMike Rapoport  *   ``mem=`` command line parameter
463e039c5cSMike Rapoport  * * ``reserved`` - describes the regions that were allocated
473e039c5cSMike Rapoport  * * ``physmap`` - describes the actual physical memory regardless of
483e039c5cSMike Rapoport  *   the possible restrictions; the ``physmap`` type is only available
493e039c5cSMike Rapoport  *   on some architectures.
503e039c5cSMike Rapoport  *
513e039c5cSMike Rapoport  * Each region is represented by :c:type:`struct memblock_region` that
523e039c5cSMike Rapoport  * defines the region extents, its attributes and NUMA node id on NUMA
533e039c5cSMike Rapoport  * systems. Every memory type is described by the :c:type:`struct
543e039c5cSMike Rapoport  * memblock_type` which contains an array of memory regions along with
553e039c5cSMike Rapoport  * the allocator metadata. The memory types are nicely wrapped with
563e039c5cSMike Rapoport  * :c:type:`struct memblock`. This structure is statically initialzed
573e039c5cSMike Rapoport  * at build time. The region arrays for the "memory" and "reserved"
583e039c5cSMike Rapoport  * types are initially sized to %INIT_MEMBLOCK_REGIONS and for the
593e039c5cSMike Rapoport  * "physmap" type to %INIT_PHYSMEM_REGIONS.
606e5af9a8SCao jin  * The memblock_allow_resize() enables automatic resizing of the region
616e5af9a8SCao jin  * arrays during addition of new regions. This feature should be used
626e5af9a8SCao jin  * with care so that memory allocated for the region array will not
636e5af9a8SCao jin  * overlap with areas that should be reserved, for example initrd.
643e039c5cSMike Rapoport  *
653e039c5cSMike Rapoport  * The early architecture setup should tell memblock what the physical
666e5af9a8SCao jin  * memory layout is by using memblock_add() or memblock_add_node()
676e5af9a8SCao jin  * functions. The first function does not assign the region to a NUMA
686e5af9a8SCao jin  * node and it is appropriate for UMA systems. Yet, it is possible to
696e5af9a8SCao jin  * use it on NUMA systems as well and assign the region to a NUMA node
706e5af9a8SCao jin  * later in the setup process using memblock_set_node(). The
716e5af9a8SCao jin  * memblock_add_node() performs such an assignment directly.
723e039c5cSMike Rapoport  *
73a2974133SMike Rapoport  * Once memblock is setup the memory can be allocated using one of the
74a2974133SMike Rapoport  * API variants:
75a2974133SMike Rapoport  *
766e5af9a8SCao jin  * * memblock_phys_alloc*() - these functions return the **physical**
776e5af9a8SCao jin  *   address of the allocated memory
786e5af9a8SCao jin  * * memblock_alloc*() - these functions return the **virtual** address
796e5af9a8SCao jin  *   of the allocated memory.
80a2974133SMike Rapoport  *
81a2974133SMike Rapoport  * Note, that both API variants use implict assumptions about allowed
82a2974133SMike Rapoport  * memory ranges and the fallback methods. Consult the documentation
836e5af9a8SCao jin  * of memblock_alloc_internal() and memblock_alloc_range_nid()
846e5af9a8SCao jin  * functions for more elaborate description.
853e039c5cSMike Rapoport  *
866e5af9a8SCao jin  * As the system boot progresses, the architecture specific mem_init()
876e5af9a8SCao jin  * function frees all the memory to the buddy page allocator.
883e039c5cSMike Rapoport  *
896e5af9a8SCao jin  * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the
903e039c5cSMike Rapoport  * memblock data structures will be discarded after the system
916e5af9a8SCao jin  * initialization completes.
923e039c5cSMike Rapoport  */
933e039c5cSMike Rapoport 
94bda49a81SMike Rapoport #ifndef CONFIG_NEED_MULTIPLE_NODES
95bda49a81SMike Rapoport struct pglist_data __refdata contig_page_data;
96bda49a81SMike Rapoport EXPORT_SYMBOL(contig_page_data);
97bda49a81SMike Rapoport #endif
98bda49a81SMike Rapoport 
99bda49a81SMike Rapoport unsigned long max_low_pfn;
100bda49a81SMike Rapoport unsigned long min_low_pfn;
101bda49a81SMike Rapoport unsigned long max_pfn;
102bda49a81SMike Rapoport unsigned long long max_possible_pfn;
103bda49a81SMike Rapoport 
104fe091c20STejun Heo static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
1058a5b403dSArd Biesheuvel static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
10670210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
10770210ed9SPhilipp Hachtmann static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
10870210ed9SPhilipp Hachtmann #endif
109fe091c20STejun Heo 
110fe091c20STejun Heo struct memblock memblock __initdata_memblock = {
111fe091c20STejun Heo 	.memory.regions		= memblock_memory_init_regions,
112fe091c20STejun Heo 	.memory.cnt		= 1,	/* empty dummy entry */
113fe091c20STejun Heo 	.memory.max		= INIT_MEMBLOCK_REGIONS,
1140262d9c8SHeiko Carstens 	.memory.name		= "memory",
115fe091c20STejun Heo 
116fe091c20STejun Heo 	.reserved.regions	= memblock_reserved_init_regions,
117fe091c20STejun Heo 	.reserved.cnt		= 1,	/* empty dummy entry */
1188a5b403dSArd Biesheuvel 	.reserved.max		= INIT_MEMBLOCK_RESERVED_REGIONS,
1190262d9c8SHeiko Carstens 	.reserved.name		= "reserved",
120fe091c20STejun Heo 
12170210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
12270210ed9SPhilipp Hachtmann 	.physmem.regions	= memblock_physmem_init_regions,
12370210ed9SPhilipp Hachtmann 	.physmem.cnt		= 1,	/* empty dummy entry */
12470210ed9SPhilipp Hachtmann 	.physmem.max		= INIT_PHYSMEM_REGIONS,
1250262d9c8SHeiko Carstens 	.physmem.name		= "physmem",
12670210ed9SPhilipp Hachtmann #endif
12770210ed9SPhilipp Hachtmann 
12879442ed1STang Chen 	.bottom_up		= false,
129fe091c20STejun Heo 	.current_limit		= MEMBLOCK_ALLOC_ANYWHERE,
130fe091c20STejun Heo };
13195f72d1eSYinghai Lu 
13210d06439SYinghai Lu int memblock_debug __initdata_memblock;
133a3f5bafcSTony Luck static bool system_has_some_mirror __initdata_memblock = false;
1341aadc056STejun Heo static int memblock_can_resize __initdata_memblock;
135181eb394SGavin Shan static int memblock_memory_in_slab __initdata_memblock = 0;
136181eb394SGavin Shan static int memblock_reserved_in_slab __initdata_memblock = 0;
13795f72d1eSYinghai Lu 
138c366ea89SMike Rapoport static enum memblock_flags __init_memblock choose_memblock_flags(void)
139a3f5bafcSTony Luck {
140a3f5bafcSTony Luck 	return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
141a3f5bafcSTony Luck }
142a3f5bafcSTony Luck 
143eb18f1b5STejun Heo /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
144eb18f1b5STejun Heo static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
145eb18f1b5STejun Heo {
1461c4bc43dSStefan Agner 	return *size = min(*size, PHYS_ADDR_MAX - base);
147eb18f1b5STejun Heo }
148eb18f1b5STejun Heo 
1496ed311b2SBenjamin Herrenschmidt /*
1506ed311b2SBenjamin Herrenschmidt  * Address comparison utilities
1516ed311b2SBenjamin Herrenschmidt  */
15210d06439SYinghai Lu static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
1532898cc4cSBenjamin Herrenschmidt 				       phys_addr_t base2, phys_addr_t size2)
15495f72d1eSYinghai Lu {
15595f72d1eSYinghai Lu 	return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
15695f72d1eSYinghai Lu }
15795f72d1eSYinghai Lu 
15895cf82ecSTang Chen bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
1592d7d3eb2SH Hartley Sweeten 					phys_addr_t base, phys_addr_t size)
1606ed311b2SBenjamin Herrenschmidt {
1616ed311b2SBenjamin Herrenschmidt 	unsigned long i;
1626ed311b2SBenjamin Herrenschmidt 
163f14516fbSAlexander Kuleshov 	for (i = 0; i < type->cnt; i++)
164f14516fbSAlexander Kuleshov 		if (memblock_addrs_overlap(base, size, type->regions[i].base,
165f14516fbSAlexander Kuleshov 					   type->regions[i].size))
1666ed311b2SBenjamin Herrenschmidt 			break;
167c5c5c9d1STang Chen 	return i < type->cnt;
1686ed311b2SBenjamin Herrenschmidt }
1696ed311b2SBenjamin Herrenschmidt 
17047cec443SMike Rapoport /**
17179442ed1STang Chen  * __memblock_find_range_bottom_up - find free area utility in bottom-up
17279442ed1STang Chen  * @start: start of candidate range
17347cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
17447cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
17579442ed1STang Chen  * @size: size of free area to find
17679442ed1STang Chen  * @align: alignment of free area to find
177b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
178fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
17979442ed1STang Chen  *
18079442ed1STang Chen  * Utility called from memblock_find_in_range_node(), find free area bottom-up.
18179442ed1STang Chen  *
18247cec443SMike Rapoport  * Return:
18379442ed1STang Chen  * Found address on success, 0 on failure.
18479442ed1STang Chen  */
18579442ed1STang Chen static phys_addr_t __init_memblock
18679442ed1STang Chen __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
187fc6daaf9STony Luck 				phys_addr_t size, phys_addr_t align, int nid,
188e1720feeSMike Rapoport 				enum memblock_flags flags)
18979442ed1STang Chen {
19079442ed1STang Chen 	phys_addr_t this_start, this_end, cand;
19179442ed1STang Chen 	u64 i;
19279442ed1STang Chen 
193fc6daaf9STony Luck 	for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
19479442ed1STang Chen 		this_start = clamp(this_start, start, end);
19579442ed1STang Chen 		this_end = clamp(this_end, start, end);
19679442ed1STang Chen 
19779442ed1STang Chen 		cand = round_up(this_start, align);
19879442ed1STang Chen 		if (cand < this_end && this_end - cand >= size)
19979442ed1STang Chen 			return cand;
20079442ed1STang Chen 	}
20179442ed1STang Chen 
20279442ed1STang Chen 	return 0;
20379442ed1STang Chen }
20479442ed1STang Chen 
2057bd0b0f0STejun Heo /**
2061402899eSTang Chen  * __memblock_find_range_top_down - find free area utility, in top-down
2071402899eSTang Chen  * @start: start of candidate range
20847cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
20947cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
2101402899eSTang Chen  * @size: size of free area to find
2111402899eSTang Chen  * @align: alignment of free area to find
212b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
213fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2141402899eSTang Chen  *
2151402899eSTang Chen  * Utility called from memblock_find_in_range_node(), find free area top-down.
2161402899eSTang Chen  *
21747cec443SMike Rapoport  * Return:
21879442ed1STang Chen  * Found address on success, 0 on failure.
2191402899eSTang Chen  */
2201402899eSTang Chen static phys_addr_t __init_memblock
2211402899eSTang Chen __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
222fc6daaf9STony Luck 			       phys_addr_t size, phys_addr_t align, int nid,
223e1720feeSMike Rapoport 			       enum memblock_flags flags)
2241402899eSTang Chen {
2251402899eSTang Chen 	phys_addr_t this_start, this_end, cand;
2261402899eSTang Chen 	u64 i;
2271402899eSTang Chen 
228fc6daaf9STony Luck 	for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
229fc6daaf9STony Luck 					NULL) {
2301402899eSTang Chen 		this_start = clamp(this_start, start, end);
2311402899eSTang Chen 		this_end = clamp(this_end, start, end);
2321402899eSTang Chen 
2331402899eSTang Chen 		if (this_end < size)
2341402899eSTang Chen 			continue;
2351402899eSTang Chen 
2361402899eSTang Chen 		cand = round_down(this_end - size, align);
2371402899eSTang Chen 		if (cand >= this_start)
2381402899eSTang Chen 			return cand;
2391402899eSTang Chen 	}
2401402899eSTang Chen 
2411402899eSTang Chen 	return 0;
2421402899eSTang Chen }
2431402899eSTang Chen 
2441402899eSTang Chen /**
2457bd0b0f0STejun Heo  * memblock_find_in_range_node - find free area in given range and node
2467bd0b0f0STejun Heo  * @size: size of free area to find
2477bd0b0f0STejun Heo  * @align: alignment of free area to find
24887029ee9SGrygorii Strashko  * @start: start of candidate range
24947cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
25047cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
251b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
252fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2537bd0b0f0STejun Heo  *
2547bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range and node.
2557bd0b0f0STejun Heo  *
25679442ed1STang Chen  * When allocation direction is bottom-up, the @start should be greater
25779442ed1STang Chen  * than the end of the kernel image. Otherwise, it will be trimmed. The
25879442ed1STang Chen  * reason is that we want the bottom-up allocation just near the kernel
25979442ed1STang Chen  * image so it is highly likely that the allocated memory and the kernel
26079442ed1STang Chen  * will reside in the same node.
26179442ed1STang Chen  *
26279442ed1STang Chen  * If bottom-up allocation failed, will try to allocate memory top-down.
26379442ed1STang Chen  *
26447cec443SMike Rapoport  * Return:
26579442ed1STang Chen  * Found address on success, 0 on failure.
2666ed311b2SBenjamin Herrenschmidt  */
267c366ea89SMike Rapoport static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
26887029ee9SGrygorii Strashko 					phys_addr_t align, phys_addr_t start,
269e1720feeSMike Rapoport 					phys_addr_t end, int nid,
270e1720feeSMike Rapoport 					enum memblock_flags flags)
271f7210e6cSTang Chen {
2720cfb8f0cSTang Chen 	phys_addr_t kernel_end, ret;
27379442ed1STang Chen 
274f7210e6cSTang Chen 	/* pump up @end */
275fed84c78SQian Cai 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
276fed84c78SQian Cai 	    end == MEMBLOCK_ALLOC_KASAN)
277f7210e6cSTang Chen 		end = memblock.current_limit;
278f7210e6cSTang Chen 
279f7210e6cSTang Chen 	/* avoid allocating the first page */
280f7210e6cSTang Chen 	start = max_t(phys_addr_t, start, PAGE_SIZE);
281f7210e6cSTang Chen 	end = max(start, end);
28279442ed1STang Chen 	kernel_end = __pa_symbol(_end);
28379442ed1STang Chen 
28479442ed1STang Chen 	/*
28579442ed1STang Chen 	 * try bottom-up allocation only when bottom-up mode
28679442ed1STang Chen 	 * is set and @end is above the kernel image.
28779442ed1STang Chen 	 */
28879442ed1STang Chen 	if (memblock_bottom_up() && end > kernel_end) {
28979442ed1STang Chen 		phys_addr_t bottom_up_start;
29079442ed1STang Chen 
29179442ed1STang Chen 		/* make sure we will allocate above the kernel */
29279442ed1STang Chen 		bottom_up_start = max(start, kernel_end);
29379442ed1STang Chen 
29479442ed1STang Chen 		/* ok, try bottom-up allocation first */
29579442ed1STang Chen 		ret = __memblock_find_range_bottom_up(bottom_up_start, end,
296fc6daaf9STony Luck 						      size, align, nid, flags);
29779442ed1STang Chen 		if (ret)
29879442ed1STang Chen 			return ret;
29979442ed1STang Chen 
30079442ed1STang Chen 		/*
30179442ed1STang Chen 		 * we always limit bottom-up allocation above the kernel,
30279442ed1STang Chen 		 * but top-down allocation doesn't have the limit, so
30379442ed1STang Chen 		 * retrying top-down allocation may succeed when bottom-up
30479442ed1STang Chen 		 * allocation failed.
30579442ed1STang Chen 		 *
30679442ed1STang Chen 		 * bottom-up allocation is expected to be fail very rarely,
30779442ed1STang Chen 		 * so we use WARN_ONCE() here to see the stack trace if
30879442ed1STang Chen 		 * fail happens.
30979442ed1STang Chen 		 */
310e3d301caSMichal Hocko 		WARN_ONCE(IS_ENABLED(CONFIG_MEMORY_HOTREMOVE),
311e3d301caSMichal Hocko 			  "memblock: bottom-up allocation failed, memory hotremove may be affected\n");
31279442ed1STang Chen 	}
313f7210e6cSTang Chen 
314fc6daaf9STony Luck 	return __memblock_find_range_top_down(start, end, size, align, nid,
315fc6daaf9STony Luck 					      flags);
316f7210e6cSTang Chen }
3176ed311b2SBenjamin Herrenschmidt 
3187bd0b0f0STejun Heo /**
3197bd0b0f0STejun Heo  * memblock_find_in_range - find free area in given range
3207bd0b0f0STejun Heo  * @start: start of candidate range
32147cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
32247cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
3237bd0b0f0STejun Heo  * @size: size of free area to find
3247bd0b0f0STejun Heo  * @align: alignment of free area to find
3257bd0b0f0STejun Heo  *
3267bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range.
3277bd0b0f0STejun Heo  *
32847cec443SMike Rapoport  * Return:
32979442ed1STang Chen  * Found address on success, 0 on failure.
3307bd0b0f0STejun Heo  */
3317bd0b0f0STejun Heo phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
3327bd0b0f0STejun Heo 					phys_addr_t end, phys_addr_t size,
3337bd0b0f0STejun Heo 					phys_addr_t align)
3347bd0b0f0STejun Heo {
335a3f5bafcSTony Luck 	phys_addr_t ret;
336e1720feeSMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
337a3f5bafcSTony Luck 
338a3f5bafcSTony Luck again:
339a3f5bafcSTony Luck 	ret = memblock_find_in_range_node(size, align, start, end,
340a3f5bafcSTony Luck 					    NUMA_NO_NODE, flags);
341a3f5bafcSTony Luck 
342a3f5bafcSTony Luck 	if (!ret && (flags & MEMBLOCK_MIRROR)) {
343a3f5bafcSTony Luck 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
344a3f5bafcSTony Luck 			&size);
345a3f5bafcSTony Luck 		flags &= ~MEMBLOCK_MIRROR;
346a3f5bafcSTony Luck 		goto again;
347a3f5bafcSTony Luck 	}
348a3f5bafcSTony Luck 
349a3f5bafcSTony Luck 	return ret;
3507bd0b0f0STejun Heo }
3517bd0b0f0STejun Heo 
35210d06439SYinghai Lu static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
35395f72d1eSYinghai Lu {
3541440c4e2STejun Heo 	type->total_size -= type->regions[r].size;
3557c0caeb8STejun Heo 	memmove(&type->regions[r], &type->regions[r + 1],
3567c0caeb8STejun Heo 		(type->cnt - (r + 1)) * sizeof(type->regions[r]));
357e3239ff9SBenjamin Herrenschmidt 	type->cnt--;
35895f72d1eSYinghai Lu 
3598f7a6605SBenjamin Herrenschmidt 	/* Special case for empty arrays */
3608f7a6605SBenjamin Herrenschmidt 	if (type->cnt == 0) {
3611440c4e2STejun Heo 		WARN_ON(type->total_size != 0);
3628f7a6605SBenjamin Herrenschmidt 		type->cnt = 1;
3638f7a6605SBenjamin Herrenschmidt 		type->regions[0].base = 0;
3648f7a6605SBenjamin Herrenschmidt 		type->regions[0].size = 0;
36566a20757STang Chen 		type->regions[0].flags = 0;
3667c0caeb8STejun Heo 		memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
3678f7a6605SBenjamin Herrenschmidt 	}
36895f72d1eSYinghai Lu }
36995f72d1eSYinghai Lu 
370350e88baSMike Rapoport #ifndef CONFIG_ARCH_KEEP_MEMBLOCK
3713010f876SPavel Tatashin /**
37247cec443SMike Rapoport  * memblock_discard - discard memory and reserved arrays if they were allocated
3733010f876SPavel Tatashin  */
3743010f876SPavel Tatashin void __init memblock_discard(void)
37529f67386SYinghai Lu {
3763010f876SPavel Tatashin 	phys_addr_t addr, size;
37729f67386SYinghai Lu 
3783010f876SPavel Tatashin 	if (memblock.reserved.regions != memblock_reserved_init_regions) {
3793010f876SPavel Tatashin 		addr = __pa(memblock.reserved.regions);
3803010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
38129f67386SYinghai Lu 				  memblock.reserved.max);
3823010f876SPavel Tatashin 		__memblock_free_late(addr, size);
38329f67386SYinghai Lu 	}
38429f67386SYinghai Lu 
38591b540f9SPavel Tatashin 	if (memblock.memory.regions != memblock_memory_init_regions) {
3863010f876SPavel Tatashin 		addr = __pa(memblock.memory.regions);
3873010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
3885e270e25SPhilipp Hachtmann 				  memblock.memory.max);
3893010f876SPavel Tatashin 		__memblock_free_late(addr, size);
3905e270e25SPhilipp Hachtmann 	}
3913010f876SPavel Tatashin }
3925e270e25SPhilipp Hachtmann #endif
3935e270e25SPhilipp Hachtmann 
39448c3b583SGreg Pearson /**
39548c3b583SGreg Pearson  * memblock_double_array - double the size of the memblock regions array
39648c3b583SGreg Pearson  * @type: memblock type of the regions array being doubled
39748c3b583SGreg Pearson  * @new_area_start: starting address of memory range to avoid overlap with
39848c3b583SGreg Pearson  * @new_area_size: size of memory range to avoid overlap with
39948c3b583SGreg Pearson  *
40048c3b583SGreg Pearson  * Double the size of the @type regions array. If memblock is being used to
40148c3b583SGreg Pearson  * allocate memory for a new reserved regions array and there is a previously
40248c3b583SGreg Pearson  * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
40348c3b583SGreg Pearson  * waiting to be reserved, ensure the memory used by the new array does
40448c3b583SGreg Pearson  * not overlap.
40548c3b583SGreg Pearson  *
40647cec443SMike Rapoport  * Return:
40748c3b583SGreg Pearson  * 0 on success, -1 on failure.
40848c3b583SGreg Pearson  */
40948c3b583SGreg Pearson static int __init_memblock memblock_double_array(struct memblock_type *type,
41048c3b583SGreg Pearson 						phys_addr_t new_area_start,
41148c3b583SGreg Pearson 						phys_addr_t new_area_size)
412142b45a7SBenjamin Herrenschmidt {
413142b45a7SBenjamin Herrenschmidt 	struct memblock_region *new_array, *old_array;
41429f67386SYinghai Lu 	phys_addr_t old_alloc_size, new_alloc_size;
415a36aab89SMike Rapoport 	phys_addr_t old_size, new_size, addr, new_end;
416142b45a7SBenjamin Herrenschmidt 	int use_slab = slab_is_available();
417181eb394SGavin Shan 	int *in_slab;
418142b45a7SBenjamin Herrenschmidt 
419142b45a7SBenjamin Herrenschmidt 	/* We don't allow resizing until we know about the reserved regions
420142b45a7SBenjamin Herrenschmidt 	 * of memory that aren't suitable for allocation
421142b45a7SBenjamin Herrenschmidt 	 */
422142b45a7SBenjamin Herrenschmidt 	if (!memblock_can_resize)
423142b45a7SBenjamin Herrenschmidt 		return -1;
424142b45a7SBenjamin Herrenschmidt 
425142b45a7SBenjamin Herrenschmidt 	/* Calculate new doubled size */
426142b45a7SBenjamin Herrenschmidt 	old_size = type->max * sizeof(struct memblock_region);
427142b45a7SBenjamin Herrenschmidt 	new_size = old_size << 1;
42829f67386SYinghai Lu 	/*
42929f67386SYinghai Lu 	 * We need to allocated new one align to PAGE_SIZE,
43029f67386SYinghai Lu 	 *   so we can free them completely later.
43129f67386SYinghai Lu 	 */
43229f67386SYinghai Lu 	old_alloc_size = PAGE_ALIGN(old_size);
43329f67386SYinghai Lu 	new_alloc_size = PAGE_ALIGN(new_size);
434142b45a7SBenjamin Herrenschmidt 
435181eb394SGavin Shan 	/* Retrieve the slab flag */
436181eb394SGavin Shan 	if (type == &memblock.memory)
437181eb394SGavin Shan 		in_slab = &memblock_memory_in_slab;
438181eb394SGavin Shan 	else
439181eb394SGavin Shan 		in_slab = &memblock_reserved_in_slab;
440181eb394SGavin Shan 
441a2974133SMike Rapoport 	/* Try to find some space for it */
442142b45a7SBenjamin Herrenschmidt 	if (use_slab) {
443142b45a7SBenjamin Herrenschmidt 		new_array = kmalloc(new_size, GFP_KERNEL);
4441f5026a7STejun Heo 		addr = new_array ? __pa(new_array) : 0;
4454e2f0775SGavin Shan 	} else {
44648c3b583SGreg Pearson 		/* only exclude range when trying to double reserved.regions */
44748c3b583SGreg Pearson 		if (type != &memblock.reserved)
44848c3b583SGreg Pearson 			new_area_start = new_area_size = 0;
44948c3b583SGreg Pearson 
45048c3b583SGreg Pearson 		addr = memblock_find_in_range(new_area_start + new_area_size,
45148c3b583SGreg Pearson 						memblock.current_limit,
45229f67386SYinghai Lu 						new_alloc_size, PAGE_SIZE);
45348c3b583SGreg Pearson 		if (!addr && new_area_size)
45448c3b583SGreg Pearson 			addr = memblock_find_in_range(0,
45548c3b583SGreg Pearson 				min(new_area_start, memblock.current_limit),
45629f67386SYinghai Lu 				new_alloc_size, PAGE_SIZE);
45748c3b583SGreg Pearson 
45815674868SSachin Kamat 		new_array = addr ? __va(addr) : NULL;
4594e2f0775SGavin Shan 	}
4601f5026a7STejun Heo 	if (!addr) {
461142b45a7SBenjamin Herrenschmidt 		pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
4620262d9c8SHeiko Carstens 		       type->name, type->max, type->max * 2);
463142b45a7SBenjamin Herrenschmidt 		return -1;
464142b45a7SBenjamin Herrenschmidt 	}
465142b45a7SBenjamin Herrenschmidt 
466a36aab89SMike Rapoport 	new_end = addr + new_size - 1;
467a36aab89SMike Rapoport 	memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
468a36aab89SMike Rapoport 			type->name, type->max * 2, &addr, &new_end);
469ea9e4376SYinghai Lu 
470fd07383bSAndrew Morton 	/*
471fd07383bSAndrew Morton 	 * Found space, we now need to move the array over before we add the
472fd07383bSAndrew Morton 	 * reserved region since it may be our reserved array itself that is
473fd07383bSAndrew Morton 	 * full.
474142b45a7SBenjamin Herrenschmidt 	 */
475142b45a7SBenjamin Herrenschmidt 	memcpy(new_array, type->regions, old_size);
476142b45a7SBenjamin Herrenschmidt 	memset(new_array + type->max, 0, old_size);
477142b45a7SBenjamin Herrenschmidt 	old_array = type->regions;
478142b45a7SBenjamin Herrenschmidt 	type->regions = new_array;
479142b45a7SBenjamin Herrenschmidt 	type->max <<= 1;
480142b45a7SBenjamin Herrenschmidt 
481fd07383bSAndrew Morton 	/* Free old array. We needn't free it if the array is the static one */
482181eb394SGavin Shan 	if (*in_slab)
483181eb394SGavin Shan 		kfree(old_array);
484181eb394SGavin Shan 	else if (old_array != memblock_memory_init_regions &&
485142b45a7SBenjamin Herrenschmidt 		 old_array != memblock_reserved_init_regions)
48629f67386SYinghai Lu 		memblock_free(__pa(old_array), old_alloc_size);
487142b45a7SBenjamin Herrenschmidt 
488fd07383bSAndrew Morton 	/*
489fd07383bSAndrew Morton 	 * Reserve the new array if that comes from the memblock.  Otherwise, we
490fd07383bSAndrew Morton 	 * needn't do it
491181eb394SGavin Shan 	 */
492181eb394SGavin Shan 	if (!use_slab)
49329f67386SYinghai Lu 		BUG_ON(memblock_reserve(addr, new_alloc_size));
494181eb394SGavin Shan 
495181eb394SGavin Shan 	/* Update slab flag */
496181eb394SGavin Shan 	*in_slab = use_slab;
497181eb394SGavin Shan 
498142b45a7SBenjamin Herrenschmidt 	return 0;
499142b45a7SBenjamin Herrenschmidt }
500142b45a7SBenjamin Herrenschmidt 
501784656f9STejun Heo /**
502784656f9STejun Heo  * memblock_merge_regions - merge neighboring compatible regions
503784656f9STejun Heo  * @type: memblock type to scan
504784656f9STejun Heo  *
505784656f9STejun Heo  * Scan @type and merge neighboring compatible regions.
506784656f9STejun Heo  */
507784656f9STejun Heo static void __init_memblock memblock_merge_regions(struct memblock_type *type)
508784656f9STejun Heo {
509784656f9STejun Heo 	int i = 0;
510784656f9STejun Heo 
511784656f9STejun Heo 	/* cnt never goes below 1 */
512784656f9STejun Heo 	while (i < type->cnt - 1) {
513784656f9STejun Heo 		struct memblock_region *this = &type->regions[i];
514784656f9STejun Heo 		struct memblock_region *next = &type->regions[i + 1];
515784656f9STejun Heo 
5167c0caeb8STejun Heo 		if (this->base + this->size != next->base ||
5177c0caeb8STejun Heo 		    memblock_get_region_node(this) !=
51866a20757STang Chen 		    memblock_get_region_node(next) ||
51966a20757STang Chen 		    this->flags != next->flags) {
520784656f9STejun Heo 			BUG_ON(this->base + this->size > next->base);
521784656f9STejun Heo 			i++;
522784656f9STejun Heo 			continue;
523784656f9STejun Heo 		}
524784656f9STejun Heo 
525784656f9STejun Heo 		this->size += next->size;
526c0232ae8SLin Feng 		/* move forward from next + 1, index of which is i + 2 */
527c0232ae8SLin Feng 		memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
528784656f9STejun Heo 		type->cnt--;
529784656f9STejun Heo 	}
530784656f9STejun Heo }
531784656f9STejun Heo 
532784656f9STejun Heo /**
533784656f9STejun Heo  * memblock_insert_region - insert new memblock region
534784656f9STejun Heo  * @type:	memblock type to insert into
535784656f9STejun Heo  * @idx:	index for the insertion point
536784656f9STejun Heo  * @base:	base address of the new region
537784656f9STejun Heo  * @size:	size of the new region
538209ff86dSTang Chen  * @nid:	node id of the new region
53966a20757STang Chen  * @flags:	flags of the new region
540784656f9STejun Heo  *
541784656f9STejun Heo  * Insert new memblock region [@base, @base + @size) into @type at @idx.
542412d0008SAlexander Kuleshov  * @type must already have extra room to accommodate the new region.
543784656f9STejun Heo  */
544784656f9STejun Heo static void __init_memblock memblock_insert_region(struct memblock_type *type,
545784656f9STejun Heo 						   int idx, phys_addr_t base,
54666a20757STang Chen 						   phys_addr_t size,
547e1720feeSMike Rapoport 						   int nid,
548e1720feeSMike Rapoport 						   enum memblock_flags flags)
549784656f9STejun Heo {
550784656f9STejun Heo 	struct memblock_region *rgn = &type->regions[idx];
551784656f9STejun Heo 
552784656f9STejun Heo 	BUG_ON(type->cnt >= type->max);
553784656f9STejun Heo 	memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
554784656f9STejun Heo 	rgn->base = base;
555784656f9STejun Heo 	rgn->size = size;
55666a20757STang Chen 	rgn->flags = flags;
5577c0caeb8STejun Heo 	memblock_set_region_node(rgn, nid);
558784656f9STejun Heo 	type->cnt++;
5591440c4e2STejun Heo 	type->total_size += size;
560784656f9STejun Heo }
561784656f9STejun Heo 
562784656f9STejun Heo /**
563f1af9d3aSPhilipp Hachtmann  * memblock_add_range - add new memblock region
564784656f9STejun Heo  * @type: memblock type to add new region into
565784656f9STejun Heo  * @base: base address of the new region
566784656f9STejun Heo  * @size: size of the new region
5677fb0bc3fSTejun Heo  * @nid: nid of the new region
56866a20757STang Chen  * @flags: flags of the new region
569784656f9STejun Heo  *
570784656f9STejun Heo  * Add new memblock region [@base, @base + @size) into @type.  The new region
571784656f9STejun Heo  * is allowed to overlap with existing ones - overlaps don't affect already
572784656f9STejun Heo  * existing regions.  @type is guaranteed to be minimal (all neighbouring
573784656f9STejun Heo  * compatible regions are merged) after the addition.
574784656f9STejun Heo  *
57547cec443SMike Rapoport  * Return:
576784656f9STejun Heo  * 0 on success, -errno on failure.
577784656f9STejun Heo  */
578*02634a44SAnshuman Khandual static int __init_memblock memblock_add_range(struct memblock_type *type,
57966a20757STang Chen 				phys_addr_t base, phys_addr_t size,
580e1720feeSMike Rapoport 				int nid, enum memblock_flags flags)
58195f72d1eSYinghai Lu {
582784656f9STejun Heo 	bool insert = false;
583eb18f1b5STejun Heo 	phys_addr_t obase = base;
584eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
5858c9c1701SAlexander Kuleshov 	int idx, nr_new;
5868c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
58795f72d1eSYinghai Lu 
588b3dc627cSTejun Heo 	if (!size)
589b3dc627cSTejun Heo 		return 0;
590b3dc627cSTejun Heo 
591784656f9STejun Heo 	/* special case for empty array */
592784656f9STejun Heo 	if (type->regions[0].size == 0) {
5931440c4e2STejun Heo 		WARN_ON(type->cnt != 1 || type->total_size);
594784656f9STejun Heo 		type->regions[0].base = base;
595784656f9STejun Heo 		type->regions[0].size = size;
59666a20757STang Chen 		type->regions[0].flags = flags;
5977fb0bc3fSTejun Heo 		memblock_set_region_node(&type->regions[0], nid);
5981440c4e2STejun Heo 		type->total_size = size;
599784656f9STejun Heo 		return 0;
600784656f9STejun Heo 	}
601784656f9STejun Heo repeat:
602784656f9STejun Heo 	/*
603784656f9STejun Heo 	 * The following is executed twice.  Once with %false @insert and
604784656f9STejun Heo 	 * then with %true.  The first counts the number of regions needed
605412d0008SAlexander Kuleshov 	 * to accommodate the new area.  The second actually inserts them.
606784656f9STejun Heo 	 */
607784656f9STejun Heo 	base = obase;
608784656f9STejun Heo 	nr_new = 0;
609784656f9STejun Heo 
61066e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
611784656f9STejun Heo 		phys_addr_t rbase = rgn->base;
612784656f9STejun Heo 		phys_addr_t rend = rbase + rgn->size;
6138f7a6605SBenjamin Herrenschmidt 
614784656f9STejun Heo 		if (rbase >= end)
6158f7a6605SBenjamin Herrenschmidt 			break;
616784656f9STejun Heo 		if (rend <= base)
617784656f9STejun Heo 			continue;
618784656f9STejun Heo 		/*
619784656f9STejun Heo 		 * @rgn overlaps.  If it separates the lower part of new
620784656f9STejun Heo 		 * area, insert that portion.
6218f7a6605SBenjamin Herrenschmidt 		 */
622784656f9STejun Heo 		if (rbase > base) {
623c0a29498SWei Yang #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
624c0a29498SWei Yang 			WARN_ON(nid != memblock_get_region_node(rgn));
625c0a29498SWei Yang #endif
6264fcab5f4SWei Yang 			WARN_ON(flags != rgn->flags);
627784656f9STejun Heo 			nr_new++;
628784656f9STejun Heo 			if (insert)
6298c9c1701SAlexander Kuleshov 				memblock_insert_region(type, idx++, base,
63066a20757STang Chen 						       rbase - base, nid,
63166a20757STang Chen 						       flags);
632784656f9STejun Heo 		}
633784656f9STejun Heo 		/* area below @rend is dealt with, forget about it */
634784656f9STejun Heo 		base = min(rend, end);
6358f7a6605SBenjamin Herrenschmidt 	}
6368f7a6605SBenjamin Herrenschmidt 
637784656f9STejun Heo 	/* insert the remaining portion */
638784656f9STejun Heo 	if (base < end) {
639784656f9STejun Heo 		nr_new++;
640784656f9STejun Heo 		if (insert)
6418c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, base, end - base,
64266a20757STang Chen 					       nid, flags);
6438f7a6605SBenjamin Herrenschmidt 	}
6448f7a6605SBenjamin Herrenschmidt 
645ef3cc4dbSnimisolo 	if (!nr_new)
646ef3cc4dbSnimisolo 		return 0;
647ef3cc4dbSnimisolo 
648784656f9STejun Heo 	/*
649784656f9STejun Heo 	 * If this was the first round, resize array and repeat for actual
650784656f9STejun Heo 	 * insertions; otherwise, merge and return.
6518f7a6605SBenjamin Herrenschmidt 	 */
652784656f9STejun Heo 	if (!insert) {
653784656f9STejun Heo 		while (type->cnt + nr_new > type->max)
65448c3b583SGreg Pearson 			if (memblock_double_array(type, obase, size) < 0)
655784656f9STejun Heo 				return -ENOMEM;
656784656f9STejun Heo 		insert = true;
657784656f9STejun Heo 		goto repeat;
65895f72d1eSYinghai Lu 	} else {
659784656f9STejun Heo 		memblock_merge_regions(type);
66095f72d1eSYinghai Lu 		return 0;
66195f72d1eSYinghai Lu 	}
662784656f9STejun Heo }
66395f72d1eSYinghai Lu 
66448a833ccSMike Rapoport /**
66548a833ccSMike Rapoport  * memblock_add_node - add new memblock region within a NUMA node
66648a833ccSMike Rapoport  * @base: base address of the new region
66748a833ccSMike Rapoport  * @size: size of the new region
66848a833ccSMike Rapoport  * @nid: nid of the new region
66948a833ccSMike Rapoport  *
67048a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
67148a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
67248a833ccSMike Rapoport  *
67348a833ccSMike Rapoport  * Return:
67448a833ccSMike Rapoport  * 0 on success, -errno on failure.
67548a833ccSMike Rapoport  */
6767fb0bc3fSTejun Heo int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
6777fb0bc3fSTejun Heo 				       int nid)
6787fb0bc3fSTejun Heo {
679f1af9d3aSPhilipp Hachtmann 	return memblock_add_range(&memblock.memory, base, size, nid, 0);
6807fb0bc3fSTejun Heo }
6817fb0bc3fSTejun Heo 
68248a833ccSMike Rapoport /**
68348a833ccSMike Rapoport  * memblock_add - add new memblock region
68448a833ccSMike Rapoport  * @base: base address of the new region
68548a833ccSMike Rapoport  * @size: size of the new region
68648a833ccSMike Rapoport  *
68748a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
68848a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
68948a833ccSMike Rapoport  *
69048a833ccSMike Rapoport  * Return:
69148a833ccSMike Rapoport  * 0 on success, -errno on failure.
69248a833ccSMike Rapoport  */
693f705ac4bSAlexander Kuleshov int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
6946a4055bcSAlexander Kuleshov {
6955d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
6965d63f81cSMiles Chen 
697d75f773cSSakari Ailus 	memblock_dbg("memblock_add: [%pa-%pa] %pS\n",
6985d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
6996a4055bcSAlexander Kuleshov 
700f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
70195f72d1eSYinghai Lu }
70295f72d1eSYinghai Lu 
7036a9ceb31STejun Heo /**
7046a9ceb31STejun Heo  * memblock_isolate_range - isolate given range into disjoint memblocks
7056a9ceb31STejun Heo  * @type: memblock type to isolate range for
7066a9ceb31STejun Heo  * @base: base of range to isolate
7076a9ceb31STejun Heo  * @size: size of range to isolate
7086a9ceb31STejun Heo  * @start_rgn: out parameter for the start of isolated region
7096a9ceb31STejun Heo  * @end_rgn: out parameter for the end of isolated region
7106a9ceb31STejun Heo  *
7116a9ceb31STejun Heo  * Walk @type and ensure that regions don't cross the boundaries defined by
7126a9ceb31STejun Heo  * [@base, @base + @size).  Crossing regions are split at the boundaries,
7136a9ceb31STejun Heo  * which may create at most two more regions.  The index of the first
7146a9ceb31STejun Heo  * region inside the range is returned in *@start_rgn and end in *@end_rgn.
7156a9ceb31STejun Heo  *
71647cec443SMike Rapoport  * Return:
7176a9ceb31STejun Heo  * 0 on success, -errno on failure.
7186a9ceb31STejun Heo  */
7196a9ceb31STejun Heo static int __init_memblock memblock_isolate_range(struct memblock_type *type,
7206a9ceb31STejun Heo 					phys_addr_t base, phys_addr_t size,
7216a9ceb31STejun Heo 					int *start_rgn, int *end_rgn)
7226a9ceb31STejun Heo {
723eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
7248c9c1701SAlexander Kuleshov 	int idx;
7258c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
7266a9ceb31STejun Heo 
7276a9ceb31STejun Heo 	*start_rgn = *end_rgn = 0;
7286a9ceb31STejun Heo 
729b3dc627cSTejun Heo 	if (!size)
730b3dc627cSTejun Heo 		return 0;
731b3dc627cSTejun Heo 
7326a9ceb31STejun Heo 	/* we'll create at most two more regions */
7336a9ceb31STejun Heo 	while (type->cnt + 2 > type->max)
73448c3b583SGreg Pearson 		if (memblock_double_array(type, base, size) < 0)
7356a9ceb31STejun Heo 			return -ENOMEM;
7366a9ceb31STejun Heo 
73766e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
7386a9ceb31STejun Heo 		phys_addr_t rbase = rgn->base;
7396a9ceb31STejun Heo 		phys_addr_t rend = rbase + rgn->size;
7406a9ceb31STejun Heo 
7416a9ceb31STejun Heo 		if (rbase >= end)
7426a9ceb31STejun Heo 			break;
7436a9ceb31STejun Heo 		if (rend <= base)
7446a9ceb31STejun Heo 			continue;
7456a9ceb31STejun Heo 
7466a9ceb31STejun Heo 		if (rbase < base) {
7476a9ceb31STejun Heo 			/*
7486a9ceb31STejun Heo 			 * @rgn intersects from below.  Split and continue
7496a9ceb31STejun Heo 			 * to process the next region - the new top half.
7506a9ceb31STejun Heo 			 */
7516a9ceb31STejun Heo 			rgn->base = base;
7521440c4e2STejun Heo 			rgn->size -= base - rbase;
7531440c4e2STejun Heo 			type->total_size -= base - rbase;
7548c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, rbase, base - rbase,
75566a20757STang Chen 					       memblock_get_region_node(rgn),
75666a20757STang Chen 					       rgn->flags);
7576a9ceb31STejun Heo 		} else if (rend > end) {
7586a9ceb31STejun Heo 			/*
7596a9ceb31STejun Heo 			 * @rgn intersects from above.  Split and redo the
7606a9ceb31STejun Heo 			 * current region - the new bottom half.
7616a9ceb31STejun Heo 			 */
7626a9ceb31STejun Heo 			rgn->base = end;
7631440c4e2STejun Heo 			rgn->size -= end - rbase;
7641440c4e2STejun Heo 			type->total_size -= end - rbase;
7658c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx--, rbase, end - rbase,
76666a20757STang Chen 					       memblock_get_region_node(rgn),
76766a20757STang Chen 					       rgn->flags);
7686a9ceb31STejun Heo 		} else {
7696a9ceb31STejun Heo 			/* @rgn is fully contained, record it */
7706a9ceb31STejun Heo 			if (!*end_rgn)
7718c9c1701SAlexander Kuleshov 				*start_rgn = idx;
7728c9c1701SAlexander Kuleshov 			*end_rgn = idx + 1;
7736a9ceb31STejun Heo 		}
7746a9ceb31STejun Heo 	}
7756a9ceb31STejun Heo 
7766a9ceb31STejun Heo 	return 0;
7776a9ceb31STejun Heo }
7786a9ceb31STejun Heo 
77935bd16a2SAlexander Kuleshov static int __init_memblock memblock_remove_range(struct memblock_type *type,
7808f7a6605SBenjamin Herrenschmidt 					  phys_addr_t base, phys_addr_t size)
78195f72d1eSYinghai Lu {
78271936180STejun Heo 	int start_rgn, end_rgn;
78371936180STejun Heo 	int i, ret;
78495f72d1eSYinghai Lu 
78571936180STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
78671936180STejun Heo 	if (ret)
78771936180STejun Heo 		return ret;
78895f72d1eSYinghai Lu 
78971936180STejun Heo 	for (i = end_rgn - 1; i >= start_rgn; i--)
79071936180STejun Heo 		memblock_remove_region(type, i);
79195f72d1eSYinghai Lu 	return 0;
79295f72d1eSYinghai Lu }
79395f72d1eSYinghai Lu 
794581adcbeSTejun Heo int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
79595f72d1eSYinghai Lu {
79625cf23d7SMinchan Kim 	phys_addr_t end = base + size - 1;
79725cf23d7SMinchan Kim 
79825cf23d7SMinchan Kim 	memblock_dbg("memblock_remove: [%pa-%pa] %pS\n",
79925cf23d7SMinchan Kim 		     &base, &end, (void *)_RET_IP_);
80025cf23d7SMinchan Kim 
801f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.memory, base, size);
80295f72d1eSYinghai Lu }
80395f72d1eSYinghai Lu 
8044d72868cSMike Rapoport /**
8054d72868cSMike Rapoport  * memblock_free - free boot memory block
8064d72868cSMike Rapoport  * @base: phys starting address of the  boot memory block
8074d72868cSMike Rapoport  * @size: size of the boot memory block in bytes
8084d72868cSMike Rapoport  *
8094d72868cSMike Rapoport  * Free boot memory block previously allocated by memblock_alloc_xx() API.
8104d72868cSMike Rapoport  * The freeing memory will not be released to the buddy allocator.
8114d72868cSMike Rapoport  */
812581adcbeSTejun Heo int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
81395f72d1eSYinghai Lu {
8145d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8155d63f81cSMiles Chen 
816d75f773cSSakari Ailus 	memblock_dbg("   memblock_free: [%pa-%pa] %pS\n",
8175d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
81824aa0788STejun Heo 
8199099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
820f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.reserved, base, size);
82195f72d1eSYinghai Lu }
82295f72d1eSYinghai Lu 
823f705ac4bSAlexander Kuleshov int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
82495f72d1eSYinghai Lu {
8255d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8265d63f81cSMiles Chen 
827d75f773cSSakari Ailus 	memblock_dbg("memblock_reserve: [%pa-%pa] %pS\n",
8285d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
82995f72d1eSYinghai Lu 
830f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
83195f72d1eSYinghai Lu }
83295f72d1eSYinghai Lu 
833*02634a44SAnshuman Khandual #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
834*02634a44SAnshuman Khandual int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size)
835*02634a44SAnshuman Khandual {
836*02634a44SAnshuman Khandual 	phys_addr_t end = base + size - 1;
837*02634a44SAnshuman Khandual 
838*02634a44SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
839*02634a44SAnshuman Khandual 		     &base, &end, (void *)_RET_IP_);
840*02634a44SAnshuman Khandual 
841*02634a44SAnshuman Khandual 	return memblock_add_range(&memblock.physmem, base, size, MAX_NUMNODES, 0);
842*02634a44SAnshuman Khandual }
843*02634a44SAnshuman Khandual #endif
844*02634a44SAnshuman Khandual 
84535fd0808STejun Heo /**
84647cec443SMike Rapoport  * memblock_setclr_flag - set or clear flag for a memory region
84747cec443SMike Rapoport  * @base: base address of the region
84847cec443SMike Rapoport  * @size: size of the region
84947cec443SMike Rapoport  * @set: set or clear the flag
85047cec443SMike Rapoport  * @flag: the flag to udpate
85166b16edfSTang Chen  *
8524308ce17STony Luck  * This function isolates region [@base, @base + @size), and sets/clears flag
85366b16edfSTang Chen  *
85447cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
85566b16edfSTang Chen  */
8564308ce17STony Luck static int __init_memblock memblock_setclr_flag(phys_addr_t base,
8574308ce17STony Luck 				phys_addr_t size, int set, int flag)
85866b16edfSTang Chen {
85966b16edfSTang Chen 	struct memblock_type *type = &memblock.memory;
86066b16edfSTang Chen 	int i, ret, start_rgn, end_rgn;
86166b16edfSTang Chen 
86266b16edfSTang Chen 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
86366b16edfSTang Chen 	if (ret)
86466b16edfSTang Chen 		return ret;
86566b16edfSTang Chen 
866fe145124SMike Rapoport 	for (i = start_rgn; i < end_rgn; i++) {
867fe145124SMike Rapoport 		struct memblock_region *r = &type->regions[i];
868fe145124SMike Rapoport 
8694308ce17STony Luck 		if (set)
870fe145124SMike Rapoport 			r->flags |= flag;
8714308ce17STony Luck 		else
872fe145124SMike Rapoport 			r->flags &= ~flag;
873fe145124SMike Rapoport 	}
87466b16edfSTang Chen 
87566b16edfSTang Chen 	memblock_merge_regions(type);
87666b16edfSTang Chen 	return 0;
87766b16edfSTang Chen }
87866b16edfSTang Chen 
87966b16edfSTang Chen /**
8804308ce17STony Luck  * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
8814308ce17STony Luck  * @base: the base phys addr of the region
8824308ce17STony Luck  * @size: the size of the region
8834308ce17STony Luck  *
88447cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
8854308ce17STony Luck  */
8864308ce17STony Luck int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
8874308ce17STony Luck {
8884308ce17STony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
8894308ce17STony Luck }
8904308ce17STony Luck 
8914308ce17STony Luck /**
89266b16edfSTang Chen  * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
89366b16edfSTang Chen  * @base: the base phys addr of the region
89466b16edfSTang Chen  * @size: the size of the region
89566b16edfSTang Chen  *
89647cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
89766b16edfSTang Chen  */
89866b16edfSTang Chen int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
89966b16edfSTang Chen {
9004308ce17STony Luck 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
90166b16edfSTang Chen }
90266b16edfSTang Chen 
90366b16edfSTang Chen /**
904a3f5bafcSTony Luck  * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
905a3f5bafcSTony Luck  * @base: the base phys addr of the region
906a3f5bafcSTony Luck  * @size: the size of the region
907a3f5bafcSTony Luck  *
90847cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
909a3f5bafcSTony Luck  */
910a3f5bafcSTony Luck int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
911a3f5bafcSTony Luck {
912a3f5bafcSTony Luck 	system_has_some_mirror = true;
913a3f5bafcSTony Luck 
914a3f5bafcSTony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
915a3f5bafcSTony Luck }
916a3f5bafcSTony Luck 
917bf3d3cc5SArd Biesheuvel /**
918bf3d3cc5SArd Biesheuvel  * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
919bf3d3cc5SArd Biesheuvel  * @base: the base phys addr of the region
920bf3d3cc5SArd Biesheuvel  * @size: the size of the region
921bf3d3cc5SArd Biesheuvel  *
92247cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
923bf3d3cc5SArd Biesheuvel  */
924bf3d3cc5SArd Biesheuvel int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
925bf3d3cc5SArd Biesheuvel {
926bf3d3cc5SArd Biesheuvel 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
927bf3d3cc5SArd Biesheuvel }
928a3f5bafcSTony Luck 
929a3f5bafcSTony Luck /**
9304c546b8aSAKASHI Takahiro  * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
9314c546b8aSAKASHI Takahiro  * @base: the base phys addr of the region
9324c546b8aSAKASHI Takahiro  * @size: the size of the region
9334c546b8aSAKASHI Takahiro  *
93447cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
9354c546b8aSAKASHI Takahiro  */
9364c546b8aSAKASHI Takahiro int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
9374c546b8aSAKASHI Takahiro {
9384c546b8aSAKASHI Takahiro 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
9394c546b8aSAKASHI Takahiro }
9404c546b8aSAKASHI Takahiro 
9414c546b8aSAKASHI Takahiro /**
9428e7a7f86SRobin Holt  * __next_reserved_mem_region - next function for for_each_reserved_region()
9438e7a7f86SRobin Holt  * @idx: pointer to u64 loop variable
9448e7a7f86SRobin Holt  * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
9458e7a7f86SRobin Holt  * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
9468e7a7f86SRobin Holt  *
9478e7a7f86SRobin Holt  * Iterate over all reserved memory regions.
9488e7a7f86SRobin Holt  */
9498e7a7f86SRobin Holt void __init_memblock __next_reserved_mem_region(u64 *idx,
9508e7a7f86SRobin Holt 					   phys_addr_t *out_start,
9518e7a7f86SRobin Holt 					   phys_addr_t *out_end)
9528e7a7f86SRobin Holt {
953567d117bSAlexander Kuleshov 	struct memblock_type *type = &memblock.reserved;
9548e7a7f86SRobin Holt 
955cd33a76bSRichard Leitner 	if (*idx < type->cnt) {
956567d117bSAlexander Kuleshov 		struct memblock_region *r = &type->regions[*idx];
9578e7a7f86SRobin Holt 		phys_addr_t base = r->base;
9588e7a7f86SRobin Holt 		phys_addr_t size = r->size;
9598e7a7f86SRobin Holt 
9608e7a7f86SRobin Holt 		if (out_start)
9618e7a7f86SRobin Holt 			*out_start = base;
9628e7a7f86SRobin Holt 		if (out_end)
9638e7a7f86SRobin Holt 			*out_end = base + size - 1;
9648e7a7f86SRobin Holt 
9658e7a7f86SRobin Holt 		*idx += 1;
9668e7a7f86SRobin Holt 		return;
9678e7a7f86SRobin Holt 	}
9688e7a7f86SRobin Holt 
9698e7a7f86SRobin Holt 	/* signal end of iteration */
9708e7a7f86SRobin Holt 	*idx = ULLONG_MAX;
9718e7a7f86SRobin Holt }
9728e7a7f86SRobin Holt 
973c9a688a3SMike Rapoport static bool should_skip_region(struct memblock_region *m, int nid, int flags)
974c9a688a3SMike Rapoport {
975c9a688a3SMike Rapoport 	int m_nid = memblock_get_region_node(m);
976c9a688a3SMike Rapoport 
977c9a688a3SMike Rapoport 	/* only memory regions are associated with nodes, check it */
978c9a688a3SMike Rapoport 	if (nid != NUMA_NO_NODE && nid != m_nid)
979c9a688a3SMike Rapoport 		return true;
980c9a688a3SMike Rapoport 
981c9a688a3SMike Rapoport 	/* skip hotpluggable memory regions if needed */
982c9a688a3SMike Rapoport 	if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
983c9a688a3SMike Rapoport 		return true;
984c9a688a3SMike Rapoport 
985c9a688a3SMike Rapoport 	/* if we want mirror memory skip non-mirror memory regions */
986c9a688a3SMike Rapoport 	if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
987c9a688a3SMike Rapoport 		return true;
988c9a688a3SMike Rapoport 
989c9a688a3SMike Rapoport 	/* skip nomap memory unless we were asked for it explicitly */
990c9a688a3SMike Rapoport 	if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
991c9a688a3SMike Rapoport 		return true;
992c9a688a3SMike Rapoport 
993c9a688a3SMike Rapoport 	return false;
994c9a688a3SMike Rapoport }
995c9a688a3SMike Rapoport 
9968e7a7f86SRobin Holt /**
997a2974133SMike Rapoport  * __next_mem_range - next function for for_each_free_mem_range() etc.
99835fd0808STejun Heo  * @idx: pointer to u64 loop variable
999b1154233SGrygorii Strashko  * @nid: node selector, %NUMA_NO_NODE for all nodes
1000fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
1001f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
1002f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
1003dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1004dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1005dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
100635fd0808STejun Heo  *
1007f1af9d3aSPhilipp Hachtmann  * Find the first area from *@idx which matches @nid, fill the out
100835fd0808STejun Heo  * parameters, and update *@idx for the next iteration.  The lower 32bit of
1009f1af9d3aSPhilipp Hachtmann  * *@idx contains index into type_a and the upper 32bit indexes the
1010f1af9d3aSPhilipp Hachtmann  * areas before each region in type_b.	For example, if type_b regions
101135fd0808STejun Heo  * look like the following,
101235fd0808STejun Heo  *
101335fd0808STejun Heo  *	0:[0-16), 1:[32-48), 2:[128-130)
101435fd0808STejun Heo  *
101535fd0808STejun Heo  * The upper 32bit indexes the following regions.
101635fd0808STejun Heo  *
101735fd0808STejun Heo  *	0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
101835fd0808STejun Heo  *
101935fd0808STejun Heo  * As both region arrays are sorted, the function advances the two indices
102035fd0808STejun Heo  * in lockstep and returns each intersection.
102135fd0808STejun Heo  */
1022e1720feeSMike Rapoport void __init_memblock __next_mem_range(u64 *idx, int nid,
1023e1720feeSMike Rapoport 				      enum memblock_flags flags,
1024f1af9d3aSPhilipp Hachtmann 				      struct memblock_type *type_a,
1025f1af9d3aSPhilipp Hachtmann 				      struct memblock_type *type_b,
102635fd0808STejun Heo 				      phys_addr_t *out_start,
102735fd0808STejun Heo 				      phys_addr_t *out_end, int *out_nid)
102835fd0808STejun Heo {
1029f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1030f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1031b1154233SGrygorii Strashko 
1032f1af9d3aSPhilipp Hachtmann 	if (WARN_ONCE(nid == MAX_NUMNODES,
1033f1af9d3aSPhilipp Hachtmann 	"Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1034560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
103535fd0808STejun Heo 
1036f1af9d3aSPhilipp Hachtmann 	for (; idx_a < type_a->cnt; idx_a++) {
1037f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1038f1af9d3aSPhilipp Hachtmann 
103935fd0808STejun Heo 		phys_addr_t m_start = m->base;
104035fd0808STejun Heo 		phys_addr_t m_end = m->base + m->size;
1041f1af9d3aSPhilipp Hachtmann 		int	    m_nid = memblock_get_region_node(m);
104235fd0808STejun Heo 
1043c9a688a3SMike Rapoport 		if (should_skip_region(m, nid, flags))
1044bf3d3cc5SArd Biesheuvel 			continue;
1045bf3d3cc5SArd Biesheuvel 
1046f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1047f1af9d3aSPhilipp Hachtmann 			if (out_start)
1048f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1049f1af9d3aSPhilipp Hachtmann 			if (out_end)
1050f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1051f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1052f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1053f1af9d3aSPhilipp Hachtmann 			idx_a++;
1054f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1055f1af9d3aSPhilipp Hachtmann 			return;
1056f1af9d3aSPhilipp Hachtmann 		}
105735fd0808STejun Heo 
1058f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1059f1af9d3aSPhilipp Hachtmann 		for (; idx_b < type_b->cnt + 1; idx_b++) {
1060f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1061f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1062f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1063f1af9d3aSPhilipp Hachtmann 
1064f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1065f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1066f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
10671c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1068f1af9d3aSPhilipp Hachtmann 
1069f1af9d3aSPhilipp Hachtmann 			/*
1070f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1071f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1072f1af9d3aSPhilipp Hachtmann 			 */
107335fd0808STejun Heo 			if (r_start >= m_end)
107435fd0808STejun Heo 				break;
107535fd0808STejun Heo 			/* if the two regions intersect, we're done */
107635fd0808STejun Heo 			if (m_start < r_end) {
107735fd0808STejun Heo 				if (out_start)
1078f1af9d3aSPhilipp Hachtmann 					*out_start =
1079f1af9d3aSPhilipp Hachtmann 						max(m_start, r_start);
108035fd0808STejun Heo 				if (out_end)
108135fd0808STejun Heo 					*out_end = min(m_end, r_end);
108235fd0808STejun Heo 				if (out_nid)
1083f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
108435fd0808STejun Heo 				/*
1085f1af9d3aSPhilipp Hachtmann 				 * The region which ends first is
1086f1af9d3aSPhilipp Hachtmann 				 * advanced for the next iteration.
108735fd0808STejun Heo 				 */
108835fd0808STejun Heo 				if (m_end <= r_end)
1089f1af9d3aSPhilipp Hachtmann 					idx_a++;
109035fd0808STejun Heo 				else
1091f1af9d3aSPhilipp Hachtmann 					idx_b++;
1092f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
109335fd0808STejun Heo 				return;
109435fd0808STejun Heo 			}
109535fd0808STejun Heo 		}
109635fd0808STejun Heo 	}
109735fd0808STejun Heo 
109835fd0808STejun Heo 	/* signal end of iteration */
109935fd0808STejun Heo 	*idx = ULLONG_MAX;
110035fd0808STejun Heo }
110135fd0808STejun Heo 
11027bd0b0f0STejun Heo /**
1103f1af9d3aSPhilipp Hachtmann  * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1104f1af9d3aSPhilipp Hachtmann  *
11057bd0b0f0STejun Heo  * @idx: pointer to u64 loop variable
1106ad5ea8cdSAlexander Kuleshov  * @nid: node selector, %NUMA_NO_NODE for all nodes
1107fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
1108f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
1109f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
1110dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1111dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1112dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
11137bd0b0f0STejun Heo  *
111447cec443SMike Rapoport  * Finds the next range from type_a which is not marked as unsuitable
111547cec443SMike Rapoport  * in type_b.
111647cec443SMike Rapoport  *
1117f1af9d3aSPhilipp Hachtmann  * Reverse of __next_mem_range().
11187bd0b0f0STejun Heo  */
1119e1720feeSMike Rapoport void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1120e1720feeSMike Rapoport 					  enum memblock_flags flags,
1121f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_a,
1122f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_b,
11237bd0b0f0STejun Heo 					  phys_addr_t *out_start,
11247bd0b0f0STejun Heo 					  phys_addr_t *out_end, int *out_nid)
11257bd0b0f0STejun Heo {
1126f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1127f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1128b1154233SGrygorii Strashko 
1129560dca27SGrygorii Strashko 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1130560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
11317bd0b0f0STejun Heo 
11327bd0b0f0STejun Heo 	if (*idx == (u64)ULLONG_MAX) {
1133f1af9d3aSPhilipp Hachtmann 		idx_a = type_a->cnt - 1;
1134e47608abSzijun_hu 		if (type_b != NULL)
1135f1af9d3aSPhilipp Hachtmann 			idx_b = type_b->cnt;
1136e47608abSzijun_hu 		else
1137e47608abSzijun_hu 			idx_b = 0;
11387bd0b0f0STejun Heo 	}
11397bd0b0f0STejun Heo 
1140f1af9d3aSPhilipp Hachtmann 	for (; idx_a >= 0; idx_a--) {
1141f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1142f1af9d3aSPhilipp Hachtmann 
11437bd0b0f0STejun Heo 		phys_addr_t m_start = m->base;
11447bd0b0f0STejun Heo 		phys_addr_t m_end = m->base + m->size;
1145f1af9d3aSPhilipp Hachtmann 		int m_nid = memblock_get_region_node(m);
11467bd0b0f0STejun Heo 
1147c9a688a3SMike Rapoport 		if (should_skip_region(m, nid, flags))
1148bf3d3cc5SArd Biesheuvel 			continue;
1149bf3d3cc5SArd Biesheuvel 
1150f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1151f1af9d3aSPhilipp Hachtmann 			if (out_start)
1152f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1153f1af9d3aSPhilipp Hachtmann 			if (out_end)
1154f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1155f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1156f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1157fb399b48Szijun_hu 			idx_a--;
1158f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1159f1af9d3aSPhilipp Hachtmann 			return;
1160f1af9d3aSPhilipp Hachtmann 		}
11617bd0b0f0STejun Heo 
1162f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1163f1af9d3aSPhilipp Hachtmann 		for (; idx_b >= 0; idx_b--) {
1164f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1165f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1166f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1167f1af9d3aSPhilipp Hachtmann 
1168f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1169f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1170f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
11711c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1172f1af9d3aSPhilipp Hachtmann 			/*
1173f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1174f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1175f1af9d3aSPhilipp Hachtmann 			 */
1176f1af9d3aSPhilipp Hachtmann 
11777bd0b0f0STejun Heo 			if (r_end <= m_start)
11787bd0b0f0STejun Heo 				break;
11797bd0b0f0STejun Heo 			/* if the two regions intersect, we're done */
11807bd0b0f0STejun Heo 			if (m_end > r_start) {
11817bd0b0f0STejun Heo 				if (out_start)
11827bd0b0f0STejun Heo 					*out_start = max(m_start, r_start);
11837bd0b0f0STejun Heo 				if (out_end)
11847bd0b0f0STejun Heo 					*out_end = min(m_end, r_end);
11857bd0b0f0STejun Heo 				if (out_nid)
1186f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
11877bd0b0f0STejun Heo 				if (m_start >= r_start)
1188f1af9d3aSPhilipp Hachtmann 					idx_a--;
11897bd0b0f0STejun Heo 				else
1190f1af9d3aSPhilipp Hachtmann 					idx_b--;
1191f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
11927bd0b0f0STejun Heo 				return;
11937bd0b0f0STejun Heo 			}
11947bd0b0f0STejun Heo 		}
11957bd0b0f0STejun Heo 	}
1196f1af9d3aSPhilipp Hachtmann 	/* signal end of iteration */
11977bd0b0f0STejun Heo 	*idx = ULLONG_MAX;
11987bd0b0f0STejun Heo }
11997bd0b0f0STejun Heo 
12007c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
12017c0caeb8STejun Heo /*
120245e79815SChen Chang  * Common iterator interface used to define for_each_mem_pfn_range().
12037c0caeb8STejun Heo  */
12047c0caeb8STejun Heo void __init_memblock __next_mem_pfn_range(int *idx, int nid,
12057c0caeb8STejun Heo 				unsigned long *out_start_pfn,
12067c0caeb8STejun Heo 				unsigned long *out_end_pfn, int *out_nid)
12077c0caeb8STejun Heo {
12087c0caeb8STejun Heo 	struct memblock_type *type = &memblock.memory;
12097c0caeb8STejun Heo 	struct memblock_region *r;
12107c0caeb8STejun Heo 
12117c0caeb8STejun Heo 	while (++*idx < type->cnt) {
12127c0caeb8STejun Heo 		r = &type->regions[*idx];
12137c0caeb8STejun Heo 
12147c0caeb8STejun Heo 		if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
12157c0caeb8STejun Heo 			continue;
12167c0caeb8STejun Heo 		if (nid == MAX_NUMNODES || nid == r->nid)
12177c0caeb8STejun Heo 			break;
12187c0caeb8STejun Heo 	}
12197c0caeb8STejun Heo 	if (*idx >= type->cnt) {
12207c0caeb8STejun Heo 		*idx = -1;
12217c0caeb8STejun Heo 		return;
12227c0caeb8STejun Heo 	}
12237c0caeb8STejun Heo 
12247c0caeb8STejun Heo 	if (out_start_pfn)
12257c0caeb8STejun Heo 		*out_start_pfn = PFN_UP(r->base);
12267c0caeb8STejun Heo 	if (out_end_pfn)
12277c0caeb8STejun Heo 		*out_end_pfn = PFN_DOWN(r->base + r->size);
12287c0caeb8STejun Heo 	if (out_nid)
12297c0caeb8STejun Heo 		*out_nid = r->nid;
12307c0caeb8STejun Heo }
12317c0caeb8STejun Heo 
12327c0caeb8STejun Heo /**
12337c0caeb8STejun Heo  * memblock_set_node - set node ID on memblock regions
12347c0caeb8STejun Heo  * @base: base of area to set node ID for
12357c0caeb8STejun Heo  * @size: size of area to set node ID for
1236e7e8de59STang Chen  * @type: memblock type to set node ID for
12377c0caeb8STejun Heo  * @nid: node ID to set
12387c0caeb8STejun Heo  *
1239e7e8de59STang Chen  * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
12407c0caeb8STejun Heo  * Regions which cross the area boundaries are split as necessary.
12417c0caeb8STejun Heo  *
124247cec443SMike Rapoport  * Return:
12437c0caeb8STejun Heo  * 0 on success, -errno on failure.
12447c0caeb8STejun Heo  */
12457c0caeb8STejun Heo int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1246e7e8de59STang Chen 				      struct memblock_type *type, int nid)
12477c0caeb8STejun Heo {
12486a9ceb31STejun Heo 	int start_rgn, end_rgn;
12496a9ceb31STejun Heo 	int i, ret;
12507c0caeb8STejun Heo 
12516a9ceb31STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
12526a9ceb31STejun Heo 	if (ret)
12536a9ceb31STejun Heo 		return ret;
12547c0caeb8STejun Heo 
12556a9ceb31STejun Heo 	for (i = start_rgn; i < end_rgn; i++)
1256e9d24ad3SWanpeng Li 		memblock_set_region_node(&type->regions[i], nid);
12577c0caeb8STejun Heo 
12587c0caeb8STejun Heo 	memblock_merge_regions(type);
12597c0caeb8STejun Heo 	return 0;
12607c0caeb8STejun Heo }
12617c0caeb8STejun Heo #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1262837566e7SAlexander Duyck #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1263837566e7SAlexander Duyck /**
1264837566e7SAlexander Duyck  * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
1265837566e7SAlexander Duyck  *
1266837566e7SAlexander Duyck  * @idx: pointer to u64 loop variable
1267837566e7SAlexander Duyck  * @zone: zone in which all of the memory blocks reside
1268837566e7SAlexander Duyck  * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
1269837566e7SAlexander Duyck  * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
1270837566e7SAlexander Duyck  *
1271837566e7SAlexander Duyck  * This function is meant to be a zone/pfn specific wrapper for the
1272837566e7SAlexander Duyck  * for_each_mem_range type iterators. Specifically they are used in the
1273837566e7SAlexander Duyck  * deferred memory init routines and as such we were duplicating much of
1274837566e7SAlexander Duyck  * this logic throughout the code. So instead of having it in multiple
1275837566e7SAlexander Duyck  * locations it seemed like it would make more sense to centralize this to
1276837566e7SAlexander Duyck  * one new iterator that does everything they need.
1277837566e7SAlexander Duyck  */
1278837566e7SAlexander Duyck void __init_memblock
1279837566e7SAlexander Duyck __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
1280837566e7SAlexander Duyck 			     unsigned long *out_spfn, unsigned long *out_epfn)
1281837566e7SAlexander Duyck {
1282837566e7SAlexander Duyck 	int zone_nid = zone_to_nid(zone);
1283837566e7SAlexander Duyck 	phys_addr_t spa, epa;
1284837566e7SAlexander Duyck 	int nid;
1285837566e7SAlexander Duyck 
1286837566e7SAlexander Duyck 	__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1287837566e7SAlexander Duyck 			 &memblock.memory, &memblock.reserved,
1288837566e7SAlexander Duyck 			 &spa, &epa, &nid);
1289837566e7SAlexander Duyck 
1290837566e7SAlexander Duyck 	while (*idx != U64_MAX) {
1291837566e7SAlexander Duyck 		unsigned long epfn = PFN_DOWN(epa);
1292837566e7SAlexander Duyck 		unsigned long spfn = PFN_UP(spa);
1293837566e7SAlexander Duyck 
1294837566e7SAlexander Duyck 		/*
1295837566e7SAlexander Duyck 		 * Verify the end is at least past the start of the zone and
1296837566e7SAlexander Duyck 		 * that we have at least one PFN to initialize.
1297837566e7SAlexander Duyck 		 */
1298837566e7SAlexander Duyck 		if (zone->zone_start_pfn < epfn && spfn < epfn) {
1299837566e7SAlexander Duyck 			/* if we went too far just stop searching */
1300837566e7SAlexander Duyck 			if (zone_end_pfn(zone) <= spfn) {
1301837566e7SAlexander Duyck 				*idx = U64_MAX;
1302837566e7SAlexander Duyck 				break;
1303837566e7SAlexander Duyck 			}
1304837566e7SAlexander Duyck 
1305837566e7SAlexander Duyck 			if (out_spfn)
1306837566e7SAlexander Duyck 				*out_spfn = max(zone->zone_start_pfn, spfn);
1307837566e7SAlexander Duyck 			if (out_epfn)
1308837566e7SAlexander Duyck 				*out_epfn = min(zone_end_pfn(zone), epfn);
1309837566e7SAlexander Duyck 
1310837566e7SAlexander Duyck 			return;
1311837566e7SAlexander Duyck 		}
1312837566e7SAlexander Duyck 
1313837566e7SAlexander Duyck 		__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1314837566e7SAlexander Duyck 				 &memblock.memory, &memblock.reserved,
1315837566e7SAlexander Duyck 				 &spa, &epa, &nid);
1316837566e7SAlexander Duyck 	}
1317837566e7SAlexander Duyck 
1318837566e7SAlexander Duyck 	/* signal end of iteration */
1319837566e7SAlexander Duyck 	if (out_spfn)
1320837566e7SAlexander Duyck 		*out_spfn = ULONG_MAX;
1321837566e7SAlexander Duyck 	if (out_epfn)
1322837566e7SAlexander Duyck 		*out_epfn = 0;
1323837566e7SAlexander Duyck }
1324837566e7SAlexander Duyck 
1325837566e7SAlexander Duyck #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
13267c0caeb8STejun Heo 
132792d12f95SMike Rapoport /**
132892d12f95SMike Rapoport  * memblock_alloc_range_nid - allocate boot memory block
132992d12f95SMike Rapoport  * @size: size of memory block to be allocated in bytes
133092d12f95SMike Rapoport  * @align: alignment of the region and block's size
133192d12f95SMike Rapoport  * @start: the lower bound of the memory region to allocate (phys address)
133292d12f95SMike Rapoport  * @end: the upper bound of the memory region to allocate (phys address)
133392d12f95SMike Rapoport  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
13340ac398b1SYunfeng Ye  * @exact_nid: control the allocation fall back to other nodes
133592d12f95SMike Rapoport  *
133692d12f95SMike Rapoport  * The allocation is performed from memory region limited by
133795830666SCao jin  * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE.
133892d12f95SMike Rapoport  *
13390ac398b1SYunfeng Ye  * If the specified node can not hold the requested memory and @exact_nid
13400ac398b1SYunfeng Ye  * is false, the allocation falls back to any node in the system.
134192d12f95SMike Rapoport  *
134292d12f95SMike Rapoport  * For systems with memory mirroring, the allocation is attempted first
134392d12f95SMike Rapoport  * from the regions with mirroring enabled and then retried from any
134492d12f95SMike Rapoport  * memory region.
134592d12f95SMike Rapoport  *
134692d12f95SMike Rapoport  * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for
134792d12f95SMike Rapoport  * allocated boot memory block, so that it is never reported as leaks.
134892d12f95SMike Rapoport  *
134992d12f95SMike Rapoport  * Return:
135092d12f95SMike Rapoport  * Physical address of allocated memory block on success, %0 on failure.
135192d12f95SMike Rapoport  */
13522bfc2862SAkinobu Mita static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
13532bfc2862SAkinobu Mita 					phys_addr_t align, phys_addr_t start,
13540ac398b1SYunfeng Ye 					phys_addr_t end, int nid,
13550ac398b1SYunfeng Ye 					bool exact_nid)
135695f72d1eSYinghai Lu {
135792d12f95SMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
13586ed311b2SBenjamin Herrenschmidt 	phys_addr_t found;
135995f72d1eSYinghai Lu 
136092d12f95SMike Rapoport 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
136192d12f95SMike Rapoport 		nid = NUMA_NO_NODE;
136292d12f95SMike Rapoport 
13632f770806SMike Rapoport 	if (!align) {
13642f770806SMike Rapoport 		/* Can't use WARNs this early in boot on powerpc */
13652f770806SMike Rapoport 		dump_stack();
13662f770806SMike Rapoport 		align = SMP_CACHE_BYTES;
13672f770806SMike Rapoport 	}
13682f770806SMike Rapoport 
136992d12f95SMike Rapoport again:
1370fc6daaf9STony Luck 	found = memblock_find_in_range_node(size, align, start, end, nid,
1371fc6daaf9STony Luck 					    flags);
137292d12f95SMike Rapoport 	if (found && !memblock_reserve(found, size))
137392d12f95SMike Rapoport 		goto done;
137492d12f95SMike Rapoport 
13750ac398b1SYunfeng Ye 	if (nid != NUMA_NO_NODE && !exact_nid) {
137692d12f95SMike Rapoport 		found = memblock_find_in_range_node(size, align, start,
137792d12f95SMike Rapoport 						    end, NUMA_NO_NODE,
137892d12f95SMike Rapoport 						    flags);
137992d12f95SMike Rapoport 		if (found && !memblock_reserve(found, size))
138092d12f95SMike Rapoport 			goto done;
138192d12f95SMike Rapoport 	}
138292d12f95SMike Rapoport 
138392d12f95SMike Rapoport 	if (flags & MEMBLOCK_MIRROR) {
138492d12f95SMike Rapoport 		flags &= ~MEMBLOCK_MIRROR;
138592d12f95SMike Rapoport 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
138692d12f95SMike Rapoport 			&size);
138792d12f95SMike Rapoport 		goto again;
138892d12f95SMike Rapoport 	}
138992d12f95SMike Rapoport 
139092d12f95SMike Rapoport 	return 0;
139192d12f95SMike Rapoport 
139292d12f95SMike Rapoport done:
139392d12f95SMike Rapoport 	/* Skip kmemleak for kasan_init() due to high volume. */
139492d12f95SMike Rapoport 	if (end != MEMBLOCK_ALLOC_KASAN)
1395aedf95eaSCatalin Marinas 		/*
139692d12f95SMike Rapoport 		 * The min_count is set to 0 so that memblock allocated
139792d12f95SMike Rapoport 		 * blocks are never reported as leaks. This is because many
139892d12f95SMike Rapoport 		 * of these blocks are only referred via the physical
139992d12f95SMike Rapoport 		 * address which is not looked up by kmemleak.
1400aedf95eaSCatalin Marinas 		 */
14019099daedSCatalin Marinas 		kmemleak_alloc_phys(found, size, 0, 0);
140292d12f95SMike Rapoport 
14036ed311b2SBenjamin Herrenschmidt 	return found;
1404aedf95eaSCatalin Marinas }
140595f72d1eSYinghai Lu 
1406a2974133SMike Rapoport /**
1407a2974133SMike Rapoport  * memblock_phys_alloc_range - allocate a memory block inside specified range
1408a2974133SMike Rapoport  * @size: size of memory block to be allocated in bytes
1409a2974133SMike Rapoport  * @align: alignment of the region and block's size
1410a2974133SMike Rapoport  * @start: the lower bound of the memory region to allocate (physical address)
1411a2974133SMike Rapoport  * @end: the upper bound of the memory region to allocate (physical address)
1412a2974133SMike Rapoport  *
1413a2974133SMike Rapoport  * Allocate @size bytes in the between @start and @end.
1414a2974133SMike Rapoport  *
1415a2974133SMike Rapoport  * Return: physical address of the allocated memory block on success,
1416a2974133SMike Rapoport  * %0 on failure.
1417a2974133SMike Rapoport  */
14188a770c2aSMike Rapoport phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
14198a770c2aSMike Rapoport 					     phys_addr_t align,
14208a770c2aSMike Rapoport 					     phys_addr_t start,
14218a770c2aSMike Rapoport 					     phys_addr_t end)
14222bfc2862SAkinobu Mita {
14230ac398b1SYunfeng Ye 	return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
14240ac398b1SYunfeng Ye 					false);
14257bd0b0f0STejun Heo }
14267bd0b0f0STejun Heo 
1427a2974133SMike Rapoport /**
1428a2974133SMike Rapoport  * memblock_phys_alloc_try_nid - allocate a memory block from specified MUMA node
1429a2974133SMike Rapoport  * @size: size of memory block to be allocated in bytes
1430a2974133SMike Rapoport  * @align: alignment of the region and block's size
1431a2974133SMike Rapoport  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1432a2974133SMike Rapoport  *
1433a2974133SMike Rapoport  * Allocates memory block from the specified NUMA node. If the node
1434a2974133SMike Rapoport  * has no available memory, attempts to allocated from any node in the
1435a2974133SMike Rapoport  * system.
1436a2974133SMike Rapoport  *
1437a2974133SMike Rapoport  * Return: physical address of the allocated memory block on success,
1438a2974133SMike Rapoport  * %0 on failure.
1439a2974133SMike Rapoport  */
14409a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
14419d1e2492SBenjamin Herrenschmidt {
144233755574SMike Rapoport 	return memblock_alloc_range_nid(size, align, 0,
14430ac398b1SYunfeng Ye 					MEMBLOCK_ALLOC_ACCESSIBLE, nid, false);
144495f72d1eSYinghai Lu }
144595f72d1eSYinghai Lu 
144626f09e9bSSantosh Shilimkar /**
1447eb31d559SMike Rapoport  * memblock_alloc_internal - allocate boot memory block
144826f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
144926f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
145026f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region to allocate (phys address)
145126f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region to allocate (phys address)
145226f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
14530ac398b1SYunfeng Ye  * @exact_nid: control the allocation fall back to other nodes
145426f09e9bSSantosh Shilimkar  *
145592d12f95SMike Rapoport  * Allocates memory block using memblock_alloc_range_nid() and
145692d12f95SMike Rapoport  * converts the returned physical address to virtual.
145792d12f95SMike Rapoport  *
145826f09e9bSSantosh Shilimkar  * The @min_addr limit is dropped if it can not be satisfied and the allocation
145992d12f95SMike Rapoport  * will fall back to memory below @min_addr. Other constraints, such
146092d12f95SMike Rapoport  * as node and mirrored memory will be handled again in
146192d12f95SMike Rapoport  * memblock_alloc_range_nid().
146226f09e9bSSantosh Shilimkar  *
146347cec443SMike Rapoport  * Return:
146426f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
146526f09e9bSSantosh Shilimkar  */
1466eb31d559SMike Rapoport static void * __init memblock_alloc_internal(
146726f09e9bSSantosh Shilimkar 				phys_addr_t size, phys_addr_t align,
146826f09e9bSSantosh Shilimkar 				phys_addr_t min_addr, phys_addr_t max_addr,
14690ac398b1SYunfeng Ye 				int nid, bool exact_nid)
147026f09e9bSSantosh Shilimkar {
147126f09e9bSSantosh Shilimkar 	phys_addr_t alloc;
147226f09e9bSSantosh Shilimkar 
147326f09e9bSSantosh Shilimkar 	/*
147426f09e9bSSantosh Shilimkar 	 * Detect any accidental use of these APIs after slab is ready, as at
147526f09e9bSSantosh Shilimkar 	 * this moment memblock may be deinitialized already and its
1476c6ffc5caSMike Rapoport 	 * internal data may be destroyed (after execution of memblock_free_all)
147726f09e9bSSantosh Shilimkar 	 */
147826f09e9bSSantosh Shilimkar 	if (WARN_ON_ONCE(slab_is_available()))
147926f09e9bSSantosh Shilimkar 		return kzalloc_node(size, GFP_NOWAIT, nid);
148026f09e9bSSantosh Shilimkar 
1481f3057ad7SMike Rapoport 	if (max_addr > memblock.current_limit)
1482f3057ad7SMike Rapoport 		max_addr = memblock.current_limit;
1483f3057ad7SMike Rapoport 
14840ac398b1SYunfeng Ye 	alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid,
14850ac398b1SYunfeng Ye 					exact_nid);
14862f770806SMike Rapoport 
148792d12f95SMike Rapoport 	/* retry allocation without lower limit */
148892d12f95SMike Rapoport 	if (!alloc && min_addr)
14890ac398b1SYunfeng Ye 		alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid,
14900ac398b1SYunfeng Ye 						exact_nid);
149126f09e9bSSantosh Shilimkar 
149292d12f95SMike Rapoport 	if (!alloc)
1493a3f5bafcSTony Luck 		return NULL;
149426f09e9bSSantosh Shilimkar 
149592d12f95SMike Rapoport 	return phys_to_virt(alloc);
149626f09e9bSSantosh Shilimkar }
149726f09e9bSSantosh Shilimkar 
149826f09e9bSSantosh Shilimkar /**
14990ac398b1SYunfeng Ye  * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node
15000ac398b1SYunfeng Ye  * without zeroing memory
15010ac398b1SYunfeng Ye  * @size: size of memory block to be allocated in bytes
15020ac398b1SYunfeng Ye  * @align: alignment of the region and block's size
15030ac398b1SYunfeng Ye  * @min_addr: the lower bound of the memory region from where the allocation
15040ac398b1SYunfeng Ye  *	  is preferred (phys address)
15050ac398b1SYunfeng Ye  * @max_addr: the upper bound of the memory region from where the allocation
15060ac398b1SYunfeng Ye  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
15070ac398b1SYunfeng Ye  *	      allocate only from memory limited by memblock.current_limit value
15080ac398b1SYunfeng Ye  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
15090ac398b1SYunfeng Ye  *
15100ac398b1SYunfeng Ye  * Public function, provides additional debug information (including caller
15110ac398b1SYunfeng Ye  * info), if enabled. Does not zero allocated memory.
15120ac398b1SYunfeng Ye  *
15130ac398b1SYunfeng Ye  * Return:
15140ac398b1SYunfeng Ye  * Virtual address of allocated memory block on success, NULL on failure.
15150ac398b1SYunfeng Ye  */
15160ac398b1SYunfeng Ye void * __init memblock_alloc_exact_nid_raw(
15170ac398b1SYunfeng Ye 			phys_addr_t size, phys_addr_t align,
15180ac398b1SYunfeng Ye 			phys_addr_t min_addr, phys_addr_t max_addr,
15190ac398b1SYunfeng Ye 			int nid)
15200ac398b1SYunfeng Ye {
15210ac398b1SYunfeng Ye 	void *ptr;
15220ac398b1SYunfeng Ye 
15230ac398b1SYunfeng Ye 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
15240ac398b1SYunfeng Ye 		     __func__, (u64)size, (u64)align, nid, &min_addr,
15250ac398b1SYunfeng Ye 		     &max_addr, (void *)_RET_IP_);
15260ac398b1SYunfeng Ye 
15270ac398b1SYunfeng Ye 	ptr = memblock_alloc_internal(size, align,
15280ac398b1SYunfeng Ye 					   min_addr, max_addr, nid, true);
15290ac398b1SYunfeng Ye 	if (ptr && size > 0)
15300ac398b1SYunfeng Ye 		page_init_poison(ptr, size);
15310ac398b1SYunfeng Ye 
15320ac398b1SYunfeng Ye 	return ptr;
15330ac398b1SYunfeng Ye }
15340ac398b1SYunfeng Ye 
15350ac398b1SYunfeng Ye /**
1536eb31d559SMike Rapoport  * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
1537ea1f5f37SPavel Tatashin  * memory and without panicking
1538ea1f5f37SPavel Tatashin  * @size: size of memory block to be allocated in bytes
1539ea1f5f37SPavel Tatashin  * @align: alignment of the region and block's size
1540ea1f5f37SPavel Tatashin  * @min_addr: the lower bound of the memory region from where the allocation
1541ea1f5f37SPavel Tatashin  *	  is preferred (phys address)
1542ea1f5f37SPavel Tatashin  * @max_addr: the upper bound of the memory region from where the allocation
154397ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1544ea1f5f37SPavel Tatashin  *	      allocate only from memory limited by memblock.current_limit value
1545ea1f5f37SPavel Tatashin  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1546ea1f5f37SPavel Tatashin  *
1547ea1f5f37SPavel Tatashin  * Public function, provides additional debug information (including caller
1548ea1f5f37SPavel Tatashin  * info), if enabled. Does not zero allocated memory, does not panic if request
1549ea1f5f37SPavel Tatashin  * cannot be satisfied.
1550ea1f5f37SPavel Tatashin  *
155147cec443SMike Rapoport  * Return:
1552ea1f5f37SPavel Tatashin  * Virtual address of allocated memory block on success, NULL on failure.
1553ea1f5f37SPavel Tatashin  */
1554eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_raw(
1555ea1f5f37SPavel Tatashin 			phys_addr_t size, phys_addr_t align,
1556ea1f5f37SPavel Tatashin 			phys_addr_t min_addr, phys_addr_t max_addr,
1557ea1f5f37SPavel Tatashin 			int nid)
1558ea1f5f37SPavel Tatashin {
1559ea1f5f37SPavel Tatashin 	void *ptr;
1560ea1f5f37SPavel Tatashin 
1561d75f773cSSakari Ailus 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1562a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1563a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1564ea1f5f37SPavel Tatashin 
1565eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
15660ac398b1SYunfeng Ye 					   min_addr, max_addr, nid, false);
1567ea1f5f37SPavel Tatashin 	if (ptr && size > 0)
1568f682a97aSAlexander Duyck 		page_init_poison(ptr, size);
1569f682a97aSAlexander Duyck 
1570ea1f5f37SPavel Tatashin 	return ptr;
1571ea1f5f37SPavel Tatashin }
1572ea1f5f37SPavel Tatashin 
1573ea1f5f37SPavel Tatashin /**
1574c0dbe825SMike Rapoport  * memblock_alloc_try_nid - allocate boot memory block
157526f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
157626f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
157726f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region from where the allocation
157826f09e9bSSantosh Shilimkar  *	  is preferred (phys address)
157926f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region from where the allocation
158097ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
158126f09e9bSSantosh Shilimkar  *	      allocate only from memory limited by memblock.current_limit value
158226f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
158326f09e9bSSantosh Shilimkar  *
1584c0dbe825SMike Rapoport  * Public function, provides additional debug information (including caller
1585c0dbe825SMike Rapoport  * info), if enabled. This function zeroes the allocated memory.
158626f09e9bSSantosh Shilimkar  *
158747cec443SMike Rapoport  * Return:
158826f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
158926f09e9bSSantosh Shilimkar  */
1590eb31d559SMike Rapoport void * __init memblock_alloc_try_nid(
159126f09e9bSSantosh Shilimkar 			phys_addr_t size, phys_addr_t align,
159226f09e9bSSantosh Shilimkar 			phys_addr_t min_addr, phys_addr_t max_addr,
159326f09e9bSSantosh Shilimkar 			int nid)
159426f09e9bSSantosh Shilimkar {
159526f09e9bSSantosh Shilimkar 	void *ptr;
159626f09e9bSSantosh Shilimkar 
1597d75f773cSSakari Ailus 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1598a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1599a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1600eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
16010ac398b1SYunfeng Ye 					   min_addr, max_addr, nid, false);
1602c0dbe825SMike Rapoport 	if (ptr)
1603ea1f5f37SPavel Tatashin 		memset(ptr, 0, size);
160426f09e9bSSantosh Shilimkar 
1605c0dbe825SMike Rapoport 	return ptr;
160626f09e9bSSantosh Shilimkar }
160726f09e9bSSantosh Shilimkar 
160826f09e9bSSantosh Shilimkar /**
1609a2974133SMike Rapoport  * __memblock_free_late - free pages directly to buddy allocator
161048a833ccSMike Rapoport  * @base: phys starting address of the  boot memory block
161126f09e9bSSantosh Shilimkar  * @size: size of the boot memory block in bytes
161226f09e9bSSantosh Shilimkar  *
1613a2974133SMike Rapoport  * This is only useful when the memblock allocator has already been torn
161426f09e9bSSantosh Shilimkar  * down, but we are still initializing the system.  Pages are released directly
1615a2974133SMike Rapoport  * to the buddy allocator.
161626f09e9bSSantosh Shilimkar  */
161726f09e9bSSantosh Shilimkar void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
161826f09e9bSSantosh Shilimkar {
1619a36aab89SMike Rapoport 	phys_addr_t cursor, end;
162026f09e9bSSantosh Shilimkar 
1621a36aab89SMike Rapoport 	end = base + size - 1;
1622d75f773cSSakari Ailus 	memblock_dbg("%s: [%pa-%pa] %pS\n",
1623a36aab89SMike Rapoport 		     __func__, &base, &end, (void *)_RET_IP_);
16249099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
162526f09e9bSSantosh Shilimkar 	cursor = PFN_UP(base);
162626f09e9bSSantosh Shilimkar 	end = PFN_DOWN(base + size);
162726f09e9bSSantosh Shilimkar 
162826f09e9bSSantosh Shilimkar 	for (; cursor < end; cursor++) {
16297c2ee349SMike Rapoport 		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
1630ca79b0c2SArun KS 		totalram_pages_inc();
163126f09e9bSSantosh Shilimkar 	}
163226f09e9bSSantosh Shilimkar }
16339d1e2492SBenjamin Herrenschmidt 
16349d1e2492SBenjamin Herrenschmidt /*
16359d1e2492SBenjamin Herrenschmidt  * Remaining API functions
16369d1e2492SBenjamin Herrenschmidt  */
16379d1e2492SBenjamin Herrenschmidt 
16381f1ffb8aSDavid Gibson phys_addr_t __init_memblock memblock_phys_mem_size(void)
163995f72d1eSYinghai Lu {
16401440c4e2STejun Heo 	return memblock.memory.total_size;
164195f72d1eSYinghai Lu }
164295f72d1eSYinghai Lu 
16438907de5dSSrikar Dronamraju phys_addr_t __init_memblock memblock_reserved_size(void)
16448907de5dSSrikar Dronamraju {
16458907de5dSSrikar Dronamraju 	return memblock.reserved.total_size;
16468907de5dSSrikar Dronamraju }
16478907de5dSSrikar Dronamraju 
1648595ad9afSYinghai Lu phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1649595ad9afSYinghai Lu {
1650595ad9afSYinghai Lu 	unsigned long pages = 0;
1651595ad9afSYinghai Lu 	struct memblock_region *r;
1652595ad9afSYinghai Lu 	unsigned long start_pfn, end_pfn;
1653595ad9afSYinghai Lu 
1654595ad9afSYinghai Lu 	for_each_memblock(memory, r) {
1655595ad9afSYinghai Lu 		start_pfn = memblock_region_memory_base_pfn(r);
1656595ad9afSYinghai Lu 		end_pfn = memblock_region_memory_end_pfn(r);
1657595ad9afSYinghai Lu 		start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1658595ad9afSYinghai Lu 		end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1659595ad9afSYinghai Lu 		pages += end_pfn - start_pfn;
1660595ad9afSYinghai Lu 	}
1661595ad9afSYinghai Lu 
166216763230SFabian Frederick 	return PFN_PHYS(pages);
1663595ad9afSYinghai Lu }
1664595ad9afSYinghai Lu 
16650a93ebefSSam Ravnborg /* lowest address */
16660a93ebefSSam Ravnborg phys_addr_t __init_memblock memblock_start_of_DRAM(void)
16670a93ebefSSam Ravnborg {
16680a93ebefSSam Ravnborg 	return memblock.memory.regions[0].base;
16690a93ebefSSam Ravnborg }
16700a93ebefSSam Ravnborg 
167110d06439SYinghai Lu phys_addr_t __init_memblock memblock_end_of_DRAM(void)
167295f72d1eSYinghai Lu {
167395f72d1eSYinghai Lu 	int idx = memblock.memory.cnt - 1;
167495f72d1eSYinghai Lu 
1675e3239ff9SBenjamin Herrenschmidt 	return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
167695f72d1eSYinghai Lu }
167795f72d1eSYinghai Lu 
1678a571d4ebSDennis Chen static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
167995f72d1eSYinghai Lu {
16801c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1681136199f0SEmil Medve 	struct memblock_region *r;
168295f72d1eSYinghai Lu 
1683a571d4ebSDennis Chen 	/*
1684a571d4ebSDennis Chen 	 * translate the memory @limit size into the max address within one of
1685a571d4ebSDennis Chen 	 * the memory memblock regions, if the @limit exceeds the total size
16861c4bc43dSStefan Agner 	 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
1687a571d4ebSDennis Chen 	 */
1688136199f0SEmil Medve 	for_each_memblock(memory, r) {
1689c0ce8fefSTejun Heo 		if (limit <= r->size) {
1690c0ce8fefSTejun Heo 			max_addr = r->base + limit;
169195f72d1eSYinghai Lu 			break;
169295f72d1eSYinghai Lu 		}
1693c0ce8fefSTejun Heo 		limit -= r->size;
169495f72d1eSYinghai Lu 	}
1695c0ce8fefSTejun Heo 
1696a571d4ebSDennis Chen 	return max_addr;
1697a571d4ebSDennis Chen }
1698a571d4ebSDennis Chen 
1699a571d4ebSDennis Chen void __init memblock_enforce_memory_limit(phys_addr_t limit)
1700a571d4ebSDennis Chen {
17011c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1702a571d4ebSDennis Chen 
1703a571d4ebSDennis Chen 	if (!limit)
1704a571d4ebSDennis Chen 		return;
1705a571d4ebSDennis Chen 
1706a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1707a571d4ebSDennis Chen 
1708a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
17091c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1710a571d4ebSDennis Chen 		return;
1711a571d4ebSDennis Chen 
1712c0ce8fefSTejun Heo 	/* truncate both memory and reserved regions */
1713f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.memory, max_addr,
17141c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
1715f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.reserved, max_addr,
17161c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
171795f72d1eSYinghai Lu }
171895f72d1eSYinghai Lu 
1719c9ca9b4eSAKASHI Takahiro void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1720c9ca9b4eSAKASHI Takahiro {
1721c9ca9b4eSAKASHI Takahiro 	int start_rgn, end_rgn;
1722c9ca9b4eSAKASHI Takahiro 	int i, ret;
1723c9ca9b4eSAKASHI Takahiro 
1724c9ca9b4eSAKASHI Takahiro 	if (!size)
1725c9ca9b4eSAKASHI Takahiro 		return;
1726c9ca9b4eSAKASHI Takahiro 
1727c9ca9b4eSAKASHI Takahiro 	ret = memblock_isolate_range(&memblock.memory, base, size,
1728c9ca9b4eSAKASHI Takahiro 						&start_rgn, &end_rgn);
1729c9ca9b4eSAKASHI Takahiro 	if (ret)
1730c9ca9b4eSAKASHI Takahiro 		return;
1731c9ca9b4eSAKASHI Takahiro 
1732c9ca9b4eSAKASHI Takahiro 	/* remove all the MAP regions */
1733c9ca9b4eSAKASHI Takahiro 	for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1734c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1735c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1736c9ca9b4eSAKASHI Takahiro 
1737c9ca9b4eSAKASHI Takahiro 	for (i = start_rgn - 1; i >= 0; i--)
1738c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1739c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1740c9ca9b4eSAKASHI Takahiro 
1741c9ca9b4eSAKASHI Takahiro 	/* truncate the reserved regions */
1742c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved, 0, base);
1743c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved,
17441c4bc43dSStefan Agner 			base + size, PHYS_ADDR_MAX);
1745c9ca9b4eSAKASHI Takahiro }
1746c9ca9b4eSAKASHI Takahiro 
1747a571d4ebSDennis Chen void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1748a571d4ebSDennis Chen {
1749a571d4ebSDennis Chen 	phys_addr_t max_addr;
1750a571d4ebSDennis Chen 
1751a571d4ebSDennis Chen 	if (!limit)
1752a571d4ebSDennis Chen 		return;
1753a571d4ebSDennis Chen 
1754a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1755a571d4ebSDennis Chen 
1756a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
17571c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1758a571d4ebSDennis Chen 		return;
1759a571d4ebSDennis Chen 
1760c9ca9b4eSAKASHI Takahiro 	memblock_cap_memory_range(0, max_addr);
1761a571d4ebSDennis Chen }
1762a571d4ebSDennis Chen 
1763cd79481dSYinghai Lu static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
176472d4b0b4SBenjamin Herrenschmidt {
176572d4b0b4SBenjamin Herrenschmidt 	unsigned int left = 0, right = type->cnt;
176672d4b0b4SBenjamin Herrenschmidt 
176772d4b0b4SBenjamin Herrenschmidt 	do {
176872d4b0b4SBenjamin Herrenschmidt 		unsigned int mid = (right + left) / 2;
176972d4b0b4SBenjamin Herrenschmidt 
177072d4b0b4SBenjamin Herrenschmidt 		if (addr < type->regions[mid].base)
177172d4b0b4SBenjamin Herrenschmidt 			right = mid;
177272d4b0b4SBenjamin Herrenschmidt 		else if (addr >= (type->regions[mid].base +
177372d4b0b4SBenjamin Herrenschmidt 				  type->regions[mid].size))
177472d4b0b4SBenjamin Herrenschmidt 			left = mid + 1;
177572d4b0b4SBenjamin Herrenschmidt 		else
177672d4b0b4SBenjamin Herrenschmidt 			return mid;
177772d4b0b4SBenjamin Herrenschmidt 	} while (left < right);
177872d4b0b4SBenjamin Herrenschmidt 	return -1;
177972d4b0b4SBenjamin Herrenschmidt }
178072d4b0b4SBenjamin Herrenschmidt 
1781f5a222dcSYueyi Li bool __init_memblock memblock_is_reserved(phys_addr_t addr)
178295f72d1eSYinghai Lu {
178372d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.reserved, addr) != -1;
178495f72d1eSYinghai Lu }
178572d4b0b4SBenjamin Herrenschmidt 
1786b4ad0c7eSYaowei Bai bool __init_memblock memblock_is_memory(phys_addr_t addr)
178772d4b0b4SBenjamin Herrenschmidt {
178872d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.memory, addr) != -1;
178972d4b0b4SBenjamin Herrenschmidt }
179072d4b0b4SBenjamin Herrenschmidt 
1791937f0c26SYaowei Bai bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
1792bf3d3cc5SArd Biesheuvel {
1793bf3d3cc5SArd Biesheuvel 	int i = memblock_search(&memblock.memory, addr);
1794bf3d3cc5SArd Biesheuvel 
1795bf3d3cc5SArd Biesheuvel 	if (i == -1)
1796bf3d3cc5SArd Biesheuvel 		return false;
1797bf3d3cc5SArd Biesheuvel 	return !memblock_is_nomap(&memblock.memory.regions[i]);
1798bf3d3cc5SArd Biesheuvel }
1799bf3d3cc5SArd Biesheuvel 
1800e76b63f8SYinghai Lu #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1801e76b63f8SYinghai Lu int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1802e76b63f8SYinghai Lu 			 unsigned long *start_pfn, unsigned long *end_pfn)
1803e76b63f8SYinghai Lu {
1804e76b63f8SYinghai Lu 	struct memblock_type *type = &memblock.memory;
180516763230SFabian Frederick 	int mid = memblock_search(type, PFN_PHYS(pfn));
1806e76b63f8SYinghai Lu 
1807e76b63f8SYinghai Lu 	if (mid == -1)
1808e76b63f8SYinghai Lu 		return -1;
1809e76b63f8SYinghai Lu 
1810f7e2f7e8SFabian Frederick 	*start_pfn = PFN_DOWN(type->regions[mid].base);
1811f7e2f7e8SFabian Frederick 	*end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
1812e76b63f8SYinghai Lu 
1813e76b63f8SYinghai Lu 	return type->regions[mid].nid;
1814e76b63f8SYinghai Lu }
1815e76b63f8SYinghai Lu #endif
1816e76b63f8SYinghai Lu 
1817eab30949SStephen Boyd /**
1818eab30949SStephen Boyd  * memblock_is_region_memory - check if a region is a subset of memory
1819eab30949SStephen Boyd  * @base: base of region to check
1820eab30949SStephen Boyd  * @size: size of region to check
1821eab30949SStephen Boyd  *
1822eab30949SStephen Boyd  * Check if the region [@base, @base + @size) is a subset of a memory block.
1823eab30949SStephen Boyd  *
182447cec443SMike Rapoport  * Return:
1825eab30949SStephen Boyd  * 0 if false, non-zero if true
1826eab30949SStephen Boyd  */
1827937f0c26SYaowei Bai bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
182872d4b0b4SBenjamin Herrenschmidt {
1829abb65272STomi Valkeinen 	int idx = memblock_search(&memblock.memory, base);
1830eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
183172d4b0b4SBenjamin Herrenschmidt 
183272d4b0b4SBenjamin Herrenschmidt 	if (idx == -1)
1833937f0c26SYaowei Bai 		return false;
1834ef415ef4SWei Yang 	return (memblock.memory.regions[idx].base +
1835eb18f1b5STejun Heo 		 memblock.memory.regions[idx].size) >= end;
183695f72d1eSYinghai Lu }
183795f72d1eSYinghai Lu 
1838eab30949SStephen Boyd /**
1839eab30949SStephen Boyd  * memblock_is_region_reserved - check if a region intersects reserved memory
1840eab30949SStephen Boyd  * @base: base of region to check
1841eab30949SStephen Boyd  * @size: size of region to check
1842eab30949SStephen Boyd  *
184347cec443SMike Rapoport  * Check if the region [@base, @base + @size) intersects a reserved
184447cec443SMike Rapoport  * memory block.
1845eab30949SStephen Boyd  *
184647cec443SMike Rapoport  * Return:
1847c5c5c9d1STang Chen  * True if they intersect, false if not.
1848eab30949SStephen Boyd  */
1849c5c5c9d1STang Chen bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
185095f72d1eSYinghai Lu {
1851eb18f1b5STejun Heo 	memblock_cap_size(base, &size);
1852c5c5c9d1STang Chen 	return memblock_overlaps_region(&memblock.reserved, base, size);
185395f72d1eSYinghai Lu }
185495f72d1eSYinghai Lu 
18556ede1fd3SYinghai Lu void __init_memblock memblock_trim_memory(phys_addr_t align)
18566ede1fd3SYinghai Lu {
18576ede1fd3SYinghai Lu 	phys_addr_t start, end, orig_start, orig_end;
1858136199f0SEmil Medve 	struct memblock_region *r;
18596ede1fd3SYinghai Lu 
1860136199f0SEmil Medve 	for_each_memblock(memory, r) {
1861136199f0SEmil Medve 		orig_start = r->base;
1862136199f0SEmil Medve 		orig_end = r->base + r->size;
18636ede1fd3SYinghai Lu 		start = round_up(orig_start, align);
18646ede1fd3SYinghai Lu 		end = round_down(orig_end, align);
18656ede1fd3SYinghai Lu 
18666ede1fd3SYinghai Lu 		if (start == orig_start && end == orig_end)
18676ede1fd3SYinghai Lu 			continue;
18686ede1fd3SYinghai Lu 
18696ede1fd3SYinghai Lu 		if (start < end) {
1870136199f0SEmil Medve 			r->base = start;
1871136199f0SEmil Medve 			r->size = end - start;
18726ede1fd3SYinghai Lu 		} else {
1873136199f0SEmil Medve 			memblock_remove_region(&memblock.memory,
1874136199f0SEmil Medve 					       r - memblock.memory.regions);
1875136199f0SEmil Medve 			r--;
18766ede1fd3SYinghai Lu 		}
18776ede1fd3SYinghai Lu 	}
18786ede1fd3SYinghai Lu }
1879e63075a3SBenjamin Herrenschmidt 
18803661ca66SYinghai Lu void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1881e63075a3SBenjamin Herrenschmidt {
1882e63075a3SBenjamin Herrenschmidt 	memblock.current_limit = limit;
1883e63075a3SBenjamin Herrenschmidt }
1884e63075a3SBenjamin Herrenschmidt 
1885fec51014SLaura Abbott phys_addr_t __init_memblock memblock_get_current_limit(void)
1886fec51014SLaura Abbott {
1887fec51014SLaura Abbott 	return memblock.current_limit;
1888fec51014SLaura Abbott }
1889fec51014SLaura Abbott 
18900262d9c8SHeiko Carstens static void __init_memblock memblock_dump(struct memblock_type *type)
18916ed311b2SBenjamin Herrenschmidt {
18925d63f81cSMiles Chen 	phys_addr_t base, end, size;
1893e1720feeSMike Rapoport 	enum memblock_flags flags;
18948c9c1701SAlexander Kuleshov 	int idx;
18958c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
18966ed311b2SBenjamin Herrenschmidt 
18970262d9c8SHeiko Carstens 	pr_info(" %s.cnt  = 0x%lx\n", type->name, type->cnt);
18986ed311b2SBenjamin Herrenschmidt 
189966e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
19007c0caeb8STejun Heo 		char nid_buf[32] = "";
19016ed311b2SBenjamin Herrenschmidt 
19027c0caeb8STejun Heo 		base = rgn->base;
19037c0caeb8STejun Heo 		size = rgn->size;
19045d63f81cSMiles Chen 		end = base + size - 1;
190566a20757STang Chen 		flags = rgn->flags;
19067c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
19077c0caeb8STejun Heo 		if (memblock_get_region_node(rgn) != MAX_NUMNODES)
19087c0caeb8STejun Heo 			snprintf(nid_buf, sizeof(nid_buf), " on node %d",
19097c0caeb8STejun Heo 				 memblock_get_region_node(rgn));
19107c0caeb8STejun Heo #endif
1911e1720feeSMike Rapoport 		pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
19120262d9c8SHeiko Carstens 			type->name, idx, &base, &end, &size, nid_buf, flags);
19136ed311b2SBenjamin Herrenschmidt 	}
19146ed311b2SBenjamin Herrenschmidt }
19156ed311b2SBenjamin Herrenschmidt 
19164ff7b82fSTejun Heo void __init_memblock __memblock_dump_all(void)
19176ed311b2SBenjamin Herrenschmidt {
19186ed311b2SBenjamin Herrenschmidt 	pr_info("MEMBLOCK configuration:\n");
19195d63f81cSMiles Chen 	pr_info(" memory size = %pa reserved size = %pa\n",
19205d63f81cSMiles Chen 		&memblock.memory.total_size,
19215d63f81cSMiles Chen 		&memblock.reserved.total_size);
19226ed311b2SBenjamin Herrenschmidt 
19230262d9c8SHeiko Carstens 	memblock_dump(&memblock.memory);
19240262d9c8SHeiko Carstens 	memblock_dump(&memblock.reserved);
1925409efd4cSHeiko Carstens #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
19260262d9c8SHeiko Carstens 	memblock_dump(&memblock.physmem);
1927409efd4cSHeiko Carstens #endif
19286ed311b2SBenjamin Herrenschmidt }
19296ed311b2SBenjamin Herrenschmidt 
19301aadc056STejun Heo void __init memblock_allow_resize(void)
19316ed311b2SBenjamin Herrenschmidt {
1932142b45a7SBenjamin Herrenschmidt 	memblock_can_resize = 1;
19336ed311b2SBenjamin Herrenschmidt }
19346ed311b2SBenjamin Herrenschmidt 
19356ed311b2SBenjamin Herrenschmidt static int __init early_memblock(char *p)
19366ed311b2SBenjamin Herrenschmidt {
19376ed311b2SBenjamin Herrenschmidt 	if (p && strstr(p, "debug"))
19386ed311b2SBenjamin Herrenschmidt 		memblock_debug = 1;
19396ed311b2SBenjamin Herrenschmidt 	return 0;
19406ed311b2SBenjamin Herrenschmidt }
19416ed311b2SBenjamin Herrenschmidt early_param("memblock", early_memblock);
19426ed311b2SBenjamin Herrenschmidt 
1943bda49a81SMike Rapoport static void __init __free_pages_memory(unsigned long start, unsigned long end)
1944bda49a81SMike Rapoport {
1945bda49a81SMike Rapoport 	int order;
1946bda49a81SMike Rapoport 
1947bda49a81SMike Rapoport 	while (start < end) {
1948bda49a81SMike Rapoport 		order = min(MAX_ORDER - 1UL, __ffs(start));
1949bda49a81SMike Rapoport 
1950bda49a81SMike Rapoport 		while (start + (1UL << order) > end)
1951bda49a81SMike Rapoport 			order--;
1952bda49a81SMike Rapoport 
1953bda49a81SMike Rapoport 		memblock_free_pages(pfn_to_page(start), start, order);
1954bda49a81SMike Rapoport 
1955bda49a81SMike Rapoport 		start += (1UL << order);
1956bda49a81SMike Rapoport 	}
1957bda49a81SMike Rapoport }
1958bda49a81SMike Rapoport 
1959bda49a81SMike Rapoport static unsigned long __init __free_memory_core(phys_addr_t start,
1960bda49a81SMike Rapoport 				 phys_addr_t end)
1961bda49a81SMike Rapoport {
1962bda49a81SMike Rapoport 	unsigned long start_pfn = PFN_UP(start);
1963bda49a81SMike Rapoport 	unsigned long end_pfn = min_t(unsigned long,
1964bda49a81SMike Rapoport 				      PFN_DOWN(end), max_low_pfn);
1965bda49a81SMike Rapoport 
1966bda49a81SMike Rapoport 	if (start_pfn >= end_pfn)
1967bda49a81SMike Rapoport 		return 0;
1968bda49a81SMike Rapoport 
1969bda49a81SMike Rapoport 	__free_pages_memory(start_pfn, end_pfn);
1970bda49a81SMike Rapoport 
1971bda49a81SMike Rapoport 	return end_pfn - start_pfn;
1972bda49a81SMike Rapoport }
1973bda49a81SMike Rapoport 
1974bda49a81SMike Rapoport static unsigned long __init free_low_memory_core_early(void)
1975bda49a81SMike Rapoport {
1976bda49a81SMike Rapoport 	unsigned long count = 0;
1977bda49a81SMike Rapoport 	phys_addr_t start, end;
1978bda49a81SMike Rapoport 	u64 i;
1979bda49a81SMike Rapoport 
1980bda49a81SMike Rapoport 	memblock_clear_hotplug(0, -1);
1981bda49a81SMike Rapoport 
1982bda49a81SMike Rapoport 	for_each_reserved_mem_region(i, &start, &end)
1983bda49a81SMike Rapoport 		reserve_bootmem_region(start, end);
1984bda49a81SMike Rapoport 
1985bda49a81SMike Rapoport 	/*
1986bda49a81SMike Rapoport 	 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
1987bda49a81SMike Rapoport 	 *  because in some case like Node0 doesn't have RAM installed
1988bda49a81SMike Rapoport 	 *  low ram will be on Node1
1989bda49a81SMike Rapoport 	 */
1990bda49a81SMike Rapoport 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
1991bda49a81SMike Rapoport 				NULL)
1992bda49a81SMike Rapoport 		count += __free_memory_core(start, end);
1993bda49a81SMike Rapoport 
1994bda49a81SMike Rapoport 	return count;
1995bda49a81SMike Rapoport }
1996bda49a81SMike Rapoport 
1997bda49a81SMike Rapoport static int reset_managed_pages_done __initdata;
1998bda49a81SMike Rapoport 
1999bda49a81SMike Rapoport void reset_node_managed_pages(pg_data_t *pgdat)
2000bda49a81SMike Rapoport {
2001bda49a81SMike Rapoport 	struct zone *z;
2002bda49a81SMike Rapoport 
2003bda49a81SMike Rapoport 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
20049705bea5SArun KS 		atomic_long_set(&z->managed_pages, 0);
2005bda49a81SMike Rapoport }
2006bda49a81SMike Rapoport 
2007bda49a81SMike Rapoport void __init reset_all_zones_managed_pages(void)
2008bda49a81SMike Rapoport {
2009bda49a81SMike Rapoport 	struct pglist_data *pgdat;
2010bda49a81SMike Rapoport 
2011bda49a81SMike Rapoport 	if (reset_managed_pages_done)
2012bda49a81SMike Rapoport 		return;
2013bda49a81SMike Rapoport 
2014bda49a81SMike Rapoport 	for_each_online_pgdat(pgdat)
2015bda49a81SMike Rapoport 		reset_node_managed_pages(pgdat);
2016bda49a81SMike Rapoport 
2017bda49a81SMike Rapoport 	reset_managed_pages_done = 1;
2018bda49a81SMike Rapoport }
2019bda49a81SMike Rapoport 
2020bda49a81SMike Rapoport /**
2021bda49a81SMike Rapoport  * memblock_free_all - release free pages to the buddy allocator
2022bda49a81SMike Rapoport  *
2023bda49a81SMike Rapoport  * Return: the number of pages actually released.
2024bda49a81SMike Rapoport  */
2025bda49a81SMike Rapoport unsigned long __init memblock_free_all(void)
2026bda49a81SMike Rapoport {
2027bda49a81SMike Rapoport 	unsigned long pages;
2028bda49a81SMike Rapoport 
2029bda49a81SMike Rapoport 	reset_all_zones_managed_pages();
2030bda49a81SMike Rapoport 
2031bda49a81SMike Rapoport 	pages = free_low_memory_core_early();
2032ca79b0c2SArun KS 	totalram_pages_add(pages);
2033bda49a81SMike Rapoport 
2034bda49a81SMike Rapoport 	return pages;
2035bda49a81SMike Rapoport }
2036bda49a81SMike Rapoport 
2037350e88baSMike Rapoport #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
20386d03b885SBenjamin Herrenschmidt 
20396d03b885SBenjamin Herrenschmidt static int memblock_debug_show(struct seq_file *m, void *private)
20406d03b885SBenjamin Herrenschmidt {
20416d03b885SBenjamin Herrenschmidt 	struct memblock_type *type = m->private;
20426d03b885SBenjamin Herrenschmidt 	struct memblock_region *reg;
20436d03b885SBenjamin Herrenschmidt 	int i;
20445d63f81cSMiles Chen 	phys_addr_t end;
20456d03b885SBenjamin Herrenschmidt 
20466d03b885SBenjamin Herrenschmidt 	for (i = 0; i < type->cnt; i++) {
20476d03b885SBenjamin Herrenschmidt 		reg = &type->regions[i];
20485d63f81cSMiles Chen 		end = reg->base + reg->size - 1;
20496d03b885SBenjamin Herrenschmidt 
20505d63f81cSMiles Chen 		seq_printf(m, "%4d: ", i);
20515d63f81cSMiles Chen 		seq_printf(m, "%pa..%pa\n", &reg->base, &end);
20526d03b885SBenjamin Herrenschmidt 	}
20536d03b885SBenjamin Herrenschmidt 	return 0;
20546d03b885SBenjamin Herrenschmidt }
20555ad35093SAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(memblock_debug);
20566d03b885SBenjamin Herrenschmidt 
20576d03b885SBenjamin Herrenschmidt static int __init memblock_init_debugfs(void)
20586d03b885SBenjamin Herrenschmidt {
20596d03b885SBenjamin Herrenschmidt 	struct dentry *root = debugfs_create_dir("memblock", NULL);
2060d9f7979cSGreg Kroah-Hartman 
20610825a6f9SJoe Perches 	debugfs_create_file("memory", 0444, root,
20620825a6f9SJoe Perches 			    &memblock.memory, &memblock_debug_fops);
20630825a6f9SJoe Perches 	debugfs_create_file("reserved", 0444, root,
20640825a6f9SJoe Perches 			    &memblock.reserved, &memblock_debug_fops);
206570210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
20660825a6f9SJoe Perches 	debugfs_create_file("physmem", 0444, root,
20670825a6f9SJoe Perches 			    &memblock.physmem, &memblock_debug_fops);
206870210ed9SPhilipp Hachtmann #endif
20696d03b885SBenjamin Herrenschmidt 
20706d03b885SBenjamin Herrenschmidt 	return 0;
20716d03b885SBenjamin Herrenschmidt }
20726d03b885SBenjamin Herrenschmidt __initcall(memblock_init_debugfs);
20736d03b885SBenjamin Herrenschmidt 
20746d03b885SBenjamin Herrenschmidt #endif /* CONFIG_DEBUG_FS */
2075