xref: /linux/mm/memblock.c (revision 95830666be2aef81a2963135822ab92f4902a06b)
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  */
578f1af9d3aSPhilipp Hachtmann 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 
83335fd0808STejun Heo /**
83447cec443SMike Rapoport  * memblock_setclr_flag - set or clear flag for a memory region
83547cec443SMike Rapoport  * @base: base address of the region
83647cec443SMike Rapoport  * @size: size of the region
83747cec443SMike Rapoport  * @set: set or clear the flag
83847cec443SMike Rapoport  * @flag: the flag to udpate
83966b16edfSTang Chen  *
8404308ce17STony Luck  * This function isolates region [@base, @base + @size), and sets/clears flag
84166b16edfSTang Chen  *
84247cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
84366b16edfSTang Chen  */
8444308ce17STony Luck static int __init_memblock memblock_setclr_flag(phys_addr_t base,
8454308ce17STony Luck 				phys_addr_t size, int set, int flag)
84666b16edfSTang Chen {
84766b16edfSTang Chen 	struct memblock_type *type = &memblock.memory;
84866b16edfSTang Chen 	int i, ret, start_rgn, end_rgn;
84966b16edfSTang Chen 
85066b16edfSTang Chen 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
85166b16edfSTang Chen 	if (ret)
85266b16edfSTang Chen 		return ret;
85366b16edfSTang Chen 
854fe145124SMike Rapoport 	for (i = start_rgn; i < end_rgn; i++) {
855fe145124SMike Rapoport 		struct memblock_region *r = &type->regions[i];
856fe145124SMike Rapoport 
8574308ce17STony Luck 		if (set)
858fe145124SMike Rapoport 			r->flags |= flag;
8594308ce17STony Luck 		else
860fe145124SMike Rapoport 			r->flags &= ~flag;
861fe145124SMike Rapoport 	}
86266b16edfSTang Chen 
86366b16edfSTang Chen 	memblock_merge_regions(type);
86466b16edfSTang Chen 	return 0;
86566b16edfSTang Chen }
86666b16edfSTang Chen 
86766b16edfSTang Chen /**
8684308ce17STony Luck  * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
8694308ce17STony Luck  * @base: the base phys addr of the region
8704308ce17STony Luck  * @size: the size of the region
8714308ce17STony Luck  *
87247cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
8734308ce17STony Luck  */
8744308ce17STony Luck int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
8754308ce17STony Luck {
8764308ce17STony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
8774308ce17STony Luck }
8784308ce17STony Luck 
8794308ce17STony Luck /**
88066b16edfSTang Chen  * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
88166b16edfSTang Chen  * @base: the base phys addr of the region
88266b16edfSTang Chen  * @size: the size of the region
88366b16edfSTang Chen  *
88447cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
88566b16edfSTang Chen  */
88666b16edfSTang Chen int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
88766b16edfSTang Chen {
8884308ce17STony Luck 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
88966b16edfSTang Chen }
89066b16edfSTang Chen 
89166b16edfSTang Chen /**
892a3f5bafcSTony Luck  * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
893a3f5bafcSTony Luck  * @base: the base phys addr of the region
894a3f5bafcSTony Luck  * @size: the size of the region
895a3f5bafcSTony Luck  *
89647cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
897a3f5bafcSTony Luck  */
898a3f5bafcSTony Luck int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
899a3f5bafcSTony Luck {
900a3f5bafcSTony Luck 	system_has_some_mirror = true;
901a3f5bafcSTony Luck 
902a3f5bafcSTony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
903a3f5bafcSTony Luck }
904a3f5bafcSTony Luck 
905bf3d3cc5SArd Biesheuvel /**
906bf3d3cc5SArd Biesheuvel  * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
907bf3d3cc5SArd Biesheuvel  * @base: the base phys addr of the region
908bf3d3cc5SArd Biesheuvel  * @size: the size of the region
909bf3d3cc5SArd Biesheuvel  *
91047cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
911bf3d3cc5SArd Biesheuvel  */
912bf3d3cc5SArd Biesheuvel int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
913bf3d3cc5SArd Biesheuvel {
914bf3d3cc5SArd Biesheuvel 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
915bf3d3cc5SArd Biesheuvel }
916a3f5bafcSTony Luck 
917a3f5bafcSTony Luck /**
9184c546b8aSAKASHI Takahiro  * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
9194c546b8aSAKASHI Takahiro  * @base: the base phys addr of the region
9204c546b8aSAKASHI Takahiro  * @size: the size of the region
9214c546b8aSAKASHI Takahiro  *
92247cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
9234c546b8aSAKASHI Takahiro  */
9244c546b8aSAKASHI Takahiro int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
9254c546b8aSAKASHI Takahiro {
9264c546b8aSAKASHI Takahiro 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
9274c546b8aSAKASHI Takahiro }
9284c546b8aSAKASHI Takahiro 
9294c546b8aSAKASHI Takahiro /**
9308e7a7f86SRobin Holt  * __next_reserved_mem_region - next function for for_each_reserved_region()
9318e7a7f86SRobin Holt  * @idx: pointer to u64 loop variable
9328e7a7f86SRobin Holt  * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
9338e7a7f86SRobin Holt  * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
9348e7a7f86SRobin Holt  *
9358e7a7f86SRobin Holt  * Iterate over all reserved memory regions.
9368e7a7f86SRobin Holt  */
9378e7a7f86SRobin Holt void __init_memblock __next_reserved_mem_region(u64 *idx,
9388e7a7f86SRobin Holt 					   phys_addr_t *out_start,
9398e7a7f86SRobin Holt 					   phys_addr_t *out_end)
9408e7a7f86SRobin Holt {
941567d117bSAlexander Kuleshov 	struct memblock_type *type = &memblock.reserved;
9428e7a7f86SRobin Holt 
943cd33a76bSRichard Leitner 	if (*idx < type->cnt) {
944567d117bSAlexander Kuleshov 		struct memblock_region *r = &type->regions[*idx];
9458e7a7f86SRobin Holt 		phys_addr_t base = r->base;
9468e7a7f86SRobin Holt 		phys_addr_t size = r->size;
9478e7a7f86SRobin Holt 
9488e7a7f86SRobin Holt 		if (out_start)
9498e7a7f86SRobin Holt 			*out_start = base;
9508e7a7f86SRobin Holt 		if (out_end)
9518e7a7f86SRobin Holt 			*out_end = base + size - 1;
9528e7a7f86SRobin Holt 
9538e7a7f86SRobin Holt 		*idx += 1;
9548e7a7f86SRobin Holt 		return;
9558e7a7f86SRobin Holt 	}
9568e7a7f86SRobin Holt 
9578e7a7f86SRobin Holt 	/* signal end of iteration */
9588e7a7f86SRobin Holt 	*idx = ULLONG_MAX;
9598e7a7f86SRobin Holt }
9608e7a7f86SRobin Holt 
961c9a688a3SMike Rapoport static bool should_skip_region(struct memblock_region *m, int nid, int flags)
962c9a688a3SMike Rapoport {
963c9a688a3SMike Rapoport 	int m_nid = memblock_get_region_node(m);
964c9a688a3SMike Rapoport 
965c9a688a3SMike Rapoport 	/* only memory regions are associated with nodes, check it */
966c9a688a3SMike Rapoport 	if (nid != NUMA_NO_NODE && nid != m_nid)
967c9a688a3SMike Rapoport 		return true;
968c9a688a3SMike Rapoport 
969c9a688a3SMike Rapoport 	/* skip hotpluggable memory regions if needed */
970c9a688a3SMike Rapoport 	if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
971c9a688a3SMike Rapoport 		return true;
972c9a688a3SMike Rapoport 
973c9a688a3SMike Rapoport 	/* if we want mirror memory skip non-mirror memory regions */
974c9a688a3SMike Rapoport 	if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
975c9a688a3SMike Rapoport 		return true;
976c9a688a3SMike Rapoport 
977c9a688a3SMike Rapoport 	/* skip nomap memory unless we were asked for it explicitly */
978c9a688a3SMike Rapoport 	if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
979c9a688a3SMike Rapoport 		return true;
980c9a688a3SMike Rapoport 
981c9a688a3SMike Rapoport 	return false;
982c9a688a3SMike Rapoport }
983c9a688a3SMike Rapoport 
9848e7a7f86SRobin Holt /**
985a2974133SMike Rapoport  * __next_mem_range - next function for for_each_free_mem_range() etc.
98635fd0808STejun Heo  * @idx: pointer to u64 loop variable
987b1154233SGrygorii Strashko  * @nid: node selector, %NUMA_NO_NODE for all nodes
988fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
989f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
990f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
991dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
992dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
993dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
99435fd0808STejun Heo  *
995f1af9d3aSPhilipp Hachtmann  * Find the first area from *@idx which matches @nid, fill the out
99635fd0808STejun Heo  * parameters, and update *@idx for the next iteration.  The lower 32bit of
997f1af9d3aSPhilipp Hachtmann  * *@idx contains index into type_a and the upper 32bit indexes the
998f1af9d3aSPhilipp Hachtmann  * areas before each region in type_b.	For example, if type_b regions
99935fd0808STejun Heo  * look like the following,
100035fd0808STejun Heo  *
100135fd0808STejun Heo  *	0:[0-16), 1:[32-48), 2:[128-130)
100235fd0808STejun Heo  *
100335fd0808STejun Heo  * The upper 32bit indexes the following regions.
100435fd0808STejun Heo  *
100535fd0808STejun Heo  *	0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
100635fd0808STejun Heo  *
100735fd0808STejun Heo  * As both region arrays are sorted, the function advances the two indices
100835fd0808STejun Heo  * in lockstep and returns each intersection.
100935fd0808STejun Heo  */
1010e1720feeSMike Rapoport void __init_memblock __next_mem_range(u64 *idx, int nid,
1011e1720feeSMike Rapoport 				      enum memblock_flags flags,
1012f1af9d3aSPhilipp Hachtmann 				      struct memblock_type *type_a,
1013f1af9d3aSPhilipp Hachtmann 				      struct memblock_type *type_b,
101435fd0808STejun Heo 				      phys_addr_t *out_start,
101535fd0808STejun Heo 				      phys_addr_t *out_end, int *out_nid)
101635fd0808STejun Heo {
1017f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1018f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1019b1154233SGrygorii Strashko 
1020f1af9d3aSPhilipp Hachtmann 	if (WARN_ONCE(nid == MAX_NUMNODES,
1021f1af9d3aSPhilipp Hachtmann 	"Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1022560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
102335fd0808STejun Heo 
1024f1af9d3aSPhilipp Hachtmann 	for (; idx_a < type_a->cnt; idx_a++) {
1025f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1026f1af9d3aSPhilipp Hachtmann 
102735fd0808STejun Heo 		phys_addr_t m_start = m->base;
102835fd0808STejun Heo 		phys_addr_t m_end = m->base + m->size;
1029f1af9d3aSPhilipp Hachtmann 		int	    m_nid = memblock_get_region_node(m);
103035fd0808STejun Heo 
1031c9a688a3SMike Rapoport 		if (should_skip_region(m, nid, flags))
1032bf3d3cc5SArd Biesheuvel 			continue;
1033bf3d3cc5SArd Biesheuvel 
1034f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1035f1af9d3aSPhilipp Hachtmann 			if (out_start)
1036f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1037f1af9d3aSPhilipp Hachtmann 			if (out_end)
1038f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1039f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1040f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1041f1af9d3aSPhilipp Hachtmann 			idx_a++;
1042f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1043f1af9d3aSPhilipp Hachtmann 			return;
1044f1af9d3aSPhilipp Hachtmann 		}
104535fd0808STejun Heo 
1046f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1047f1af9d3aSPhilipp Hachtmann 		for (; idx_b < type_b->cnt + 1; idx_b++) {
1048f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1049f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1050f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1051f1af9d3aSPhilipp Hachtmann 
1052f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1053f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1054f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
10551c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1056f1af9d3aSPhilipp Hachtmann 
1057f1af9d3aSPhilipp Hachtmann 			/*
1058f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1059f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1060f1af9d3aSPhilipp Hachtmann 			 */
106135fd0808STejun Heo 			if (r_start >= m_end)
106235fd0808STejun Heo 				break;
106335fd0808STejun Heo 			/* if the two regions intersect, we're done */
106435fd0808STejun Heo 			if (m_start < r_end) {
106535fd0808STejun Heo 				if (out_start)
1066f1af9d3aSPhilipp Hachtmann 					*out_start =
1067f1af9d3aSPhilipp Hachtmann 						max(m_start, r_start);
106835fd0808STejun Heo 				if (out_end)
106935fd0808STejun Heo 					*out_end = min(m_end, r_end);
107035fd0808STejun Heo 				if (out_nid)
1071f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
107235fd0808STejun Heo 				/*
1073f1af9d3aSPhilipp Hachtmann 				 * The region which ends first is
1074f1af9d3aSPhilipp Hachtmann 				 * advanced for the next iteration.
107535fd0808STejun Heo 				 */
107635fd0808STejun Heo 				if (m_end <= r_end)
1077f1af9d3aSPhilipp Hachtmann 					idx_a++;
107835fd0808STejun Heo 				else
1079f1af9d3aSPhilipp Hachtmann 					idx_b++;
1080f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
108135fd0808STejun Heo 				return;
108235fd0808STejun Heo 			}
108335fd0808STejun Heo 		}
108435fd0808STejun Heo 	}
108535fd0808STejun Heo 
108635fd0808STejun Heo 	/* signal end of iteration */
108735fd0808STejun Heo 	*idx = ULLONG_MAX;
108835fd0808STejun Heo }
108935fd0808STejun Heo 
10907bd0b0f0STejun Heo /**
1091f1af9d3aSPhilipp Hachtmann  * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1092f1af9d3aSPhilipp Hachtmann  *
10937bd0b0f0STejun Heo  * @idx: pointer to u64 loop variable
1094ad5ea8cdSAlexander Kuleshov  * @nid: node selector, %NUMA_NO_NODE for all nodes
1095fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
1096f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
1097f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
1098dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1099dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1100dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
11017bd0b0f0STejun Heo  *
110247cec443SMike Rapoport  * Finds the next range from type_a which is not marked as unsuitable
110347cec443SMike Rapoport  * in type_b.
110447cec443SMike Rapoport  *
1105f1af9d3aSPhilipp Hachtmann  * Reverse of __next_mem_range().
11067bd0b0f0STejun Heo  */
1107e1720feeSMike Rapoport void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1108e1720feeSMike Rapoport 					  enum memblock_flags flags,
1109f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_a,
1110f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_b,
11117bd0b0f0STejun Heo 					  phys_addr_t *out_start,
11127bd0b0f0STejun Heo 					  phys_addr_t *out_end, int *out_nid)
11137bd0b0f0STejun Heo {
1114f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1115f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1116b1154233SGrygorii Strashko 
1117560dca27SGrygorii Strashko 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1118560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
11197bd0b0f0STejun Heo 
11207bd0b0f0STejun Heo 	if (*idx == (u64)ULLONG_MAX) {
1121f1af9d3aSPhilipp Hachtmann 		idx_a = type_a->cnt - 1;
1122e47608abSzijun_hu 		if (type_b != NULL)
1123f1af9d3aSPhilipp Hachtmann 			idx_b = type_b->cnt;
1124e47608abSzijun_hu 		else
1125e47608abSzijun_hu 			idx_b = 0;
11267bd0b0f0STejun Heo 	}
11277bd0b0f0STejun Heo 
1128f1af9d3aSPhilipp Hachtmann 	for (; idx_a >= 0; idx_a--) {
1129f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1130f1af9d3aSPhilipp Hachtmann 
11317bd0b0f0STejun Heo 		phys_addr_t m_start = m->base;
11327bd0b0f0STejun Heo 		phys_addr_t m_end = m->base + m->size;
1133f1af9d3aSPhilipp Hachtmann 		int m_nid = memblock_get_region_node(m);
11347bd0b0f0STejun Heo 
1135c9a688a3SMike Rapoport 		if (should_skip_region(m, nid, flags))
1136bf3d3cc5SArd Biesheuvel 			continue;
1137bf3d3cc5SArd Biesheuvel 
1138f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1139f1af9d3aSPhilipp Hachtmann 			if (out_start)
1140f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1141f1af9d3aSPhilipp Hachtmann 			if (out_end)
1142f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1143f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1144f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1145fb399b48Szijun_hu 			idx_a--;
1146f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1147f1af9d3aSPhilipp Hachtmann 			return;
1148f1af9d3aSPhilipp Hachtmann 		}
11497bd0b0f0STejun Heo 
1150f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1151f1af9d3aSPhilipp Hachtmann 		for (; idx_b >= 0; idx_b--) {
1152f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1153f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1154f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1155f1af9d3aSPhilipp Hachtmann 
1156f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1157f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1158f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
11591c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1160f1af9d3aSPhilipp Hachtmann 			/*
1161f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1162f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1163f1af9d3aSPhilipp Hachtmann 			 */
1164f1af9d3aSPhilipp Hachtmann 
11657bd0b0f0STejun Heo 			if (r_end <= m_start)
11667bd0b0f0STejun Heo 				break;
11677bd0b0f0STejun Heo 			/* if the two regions intersect, we're done */
11687bd0b0f0STejun Heo 			if (m_end > r_start) {
11697bd0b0f0STejun Heo 				if (out_start)
11707bd0b0f0STejun Heo 					*out_start = max(m_start, r_start);
11717bd0b0f0STejun Heo 				if (out_end)
11727bd0b0f0STejun Heo 					*out_end = min(m_end, r_end);
11737bd0b0f0STejun Heo 				if (out_nid)
1174f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
11757bd0b0f0STejun Heo 				if (m_start >= r_start)
1176f1af9d3aSPhilipp Hachtmann 					idx_a--;
11777bd0b0f0STejun Heo 				else
1178f1af9d3aSPhilipp Hachtmann 					idx_b--;
1179f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
11807bd0b0f0STejun Heo 				return;
11817bd0b0f0STejun Heo 			}
11827bd0b0f0STejun Heo 		}
11837bd0b0f0STejun Heo 	}
1184f1af9d3aSPhilipp Hachtmann 	/* signal end of iteration */
11857bd0b0f0STejun Heo 	*idx = ULLONG_MAX;
11867bd0b0f0STejun Heo }
11877bd0b0f0STejun Heo 
11887c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
11897c0caeb8STejun Heo /*
119045e79815SChen Chang  * Common iterator interface used to define for_each_mem_pfn_range().
11917c0caeb8STejun Heo  */
11927c0caeb8STejun Heo void __init_memblock __next_mem_pfn_range(int *idx, int nid,
11937c0caeb8STejun Heo 				unsigned long *out_start_pfn,
11947c0caeb8STejun Heo 				unsigned long *out_end_pfn, int *out_nid)
11957c0caeb8STejun Heo {
11967c0caeb8STejun Heo 	struct memblock_type *type = &memblock.memory;
11977c0caeb8STejun Heo 	struct memblock_region *r;
11987c0caeb8STejun Heo 
11997c0caeb8STejun Heo 	while (++*idx < type->cnt) {
12007c0caeb8STejun Heo 		r = &type->regions[*idx];
12017c0caeb8STejun Heo 
12027c0caeb8STejun Heo 		if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
12037c0caeb8STejun Heo 			continue;
12047c0caeb8STejun Heo 		if (nid == MAX_NUMNODES || nid == r->nid)
12057c0caeb8STejun Heo 			break;
12067c0caeb8STejun Heo 	}
12077c0caeb8STejun Heo 	if (*idx >= type->cnt) {
12087c0caeb8STejun Heo 		*idx = -1;
12097c0caeb8STejun Heo 		return;
12107c0caeb8STejun Heo 	}
12117c0caeb8STejun Heo 
12127c0caeb8STejun Heo 	if (out_start_pfn)
12137c0caeb8STejun Heo 		*out_start_pfn = PFN_UP(r->base);
12147c0caeb8STejun Heo 	if (out_end_pfn)
12157c0caeb8STejun Heo 		*out_end_pfn = PFN_DOWN(r->base + r->size);
12167c0caeb8STejun Heo 	if (out_nid)
12177c0caeb8STejun Heo 		*out_nid = r->nid;
12187c0caeb8STejun Heo }
12197c0caeb8STejun Heo 
12207c0caeb8STejun Heo /**
12217c0caeb8STejun Heo  * memblock_set_node - set node ID on memblock regions
12227c0caeb8STejun Heo  * @base: base of area to set node ID for
12237c0caeb8STejun Heo  * @size: size of area to set node ID for
1224e7e8de59STang Chen  * @type: memblock type to set node ID for
12257c0caeb8STejun Heo  * @nid: node ID to set
12267c0caeb8STejun Heo  *
1227e7e8de59STang Chen  * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
12287c0caeb8STejun Heo  * Regions which cross the area boundaries are split as necessary.
12297c0caeb8STejun Heo  *
123047cec443SMike Rapoport  * Return:
12317c0caeb8STejun Heo  * 0 on success, -errno on failure.
12327c0caeb8STejun Heo  */
12337c0caeb8STejun Heo int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1234e7e8de59STang Chen 				      struct memblock_type *type, int nid)
12357c0caeb8STejun Heo {
12366a9ceb31STejun Heo 	int start_rgn, end_rgn;
12376a9ceb31STejun Heo 	int i, ret;
12387c0caeb8STejun Heo 
12396a9ceb31STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
12406a9ceb31STejun Heo 	if (ret)
12416a9ceb31STejun Heo 		return ret;
12427c0caeb8STejun Heo 
12436a9ceb31STejun Heo 	for (i = start_rgn; i < end_rgn; i++)
1244e9d24ad3SWanpeng Li 		memblock_set_region_node(&type->regions[i], nid);
12457c0caeb8STejun Heo 
12467c0caeb8STejun Heo 	memblock_merge_regions(type);
12477c0caeb8STejun Heo 	return 0;
12487c0caeb8STejun Heo }
12497c0caeb8STejun Heo #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1250837566e7SAlexander Duyck #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1251837566e7SAlexander Duyck /**
1252837566e7SAlexander Duyck  * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
1253837566e7SAlexander Duyck  *
1254837566e7SAlexander Duyck  * @idx: pointer to u64 loop variable
1255837566e7SAlexander Duyck  * @zone: zone in which all of the memory blocks reside
1256837566e7SAlexander Duyck  * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
1257837566e7SAlexander Duyck  * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
1258837566e7SAlexander Duyck  *
1259837566e7SAlexander Duyck  * This function is meant to be a zone/pfn specific wrapper for the
1260837566e7SAlexander Duyck  * for_each_mem_range type iterators. Specifically they are used in the
1261837566e7SAlexander Duyck  * deferred memory init routines and as such we were duplicating much of
1262837566e7SAlexander Duyck  * this logic throughout the code. So instead of having it in multiple
1263837566e7SAlexander Duyck  * locations it seemed like it would make more sense to centralize this to
1264837566e7SAlexander Duyck  * one new iterator that does everything they need.
1265837566e7SAlexander Duyck  */
1266837566e7SAlexander Duyck void __init_memblock
1267837566e7SAlexander Duyck __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
1268837566e7SAlexander Duyck 			     unsigned long *out_spfn, unsigned long *out_epfn)
1269837566e7SAlexander Duyck {
1270837566e7SAlexander Duyck 	int zone_nid = zone_to_nid(zone);
1271837566e7SAlexander Duyck 	phys_addr_t spa, epa;
1272837566e7SAlexander Duyck 	int nid;
1273837566e7SAlexander Duyck 
1274837566e7SAlexander Duyck 	__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1275837566e7SAlexander Duyck 			 &memblock.memory, &memblock.reserved,
1276837566e7SAlexander Duyck 			 &spa, &epa, &nid);
1277837566e7SAlexander Duyck 
1278837566e7SAlexander Duyck 	while (*idx != U64_MAX) {
1279837566e7SAlexander Duyck 		unsigned long epfn = PFN_DOWN(epa);
1280837566e7SAlexander Duyck 		unsigned long spfn = PFN_UP(spa);
1281837566e7SAlexander Duyck 
1282837566e7SAlexander Duyck 		/*
1283837566e7SAlexander Duyck 		 * Verify the end is at least past the start of the zone and
1284837566e7SAlexander Duyck 		 * that we have at least one PFN to initialize.
1285837566e7SAlexander Duyck 		 */
1286837566e7SAlexander Duyck 		if (zone->zone_start_pfn < epfn && spfn < epfn) {
1287837566e7SAlexander Duyck 			/* if we went too far just stop searching */
1288837566e7SAlexander Duyck 			if (zone_end_pfn(zone) <= spfn) {
1289837566e7SAlexander Duyck 				*idx = U64_MAX;
1290837566e7SAlexander Duyck 				break;
1291837566e7SAlexander Duyck 			}
1292837566e7SAlexander Duyck 
1293837566e7SAlexander Duyck 			if (out_spfn)
1294837566e7SAlexander Duyck 				*out_spfn = max(zone->zone_start_pfn, spfn);
1295837566e7SAlexander Duyck 			if (out_epfn)
1296837566e7SAlexander Duyck 				*out_epfn = min(zone_end_pfn(zone), epfn);
1297837566e7SAlexander Duyck 
1298837566e7SAlexander Duyck 			return;
1299837566e7SAlexander Duyck 		}
1300837566e7SAlexander Duyck 
1301837566e7SAlexander Duyck 		__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1302837566e7SAlexander Duyck 				 &memblock.memory, &memblock.reserved,
1303837566e7SAlexander Duyck 				 &spa, &epa, &nid);
1304837566e7SAlexander Duyck 	}
1305837566e7SAlexander Duyck 
1306837566e7SAlexander Duyck 	/* signal end of iteration */
1307837566e7SAlexander Duyck 	if (out_spfn)
1308837566e7SAlexander Duyck 		*out_spfn = ULONG_MAX;
1309837566e7SAlexander Duyck 	if (out_epfn)
1310837566e7SAlexander Duyck 		*out_epfn = 0;
1311837566e7SAlexander Duyck }
1312837566e7SAlexander Duyck 
1313837566e7SAlexander Duyck #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
13147c0caeb8STejun Heo 
131592d12f95SMike Rapoport /**
131692d12f95SMike Rapoport  * memblock_alloc_range_nid - allocate boot memory block
131792d12f95SMike Rapoport  * @size: size of memory block to be allocated in bytes
131892d12f95SMike Rapoport  * @align: alignment of the region and block's size
131992d12f95SMike Rapoport  * @start: the lower bound of the memory region to allocate (phys address)
132092d12f95SMike Rapoport  * @end: the upper bound of the memory region to allocate (phys address)
132192d12f95SMike Rapoport  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
132292d12f95SMike Rapoport  *
132392d12f95SMike Rapoport  * The allocation is performed from memory region limited by
1324*95830666SCao jin  * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE.
132592d12f95SMike Rapoport  *
132692d12f95SMike Rapoport  * If the specified node can not hold the requested memory the
132792d12f95SMike Rapoport  * allocation falls back to any node in the system
132892d12f95SMike Rapoport  *
132992d12f95SMike Rapoport  * For systems with memory mirroring, the allocation is attempted first
133092d12f95SMike Rapoport  * from the regions with mirroring enabled and then retried from any
133192d12f95SMike Rapoport  * memory region.
133292d12f95SMike Rapoport  *
133392d12f95SMike Rapoport  * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for
133492d12f95SMike Rapoport  * allocated boot memory block, so that it is never reported as leaks.
133592d12f95SMike Rapoport  *
133692d12f95SMike Rapoport  * Return:
133792d12f95SMike Rapoport  * Physical address of allocated memory block on success, %0 on failure.
133892d12f95SMike Rapoport  */
13392bfc2862SAkinobu Mita static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
13402bfc2862SAkinobu Mita 					phys_addr_t align, phys_addr_t start,
134192d12f95SMike Rapoport 					phys_addr_t end, int nid)
134295f72d1eSYinghai Lu {
134392d12f95SMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
13446ed311b2SBenjamin Herrenschmidt 	phys_addr_t found;
134595f72d1eSYinghai Lu 
134692d12f95SMike Rapoport 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
134792d12f95SMike Rapoport 		nid = NUMA_NO_NODE;
134892d12f95SMike Rapoport 
13492f770806SMike Rapoport 	if (!align) {
13502f770806SMike Rapoport 		/* Can't use WARNs this early in boot on powerpc */
13512f770806SMike Rapoport 		dump_stack();
13522f770806SMike Rapoport 		align = SMP_CACHE_BYTES;
13532f770806SMike Rapoport 	}
13542f770806SMike Rapoport 
135592d12f95SMike Rapoport again:
1356fc6daaf9STony Luck 	found = memblock_find_in_range_node(size, align, start, end, nid,
1357fc6daaf9STony Luck 					    flags);
135892d12f95SMike Rapoport 	if (found && !memblock_reserve(found, size))
135992d12f95SMike Rapoport 		goto done;
136092d12f95SMike Rapoport 
136192d12f95SMike Rapoport 	if (nid != NUMA_NO_NODE) {
136292d12f95SMike Rapoport 		found = memblock_find_in_range_node(size, align, start,
136392d12f95SMike Rapoport 						    end, NUMA_NO_NODE,
136492d12f95SMike Rapoport 						    flags);
136592d12f95SMike Rapoport 		if (found && !memblock_reserve(found, size))
136692d12f95SMike Rapoport 			goto done;
136792d12f95SMike Rapoport 	}
136892d12f95SMike Rapoport 
136992d12f95SMike Rapoport 	if (flags & MEMBLOCK_MIRROR) {
137092d12f95SMike Rapoport 		flags &= ~MEMBLOCK_MIRROR;
137192d12f95SMike Rapoport 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
137292d12f95SMike Rapoport 			&size);
137392d12f95SMike Rapoport 		goto again;
137492d12f95SMike Rapoport 	}
137592d12f95SMike Rapoport 
137692d12f95SMike Rapoport 	return 0;
137792d12f95SMike Rapoport 
137892d12f95SMike Rapoport done:
137992d12f95SMike Rapoport 	/* Skip kmemleak for kasan_init() due to high volume. */
138092d12f95SMike Rapoport 	if (end != MEMBLOCK_ALLOC_KASAN)
1381aedf95eaSCatalin Marinas 		/*
138292d12f95SMike Rapoport 		 * The min_count is set to 0 so that memblock allocated
138392d12f95SMike Rapoport 		 * blocks are never reported as leaks. This is because many
138492d12f95SMike Rapoport 		 * of these blocks are only referred via the physical
138592d12f95SMike Rapoport 		 * address which is not looked up by kmemleak.
1386aedf95eaSCatalin Marinas 		 */
13879099daedSCatalin Marinas 		kmemleak_alloc_phys(found, size, 0, 0);
138892d12f95SMike Rapoport 
13896ed311b2SBenjamin Herrenschmidt 	return found;
1390aedf95eaSCatalin Marinas }
139195f72d1eSYinghai Lu 
1392a2974133SMike Rapoport /**
1393a2974133SMike Rapoport  * memblock_phys_alloc_range - allocate a memory block inside specified range
1394a2974133SMike Rapoport  * @size: size of memory block to be allocated in bytes
1395a2974133SMike Rapoport  * @align: alignment of the region and block's size
1396a2974133SMike Rapoport  * @start: the lower bound of the memory region to allocate (physical address)
1397a2974133SMike Rapoport  * @end: the upper bound of the memory region to allocate (physical address)
1398a2974133SMike Rapoport  *
1399a2974133SMike Rapoport  * Allocate @size bytes in the between @start and @end.
1400a2974133SMike Rapoport  *
1401a2974133SMike Rapoport  * Return: physical address of the allocated memory block on success,
1402a2974133SMike Rapoport  * %0 on failure.
1403a2974133SMike Rapoport  */
14048a770c2aSMike Rapoport phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
14058a770c2aSMike Rapoport 					     phys_addr_t align,
14068a770c2aSMike Rapoport 					     phys_addr_t start,
14078a770c2aSMike Rapoport 					     phys_addr_t end)
14082bfc2862SAkinobu Mita {
140992d12f95SMike Rapoport 	return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE);
14107bd0b0f0STejun Heo }
14117bd0b0f0STejun Heo 
1412a2974133SMike Rapoport /**
1413a2974133SMike Rapoport  * memblock_phys_alloc_try_nid - allocate a memory block from specified MUMA node
1414a2974133SMike Rapoport  * @size: size of memory block to be allocated in bytes
1415a2974133SMike Rapoport  * @align: alignment of the region and block's size
1416a2974133SMike Rapoport  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1417a2974133SMike Rapoport  *
1418a2974133SMike Rapoport  * Allocates memory block from the specified NUMA node. If the node
1419a2974133SMike Rapoport  * has no available memory, attempts to allocated from any node in the
1420a2974133SMike Rapoport  * system.
1421a2974133SMike Rapoport  *
1422a2974133SMike Rapoport  * Return: physical address of the allocated memory block on success,
1423a2974133SMike Rapoport  * %0 on failure.
1424a2974133SMike Rapoport  */
14259a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
14269d1e2492SBenjamin Herrenschmidt {
142733755574SMike Rapoport 	return memblock_alloc_range_nid(size, align, 0,
142892d12f95SMike Rapoport 					MEMBLOCK_ALLOC_ACCESSIBLE, nid);
142995f72d1eSYinghai Lu }
143095f72d1eSYinghai Lu 
143126f09e9bSSantosh Shilimkar /**
1432eb31d559SMike Rapoport  * memblock_alloc_internal - allocate boot memory block
143326f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
143426f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
143526f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region to allocate (phys address)
143626f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region to allocate (phys address)
143726f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
143826f09e9bSSantosh Shilimkar  *
143992d12f95SMike Rapoport  * Allocates memory block using memblock_alloc_range_nid() and
144092d12f95SMike Rapoport  * converts the returned physical address to virtual.
144192d12f95SMike Rapoport  *
144226f09e9bSSantosh Shilimkar  * The @min_addr limit is dropped if it can not be satisfied and the allocation
144392d12f95SMike Rapoport  * will fall back to memory below @min_addr. Other constraints, such
144492d12f95SMike Rapoport  * as node and mirrored memory will be handled again in
144592d12f95SMike Rapoport  * memblock_alloc_range_nid().
144626f09e9bSSantosh Shilimkar  *
144747cec443SMike Rapoport  * Return:
144826f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
144926f09e9bSSantosh Shilimkar  */
1450eb31d559SMike Rapoport static void * __init memblock_alloc_internal(
145126f09e9bSSantosh Shilimkar 				phys_addr_t size, phys_addr_t align,
145226f09e9bSSantosh Shilimkar 				phys_addr_t min_addr, phys_addr_t max_addr,
145326f09e9bSSantosh Shilimkar 				int nid)
145426f09e9bSSantosh Shilimkar {
145526f09e9bSSantosh Shilimkar 	phys_addr_t alloc;
145626f09e9bSSantosh Shilimkar 
145726f09e9bSSantosh Shilimkar 	/*
145826f09e9bSSantosh Shilimkar 	 * Detect any accidental use of these APIs after slab is ready, as at
145926f09e9bSSantosh Shilimkar 	 * this moment memblock may be deinitialized already and its
1460c6ffc5caSMike Rapoport 	 * internal data may be destroyed (after execution of memblock_free_all)
146126f09e9bSSantosh Shilimkar 	 */
146226f09e9bSSantosh Shilimkar 	if (WARN_ON_ONCE(slab_is_available()))
146326f09e9bSSantosh Shilimkar 		return kzalloc_node(size, GFP_NOWAIT, nid);
146426f09e9bSSantosh Shilimkar 
1465f3057ad7SMike Rapoport 	if (max_addr > memblock.current_limit)
1466f3057ad7SMike Rapoport 		max_addr = memblock.current_limit;
1467f3057ad7SMike Rapoport 
146892d12f95SMike Rapoport 	alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid);
14692f770806SMike Rapoport 
147092d12f95SMike Rapoport 	/* retry allocation without lower limit */
147192d12f95SMike Rapoport 	if (!alloc && min_addr)
147292d12f95SMike Rapoport 		alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid);
147326f09e9bSSantosh Shilimkar 
147492d12f95SMike Rapoport 	if (!alloc)
1475a3f5bafcSTony Luck 		return NULL;
147626f09e9bSSantosh Shilimkar 
147792d12f95SMike Rapoport 	return phys_to_virt(alloc);
147826f09e9bSSantosh Shilimkar }
147926f09e9bSSantosh Shilimkar 
148026f09e9bSSantosh Shilimkar /**
1481eb31d559SMike Rapoport  * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
1482ea1f5f37SPavel Tatashin  * memory and without panicking
1483ea1f5f37SPavel Tatashin  * @size: size of memory block to be allocated in bytes
1484ea1f5f37SPavel Tatashin  * @align: alignment of the region and block's size
1485ea1f5f37SPavel Tatashin  * @min_addr: the lower bound of the memory region from where the allocation
1486ea1f5f37SPavel Tatashin  *	  is preferred (phys address)
1487ea1f5f37SPavel Tatashin  * @max_addr: the upper bound of the memory region from where the allocation
148897ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1489ea1f5f37SPavel Tatashin  *	      allocate only from memory limited by memblock.current_limit value
1490ea1f5f37SPavel Tatashin  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1491ea1f5f37SPavel Tatashin  *
1492ea1f5f37SPavel Tatashin  * Public function, provides additional debug information (including caller
1493ea1f5f37SPavel Tatashin  * info), if enabled. Does not zero allocated memory, does not panic if request
1494ea1f5f37SPavel Tatashin  * cannot be satisfied.
1495ea1f5f37SPavel Tatashin  *
149647cec443SMike Rapoport  * Return:
1497ea1f5f37SPavel Tatashin  * Virtual address of allocated memory block on success, NULL on failure.
1498ea1f5f37SPavel Tatashin  */
1499eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_raw(
1500ea1f5f37SPavel Tatashin 			phys_addr_t size, phys_addr_t align,
1501ea1f5f37SPavel Tatashin 			phys_addr_t min_addr, phys_addr_t max_addr,
1502ea1f5f37SPavel Tatashin 			int nid)
1503ea1f5f37SPavel Tatashin {
1504ea1f5f37SPavel Tatashin 	void *ptr;
1505ea1f5f37SPavel Tatashin 
1506d75f773cSSakari Ailus 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1507a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1508a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1509ea1f5f37SPavel Tatashin 
1510eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
1511ea1f5f37SPavel Tatashin 					   min_addr, max_addr, nid);
1512ea1f5f37SPavel Tatashin 	if (ptr && size > 0)
1513f682a97aSAlexander Duyck 		page_init_poison(ptr, size);
1514f682a97aSAlexander Duyck 
1515ea1f5f37SPavel Tatashin 	return ptr;
1516ea1f5f37SPavel Tatashin }
1517ea1f5f37SPavel Tatashin 
1518ea1f5f37SPavel Tatashin /**
1519c0dbe825SMike Rapoport  * memblock_alloc_try_nid - allocate boot memory block
152026f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
152126f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
152226f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region from where the allocation
152326f09e9bSSantosh Shilimkar  *	  is preferred (phys address)
152426f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region from where the allocation
152597ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
152626f09e9bSSantosh Shilimkar  *	      allocate only from memory limited by memblock.current_limit value
152726f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
152826f09e9bSSantosh Shilimkar  *
1529c0dbe825SMike Rapoport  * Public function, provides additional debug information (including caller
1530c0dbe825SMike Rapoport  * info), if enabled. This function zeroes the allocated memory.
153126f09e9bSSantosh Shilimkar  *
153247cec443SMike Rapoport  * Return:
153326f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
153426f09e9bSSantosh Shilimkar  */
1535eb31d559SMike Rapoport void * __init memblock_alloc_try_nid(
153626f09e9bSSantosh Shilimkar 			phys_addr_t size, phys_addr_t align,
153726f09e9bSSantosh Shilimkar 			phys_addr_t min_addr, phys_addr_t max_addr,
153826f09e9bSSantosh Shilimkar 			int nid)
153926f09e9bSSantosh Shilimkar {
154026f09e9bSSantosh Shilimkar 	void *ptr;
154126f09e9bSSantosh Shilimkar 
1542d75f773cSSakari Ailus 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1543a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1544a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1545eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
154626f09e9bSSantosh Shilimkar 					   min_addr, max_addr, nid);
1547c0dbe825SMike Rapoport 	if (ptr)
1548ea1f5f37SPavel Tatashin 		memset(ptr, 0, size);
154926f09e9bSSantosh Shilimkar 
1550c0dbe825SMike Rapoport 	return ptr;
155126f09e9bSSantosh Shilimkar }
155226f09e9bSSantosh Shilimkar 
155326f09e9bSSantosh Shilimkar /**
1554a2974133SMike Rapoport  * __memblock_free_late - free pages directly to buddy allocator
155548a833ccSMike Rapoport  * @base: phys starting address of the  boot memory block
155626f09e9bSSantosh Shilimkar  * @size: size of the boot memory block in bytes
155726f09e9bSSantosh Shilimkar  *
1558a2974133SMike Rapoport  * This is only useful when the memblock allocator has already been torn
155926f09e9bSSantosh Shilimkar  * down, but we are still initializing the system.  Pages are released directly
1560a2974133SMike Rapoport  * to the buddy allocator.
156126f09e9bSSantosh Shilimkar  */
156226f09e9bSSantosh Shilimkar void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
156326f09e9bSSantosh Shilimkar {
1564a36aab89SMike Rapoport 	phys_addr_t cursor, end;
156526f09e9bSSantosh Shilimkar 
1566a36aab89SMike Rapoport 	end = base + size - 1;
1567d75f773cSSakari Ailus 	memblock_dbg("%s: [%pa-%pa] %pS\n",
1568a36aab89SMike Rapoport 		     __func__, &base, &end, (void *)_RET_IP_);
15699099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
157026f09e9bSSantosh Shilimkar 	cursor = PFN_UP(base);
157126f09e9bSSantosh Shilimkar 	end = PFN_DOWN(base + size);
157226f09e9bSSantosh Shilimkar 
157326f09e9bSSantosh Shilimkar 	for (; cursor < end; cursor++) {
15747c2ee349SMike Rapoport 		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
1575ca79b0c2SArun KS 		totalram_pages_inc();
157626f09e9bSSantosh Shilimkar 	}
157726f09e9bSSantosh Shilimkar }
15789d1e2492SBenjamin Herrenschmidt 
15799d1e2492SBenjamin Herrenschmidt /*
15809d1e2492SBenjamin Herrenschmidt  * Remaining API functions
15819d1e2492SBenjamin Herrenschmidt  */
15829d1e2492SBenjamin Herrenschmidt 
15831f1ffb8aSDavid Gibson phys_addr_t __init_memblock memblock_phys_mem_size(void)
158495f72d1eSYinghai Lu {
15851440c4e2STejun Heo 	return memblock.memory.total_size;
158695f72d1eSYinghai Lu }
158795f72d1eSYinghai Lu 
15888907de5dSSrikar Dronamraju phys_addr_t __init_memblock memblock_reserved_size(void)
15898907de5dSSrikar Dronamraju {
15908907de5dSSrikar Dronamraju 	return memblock.reserved.total_size;
15918907de5dSSrikar Dronamraju }
15928907de5dSSrikar Dronamraju 
1593595ad9afSYinghai Lu phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1594595ad9afSYinghai Lu {
1595595ad9afSYinghai Lu 	unsigned long pages = 0;
1596595ad9afSYinghai Lu 	struct memblock_region *r;
1597595ad9afSYinghai Lu 	unsigned long start_pfn, end_pfn;
1598595ad9afSYinghai Lu 
1599595ad9afSYinghai Lu 	for_each_memblock(memory, r) {
1600595ad9afSYinghai Lu 		start_pfn = memblock_region_memory_base_pfn(r);
1601595ad9afSYinghai Lu 		end_pfn = memblock_region_memory_end_pfn(r);
1602595ad9afSYinghai Lu 		start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1603595ad9afSYinghai Lu 		end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1604595ad9afSYinghai Lu 		pages += end_pfn - start_pfn;
1605595ad9afSYinghai Lu 	}
1606595ad9afSYinghai Lu 
160716763230SFabian Frederick 	return PFN_PHYS(pages);
1608595ad9afSYinghai Lu }
1609595ad9afSYinghai Lu 
16100a93ebefSSam Ravnborg /* lowest address */
16110a93ebefSSam Ravnborg phys_addr_t __init_memblock memblock_start_of_DRAM(void)
16120a93ebefSSam Ravnborg {
16130a93ebefSSam Ravnborg 	return memblock.memory.regions[0].base;
16140a93ebefSSam Ravnborg }
16150a93ebefSSam Ravnborg 
161610d06439SYinghai Lu phys_addr_t __init_memblock memblock_end_of_DRAM(void)
161795f72d1eSYinghai Lu {
161895f72d1eSYinghai Lu 	int idx = memblock.memory.cnt - 1;
161995f72d1eSYinghai Lu 
1620e3239ff9SBenjamin Herrenschmidt 	return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
162195f72d1eSYinghai Lu }
162295f72d1eSYinghai Lu 
1623a571d4ebSDennis Chen static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
162495f72d1eSYinghai Lu {
16251c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1626136199f0SEmil Medve 	struct memblock_region *r;
162795f72d1eSYinghai Lu 
1628a571d4ebSDennis Chen 	/*
1629a571d4ebSDennis Chen 	 * translate the memory @limit size into the max address within one of
1630a571d4ebSDennis Chen 	 * the memory memblock regions, if the @limit exceeds the total size
16311c4bc43dSStefan Agner 	 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
1632a571d4ebSDennis Chen 	 */
1633136199f0SEmil Medve 	for_each_memblock(memory, r) {
1634c0ce8fefSTejun Heo 		if (limit <= r->size) {
1635c0ce8fefSTejun Heo 			max_addr = r->base + limit;
163695f72d1eSYinghai Lu 			break;
163795f72d1eSYinghai Lu 		}
1638c0ce8fefSTejun Heo 		limit -= r->size;
163995f72d1eSYinghai Lu 	}
1640c0ce8fefSTejun Heo 
1641a571d4ebSDennis Chen 	return max_addr;
1642a571d4ebSDennis Chen }
1643a571d4ebSDennis Chen 
1644a571d4ebSDennis Chen void __init memblock_enforce_memory_limit(phys_addr_t limit)
1645a571d4ebSDennis Chen {
16461c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1647a571d4ebSDennis Chen 
1648a571d4ebSDennis Chen 	if (!limit)
1649a571d4ebSDennis Chen 		return;
1650a571d4ebSDennis Chen 
1651a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1652a571d4ebSDennis Chen 
1653a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
16541c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1655a571d4ebSDennis Chen 		return;
1656a571d4ebSDennis Chen 
1657c0ce8fefSTejun Heo 	/* truncate both memory and reserved regions */
1658f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.memory, max_addr,
16591c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
1660f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.reserved, max_addr,
16611c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
166295f72d1eSYinghai Lu }
166395f72d1eSYinghai Lu 
1664c9ca9b4eSAKASHI Takahiro void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1665c9ca9b4eSAKASHI Takahiro {
1666c9ca9b4eSAKASHI Takahiro 	int start_rgn, end_rgn;
1667c9ca9b4eSAKASHI Takahiro 	int i, ret;
1668c9ca9b4eSAKASHI Takahiro 
1669c9ca9b4eSAKASHI Takahiro 	if (!size)
1670c9ca9b4eSAKASHI Takahiro 		return;
1671c9ca9b4eSAKASHI Takahiro 
1672c9ca9b4eSAKASHI Takahiro 	ret = memblock_isolate_range(&memblock.memory, base, size,
1673c9ca9b4eSAKASHI Takahiro 						&start_rgn, &end_rgn);
1674c9ca9b4eSAKASHI Takahiro 	if (ret)
1675c9ca9b4eSAKASHI Takahiro 		return;
1676c9ca9b4eSAKASHI Takahiro 
1677c9ca9b4eSAKASHI Takahiro 	/* remove all the MAP regions */
1678c9ca9b4eSAKASHI Takahiro 	for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1679c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1680c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1681c9ca9b4eSAKASHI Takahiro 
1682c9ca9b4eSAKASHI Takahiro 	for (i = start_rgn - 1; i >= 0; i--)
1683c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1684c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1685c9ca9b4eSAKASHI Takahiro 
1686c9ca9b4eSAKASHI Takahiro 	/* truncate the reserved regions */
1687c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved, 0, base);
1688c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved,
16891c4bc43dSStefan Agner 			base + size, PHYS_ADDR_MAX);
1690c9ca9b4eSAKASHI Takahiro }
1691c9ca9b4eSAKASHI Takahiro 
1692a571d4ebSDennis Chen void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1693a571d4ebSDennis Chen {
1694a571d4ebSDennis Chen 	phys_addr_t max_addr;
1695a571d4ebSDennis Chen 
1696a571d4ebSDennis Chen 	if (!limit)
1697a571d4ebSDennis Chen 		return;
1698a571d4ebSDennis Chen 
1699a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1700a571d4ebSDennis Chen 
1701a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
17021c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1703a571d4ebSDennis Chen 		return;
1704a571d4ebSDennis Chen 
1705c9ca9b4eSAKASHI Takahiro 	memblock_cap_memory_range(0, max_addr);
1706a571d4ebSDennis Chen }
1707a571d4ebSDennis Chen 
1708cd79481dSYinghai Lu static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
170972d4b0b4SBenjamin Herrenschmidt {
171072d4b0b4SBenjamin Herrenschmidt 	unsigned int left = 0, right = type->cnt;
171172d4b0b4SBenjamin Herrenschmidt 
171272d4b0b4SBenjamin Herrenschmidt 	do {
171372d4b0b4SBenjamin Herrenschmidt 		unsigned int mid = (right + left) / 2;
171472d4b0b4SBenjamin Herrenschmidt 
171572d4b0b4SBenjamin Herrenschmidt 		if (addr < type->regions[mid].base)
171672d4b0b4SBenjamin Herrenschmidt 			right = mid;
171772d4b0b4SBenjamin Herrenschmidt 		else if (addr >= (type->regions[mid].base +
171872d4b0b4SBenjamin Herrenschmidt 				  type->regions[mid].size))
171972d4b0b4SBenjamin Herrenschmidt 			left = mid + 1;
172072d4b0b4SBenjamin Herrenschmidt 		else
172172d4b0b4SBenjamin Herrenschmidt 			return mid;
172272d4b0b4SBenjamin Herrenschmidt 	} while (left < right);
172372d4b0b4SBenjamin Herrenschmidt 	return -1;
172472d4b0b4SBenjamin Herrenschmidt }
172572d4b0b4SBenjamin Herrenschmidt 
1726f5a222dcSYueyi Li bool __init_memblock memblock_is_reserved(phys_addr_t addr)
172795f72d1eSYinghai Lu {
172872d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.reserved, addr) != -1;
172995f72d1eSYinghai Lu }
173072d4b0b4SBenjamin Herrenschmidt 
1731b4ad0c7eSYaowei Bai bool __init_memblock memblock_is_memory(phys_addr_t addr)
173272d4b0b4SBenjamin Herrenschmidt {
173372d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.memory, addr) != -1;
173472d4b0b4SBenjamin Herrenschmidt }
173572d4b0b4SBenjamin Herrenschmidt 
1736937f0c26SYaowei Bai bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
1737bf3d3cc5SArd Biesheuvel {
1738bf3d3cc5SArd Biesheuvel 	int i = memblock_search(&memblock.memory, addr);
1739bf3d3cc5SArd Biesheuvel 
1740bf3d3cc5SArd Biesheuvel 	if (i == -1)
1741bf3d3cc5SArd Biesheuvel 		return false;
1742bf3d3cc5SArd Biesheuvel 	return !memblock_is_nomap(&memblock.memory.regions[i]);
1743bf3d3cc5SArd Biesheuvel }
1744bf3d3cc5SArd Biesheuvel 
1745e76b63f8SYinghai Lu #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1746e76b63f8SYinghai Lu int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1747e76b63f8SYinghai Lu 			 unsigned long *start_pfn, unsigned long *end_pfn)
1748e76b63f8SYinghai Lu {
1749e76b63f8SYinghai Lu 	struct memblock_type *type = &memblock.memory;
175016763230SFabian Frederick 	int mid = memblock_search(type, PFN_PHYS(pfn));
1751e76b63f8SYinghai Lu 
1752e76b63f8SYinghai Lu 	if (mid == -1)
1753e76b63f8SYinghai Lu 		return -1;
1754e76b63f8SYinghai Lu 
1755f7e2f7e8SFabian Frederick 	*start_pfn = PFN_DOWN(type->regions[mid].base);
1756f7e2f7e8SFabian Frederick 	*end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
1757e76b63f8SYinghai Lu 
1758e76b63f8SYinghai Lu 	return type->regions[mid].nid;
1759e76b63f8SYinghai Lu }
1760e76b63f8SYinghai Lu #endif
1761e76b63f8SYinghai Lu 
1762eab30949SStephen Boyd /**
1763eab30949SStephen Boyd  * memblock_is_region_memory - check if a region is a subset of memory
1764eab30949SStephen Boyd  * @base: base of region to check
1765eab30949SStephen Boyd  * @size: size of region to check
1766eab30949SStephen Boyd  *
1767eab30949SStephen Boyd  * Check if the region [@base, @base + @size) is a subset of a memory block.
1768eab30949SStephen Boyd  *
176947cec443SMike Rapoport  * Return:
1770eab30949SStephen Boyd  * 0 if false, non-zero if true
1771eab30949SStephen Boyd  */
1772937f0c26SYaowei Bai bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
177372d4b0b4SBenjamin Herrenschmidt {
1774abb65272STomi Valkeinen 	int idx = memblock_search(&memblock.memory, base);
1775eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
177672d4b0b4SBenjamin Herrenschmidt 
177772d4b0b4SBenjamin Herrenschmidt 	if (idx == -1)
1778937f0c26SYaowei Bai 		return false;
1779ef415ef4SWei Yang 	return (memblock.memory.regions[idx].base +
1780eb18f1b5STejun Heo 		 memblock.memory.regions[idx].size) >= end;
178195f72d1eSYinghai Lu }
178295f72d1eSYinghai Lu 
1783eab30949SStephen Boyd /**
1784eab30949SStephen Boyd  * memblock_is_region_reserved - check if a region intersects reserved memory
1785eab30949SStephen Boyd  * @base: base of region to check
1786eab30949SStephen Boyd  * @size: size of region to check
1787eab30949SStephen Boyd  *
178847cec443SMike Rapoport  * Check if the region [@base, @base + @size) intersects a reserved
178947cec443SMike Rapoport  * memory block.
1790eab30949SStephen Boyd  *
179147cec443SMike Rapoport  * Return:
1792c5c5c9d1STang Chen  * True if they intersect, false if not.
1793eab30949SStephen Boyd  */
1794c5c5c9d1STang Chen bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
179595f72d1eSYinghai Lu {
1796eb18f1b5STejun Heo 	memblock_cap_size(base, &size);
1797c5c5c9d1STang Chen 	return memblock_overlaps_region(&memblock.reserved, base, size);
179895f72d1eSYinghai Lu }
179995f72d1eSYinghai Lu 
18006ede1fd3SYinghai Lu void __init_memblock memblock_trim_memory(phys_addr_t align)
18016ede1fd3SYinghai Lu {
18026ede1fd3SYinghai Lu 	phys_addr_t start, end, orig_start, orig_end;
1803136199f0SEmil Medve 	struct memblock_region *r;
18046ede1fd3SYinghai Lu 
1805136199f0SEmil Medve 	for_each_memblock(memory, r) {
1806136199f0SEmil Medve 		orig_start = r->base;
1807136199f0SEmil Medve 		orig_end = r->base + r->size;
18086ede1fd3SYinghai Lu 		start = round_up(orig_start, align);
18096ede1fd3SYinghai Lu 		end = round_down(orig_end, align);
18106ede1fd3SYinghai Lu 
18116ede1fd3SYinghai Lu 		if (start == orig_start && end == orig_end)
18126ede1fd3SYinghai Lu 			continue;
18136ede1fd3SYinghai Lu 
18146ede1fd3SYinghai Lu 		if (start < end) {
1815136199f0SEmil Medve 			r->base = start;
1816136199f0SEmil Medve 			r->size = end - start;
18176ede1fd3SYinghai Lu 		} else {
1818136199f0SEmil Medve 			memblock_remove_region(&memblock.memory,
1819136199f0SEmil Medve 					       r - memblock.memory.regions);
1820136199f0SEmil Medve 			r--;
18216ede1fd3SYinghai Lu 		}
18226ede1fd3SYinghai Lu 	}
18236ede1fd3SYinghai Lu }
1824e63075a3SBenjamin Herrenschmidt 
18253661ca66SYinghai Lu void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1826e63075a3SBenjamin Herrenschmidt {
1827e63075a3SBenjamin Herrenschmidt 	memblock.current_limit = limit;
1828e63075a3SBenjamin Herrenschmidt }
1829e63075a3SBenjamin Herrenschmidt 
1830fec51014SLaura Abbott phys_addr_t __init_memblock memblock_get_current_limit(void)
1831fec51014SLaura Abbott {
1832fec51014SLaura Abbott 	return memblock.current_limit;
1833fec51014SLaura Abbott }
1834fec51014SLaura Abbott 
18350262d9c8SHeiko Carstens static void __init_memblock memblock_dump(struct memblock_type *type)
18366ed311b2SBenjamin Herrenschmidt {
18375d63f81cSMiles Chen 	phys_addr_t base, end, size;
1838e1720feeSMike Rapoport 	enum memblock_flags flags;
18398c9c1701SAlexander Kuleshov 	int idx;
18408c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
18416ed311b2SBenjamin Herrenschmidt 
18420262d9c8SHeiko Carstens 	pr_info(" %s.cnt  = 0x%lx\n", type->name, type->cnt);
18436ed311b2SBenjamin Herrenschmidt 
184466e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
18457c0caeb8STejun Heo 		char nid_buf[32] = "";
18466ed311b2SBenjamin Herrenschmidt 
18477c0caeb8STejun Heo 		base = rgn->base;
18487c0caeb8STejun Heo 		size = rgn->size;
18495d63f81cSMiles Chen 		end = base + size - 1;
185066a20757STang Chen 		flags = rgn->flags;
18517c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
18527c0caeb8STejun Heo 		if (memblock_get_region_node(rgn) != MAX_NUMNODES)
18537c0caeb8STejun Heo 			snprintf(nid_buf, sizeof(nid_buf), " on node %d",
18547c0caeb8STejun Heo 				 memblock_get_region_node(rgn));
18557c0caeb8STejun Heo #endif
1856e1720feeSMike Rapoport 		pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
18570262d9c8SHeiko Carstens 			type->name, idx, &base, &end, &size, nid_buf, flags);
18586ed311b2SBenjamin Herrenschmidt 	}
18596ed311b2SBenjamin Herrenschmidt }
18606ed311b2SBenjamin Herrenschmidt 
18614ff7b82fSTejun Heo void __init_memblock __memblock_dump_all(void)
18626ed311b2SBenjamin Herrenschmidt {
18636ed311b2SBenjamin Herrenschmidt 	pr_info("MEMBLOCK configuration:\n");
18645d63f81cSMiles Chen 	pr_info(" memory size = %pa reserved size = %pa\n",
18655d63f81cSMiles Chen 		&memblock.memory.total_size,
18665d63f81cSMiles Chen 		&memblock.reserved.total_size);
18676ed311b2SBenjamin Herrenschmidt 
18680262d9c8SHeiko Carstens 	memblock_dump(&memblock.memory);
18690262d9c8SHeiko Carstens 	memblock_dump(&memblock.reserved);
1870409efd4cSHeiko Carstens #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
18710262d9c8SHeiko Carstens 	memblock_dump(&memblock.physmem);
1872409efd4cSHeiko Carstens #endif
18736ed311b2SBenjamin Herrenschmidt }
18746ed311b2SBenjamin Herrenschmidt 
18751aadc056STejun Heo void __init memblock_allow_resize(void)
18766ed311b2SBenjamin Herrenschmidt {
1877142b45a7SBenjamin Herrenschmidt 	memblock_can_resize = 1;
18786ed311b2SBenjamin Herrenschmidt }
18796ed311b2SBenjamin Herrenschmidt 
18806ed311b2SBenjamin Herrenschmidt static int __init early_memblock(char *p)
18816ed311b2SBenjamin Herrenschmidt {
18826ed311b2SBenjamin Herrenschmidt 	if (p && strstr(p, "debug"))
18836ed311b2SBenjamin Herrenschmidt 		memblock_debug = 1;
18846ed311b2SBenjamin Herrenschmidt 	return 0;
18856ed311b2SBenjamin Herrenschmidt }
18866ed311b2SBenjamin Herrenschmidt early_param("memblock", early_memblock);
18876ed311b2SBenjamin Herrenschmidt 
1888bda49a81SMike Rapoport static void __init __free_pages_memory(unsigned long start, unsigned long end)
1889bda49a81SMike Rapoport {
1890bda49a81SMike Rapoport 	int order;
1891bda49a81SMike Rapoport 
1892bda49a81SMike Rapoport 	while (start < end) {
1893bda49a81SMike Rapoport 		order = min(MAX_ORDER - 1UL, __ffs(start));
1894bda49a81SMike Rapoport 
1895bda49a81SMike Rapoport 		while (start + (1UL << order) > end)
1896bda49a81SMike Rapoport 			order--;
1897bda49a81SMike Rapoport 
1898bda49a81SMike Rapoport 		memblock_free_pages(pfn_to_page(start), start, order);
1899bda49a81SMike Rapoport 
1900bda49a81SMike Rapoport 		start += (1UL << order);
1901bda49a81SMike Rapoport 	}
1902bda49a81SMike Rapoport }
1903bda49a81SMike Rapoport 
1904bda49a81SMike Rapoport static unsigned long __init __free_memory_core(phys_addr_t start,
1905bda49a81SMike Rapoport 				 phys_addr_t end)
1906bda49a81SMike Rapoport {
1907bda49a81SMike Rapoport 	unsigned long start_pfn = PFN_UP(start);
1908bda49a81SMike Rapoport 	unsigned long end_pfn = min_t(unsigned long,
1909bda49a81SMike Rapoport 				      PFN_DOWN(end), max_low_pfn);
1910bda49a81SMike Rapoport 
1911bda49a81SMike Rapoport 	if (start_pfn >= end_pfn)
1912bda49a81SMike Rapoport 		return 0;
1913bda49a81SMike Rapoport 
1914bda49a81SMike Rapoport 	__free_pages_memory(start_pfn, end_pfn);
1915bda49a81SMike Rapoport 
1916bda49a81SMike Rapoport 	return end_pfn - start_pfn;
1917bda49a81SMike Rapoport }
1918bda49a81SMike Rapoport 
1919bda49a81SMike Rapoport static unsigned long __init free_low_memory_core_early(void)
1920bda49a81SMike Rapoport {
1921bda49a81SMike Rapoport 	unsigned long count = 0;
1922bda49a81SMike Rapoport 	phys_addr_t start, end;
1923bda49a81SMike Rapoport 	u64 i;
1924bda49a81SMike Rapoport 
1925bda49a81SMike Rapoport 	memblock_clear_hotplug(0, -1);
1926bda49a81SMike Rapoport 
1927bda49a81SMike Rapoport 	for_each_reserved_mem_region(i, &start, &end)
1928bda49a81SMike Rapoport 		reserve_bootmem_region(start, end);
1929bda49a81SMike Rapoport 
1930bda49a81SMike Rapoport 	/*
1931bda49a81SMike Rapoport 	 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
1932bda49a81SMike Rapoport 	 *  because in some case like Node0 doesn't have RAM installed
1933bda49a81SMike Rapoport 	 *  low ram will be on Node1
1934bda49a81SMike Rapoport 	 */
1935bda49a81SMike Rapoport 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
1936bda49a81SMike Rapoport 				NULL)
1937bda49a81SMike Rapoport 		count += __free_memory_core(start, end);
1938bda49a81SMike Rapoport 
1939bda49a81SMike Rapoport 	return count;
1940bda49a81SMike Rapoport }
1941bda49a81SMike Rapoport 
1942bda49a81SMike Rapoport static int reset_managed_pages_done __initdata;
1943bda49a81SMike Rapoport 
1944bda49a81SMike Rapoport void reset_node_managed_pages(pg_data_t *pgdat)
1945bda49a81SMike Rapoport {
1946bda49a81SMike Rapoport 	struct zone *z;
1947bda49a81SMike Rapoport 
1948bda49a81SMike Rapoport 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
19499705bea5SArun KS 		atomic_long_set(&z->managed_pages, 0);
1950bda49a81SMike Rapoport }
1951bda49a81SMike Rapoport 
1952bda49a81SMike Rapoport void __init reset_all_zones_managed_pages(void)
1953bda49a81SMike Rapoport {
1954bda49a81SMike Rapoport 	struct pglist_data *pgdat;
1955bda49a81SMike Rapoport 
1956bda49a81SMike Rapoport 	if (reset_managed_pages_done)
1957bda49a81SMike Rapoport 		return;
1958bda49a81SMike Rapoport 
1959bda49a81SMike Rapoport 	for_each_online_pgdat(pgdat)
1960bda49a81SMike Rapoport 		reset_node_managed_pages(pgdat);
1961bda49a81SMike Rapoport 
1962bda49a81SMike Rapoport 	reset_managed_pages_done = 1;
1963bda49a81SMike Rapoport }
1964bda49a81SMike Rapoport 
1965bda49a81SMike Rapoport /**
1966bda49a81SMike Rapoport  * memblock_free_all - release free pages to the buddy allocator
1967bda49a81SMike Rapoport  *
1968bda49a81SMike Rapoport  * Return: the number of pages actually released.
1969bda49a81SMike Rapoport  */
1970bda49a81SMike Rapoport unsigned long __init memblock_free_all(void)
1971bda49a81SMike Rapoport {
1972bda49a81SMike Rapoport 	unsigned long pages;
1973bda49a81SMike Rapoport 
1974bda49a81SMike Rapoport 	reset_all_zones_managed_pages();
1975bda49a81SMike Rapoport 
1976bda49a81SMike Rapoport 	pages = free_low_memory_core_early();
1977ca79b0c2SArun KS 	totalram_pages_add(pages);
1978bda49a81SMike Rapoport 
1979bda49a81SMike Rapoport 	return pages;
1980bda49a81SMike Rapoport }
1981bda49a81SMike Rapoport 
1982350e88baSMike Rapoport #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
19836d03b885SBenjamin Herrenschmidt 
19846d03b885SBenjamin Herrenschmidt static int memblock_debug_show(struct seq_file *m, void *private)
19856d03b885SBenjamin Herrenschmidt {
19866d03b885SBenjamin Herrenschmidt 	struct memblock_type *type = m->private;
19876d03b885SBenjamin Herrenschmidt 	struct memblock_region *reg;
19886d03b885SBenjamin Herrenschmidt 	int i;
19895d63f81cSMiles Chen 	phys_addr_t end;
19906d03b885SBenjamin Herrenschmidt 
19916d03b885SBenjamin Herrenschmidt 	for (i = 0; i < type->cnt; i++) {
19926d03b885SBenjamin Herrenschmidt 		reg = &type->regions[i];
19935d63f81cSMiles Chen 		end = reg->base + reg->size - 1;
19946d03b885SBenjamin Herrenschmidt 
19955d63f81cSMiles Chen 		seq_printf(m, "%4d: ", i);
19965d63f81cSMiles Chen 		seq_printf(m, "%pa..%pa\n", &reg->base, &end);
19976d03b885SBenjamin Herrenschmidt 	}
19986d03b885SBenjamin Herrenschmidt 	return 0;
19996d03b885SBenjamin Herrenschmidt }
20005ad35093SAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(memblock_debug);
20016d03b885SBenjamin Herrenschmidt 
20026d03b885SBenjamin Herrenschmidt static int __init memblock_init_debugfs(void)
20036d03b885SBenjamin Herrenschmidt {
20046d03b885SBenjamin Herrenschmidt 	struct dentry *root = debugfs_create_dir("memblock", NULL);
2005d9f7979cSGreg Kroah-Hartman 
20060825a6f9SJoe Perches 	debugfs_create_file("memory", 0444, root,
20070825a6f9SJoe Perches 			    &memblock.memory, &memblock_debug_fops);
20080825a6f9SJoe Perches 	debugfs_create_file("reserved", 0444, root,
20090825a6f9SJoe Perches 			    &memblock.reserved, &memblock_debug_fops);
201070210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
20110825a6f9SJoe Perches 	debugfs_create_file("physmem", 0444, root,
20120825a6f9SJoe Perches 			    &memblock.physmem, &memblock_debug_fops);
201370210ed9SPhilipp Hachtmann #endif
20146d03b885SBenjamin Herrenschmidt 
20156d03b885SBenjamin Herrenschmidt 	return 0;
20166d03b885SBenjamin Herrenschmidt }
20176d03b885SBenjamin Herrenschmidt __initcall(memblock_init_debugfs);
20186d03b885SBenjamin Herrenschmidt 
20196d03b885SBenjamin Herrenschmidt #endif /* CONFIG_DEBUG_FS */
2020