xref: /linux/mm/memblock.c (revision 87c55870f01266fe22f345a0767162f85f1cf8f1)
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
4777649905SDavid Hildenbrand  * * ``physmem`` - describes the actual physical memory available during
4877649905SDavid Hildenbrand  *   boot regardless of the possible restrictions and memory hot(un)plug;
4977649905SDavid Hildenbrand  *   the ``physmem`` type is only available 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
5577649905SDavid Hildenbrand  * the allocator metadata. The "memory" and "reserved" types are nicely
5677649905SDavid Hildenbrand  * wrapped with :c:type:`struct memblock`. This structure is statically
5777649905SDavid Hildenbrand  * initialized at build time. The region arrays are initially sized to
5877649905SDavid Hildenbrand  * %INIT_MEMBLOCK_REGIONS for "memory" and %INIT_MEMBLOCK_RESERVED_REGIONS
5977649905SDavid Hildenbrand  * for "reserved". The region array for "physmem" is initially sized to
6077649905SDavid Hildenbrand  * %INIT_PHYSMEM_REGIONS.
616e5af9a8SCao jin  * The memblock_allow_resize() enables automatic resizing of the region
626e5af9a8SCao jin  * arrays during addition of new regions. This feature should be used
636e5af9a8SCao jin  * with care so that memory allocated for the region array will not
646e5af9a8SCao jin  * overlap with areas that should be reserved, for example initrd.
653e039c5cSMike Rapoport  *
663e039c5cSMike Rapoport  * The early architecture setup should tell memblock what the physical
676e5af9a8SCao jin  * memory layout is by using memblock_add() or memblock_add_node()
686e5af9a8SCao jin  * functions. The first function does not assign the region to a NUMA
696e5af9a8SCao jin  * node and it is appropriate for UMA systems. Yet, it is possible to
706e5af9a8SCao jin  * use it on NUMA systems as well and assign the region to a NUMA node
716e5af9a8SCao jin  * later in the setup process using memblock_set_node(). The
726e5af9a8SCao jin  * memblock_add_node() performs such an assignment directly.
733e039c5cSMike Rapoport  *
74a2974133SMike Rapoport  * Once memblock is setup the memory can be allocated using one of the
75a2974133SMike Rapoport  * API variants:
76a2974133SMike Rapoport  *
776e5af9a8SCao jin  * * memblock_phys_alloc*() - these functions return the **physical**
786e5af9a8SCao jin  *   address of the allocated memory
796e5af9a8SCao jin  * * memblock_alloc*() - these functions return the **virtual** address
806e5af9a8SCao jin  *   of the allocated memory.
81a2974133SMike Rapoport  *
82df1758d9SEthon Paul  * Note, that both API variants use implicit assumptions about allowed
83a2974133SMike Rapoport  * memory ranges and the fallback methods. Consult the documentation
846e5af9a8SCao jin  * of memblock_alloc_internal() and memblock_alloc_range_nid()
856e5af9a8SCao jin  * functions for more elaborate description.
863e039c5cSMike Rapoport  *
876e5af9a8SCao jin  * As the system boot progresses, the architecture specific mem_init()
886e5af9a8SCao jin  * function frees all the memory to the buddy page allocator.
893e039c5cSMike Rapoport  *
906e5af9a8SCao jin  * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the
9177649905SDavid Hildenbrand  * memblock data structures (except "physmem") will be discarded after the
9277649905SDavid Hildenbrand  * system initialization completes.
933e039c5cSMike Rapoport  */
943e039c5cSMike Rapoport 
95bda49a81SMike Rapoport #ifndef CONFIG_NEED_MULTIPLE_NODES
96bda49a81SMike Rapoport struct pglist_data __refdata contig_page_data;
97bda49a81SMike Rapoport EXPORT_SYMBOL(contig_page_data);
98bda49a81SMike Rapoport #endif
99bda49a81SMike Rapoport 
100bda49a81SMike Rapoport unsigned long max_low_pfn;
101bda49a81SMike Rapoport unsigned long min_low_pfn;
102bda49a81SMike Rapoport unsigned long max_pfn;
103bda49a81SMike Rapoport unsigned long long max_possible_pfn;
104bda49a81SMike Rapoport 
105fe091c20STejun Heo static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
1068a5b403dSArd Biesheuvel static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
10770210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
10877649905SDavid Hildenbrand static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS];
10970210ed9SPhilipp Hachtmann #endif
110fe091c20STejun Heo 
111fe091c20STejun Heo struct memblock memblock __initdata_memblock = {
112fe091c20STejun Heo 	.memory.regions		= memblock_memory_init_regions,
113fe091c20STejun Heo 	.memory.cnt		= 1,	/* empty dummy entry */
114fe091c20STejun Heo 	.memory.max		= INIT_MEMBLOCK_REGIONS,
1150262d9c8SHeiko Carstens 	.memory.name		= "memory",
116fe091c20STejun Heo 
117fe091c20STejun Heo 	.reserved.regions	= memblock_reserved_init_regions,
118fe091c20STejun Heo 	.reserved.cnt		= 1,	/* empty dummy entry */
1198a5b403dSArd Biesheuvel 	.reserved.max		= INIT_MEMBLOCK_RESERVED_REGIONS,
1200262d9c8SHeiko Carstens 	.reserved.name		= "reserved",
121fe091c20STejun Heo 
12279442ed1STang Chen 	.bottom_up		= false,
123fe091c20STejun Heo 	.current_limit		= MEMBLOCK_ALLOC_ANYWHERE,
124fe091c20STejun Heo };
12595f72d1eSYinghai Lu 
12677649905SDavid Hildenbrand #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
12777649905SDavid Hildenbrand struct memblock_type physmem = {
12877649905SDavid Hildenbrand 	.regions		= memblock_physmem_init_regions,
12977649905SDavid Hildenbrand 	.cnt			= 1,	/* empty dummy entry */
13077649905SDavid Hildenbrand 	.max			= INIT_PHYSMEM_REGIONS,
13177649905SDavid Hildenbrand 	.name			= "physmem",
13277649905SDavid Hildenbrand };
13377649905SDavid Hildenbrand #endif
13477649905SDavid Hildenbrand 
135cd991db8SMike Rapoport #define for_each_memblock_type(i, memblock_type, rgn)			\
136cd991db8SMike Rapoport 	for (i = 0, rgn = &memblock_type->regions[0];			\
137cd991db8SMike Rapoport 	     i < memblock_type->cnt;					\
138cd991db8SMike Rapoport 	     i++, rgn = &memblock_type->regions[i])
139cd991db8SMike Rapoport 
140*87c55870SMike Rapoport #define memblock_dbg(fmt, ...)						\
141*87c55870SMike Rapoport 	do {								\
142*87c55870SMike Rapoport 		if (memblock_debug)					\
143*87c55870SMike Rapoport 			pr_info(fmt, ##__VA_ARGS__);			\
144*87c55870SMike Rapoport 	} while (0)
145*87c55870SMike Rapoport 
146*87c55870SMike Rapoport static int memblock_debug __initdata_memblock;
147a3f5bafcSTony Luck static bool system_has_some_mirror __initdata_memblock = false;
1481aadc056STejun Heo static int memblock_can_resize __initdata_memblock;
149181eb394SGavin Shan static int memblock_memory_in_slab __initdata_memblock = 0;
150181eb394SGavin Shan static int memblock_reserved_in_slab __initdata_memblock = 0;
15195f72d1eSYinghai Lu 
152c366ea89SMike Rapoport static enum memblock_flags __init_memblock choose_memblock_flags(void)
153a3f5bafcSTony Luck {
154a3f5bafcSTony Luck 	return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
155a3f5bafcSTony Luck }
156a3f5bafcSTony Luck 
157eb18f1b5STejun Heo /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
158eb18f1b5STejun Heo static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
159eb18f1b5STejun Heo {
1601c4bc43dSStefan Agner 	return *size = min(*size, PHYS_ADDR_MAX - base);
161eb18f1b5STejun Heo }
162eb18f1b5STejun Heo 
1636ed311b2SBenjamin Herrenschmidt /*
1646ed311b2SBenjamin Herrenschmidt  * Address comparison utilities
1656ed311b2SBenjamin Herrenschmidt  */
16610d06439SYinghai Lu static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
1672898cc4cSBenjamin Herrenschmidt 				       phys_addr_t base2, phys_addr_t size2)
16895f72d1eSYinghai Lu {
16995f72d1eSYinghai Lu 	return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
17095f72d1eSYinghai Lu }
17195f72d1eSYinghai Lu 
17295cf82ecSTang Chen bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
1732d7d3eb2SH Hartley Sweeten 					phys_addr_t base, phys_addr_t size)
1746ed311b2SBenjamin Herrenschmidt {
1756ed311b2SBenjamin Herrenschmidt 	unsigned long i;
1766ed311b2SBenjamin Herrenschmidt 
177f14516fbSAlexander Kuleshov 	for (i = 0; i < type->cnt; i++)
178f14516fbSAlexander Kuleshov 		if (memblock_addrs_overlap(base, size, type->regions[i].base,
179f14516fbSAlexander Kuleshov 					   type->regions[i].size))
1806ed311b2SBenjamin Herrenschmidt 			break;
181c5c5c9d1STang Chen 	return i < type->cnt;
1826ed311b2SBenjamin Herrenschmidt }
1836ed311b2SBenjamin Herrenschmidt 
18447cec443SMike Rapoport /**
18579442ed1STang Chen  * __memblock_find_range_bottom_up - find free area utility in bottom-up
18679442ed1STang Chen  * @start: start of candidate range
18747cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
18847cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
18979442ed1STang Chen  * @size: size of free area to find
19079442ed1STang Chen  * @align: alignment of free area to find
191b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
192fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
19379442ed1STang Chen  *
19479442ed1STang Chen  * Utility called from memblock_find_in_range_node(), find free area bottom-up.
19579442ed1STang Chen  *
19647cec443SMike Rapoport  * Return:
19779442ed1STang Chen  * Found address on success, 0 on failure.
19879442ed1STang Chen  */
19979442ed1STang Chen static phys_addr_t __init_memblock
20079442ed1STang Chen __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
201fc6daaf9STony Luck 				phys_addr_t size, phys_addr_t align, int nid,
202e1720feeSMike Rapoport 				enum memblock_flags flags)
20379442ed1STang Chen {
20479442ed1STang Chen 	phys_addr_t this_start, this_end, cand;
20579442ed1STang Chen 	u64 i;
20679442ed1STang Chen 
207fc6daaf9STony Luck 	for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
20879442ed1STang Chen 		this_start = clamp(this_start, start, end);
20979442ed1STang Chen 		this_end = clamp(this_end, start, end);
21079442ed1STang Chen 
21179442ed1STang Chen 		cand = round_up(this_start, align);
21279442ed1STang Chen 		if (cand < this_end && this_end - cand >= size)
21379442ed1STang Chen 			return cand;
21479442ed1STang Chen 	}
21579442ed1STang Chen 
21679442ed1STang Chen 	return 0;
21779442ed1STang Chen }
21879442ed1STang Chen 
2197bd0b0f0STejun Heo /**
2201402899eSTang Chen  * __memblock_find_range_top_down - find free area utility, in top-down
2211402899eSTang Chen  * @start: start of candidate range
22247cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
22347cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
2241402899eSTang Chen  * @size: size of free area to find
2251402899eSTang Chen  * @align: alignment of free area to find
226b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
227fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2281402899eSTang Chen  *
2291402899eSTang Chen  * Utility called from memblock_find_in_range_node(), find free area top-down.
2301402899eSTang Chen  *
23147cec443SMike Rapoport  * Return:
23279442ed1STang Chen  * Found address on success, 0 on failure.
2331402899eSTang Chen  */
2341402899eSTang Chen static phys_addr_t __init_memblock
2351402899eSTang Chen __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
236fc6daaf9STony Luck 			       phys_addr_t size, phys_addr_t align, int nid,
237e1720feeSMike Rapoport 			       enum memblock_flags flags)
2381402899eSTang Chen {
2391402899eSTang Chen 	phys_addr_t this_start, this_end, cand;
2401402899eSTang Chen 	u64 i;
2411402899eSTang Chen 
242fc6daaf9STony Luck 	for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
243fc6daaf9STony Luck 					NULL) {
2441402899eSTang Chen 		this_start = clamp(this_start, start, end);
2451402899eSTang Chen 		this_end = clamp(this_end, start, end);
2461402899eSTang Chen 
2471402899eSTang Chen 		if (this_end < size)
2481402899eSTang Chen 			continue;
2491402899eSTang Chen 
2501402899eSTang Chen 		cand = round_down(this_end - size, align);
2511402899eSTang Chen 		if (cand >= this_start)
2521402899eSTang Chen 			return cand;
2531402899eSTang Chen 	}
2541402899eSTang Chen 
2551402899eSTang Chen 	return 0;
2561402899eSTang Chen }
2571402899eSTang Chen 
2581402899eSTang Chen /**
2597bd0b0f0STejun Heo  * memblock_find_in_range_node - find free area in given range and node
2607bd0b0f0STejun Heo  * @size: size of free area to find
2617bd0b0f0STejun Heo  * @align: alignment of free area to find
26287029ee9SGrygorii Strashko  * @start: start of candidate range
26347cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
26447cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
265b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
266fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2677bd0b0f0STejun Heo  *
2687bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range and node.
2697bd0b0f0STejun Heo  *
27079442ed1STang Chen  * When allocation direction is bottom-up, the @start should be greater
27179442ed1STang Chen  * than the end of the kernel image. Otherwise, it will be trimmed. The
27279442ed1STang Chen  * reason is that we want the bottom-up allocation just near the kernel
27379442ed1STang Chen  * image so it is highly likely that the allocated memory and the kernel
27479442ed1STang Chen  * will reside in the same node.
27579442ed1STang Chen  *
27679442ed1STang Chen  * If bottom-up allocation failed, will try to allocate memory top-down.
27779442ed1STang Chen  *
27847cec443SMike Rapoport  * Return:
27979442ed1STang Chen  * Found address on success, 0 on failure.
2806ed311b2SBenjamin Herrenschmidt  */
281c366ea89SMike Rapoport static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
28287029ee9SGrygorii Strashko 					phys_addr_t align, phys_addr_t start,
283e1720feeSMike Rapoport 					phys_addr_t end, int nid,
284e1720feeSMike Rapoport 					enum memblock_flags flags)
285f7210e6cSTang Chen {
2860cfb8f0cSTang Chen 	phys_addr_t kernel_end, ret;
28779442ed1STang Chen 
288f7210e6cSTang Chen 	/* pump up @end */
289fed84c78SQian Cai 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
290fed84c78SQian Cai 	    end == MEMBLOCK_ALLOC_KASAN)
291f7210e6cSTang Chen 		end = memblock.current_limit;
292f7210e6cSTang Chen 
293f7210e6cSTang Chen 	/* avoid allocating the first page */
294f7210e6cSTang Chen 	start = max_t(phys_addr_t, start, PAGE_SIZE);
295f7210e6cSTang Chen 	end = max(start, end);
29679442ed1STang Chen 	kernel_end = __pa_symbol(_end);
29779442ed1STang Chen 
29879442ed1STang Chen 	/*
29979442ed1STang Chen 	 * try bottom-up allocation only when bottom-up mode
30079442ed1STang Chen 	 * is set and @end is above the kernel image.
30179442ed1STang Chen 	 */
30279442ed1STang Chen 	if (memblock_bottom_up() && end > kernel_end) {
30379442ed1STang Chen 		phys_addr_t bottom_up_start;
30479442ed1STang Chen 
30579442ed1STang Chen 		/* make sure we will allocate above the kernel */
30679442ed1STang Chen 		bottom_up_start = max(start, kernel_end);
30779442ed1STang Chen 
30879442ed1STang Chen 		/* ok, try bottom-up allocation first */
30979442ed1STang Chen 		ret = __memblock_find_range_bottom_up(bottom_up_start, end,
310fc6daaf9STony Luck 						      size, align, nid, flags);
31179442ed1STang Chen 		if (ret)
31279442ed1STang Chen 			return ret;
31379442ed1STang Chen 
31479442ed1STang Chen 		/*
31579442ed1STang Chen 		 * we always limit bottom-up allocation above the kernel,
31679442ed1STang Chen 		 * but top-down allocation doesn't have the limit, so
31779442ed1STang Chen 		 * retrying top-down allocation may succeed when bottom-up
31879442ed1STang Chen 		 * allocation failed.
31979442ed1STang Chen 		 *
32079442ed1STang Chen 		 * bottom-up allocation is expected to be fail very rarely,
32179442ed1STang Chen 		 * so we use WARN_ONCE() here to see the stack trace if
32279442ed1STang Chen 		 * fail happens.
32379442ed1STang Chen 		 */
324e3d301caSMichal Hocko 		WARN_ONCE(IS_ENABLED(CONFIG_MEMORY_HOTREMOVE),
325e3d301caSMichal Hocko 			  "memblock: bottom-up allocation failed, memory hotremove may be affected\n");
32679442ed1STang Chen 	}
327f7210e6cSTang Chen 
328fc6daaf9STony Luck 	return __memblock_find_range_top_down(start, end, size, align, nid,
329fc6daaf9STony Luck 					      flags);
330f7210e6cSTang Chen }
3316ed311b2SBenjamin Herrenschmidt 
3327bd0b0f0STejun Heo /**
3337bd0b0f0STejun Heo  * memblock_find_in_range - find free area in given range
3347bd0b0f0STejun Heo  * @start: start of candidate range
33547cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
33647cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
3377bd0b0f0STejun Heo  * @size: size of free area to find
3387bd0b0f0STejun Heo  * @align: alignment of free area to find
3397bd0b0f0STejun Heo  *
3407bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range.
3417bd0b0f0STejun Heo  *
34247cec443SMike Rapoport  * Return:
34379442ed1STang Chen  * Found address on success, 0 on failure.
3447bd0b0f0STejun Heo  */
3457bd0b0f0STejun Heo phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
3467bd0b0f0STejun Heo 					phys_addr_t end, phys_addr_t size,
3477bd0b0f0STejun Heo 					phys_addr_t align)
3487bd0b0f0STejun Heo {
349a3f5bafcSTony Luck 	phys_addr_t ret;
350e1720feeSMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
351a3f5bafcSTony Luck 
352a3f5bafcSTony Luck again:
353a3f5bafcSTony Luck 	ret = memblock_find_in_range_node(size, align, start, end,
354a3f5bafcSTony Luck 					    NUMA_NO_NODE, flags);
355a3f5bafcSTony Luck 
356a3f5bafcSTony Luck 	if (!ret && (flags & MEMBLOCK_MIRROR)) {
357a3f5bafcSTony Luck 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
358a3f5bafcSTony Luck 			&size);
359a3f5bafcSTony Luck 		flags &= ~MEMBLOCK_MIRROR;
360a3f5bafcSTony Luck 		goto again;
361a3f5bafcSTony Luck 	}
362a3f5bafcSTony Luck 
363a3f5bafcSTony Luck 	return ret;
3647bd0b0f0STejun Heo }
3657bd0b0f0STejun Heo 
36610d06439SYinghai Lu static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
36795f72d1eSYinghai Lu {
3681440c4e2STejun Heo 	type->total_size -= type->regions[r].size;
3697c0caeb8STejun Heo 	memmove(&type->regions[r], &type->regions[r + 1],
3707c0caeb8STejun Heo 		(type->cnt - (r + 1)) * sizeof(type->regions[r]));
371e3239ff9SBenjamin Herrenschmidt 	type->cnt--;
37295f72d1eSYinghai Lu 
3738f7a6605SBenjamin Herrenschmidt 	/* Special case for empty arrays */
3748f7a6605SBenjamin Herrenschmidt 	if (type->cnt == 0) {
3751440c4e2STejun Heo 		WARN_ON(type->total_size != 0);
3768f7a6605SBenjamin Herrenschmidt 		type->cnt = 1;
3778f7a6605SBenjamin Herrenschmidt 		type->regions[0].base = 0;
3788f7a6605SBenjamin Herrenschmidt 		type->regions[0].size = 0;
37966a20757STang Chen 		type->regions[0].flags = 0;
3807c0caeb8STejun Heo 		memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
3818f7a6605SBenjamin Herrenschmidt 	}
38295f72d1eSYinghai Lu }
38395f72d1eSYinghai Lu 
384350e88baSMike Rapoport #ifndef CONFIG_ARCH_KEEP_MEMBLOCK
3853010f876SPavel Tatashin /**
38647cec443SMike Rapoport  * memblock_discard - discard memory and reserved arrays if they were allocated
3873010f876SPavel Tatashin  */
3883010f876SPavel Tatashin void __init memblock_discard(void)
38929f67386SYinghai Lu {
3903010f876SPavel Tatashin 	phys_addr_t addr, size;
39129f67386SYinghai Lu 
3923010f876SPavel Tatashin 	if (memblock.reserved.regions != memblock_reserved_init_regions) {
3933010f876SPavel Tatashin 		addr = __pa(memblock.reserved.regions);
3943010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
39529f67386SYinghai Lu 				  memblock.reserved.max);
3963010f876SPavel Tatashin 		__memblock_free_late(addr, size);
39729f67386SYinghai Lu 	}
39829f67386SYinghai Lu 
39991b540f9SPavel Tatashin 	if (memblock.memory.regions != memblock_memory_init_regions) {
4003010f876SPavel Tatashin 		addr = __pa(memblock.memory.regions);
4013010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
4025e270e25SPhilipp Hachtmann 				  memblock.memory.max);
4033010f876SPavel Tatashin 		__memblock_free_late(addr, size);
4045e270e25SPhilipp Hachtmann 	}
4053010f876SPavel Tatashin }
4065e270e25SPhilipp Hachtmann #endif
4075e270e25SPhilipp Hachtmann 
40848c3b583SGreg Pearson /**
40948c3b583SGreg Pearson  * memblock_double_array - double the size of the memblock regions array
41048c3b583SGreg Pearson  * @type: memblock type of the regions array being doubled
41148c3b583SGreg Pearson  * @new_area_start: starting address of memory range to avoid overlap with
41248c3b583SGreg Pearson  * @new_area_size: size of memory range to avoid overlap with
41348c3b583SGreg Pearson  *
41448c3b583SGreg Pearson  * Double the size of the @type regions array. If memblock is being used to
41548c3b583SGreg Pearson  * allocate memory for a new reserved regions array and there is a previously
41648c3b583SGreg Pearson  * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
41748c3b583SGreg Pearson  * waiting to be reserved, ensure the memory used by the new array does
41848c3b583SGreg Pearson  * not overlap.
41948c3b583SGreg Pearson  *
42047cec443SMike Rapoport  * Return:
42148c3b583SGreg Pearson  * 0 on success, -1 on failure.
42248c3b583SGreg Pearson  */
42348c3b583SGreg Pearson static int __init_memblock memblock_double_array(struct memblock_type *type,
42448c3b583SGreg Pearson 						phys_addr_t new_area_start,
42548c3b583SGreg Pearson 						phys_addr_t new_area_size)
426142b45a7SBenjamin Herrenschmidt {
427142b45a7SBenjamin Herrenschmidt 	struct memblock_region *new_array, *old_array;
42829f67386SYinghai Lu 	phys_addr_t old_alloc_size, new_alloc_size;
429a36aab89SMike Rapoport 	phys_addr_t old_size, new_size, addr, new_end;
430142b45a7SBenjamin Herrenschmidt 	int use_slab = slab_is_available();
431181eb394SGavin Shan 	int *in_slab;
432142b45a7SBenjamin Herrenschmidt 
433142b45a7SBenjamin Herrenschmidt 	/* We don't allow resizing until we know about the reserved regions
434142b45a7SBenjamin Herrenschmidt 	 * of memory that aren't suitable for allocation
435142b45a7SBenjamin Herrenschmidt 	 */
436142b45a7SBenjamin Herrenschmidt 	if (!memblock_can_resize)
437142b45a7SBenjamin Herrenschmidt 		return -1;
438142b45a7SBenjamin Herrenschmidt 
439142b45a7SBenjamin Herrenschmidt 	/* Calculate new doubled size */
440142b45a7SBenjamin Herrenschmidt 	old_size = type->max * sizeof(struct memblock_region);
441142b45a7SBenjamin Herrenschmidt 	new_size = old_size << 1;
44229f67386SYinghai Lu 	/*
44329f67386SYinghai Lu 	 * We need to allocated new one align to PAGE_SIZE,
44429f67386SYinghai Lu 	 *   so we can free them completely later.
44529f67386SYinghai Lu 	 */
44629f67386SYinghai Lu 	old_alloc_size = PAGE_ALIGN(old_size);
44729f67386SYinghai Lu 	new_alloc_size = PAGE_ALIGN(new_size);
448142b45a7SBenjamin Herrenschmidt 
449181eb394SGavin Shan 	/* Retrieve the slab flag */
450181eb394SGavin Shan 	if (type == &memblock.memory)
451181eb394SGavin Shan 		in_slab = &memblock_memory_in_slab;
452181eb394SGavin Shan 	else
453181eb394SGavin Shan 		in_slab = &memblock_reserved_in_slab;
454181eb394SGavin Shan 
455a2974133SMike Rapoport 	/* Try to find some space for it */
456142b45a7SBenjamin Herrenschmidt 	if (use_slab) {
457142b45a7SBenjamin Herrenschmidt 		new_array = kmalloc(new_size, GFP_KERNEL);
4581f5026a7STejun Heo 		addr = new_array ? __pa(new_array) : 0;
4594e2f0775SGavin Shan 	} else {
46048c3b583SGreg Pearson 		/* only exclude range when trying to double reserved.regions */
46148c3b583SGreg Pearson 		if (type != &memblock.reserved)
46248c3b583SGreg Pearson 			new_area_start = new_area_size = 0;
46348c3b583SGreg Pearson 
46448c3b583SGreg Pearson 		addr = memblock_find_in_range(new_area_start + new_area_size,
46548c3b583SGreg Pearson 						memblock.current_limit,
46629f67386SYinghai Lu 						new_alloc_size, PAGE_SIZE);
46748c3b583SGreg Pearson 		if (!addr && new_area_size)
46848c3b583SGreg Pearson 			addr = memblock_find_in_range(0,
46948c3b583SGreg Pearson 				min(new_area_start, memblock.current_limit),
47029f67386SYinghai Lu 				new_alloc_size, PAGE_SIZE);
47148c3b583SGreg Pearson 
47215674868SSachin Kamat 		new_array = addr ? __va(addr) : NULL;
4734e2f0775SGavin Shan 	}
4741f5026a7STejun Heo 	if (!addr) {
475142b45a7SBenjamin Herrenschmidt 		pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
4760262d9c8SHeiko Carstens 		       type->name, type->max, type->max * 2);
477142b45a7SBenjamin Herrenschmidt 		return -1;
478142b45a7SBenjamin Herrenschmidt 	}
479142b45a7SBenjamin Herrenschmidt 
480a36aab89SMike Rapoport 	new_end = addr + new_size - 1;
481a36aab89SMike Rapoport 	memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
482a36aab89SMike Rapoport 			type->name, type->max * 2, &addr, &new_end);
483ea9e4376SYinghai Lu 
484fd07383bSAndrew Morton 	/*
485fd07383bSAndrew Morton 	 * Found space, we now need to move the array over before we add the
486fd07383bSAndrew Morton 	 * reserved region since it may be our reserved array itself that is
487fd07383bSAndrew Morton 	 * full.
488142b45a7SBenjamin Herrenschmidt 	 */
489142b45a7SBenjamin Herrenschmidt 	memcpy(new_array, type->regions, old_size);
490142b45a7SBenjamin Herrenschmidt 	memset(new_array + type->max, 0, old_size);
491142b45a7SBenjamin Herrenschmidt 	old_array = type->regions;
492142b45a7SBenjamin Herrenschmidt 	type->regions = new_array;
493142b45a7SBenjamin Herrenschmidt 	type->max <<= 1;
494142b45a7SBenjamin Herrenschmidt 
495fd07383bSAndrew Morton 	/* Free old array. We needn't free it if the array is the static one */
496181eb394SGavin Shan 	if (*in_slab)
497181eb394SGavin Shan 		kfree(old_array);
498181eb394SGavin Shan 	else if (old_array != memblock_memory_init_regions &&
499142b45a7SBenjamin Herrenschmidt 		 old_array != memblock_reserved_init_regions)
50029f67386SYinghai Lu 		memblock_free(__pa(old_array), old_alloc_size);
501142b45a7SBenjamin Herrenschmidt 
502fd07383bSAndrew Morton 	/*
503fd07383bSAndrew Morton 	 * Reserve the new array if that comes from the memblock.  Otherwise, we
504fd07383bSAndrew Morton 	 * needn't do it
505181eb394SGavin Shan 	 */
506181eb394SGavin Shan 	if (!use_slab)
50729f67386SYinghai Lu 		BUG_ON(memblock_reserve(addr, new_alloc_size));
508181eb394SGavin Shan 
509181eb394SGavin Shan 	/* Update slab flag */
510181eb394SGavin Shan 	*in_slab = use_slab;
511181eb394SGavin Shan 
512142b45a7SBenjamin Herrenschmidt 	return 0;
513142b45a7SBenjamin Herrenschmidt }
514142b45a7SBenjamin Herrenschmidt 
515784656f9STejun Heo /**
516784656f9STejun Heo  * memblock_merge_regions - merge neighboring compatible regions
517784656f9STejun Heo  * @type: memblock type to scan
518784656f9STejun Heo  *
519784656f9STejun Heo  * Scan @type and merge neighboring compatible regions.
520784656f9STejun Heo  */
521784656f9STejun Heo static void __init_memblock memblock_merge_regions(struct memblock_type *type)
522784656f9STejun Heo {
523784656f9STejun Heo 	int i = 0;
524784656f9STejun Heo 
525784656f9STejun Heo 	/* cnt never goes below 1 */
526784656f9STejun Heo 	while (i < type->cnt - 1) {
527784656f9STejun Heo 		struct memblock_region *this = &type->regions[i];
528784656f9STejun Heo 		struct memblock_region *next = &type->regions[i + 1];
529784656f9STejun Heo 
5307c0caeb8STejun Heo 		if (this->base + this->size != next->base ||
5317c0caeb8STejun Heo 		    memblock_get_region_node(this) !=
53266a20757STang Chen 		    memblock_get_region_node(next) ||
53366a20757STang Chen 		    this->flags != next->flags) {
534784656f9STejun Heo 			BUG_ON(this->base + this->size > next->base);
535784656f9STejun Heo 			i++;
536784656f9STejun Heo 			continue;
537784656f9STejun Heo 		}
538784656f9STejun Heo 
539784656f9STejun Heo 		this->size += next->size;
540c0232ae8SLin Feng 		/* move forward from next + 1, index of which is i + 2 */
541c0232ae8SLin Feng 		memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
542784656f9STejun Heo 		type->cnt--;
543784656f9STejun Heo 	}
544784656f9STejun Heo }
545784656f9STejun Heo 
546784656f9STejun Heo /**
547784656f9STejun Heo  * memblock_insert_region - insert new memblock region
548784656f9STejun Heo  * @type:	memblock type to insert into
549784656f9STejun Heo  * @idx:	index for the insertion point
550784656f9STejun Heo  * @base:	base address of the new region
551784656f9STejun Heo  * @size:	size of the new region
552209ff86dSTang Chen  * @nid:	node id of the new region
55366a20757STang Chen  * @flags:	flags of the new region
554784656f9STejun Heo  *
555784656f9STejun Heo  * Insert new memblock region [@base, @base + @size) into @type at @idx.
556412d0008SAlexander Kuleshov  * @type must already have extra room to accommodate the new region.
557784656f9STejun Heo  */
558784656f9STejun Heo static void __init_memblock memblock_insert_region(struct memblock_type *type,
559784656f9STejun Heo 						   int idx, phys_addr_t base,
56066a20757STang Chen 						   phys_addr_t size,
561e1720feeSMike Rapoport 						   int nid,
562e1720feeSMike Rapoport 						   enum memblock_flags flags)
563784656f9STejun Heo {
564784656f9STejun Heo 	struct memblock_region *rgn = &type->regions[idx];
565784656f9STejun Heo 
566784656f9STejun Heo 	BUG_ON(type->cnt >= type->max);
567784656f9STejun Heo 	memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
568784656f9STejun Heo 	rgn->base = base;
569784656f9STejun Heo 	rgn->size = size;
57066a20757STang Chen 	rgn->flags = flags;
5717c0caeb8STejun Heo 	memblock_set_region_node(rgn, nid);
572784656f9STejun Heo 	type->cnt++;
5731440c4e2STejun Heo 	type->total_size += size;
574784656f9STejun Heo }
575784656f9STejun Heo 
576784656f9STejun Heo /**
577f1af9d3aSPhilipp Hachtmann  * memblock_add_range - add new memblock region
578784656f9STejun Heo  * @type: memblock type to add new region into
579784656f9STejun Heo  * @base: base address of the new region
580784656f9STejun Heo  * @size: size of the new region
5817fb0bc3fSTejun Heo  * @nid: nid of the new region
58266a20757STang Chen  * @flags: flags of the new region
583784656f9STejun Heo  *
584784656f9STejun Heo  * Add new memblock region [@base, @base + @size) into @type.  The new region
585784656f9STejun Heo  * is allowed to overlap with existing ones - overlaps don't affect already
586784656f9STejun Heo  * existing regions.  @type is guaranteed to be minimal (all neighbouring
587784656f9STejun Heo  * compatible regions are merged) after the addition.
588784656f9STejun Heo  *
58947cec443SMike Rapoport  * Return:
590784656f9STejun Heo  * 0 on success, -errno on failure.
591784656f9STejun Heo  */
59202634a44SAnshuman Khandual static int __init_memblock memblock_add_range(struct memblock_type *type,
59366a20757STang Chen 				phys_addr_t base, phys_addr_t size,
594e1720feeSMike Rapoport 				int nid, enum memblock_flags flags)
59595f72d1eSYinghai Lu {
596784656f9STejun Heo 	bool insert = false;
597eb18f1b5STejun Heo 	phys_addr_t obase = base;
598eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
5998c9c1701SAlexander Kuleshov 	int idx, nr_new;
6008c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
60195f72d1eSYinghai Lu 
602b3dc627cSTejun Heo 	if (!size)
603b3dc627cSTejun Heo 		return 0;
604b3dc627cSTejun Heo 
605784656f9STejun Heo 	/* special case for empty array */
606784656f9STejun Heo 	if (type->regions[0].size == 0) {
6071440c4e2STejun Heo 		WARN_ON(type->cnt != 1 || type->total_size);
608784656f9STejun Heo 		type->regions[0].base = base;
609784656f9STejun Heo 		type->regions[0].size = size;
61066a20757STang Chen 		type->regions[0].flags = flags;
6117fb0bc3fSTejun Heo 		memblock_set_region_node(&type->regions[0], nid);
6121440c4e2STejun Heo 		type->total_size = size;
613784656f9STejun Heo 		return 0;
614784656f9STejun Heo 	}
615784656f9STejun Heo repeat:
616784656f9STejun Heo 	/*
617784656f9STejun Heo 	 * The following is executed twice.  Once with %false @insert and
618784656f9STejun Heo 	 * then with %true.  The first counts the number of regions needed
619412d0008SAlexander Kuleshov 	 * to accommodate the new area.  The second actually inserts them.
620784656f9STejun Heo 	 */
621784656f9STejun Heo 	base = obase;
622784656f9STejun Heo 	nr_new = 0;
623784656f9STejun Heo 
62466e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
625784656f9STejun Heo 		phys_addr_t rbase = rgn->base;
626784656f9STejun Heo 		phys_addr_t rend = rbase + rgn->size;
6278f7a6605SBenjamin Herrenschmidt 
628784656f9STejun Heo 		if (rbase >= end)
6298f7a6605SBenjamin Herrenschmidt 			break;
630784656f9STejun Heo 		if (rend <= base)
631784656f9STejun Heo 			continue;
632784656f9STejun Heo 		/*
633784656f9STejun Heo 		 * @rgn overlaps.  If it separates the lower part of new
634784656f9STejun Heo 		 * area, insert that portion.
6358f7a6605SBenjamin Herrenschmidt 		 */
636784656f9STejun Heo 		if (rbase > base) {
6373f08a302SMike Rapoport #ifdef CONFIG_NEED_MULTIPLE_NODES
638c0a29498SWei Yang 			WARN_ON(nid != memblock_get_region_node(rgn));
639c0a29498SWei Yang #endif
6404fcab5f4SWei Yang 			WARN_ON(flags != rgn->flags);
641784656f9STejun Heo 			nr_new++;
642784656f9STejun Heo 			if (insert)
6438c9c1701SAlexander Kuleshov 				memblock_insert_region(type, idx++, base,
64466a20757STang Chen 						       rbase - base, nid,
64566a20757STang Chen 						       flags);
646784656f9STejun Heo 		}
647784656f9STejun Heo 		/* area below @rend is dealt with, forget about it */
648784656f9STejun Heo 		base = min(rend, end);
6498f7a6605SBenjamin Herrenschmidt 	}
6508f7a6605SBenjamin Herrenschmidt 
651784656f9STejun Heo 	/* insert the remaining portion */
652784656f9STejun Heo 	if (base < end) {
653784656f9STejun Heo 		nr_new++;
654784656f9STejun Heo 		if (insert)
6558c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, base, end - base,
65666a20757STang Chen 					       nid, flags);
6578f7a6605SBenjamin Herrenschmidt 	}
6588f7a6605SBenjamin Herrenschmidt 
659ef3cc4dbSnimisolo 	if (!nr_new)
660ef3cc4dbSnimisolo 		return 0;
661ef3cc4dbSnimisolo 
662784656f9STejun Heo 	/*
663784656f9STejun Heo 	 * If this was the first round, resize array and repeat for actual
664784656f9STejun Heo 	 * insertions; otherwise, merge and return.
6658f7a6605SBenjamin Herrenschmidt 	 */
666784656f9STejun Heo 	if (!insert) {
667784656f9STejun Heo 		while (type->cnt + nr_new > type->max)
66848c3b583SGreg Pearson 			if (memblock_double_array(type, obase, size) < 0)
669784656f9STejun Heo 				return -ENOMEM;
670784656f9STejun Heo 		insert = true;
671784656f9STejun Heo 		goto repeat;
67295f72d1eSYinghai Lu 	} else {
673784656f9STejun Heo 		memblock_merge_regions(type);
67495f72d1eSYinghai Lu 		return 0;
67595f72d1eSYinghai Lu 	}
676784656f9STejun Heo }
67795f72d1eSYinghai Lu 
67848a833ccSMike Rapoport /**
67948a833ccSMike Rapoport  * memblock_add_node - add new memblock region within a NUMA node
68048a833ccSMike Rapoport  * @base: base address of the new region
68148a833ccSMike Rapoport  * @size: size of the new region
68248a833ccSMike Rapoport  * @nid: nid of the new region
68348a833ccSMike Rapoport  *
68448a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
68548a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
68648a833ccSMike Rapoport  *
68748a833ccSMike Rapoport  * Return:
68848a833ccSMike Rapoport  * 0 on success, -errno on failure.
68948a833ccSMike Rapoport  */
6907fb0bc3fSTejun Heo int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
6917fb0bc3fSTejun Heo 				       int nid)
6927fb0bc3fSTejun Heo {
693f1af9d3aSPhilipp Hachtmann 	return memblock_add_range(&memblock.memory, base, size, nid, 0);
6947fb0bc3fSTejun Heo }
6957fb0bc3fSTejun Heo 
69648a833ccSMike Rapoport /**
69748a833ccSMike Rapoport  * memblock_add - add new memblock region
69848a833ccSMike Rapoport  * @base: base address of the new region
69948a833ccSMike Rapoport  * @size: size of the new region
70048a833ccSMike Rapoport  *
70148a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
70248a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
70348a833ccSMike Rapoport  *
70448a833ccSMike Rapoport  * Return:
70548a833ccSMike Rapoport  * 0 on success, -errno on failure.
70648a833ccSMike Rapoport  */
707f705ac4bSAlexander Kuleshov int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
7086a4055bcSAlexander Kuleshov {
7095d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
7105d63f81cSMiles Chen 
711a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
7125d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
7136a4055bcSAlexander Kuleshov 
714f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
71595f72d1eSYinghai Lu }
71695f72d1eSYinghai Lu 
7176a9ceb31STejun Heo /**
7186a9ceb31STejun Heo  * memblock_isolate_range - isolate given range into disjoint memblocks
7196a9ceb31STejun Heo  * @type: memblock type to isolate range for
7206a9ceb31STejun Heo  * @base: base of range to isolate
7216a9ceb31STejun Heo  * @size: size of range to isolate
7226a9ceb31STejun Heo  * @start_rgn: out parameter for the start of isolated region
7236a9ceb31STejun Heo  * @end_rgn: out parameter for the end of isolated region
7246a9ceb31STejun Heo  *
7256a9ceb31STejun Heo  * Walk @type and ensure that regions don't cross the boundaries defined by
7266a9ceb31STejun Heo  * [@base, @base + @size).  Crossing regions are split at the boundaries,
7276a9ceb31STejun Heo  * which may create at most two more regions.  The index of the first
7286a9ceb31STejun Heo  * region inside the range is returned in *@start_rgn and end in *@end_rgn.
7296a9ceb31STejun Heo  *
73047cec443SMike Rapoport  * Return:
7316a9ceb31STejun Heo  * 0 on success, -errno on failure.
7326a9ceb31STejun Heo  */
7336a9ceb31STejun Heo static int __init_memblock memblock_isolate_range(struct memblock_type *type,
7346a9ceb31STejun Heo 					phys_addr_t base, phys_addr_t size,
7356a9ceb31STejun Heo 					int *start_rgn, int *end_rgn)
7366a9ceb31STejun Heo {
737eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
7388c9c1701SAlexander Kuleshov 	int idx;
7398c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
7406a9ceb31STejun Heo 
7416a9ceb31STejun Heo 	*start_rgn = *end_rgn = 0;
7426a9ceb31STejun Heo 
743b3dc627cSTejun Heo 	if (!size)
744b3dc627cSTejun Heo 		return 0;
745b3dc627cSTejun Heo 
7466a9ceb31STejun Heo 	/* we'll create at most two more regions */
7476a9ceb31STejun Heo 	while (type->cnt + 2 > type->max)
74848c3b583SGreg Pearson 		if (memblock_double_array(type, base, size) < 0)
7496a9ceb31STejun Heo 			return -ENOMEM;
7506a9ceb31STejun Heo 
75166e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
7526a9ceb31STejun Heo 		phys_addr_t rbase = rgn->base;
7536a9ceb31STejun Heo 		phys_addr_t rend = rbase + rgn->size;
7546a9ceb31STejun Heo 
7556a9ceb31STejun Heo 		if (rbase >= end)
7566a9ceb31STejun Heo 			break;
7576a9ceb31STejun Heo 		if (rend <= base)
7586a9ceb31STejun Heo 			continue;
7596a9ceb31STejun Heo 
7606a9ceb31STejun Heo 		if (rbase < base) {
7616a9ceb31STejun Heo 			/*
7626a9ceb31STejun Heo 			 * @rgn intersects from below.  Split and continue
7636a9ceb31STejun Heo 			 * to process the next region - the new top half.
7646a9ceb31STejun Heo 			 */
7656a9ceb31STejun Heo 			rgn->base = base;
7661440c4e2STejun Heo 			rgn->size -= base - rbase;
7671440c4e2STejun Heo 			type->total_size -= base - rbase;
7688c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, rbase, base - rbase,
76966a20757STang Chen 					       memblock_get_region_node(rgn),
77066a20757STang Chen 					       rgn->flags);
7716a9ceb31STejun Heo 		} else if (rend > end) {
7726a9ceb31STejun Heo 			/*
7736a9ceb31STejun Heo 			 * @rgn intersects from above.  Split and redo the
7746a9ceb31STejun Heo 			 * current region - the new bottom half.
7756a9ceb31STejun Heo 			 */
7766a9ceb31STejun Heo 			rgn->base = end;
7771440c4e2STejun Heo 			rgn->size -= end - rbase;
7781440c4e2STejun Heo 			type->total_size -= end - rbase;
7798c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx--, rbase, end - rbase,
78066a20757STang Chen 					       memblock_get_region_node(rgn),
78166a20757STang Chen 					       rgn->flags);
7826a9ceb31STejun Heo 		} else {
7836a9ceb31STejun Heo 			/* @rgn is fully contained, record it */
7846a9ceb31STejun Heo 			if (!*end_rgn)
7858c9c1701SAlexander Kuleshov 				*start_rgn = idx;
7868c9c1701SAlexander Kuleshov 			*end_rgn = idx + 1;
7876a9ceb31STejun Heo 		}
7886a9ceb31STejun Heo 	}
7896a9ceb31STejun Heo 
7906a9ceb31STejun Heo 	return 0;
7916a9ceb31STejun Heo }
7926a9ceb31STejun Heo 
79335bd16a2SAlexander Kuleshov static int __init_memblock memblock_remove_range(struct memblock_type *type,
7948f7a6605SBenjamin Herrenschmidt 					  phys_addr_t base, phys_addr_t size)
79595f72d1eSYinghai Lu {
79671936180STejun Heo 	int start_rgn, end_rgn;
79771936180STejun Heo 	int i, ret;
79895f72d1eSYinghai Lu 
79971936180STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
80071936180STejun Heo 	if (ret)
80171936180STejun Heo 		return ret;
80295f72d1eSYinghai Lu 
80371936180STejun Heo 	for (i = end_rgn - 1; i >= start_rgn; i--)
80471936180STejun Heo 		memblock_remove_region(type, i);
80595f72d1eSYinghai Lu 	return 0;
80695f72d1eSYinghai Lu }
80795f72d1eSYinghai Lu 
808581adcbeSTejun Heo int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
80995f72d1eSYinghai Lu {
81025cf23d7SMinchan Kim 	phys_addr_t end = base + size - 1;
81125cf23d7SMinchan Kim 
812a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
81325cf23d7SMinchan Kim 		     &base, &end, (void *)_RET_IP_);
81425cf23d7SMinchan Kim 
815f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.memory, base, size);
81695f72d1eSYinghai Lu }
81795f72d1eSYinghai Lu 
8184d72868cSMike Rapoport /**
8194d72868cSMike Rapoport  * memblock_free - free boot memory block
8204d72868cSMike Rapoport  * @base: phys starting address of the  boot memory block
8214d72868cSMike Rapoport  * @size: size of the boot memory block in bytes
8224d72868cSMike Rapoport  *
8234d72868cSMike Rapoport  * Free boot memory block previously allocated by memblock_alloc_xx() API.
8244d72868cSMike Rapoport  * The freeing memory will not be released to the buddy allocator.
8254d72868cSMike Rapoport  */
826581adcbeSTejun Heo int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
82795f72d1eSYinghai Lu {
8285d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8295d63f81cSMiles Chen 
830a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
8315d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
83224aa0788STejun Heo 
8339099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
834f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.reserved, base, size);
83595f72d1eSYinghai Lu }
83695f72d1eSYinghai Lu 
837f705ac4bSAlexander Kuleshov int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
83895f72d1eSYinghai Lu {
8395d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8405d63f81cSMiles Chen 
841a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
8425d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
84395f72d1eSYinghai Lu 
844f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
84595f72d1eSYinghai Lu }
84695f72d1eSYinghai Lu 
84702634a44SAnshuman Khandual #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
84802634a44SAnshuman Khandual int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size)
84902634a44SAnshuman Khandual {
85002634a44SAnshuman Khandual 	phys_addr_t end = base + size - 1;
85102634a44SAnshuman Khandual 
85202634a44SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
85302634a44SAnshuman Khandual 		     &base, &end, (void *)_RET_IP_);
85402634a44SAnshuman Khandual 
85577649905SDavid Hildenbrand 	return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0);
85602634a44SAnshuman Khandual }
85702634a44SAnshuman Khandual #endif
85802634a44SAnshuman Khandual 
85935fd0808STejun Heo /**
86047cec443SMike Rapoport  * memblock_setclr_flag - set or clear flag for a memory region
86147cec443SMike Rapoport  * @base: base address of the region
86247cec443SMike Rapoport  * @size: size of the region
86347cec443SMike Rapoport  * @set: set or clear the flag
86447cec443SMike Rapoport  * @flag: the flag to udpate
86566b16edfSTang Chen  *
8664308ce17STony Luck  * This function isolates region [@base, @base + @size), and sets/clears flag
86766b16edfSTang Chen  *
86847cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
86966b16edfSTang Chen  */
8704308ce17STony Luck static int __init_memblock memblock_setclr_flag(phys_addr_t base,
8714308ce17STony Luck 				phys_addr_t size, int set, int flag)
87266b16edfSTang Chen {
87366b16edfSTang Chen 	struct memblock_type *type = &memblock.memory;
87466b16edfSTang Chen 	int i, ret, start_rgn, end_rgn;
87566b16edfSTang Chen 
87666b16edfSTang Chen 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
87766b16edfSTang Chen 	if (ret)
87866b16edfSTang Chen 		return ret;
87966b16edfSTang Chen 
880fe145124SMike Rapoport 	for (i = start_rgn; i < end_rgn; i++) {
881fe145124SMike Rapoport 		struct memblock_region *r = &type->regions[i];
882fe145124SMike Rapoport 
8834308ce17STony Luck 		if (set)
884fe145124SMike Rapoport 			r->flags |= flag;
8854308ce17STony Luck 		else
886fe145124SMike Rapoport 			r->flags &= ~flag;
887fe145124SMike Rapoport 	}
88866b16edfSTang Chen 
88966b16edfSTang Chen 	memblock_merge_regions(type);
89066b16edfSTang Chen 	return 0;
89166b16edfSTang Chen }
89266b16edfSTang Chen 
89366b16edfSTang Chen /**
8944308ce17STony Luck  * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
8954308ce17STony Luck  * @base: the base phys addr of the region
8964308ce17STony Luck  * @size: the size of the region
8974308ce17STony Luck  *
89847cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
8994308ce17STony Luck  */
9004308ce17STony Luck int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
9014308ce17STony Luck {
9024308ce17STony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
9034308ce17STony Luck }
9044308ce17STony Luck 
9054308ce17STony Luck /**
90666b16edfSTang Chen  * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
90766b16edfSTang Chen  * @base: the base phys addr of the region
90866b16edfSTang Chen  * @size: the size of the region
90966b16edfSTang Chen  *
91047cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
91166b16edfSTang Chen  */
91266b16edfSTang Chen int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
91366b16edfSTang Chen {
9144308ce17STony Luck 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
91566b16edfSTang Chen }
91666b16edfSTang Chen 
91766b16edfSTang Chen /**
918a3f5bafcSTony Luck  * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
919a3f5bafcSTony Luck  * @base: the base phys addr of the region
920a3f5bafcSTony Luck  * @size: the size of the region
921a3f5bafcSTony Luck  *
92247cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
923a3f5bafcSTony Luck  */
924a3f5bafcSTony Luck int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
925a3f5bafcSTony Luck {
926a3f5bafcSTony Luck 	system_has_some_mirror = true;
927a3f5bafcSTony Luck 
928a3f5bafcSTony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
929a3f5bafcSTony Luck }
930a3f5bafcSTony Luck 
931bf3d3cc5SArd Biesheuvel /**
932bf3d3cc5SArd Biesheuvel  * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
933bf3d3cc5SArd Biesheuvel  * @base: the base phys addr of the region
934bf3d3cc5SArd Biesheuvel  * @size: the size of the region
935bf3d3cc5SArd Biesheuvel  *
93647cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
937bf3d3cc5SArd Biesheuvel  */
938bf3d3cc5SArd Biesheuvel int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
939bf3d3cc5SArd Biesheuvel {
940bf3d3cc5SArd Biesheuvel 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
941bf3d3cc5SArd Biesheuvel }
942a3f5bafcSTony Luck 
943a3f5bafcSTony Luck /**
9444c546b8aSAKASHI Takahiro  * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
9454c546b8aSAKASHI Takahiro  * @base: the base phys addr of the region
9464c546b8aSAKASHI Takahiro  * @size: the size of the region
9474c546b8aSAKASHI Takahiro  *
94847cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
9494c546b8aSAKASHI Takahiro  */
9504c546b8aSAKASHI Takahiro int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
9514c546b8aSAKASHI Takahiro {
9524c546b8aSAKASHI Takahiro 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
9534c546b8aSAKASHI Takahiro }
9544c546b8aSAKASHI Takahiro 
9554c546b8aSAKASHI Takahiro /**
9568e7a7f86SRobin Holt  * __next_reserved_mem_region - next function for for_each_reserved_region()
9578e7a7f86SRobin Holt  * @idx: pointer to u64 loop variable
9588e7a7f86SRobin Holt  * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
9598e7a7f86SRobin Holt  * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
9608e7a7f86SRobin Holt  *
9618e7a7f86SRobin Holt  * Iterate over all reserved memory regions.
9628e7a7f86SRobin Holt  */
9638e7a7f86SRobin Holt void __init_memblock __next_reserved_mem_region(u64 *idx,
9648e7a7f86SRobin Holt 					   phys_addr_t *out_start,
9658e7a7f86SRobin Holt 					   phys_addr_t *out_end)
9668e7a7f86SRobin Holt {
967567d117bSAlexander Kuleshov 	struct memblock_type *type = &memblock.reserved;
9688e7a7f86SRobin Holt 
969cd33a76bSRichard Leitner 	if (*idx < type->cnt) {
970567d117bSAlexander Kuleshov 		struct memblock_region *r = &type->regions[*idx];
9718e7a7f86SRobin Holt 		phys_addr_t base = r->base;
9728e7a7f86SRobin Holt 		phys_addr_t size = r->size;
9738e7a7f86SRobin Holt 
9748e7a7f86SRobin Holt 		if (out_start)
9758e7a7f86SRobin Holt 			*out_start = base;
9768e7a7f86SRobin Holt 		if (out_end)
9778e7a7f86SRobin Holt 			*out_end = base + size - 1;
9788e7a7f86SRobin Holt 
9798e7a7f86SRobin Holt 		*idx += 1;
9808e7a7f86SRobin Holt 		return;
9818e7a7f86SRobin Holt 	}
9828e7a7f86SRobin Holt 
9838e7a7f86SRobin Holt 	/* signal end of iteration */
9848e7a7f86SRobin Holt 	*idx = ULLONG_MAX;
9858e7a7f86SRobin Holt }
9868e7a7f86SRobin Holt 
987c9a688a3SMike Rapoport static bool should_skip_region(struct memblock_region *m, int nid, int flags)
988c9a688a3SMike Rapoport {
989c9a688a3SMike Rapoport 	int m_nid = memblock_get_region_node(m);
990c9a688a3SMike Rapoport 
991c9a688a3SMike Rapoport 	/* only memory regions are associated with nodes, check it */
992c9a688a3SMike Rapoport 	if (nid != NUMA_NO_NODE && nid != m_nid)
993c9a688a3SMike Rapoport 		return true;
994c9a688a3SMike Rapoport 
995c9a688a3SMike Rapoport 	/* skip hotpluggable memory regions if needed */
996c9a688a3SMike Rapoport 	if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
997c9a688a3SMike Rapoport 		return true;
998c9a688a3SMike Rapoport 
999c9a688a3SMike Rapoport 	/* if we want mirror memory skip non-mirror memory regions */
1000c9a688a3SMike Rapoport 	if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1001c9a688a3SMike Rapoport 		return true;
1002c9a688a3SMike Rapoport 
1003c9a688a3SMike Rapoport 	/* skip nomap memory unless we were asked for it explicitly */
1004c9a688a3SMike Rapoport 	if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1005c9a688a3SMike Rapoport 		return true;
1006c9a688a3SMike Rapoport 
1007c9a688a3SMike Rapoport 	return false;
1008c9a688a3SMike Rapoport }
1009c9a688a3SMike Rapoport 
10108e7a7f86SRobin Holt /**
1011a2974133SMike Rapoport  * __next_mem_range - next function for for_each_free_mem_range() etc.
101235fd0808STejun Heo  * @idx: pointer to u64 loop variable
1013b1154233SGrygorii Strashko  * @nid: node selector, %NUMA_NO_NODE for all nodes
1014fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
1015f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
1016f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
1017dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1018dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1019dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
102035fd0808STejun Heo  *
1021f1af9d3aSPhilipp Hachtmann  * Find the first area from *@idx which matches @nid, fill the out
102235fd0808STejun Heo  * parameters, and update *@idx for the next iteration.  The lower 32bit of
1023f1af9d3aSPhilipp Hachtmann  * *@idx contains index into type_a and the upper 32bit indexes the
1024f1af9d3aSPhilipp Hachtmann  * areas before each region in type_b.	For example, if type_b regions
102535fd0808STejun Heo  * look like the following,
102635fd0808STejun Heo  *
102735fd0808STejun Heo  *	0:[0-16), 1:[32-48), 2:[128-130)
102835fd0808STejun Heo  *
102935fd0808STejun Heo  * The upper 32bit indexes the following regions.
103035fd0808STejun Heo  *
103135fd0808STejun Heo  *	0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
103235fd0808STejun Heo  *
103335fd0808STejun Heo  * As both region arrays are sorted, the function advances the two indices
103435fd0808STejun Heo  * in lockstep and returns each intersection.
103535fd0808STejun Heo  */
103677649905SDavid Hildenbrand void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
1037f1af9d3aSPhilipp Hachtmann 		      struct memblock_type *type_a,
103877649905SDavid Hildenbrand 		      struct memblock_type *type_b, phys_addr_t *out_start,
103935fd0808STejun Heo 		      phys_addr_t *out_end, int *out_nid)
104035fd0808STejun Heo {
1041f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1042f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1043b1154233SGrygorii Strashko 
1044f1af9d3aSPhilipp Hachtmann 	if (WARN_ONCE(nid == MAX_NUMNODES,
1045f1af9d3aSPhilipp Hachtmann 	"Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1046560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
104735fd0808STejun Heo 
1048f1af9d3aSPhilipp Hachtmann 	for (; idx_a < type_a->cnt; idx_a++) {
1049f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1050f1af9d3aSPhilipp Hachtmann 
105135fd0808STejun Heo 		phys_addr_t m_start = m->base;
105235fd0808STejun Heo 		phys_addr_t m_end = m->base + m->size;
1053f1af9d3aSPhilipp Hachtmann 		int	    m_nid = memblock_get_region_node(m);
105435fd0808STejun Heo 
1055c9a688a3SMike Rapoport 		if (should_skip_region(m, nid, flags))
1056bf3d3cc5SArd Biesheuvel 			continue;
1057bf3d3cc5SArd Biesheuvel 
1058f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1059f1af9d3aSPhilipp Hachtmann 			if (out_start)
1060f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1061f1af9d3aSPhilipp Hachtmann 			if (out_end)
1062f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1063f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1064f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1065f1af9d3aSPhilipp Hachtmann 			idx_a++;
1066f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1067f1af9d3aSPhilipp Hachtmann 			return;
1068f1af9d3aSPhilipp Hachtmann 		}
106935fd0808STejun Heo 
1070f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1071f1af9d3aSPhilipp Hachtmann 		for (; idx_b < type_b->cnt + 1; idx_b++) {
1072f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1073f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1074f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1075f1af9d3aSPhilipp Hachtmann 
1076f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1077f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1078f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
10791c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1080f1af9d3aSPhilipp Hachtmann 
1081f1af9d3aSPhilipp Hachtmann 			/*
1082f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1083f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1084f1af9d3aSPhilipp Hachtmann 			 */
108535fd0808STejun Heo 			if (r_start >= m_end)
108635fd0808STejun Heo 				break;
108735fd0808STejun Heo 			/* if the two regions intersect, we're done */
108835fd0808STejun Heo 			if (m_start < r_end) {
108935fd0808STejun Heo 				if (out_start)
1090f1af9d3aSPhilipp Hachtmann 					*out_start =
1091f1af9d3aSPhilipp Hachtmann 						max(m_start, r_start);
109235fd0808STejun Heo 				if (out_end)
109335fd0808STejun Heo 					*out_end = min(m_end, r_end);
109435fd0808STejun Heo 				if (out_nid)
1095f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
109635fd0808STejun Heo 				/*
1097f1af9d3aSPhilipp Hachtmann 				 * The region which ends first is
1098f1af9d3aSPhilipp Hachtmann 				 * advanced for the next iteration.
109935fd0808STejun Heo 				 */
110035fd0808STejun Heo 				if (m_end <= r_end)
1101f1af9d3aSPhilipp Hachtmann 					idx_a++;
110235fd0808STejun Heo 				else
1103f1af9d3aSPhilipp Hachtmann 					idx_b++;
1104f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
110535fd0808STejun Heo 				return;
110635fd0808STejun Heo 			}
110735fd0808STejun Heo 		}
110835fd0808STejun Heo 	}
110935fd0808STejun Heo 
111035fd0808STejun Heo 	/* signal end of iteration */
111135fd0808STejun Heo 	*idx = ULLONG_MAX;
111235fd0808STejun Heo }
111335fd0808STejun Heo 
11147bd0b0f0STejun Heo /**
1115f1af9d3aSPhilipp Hachtmann  * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1116f1af9d3aSPhilipp Hachtmann  *
11177bd0b0f0STejun Heo  * @idx: pointer to u64 loop variable
1118ad5ea8cdSAlexander Kuleshov  * @nid: node selector, %NUMA_NO_NODE for all nodes
1119fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
1120f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
1121f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
1122dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1123dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1124dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
11257bd0b0f0STejun Heo  *
112647cec443SMike Rapoport  * Finds the next range from type_a which is not marked as unsuitable
112747cec443SMike Rapoport  * in type_b.
112847cec443SMike Rapoport  *
1129f1af9d3aSPhilipp Hachtmann  * Reverse of __next_mem_range().
11307bd0b0f0STejun Heo  */
1131e1720feeSMike Rapoport void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1132e1720feeSMike Rapoport 					  enum memblock_flags flags,
1133f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_a,
1134f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_b,
11357bd0b0f0STejun Heo 					  phys_addr_t *out_start,
11367bd0b0f0STejun Heo 					  phys_addr_t *out_end, int *out_nid)
11377bd0b0f0STejun Heo {
1138f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1139f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1140b1154233SGrygorii Strashko 
1141560dca27SGrygorii Strashko 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1142560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
11437bd0b0f0STejun Heo 
11447bd0b0f0STejun Heo 	if (*idx == (u64)ULLONG_MAX) {
1145f1af9d3aSPhilipp Hachtmann 		idx_a = type_a->cnt - 1;
1146e47608abSzijun_hu 		if (type_b != NULL)
1147f1af9d3aSPhilipp Hachtmann 			idx_b = type_b->cnt;
1148e47608abSzijun_hu 		else
1149e47608abSzijun_hu 			idx_b = 0;
11507bd0b0f0STejun Heo 	}
11517bd0b0f0STejun Heo 
1152f1af9d3aSPhilipp Hachtmann 	for (; idx_a >= 0; idx_a--) {
1153f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1154f1af9d3aSPhilipp Hachtmann 
11557bd0b0f0STejun Heo 		phys_addr_t m_start = m->base;
11567bd0b0f0STejun Heo 		phys_addr_t m_end = m->base + m->size;
1157f1af9d3aSPhilipp Hachtmann 		int m_nid = memblock_get_region_node(m);
11587bd0b0f0STejun Heo 
1159c9a688a3SMike Rapoport 		if (should_skip_region(m, nid, flags))
1160bf3d3cc5SArd Biesheuvel 			continue;
1161bf3d3cc5SArd Biesheuvel 
1162f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1163f1af9d3aSPhilipp Hachtmann 			if (out_start)
1164f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1165f1af9d3aSPhilipp Hachtmann 			if (out_end)
1166f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1167f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1168f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1169fb399b48Szijun_hu 			idx_a--;
1170f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1171f1af9d3aSPhilipp Hachtmann 			return;
1172f1af9d3aSPhilipp Hachtmann 		}
11737bd0b0f0STejun Heo 
1174f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1175f1af9d3aSPhilipp Hachtmann 		for (; idx_b >= 0; idx_b--) {
1176f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1177f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1178f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1179f1af9d3aSPhilipp Hachtmann 
1180f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1181f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1182f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
11831c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1184f1af9d3aSPhilipp Hachtmann 			/*
1185f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1186f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1187f1af9d3aSPhilipp Hachtmann 			 */
1188f1af9d3aSPhilipp Hachtmann 
11897bd0b0f0STejun Heo 			if (r_end <= m_start)
11907bd0b0f0STejun Heo 				break;
11917bd0b0f0STejun Heo 			/* if the two regions intersect, we're done */
11927bd0b0f0STejun Heo 			if (m_end > r_start) {
11937bd0b0f0STejun Heo 				if (out_start)
11947bd0b0f0STejun Heo 					*out_start = max(m_start, r_start);
11957bd0b0f0STejun Heo 				if (out_end)
11967bd0b0f0STejun Heo 					*out_end = min(m_end, r_end);
11977bd0b0f0STejun Heo 				if (out_nid)
1198f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
11997bd0b0f0STejun Heo 				if (m_start >= r_start)
1200f1af9d3aSPhilipp Hachtmann 					idx_a--;
12017bd0b0f0STejun Heo 				else
1202f1af9d3aSPhilipp Hachtmann 					idx_b--;
1203f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
12047bd0b0f0STejun Heo 				return;
12057bd0b0f0STejun Heo 			}
12067bd0b0f0STejun Heo 		}
12077bd0b0f0STejun Heo 	}
1208f1af9d3aSPhilipp Hachtmann 	/* signal end of iteration */
12097bd0b0f0STejun Heo 	*idx = ULLONG_MAX;
12107bd0b0f0STejun Heo }
12117bd0b0f0STejun Heo 
12127c0caeb8STejun Heo /*
121345e79815SChen Chang  * Common iterator interface used to define for_each_mem_pfn_range().
12147c0caeb8STejun Heo  */
12157c0caeb8STejun Heo void __init_memblock __next_mem_pfn_range(int *idx, int nid,
12167c0caeb8STejun Heo 				unsigned long *out_start_pfn,
12177c0caeb8STejun Heo 				unsigned long *out_end_pfn, int *out_nid)
12187c0caeb8STejun Heo {
12197c0caeb8STejun Heo 	struct memblock_type *type = &memblock.memory;
12207c0caeb8STejun Heo 	struct memblock_region *r;
1221d622abf7SMike Rapoport 	int r_nid;
12227c0caeb8STejun Heo 
12237c0caeb8STejun Heo 	while (++*idx < type->cnt) {
12247c0caeb8STejun Heo 		r = &type->regions[*idx];
1225d622abf7SMike Rapoport 		r_nid = memblock_get_region_node(r);
12267c0caeb8STejun Heo 
12277c0caeb8STejun Heo 		if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
12287c0caeb8STejun Heo 			continue;
1229d622abf7SMike Rapoport 		if (nid == MAX_NUMNODES || nid == r_nid)
12307c0caeb8STejun Heo 			break;
12317c0caeb8STejun Heo 	}
12327c0caeb8STejun Heo 	if (*idx >= type->cnt) {
12337c0caeb8STejun Heo 		*idx = -1;
12347c0caeb8STejun Heo 		return;
12357c0caeb8STejun Heo 	}
12367c0caeb8STejun Heo 
12377c0caeb8STejun Heo 	if (out_start_pfn)
12387c0caeb8STejun Heo 		*out_start_pfn = PFN_UP(r->base);
12397c0caeb8STejun Heo 	if (out_end_pfn)
12407c0caeb8STejun Heo 		*out_end_pfn = PFN_DOWN(r->base + r->size);
12417c0caeb8STejun Heo 	if (out_nid)
1242d622abf7SMike Rapoport 		*out_nid = r_nid;
12437c0caeb8STejun Heo }
12447c0caeb8STejun Heo 
12457c0caeb8STejun Heo /**
12467c0caeb8STejun Heo  * memblock_set_node - set node ID on memblock regions
12477c0caeb8STejun Heo  * @base: base of area to set node ID for
12487c0caeb8STejun Heo  * @size: size of area to set node ID for
1249e7e8de59STang Chen  * @type: memblock type to set node ID for
12507c0caeb8STejun Heo  * @nid: node ID to set
12517c0caeb8STejun Heo  *
1252e7e8de59STang Chen  * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
12537c0caeb8STejun Heo  * Regions which cross the area boundaries are split as necessary.
12547c0caeb8STejun Heo  *
125547cec443SMike Rapoport  * Return:
12567c0caeb8STejun Heo  * 0 on success, -errno on failure.
12577c0caeb8STejun Heo  */
12587c0caeb8STejun Heo int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1259e7e8de59STang Chen 				      struct memblock_type *type, int nid)
12607c0caeb8STejun Heo {
12613f08a302SMike Rapoport #ifdef CONFIG_NEED_MULTIPLE_NODES
12626a9ceb31STejun Heo 	int start_rgn, end_rgn;
12636a9ceb31STejun Heo 	int i, ret;
12647c0caeb8STejun Heo 
12656a9ceb31STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
12666a9ceb31STejun Heo 	if (ret)
12676a9ceb31STejun Heo 		return ret;
12687c0caeb8STejun Heo 
12696a9ceb31STejun Heo 	for (i = start_rgn; i < end_rgn; i++)
1270e9d24ad3SWanpeng Li 		memblock_set_region_node(&type->regions[i], nid);
12717c0caeb8STejun Heo 
12727c0caeb8STejun Heo 	memblock_merge_regions(type);
12733f08a302SMike Rapoport #endif
12747c0caeb8STejun Heo 	return 0;
12757c0caeb8STejun Heo }
12763f08a302SMike Rapoport 
1277837566e7SAlexander Duyck #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1278837566e7SAlexander Duyck /**
1279837566e7SAlexander Duyck  * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
1280837566e7SAlexander Duyck  *
1281837566e7SAlexander Duyck  * @idx: pointer to u64 loop variable
1282837566e7SAlexander Duyck  * @zone: zone in which all of the memory blocks reside
1283837566e7SAlexander Duyck  * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
1284837566e7SAlexander Duyck  * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
1285837566e7SAlexander Duyck  *
1286837566e7SAlexander Duyck  * This function is meant to be a zone/pfn specific wrapper for the
1287837566e7SAlexander Duyck  * for_each_mem_range type iterators. Specifically they are used in the
1288837566e7SAlexander Duyck  * deferred memory init routines and as such we were duplicating much of
1289837566e7SAlexander Duyck  * this logic throughout the code. So instead of having it in multiple
1290837566e7SAlexander Duyck  * locations it seemed like it would make more sense to centralize this to
1291837566e7SAlexander Duyck  * one new iterator that does everything they need.
1292837566e7SAlexander Duyck  */
1293837566e7SAlexander Duyck void __init_memblock
1294837566e7SAlexander Duyck __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
1295837566e7SAlexander Duyck 			     unsigned long *out_spfn, unsigned long *out_epfn)
1296837566e7SAlexander Duyck {
1297837566e7SAlexander Duyck 	int zone_nid = zone_to_nid(zone);
1298837566e7SAlexander Duyck 	phys_addr_t spa, epa;
1299837566e7SAlexander Duyck 	int nid;
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 	while (*idx != U64_MAX) {
1306837566e7SAlexander Duyck 		unsigned long epfn = PFN_DOWN(epa);
1307837566e7SAlexander Duyck 		unsigned long spfn = PFN_UP(spa);
1308837566e7SAlexander Duyck 
1309837566e7SAlexander Duyck 		/*
1310837566e7SAlexander Duyck 		 * Verify the end is at least past the start of the zone and
1311837566e7SAlexander Duyck 		 * that we have at least one PFN to initialize.
1312837566e7SAlexander Duyck 		 */
1313837566e7SAlexander Duyck 		if (zone->zone_start_pfn < epfn && spfn < epfn) {
1314837566e7SAlexander Duyck 			/* if we went too far just stop searching */
1315837566e7SAlexander Duyck 			if (zone_end_pfn(zone) <= spfn) {
1316837566e7SAlexander Duyck 				*idx = U64_MAX;
1317837566e7SAlexander Duyck 				break;
1318837566e7SAlexander Duyck 			}
1319837566e7SAlexander Duyck 
1320837566e7SAlexander Duyck 			if (out_spfn)
1321837566e7SAlexander Duyck 				*out_spfn = max(zone->zone_start_pfn, spfn);
1322837566e7SAlexander Duyck 			if (out_epfn)
1323837566e7SAlexander Duyck 				*out_epfn = min(zone_end_pfn(zone), epfn);
1324837566e7SAlexander Duyck 
1325837566e7SAlexander Duyck 			return;
1326837566e7SAlexander Duyck 		}
1327837566e7SAlexander Duyck 
1328837566e7SAlexander Duyck 		__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1329837566e7SAlexander Duyck 				 &memblock.memory, &memblock.reserved,
1330837566e7SAlexander Duyck 				 &spa, &epa, &nid);
1331837566e7SAlexander Duyck 	}
1332837566e7SAlexander Duyck 
1333837566e7SAlexander Duyck 	/* signal end of iteration */
1334837566e7SAlexander Duyck 	if (out_spfn)
1335837566e7SAlexander Duyck 		*out_spfn = ULONG_MAX;
1336837566e7SAlexander Duyck 	if (out_epfn)
1337837566e7SAlexander Duyck 		*out_epfn = 0;
1338837566e7SAlexander Duyck }
1339837566e7SAlexander Duyck 
1340837566e7SAlexander Duyck #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
13417c0caeb8STejun Heo 
134292d12f95SMike Rapoport /**
134392d12f95SMike Rapoport  * memblock_alloc_range_nid - allocate boot memory block
134492d12f95SMike Rapoport  * @size: size of memory block to be allocated in bytes
134592d12f95SMike Rapoport  * @align: alignment of the region and block's size
134692d12f95SMike Rapoport  * @start: the lower bound of the memory region to allocate (phys address)
134792d12f95SMike Rapoport  * @end: the upper bound of the memory region to allocate (phys address)
134892d12f95SMike Rapoport  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
13490ac398b1SYunfeng Ye  * @exact_nid: control the allocation fall back to other nodes
135092d12f95SMike Rapoport  *
135192d12f95SMike Rapoport  * The allocation is performed from memory region limited by
135295830666SCao jin  * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE.
135392d12f95SMike Rapoport  *
13540ac398b1SYunfeng Ye  * If the specified node can not hold the requested memory and @exact_nid
13550ac398b1SYunfeng Ye  * is false, the allocation falls back to any node in the system.
135692d12f95SMike Rapoport  *
135792d12f95SMike Rapoport  * For systems with memory mirroring, the allocation is attempted first
135892d12f95SMike Rapoport  * from the regions with mirroring enabled and then retried from any
135992d12f95SMike Rapoport  * memory region.
136092d12f95SMike Rapoport  *
136192d12f95SMike Rapoport  * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for
136292d12f95SMike Rapoport  * allocated boot memory block, so that it is never reported as leaks.
136392d12f95SMike Rapoport  *
136492d12f95SMike Rapoport  * Return:
136592d12f95SMike Rapoport  * Physical address of allocated memory block on success, %0 on failure.
136692d12f95SMike Rapoport  */
13678676af1fSAslan Bakirov phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
13682bfc2862SAkinobu Mita 					phys_addr_t align, phys_addr_t start,
13690ac398b1SYunfeng Ye 					phys_addr_t end, int nid,
13700ac398b1SYunfeng Ye 					bool exact_nid)
137195f72d1eSYinghai Lu {
137292d12f95SMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
13736ed311b2SBenjamin Herrenschmidt 	phys_addr_t found;
137495f72d1eSYinghai Lu 
137592d12f95SMike Rapoport 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
137692d12f95SMike Rapoport 		nid = NUMA_NO_NODE;
137792d12f95SMike Rapoport 
13782f770806SMike Rapoport 	if (!align) {
13792f770806SMike Rapoport 		/* Can't use WARNs this early in boot on powerpc */
13802f770806SMike Rapoport 		dump_stack();
13812f770806SMike Rapoport 		align = SMP_CACHE_BYTES;
13822f770806SMike Rapoport 	}
13832f770806SMike Rapoport 
138492d12f95SMike Rapoport again:
1385fc6daaf9STony Luck 	found = memblock_find_in_range_node(size, align, start, end, nid,
1386fc6daaf9STony Luck 					    flags);
138792d12f95SMike Rapoport 	if (found && !memblock_reserve(found, size))
138892d12f95SMike Rapoport 		goto done;
138992d12f95SMike Rapoport 
13900ac398b1SYunfeng Ye 	if (nid != NUMA_NO_NODE && !exact_nid) {
139192d12f95SMike Rapoport 		found = memblock_find_in_range_node(size, align, start,
139292d12f95SMike Rapoport 						    end, NUMA_NO_NODE,
139392d12f95SMike Rapoport 						    flags);
139492d12f95SMike Rapoport 		if (found && !memblock_reserve(found, size))
139592d12f95SMike Rapoport 			goto done;
139692d12f95SMike Rapoport 	}
139792d12f95SMike Rapoport 
139892d12f95SMike Rapoport 	if (flags & MEMBLOCK_MIRROR) {
139992d12f95SMike Rapoport 		flags &= ~MEMBLOCK_MIRROR;
140092d12f95SMike Rapoport 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
140192d12f95SMike Rapoport 			&size);
140292d12f95SMike Rapoport 		goto again;
140392d12f95SMike Rapoport 	}
140492d12f95SMike Rapoport 
140592d12f95SMike Rapoport 	return 0;
140692d12f95SMike Rapoport 
140792d12f95SMike Rapoport done:
140892d12f95SMike Rapoport 	/* Skip kmemleak for kasan_init() due to high volume. */
140992d12f95SMike Rapoport 	if (end != MEMBLOCK_ALLOC_KASAN)
1410aedf95eaSCatalin Marinas 		/*
141192d12f95SMike Rapoport 		 * The min_count is set to 0 so that memblock allocated
141292d12f95SMike Rapoport 		 * blocks are never reported as leaks. This is because many
141392d12f95SMike Rapoport 		 * of these blocks are only referred via the physical
141492d12f95SMike Rapoport 		 * address which is not looked up by kmemleak.
1415aedf95eaSCatalin Marinas 		 */
14169099daedSCatalin Marinas 		kmemleak_alloc_phys(found, size, 0, 0);
141792d12f95SMike Rapoport 
14186ed311b2SBenjamin Herrenschmidt 	return found;
1419aedf95eaSCatalin Marinas }
142095f72d1eSYinghai Lu 
1421a2974133SMike Rapoport /**
1422a2974133SMike Rapoport  * memblock_phys_alloc_range - allocate a memory block inside specified range
1423a2974133SMike Rapoport  * @size: size of memory block to be allocated in bytes
1424a2974133SMike Rapoport  * @align: alignment of the region and block's size
1425a2974133SMike Rapoport  * @start: the lower bound of the memory region to allocate (physical address)
1426a2974133SMike Rapoport  * @end: the upper bound of the memory region to allocate (physical address)
1427a2974133SMike Rapoport  *
1428a2974133SMike Rapoport  * Allocate @size bytes in the between @start and @end.
1429a2974133SMike Rapoport  *
1430a2974133SMike Rapoport  * Return: physical address of the allocated memory block on success,
1431a2974133SMike Rapoport  * %0 on failure.
1432a2974133SMike Rapoport  */
14338a770c2aSMike Rapoport phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
14348a770c2aSMike Rapoport 					     phys_addr_t align,
14358a770c2aSMike Rapoport 					     phys_addr_t start,
14368a770c2aSMike Rapoport 					     phys_addr_t end)
14372bfc2862SAkinobu Mita {
14380ac398b1SYunfeng Ye 	return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
14390ac398b1SYunfeng Ye 					false);
14407bd0b0f0STejun Heo }
14417bd0b0f0STejun Heo 
1442a2974133SMike Rapoport /**
1443a2974133SMike Rapoport  * memblock_phys_alloc_try_nid - allocate a memory block from specified MUMA node
1444a2974133SMike Rapoport  * @size: size of memory block to be allocated in bytes
1445a2974133SMike Rapoport  * @align: alignment of the region and block's size
1446a2974133SMike Rapoport  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1447a2974133SMike Rapoport  *
1448a2974133SMike Rapoport  * Allocates memory block from the specified NUMA node. If the node
1449a2974133SMike Rapoport  * has no available memory, attempts to allocated from any node in the
1450a2974133SMike Rapoport  * system.
1451a2974133SMike Rapoport  *
1452a2974133SMike Rapoport  * Return: physical address of the allocated memory block on success,
1453a2974133SMike Rapoport  * %0 on failure.
1454a2974133SMike Rapoport  */
14559a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
14569d1e2492SBenjamin Herrenschmidt {
145733755574SMike Rapoport 	return memblock_alloc_range_nid(size, align, 0,
14580ac398b1SYunfeng Ye 					MEMBLOCK_ALLOC_ACCESSIBLE, nid, false);
145995f72d1eSYinghai Lu }
146095f72d1eSYinghai Lu 
146126f09e9bSSantosh Shilimkar /**
1462eb31d559SMike Rapoport  * memblock_alloc_internal - allocate boot memory block
146326f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
146426f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
146526f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region to allocate (phys address)
146626f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region to allocate (phys address)
146726f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
14680ac398b1SYunfeng Ye  * @exact_nid: control the allocation fall back to other nodes
146926f09e9bSSantosh Shilimkar  *
147092d12f95SMike Rapoport  * Allocates memory block using memblock_alloc_range_nid() and
147192d12f95SMike Rapoport  * converts the returned physical address to virtual.
147292d12f95SMike Rapoport  *
147326f09e9bSSantosh Shilimkar  * The @min_addr limit is dropped if it can not be satisfied and the allocation
147492d12f95SMike Rapoport  * will fall back to memory below @min_addr. Other constraints, such
147592d12f95SMike Rapoport  * as node and mirrored memory will be handled again in
147692d12f95SMike Rapoport  * memblock_alloc_range_nid().
147726f09e9bSSantosh Shilimkar  *
147847cec443SMike Rapoport  * Return:
147926f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
148026f09e9bSSantosh Shilimkar  */
1481eb31d559SMike Rapoport static void * __init memblock_alloc_internal(
148226f09e9bSSantosh Shilimkar 				phys_addr_t size, phys_addr_t align,
148326f09e9bSSantosh Shilimkar 				phys_addr_t min_addr, phys_addr_t max_addr,
14840ac398b1SYunfeng Ye 				int nid, bool exact_nid)
148526f09e9bSSantosh Shilimkar {
148626f09e9bSSantosh Shilimkar 	phys_addr_t alloc;
148726f09e9bSSantosh Shilimkar 
148826f09e9bSSantosh Shilimkar 	/*
148926f09e9bSSantosh Shilimkar 	 * Detect any accidental use of these APIs after slab is ready, as at
149026f09e9bSSantosh Shilimkar 	 * this moment memblock may be deinitialized already and its
1491c6ffc5caSMike Rapoport 	 * internal data may be destroyed (after execution of memblock_free_all)
149226f09e9bSSantosh Shilimkar 	 */
149326f09e9bSSantosh Shilimkar 	if (WARN_ON_ONCE(slab_is_available()))
149426f09e9bSSantosh Shilimkar 		return kzalloc_node(size, GFP_NOWAIT, nid);
149526f09e9bSSantosh Shilimkar 
1496f3057ad7SMike Rapoport 	if (max_addr > memblock.current_limit)
1497f3057ad7SMike Rapoport 		max_addr = memblock.current_limit;
1498f3057ad7SMike Rapoport 
14990ac398b1SYunfeng Ye 	alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid,
15000ac398b1SYunfeng Ye 					exact_nid);
15012f770806SMike Rapoport 
150292d12f95SMike Rapoport 	/* retry allocation without lower limit */
150392d12f95SMike Rapoport 	if (!alloc && min_addr)
15040ac398b1SYunfeng Ye 		alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid,
15050ac398b1SYunfeng Ye 						exact_nid);
150626f09e9bSSantosh Shilimkar 
150792d12f95SMike Rapoport 	if (!alloc)
1508a3f5bafcSTony Luck 		return NULL;
150926f09e9bSSantosh Shilimkar 
151092d12f95SMike Rapoport 	return phys_to_virt(alloc);
151126f09e9bSSantosh Shilimkar }
151226f09e9bSSantosh Shilimkar 
151326f09e9bSSantosh Shilimkar /**
15140ac398b1SYunfeng Ye  * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node
15150ac398b1SYunfeng Ye  * without zeroing memory
15160ac398b1SYunfeng Ye  * @size: size of memory block to be allocated in bytes
15170ac398b1SYunfeng Ye  * @align: alignment of the region and block's size
15180ac398b1SYunfeng Ye  * @min_addr: the lower bound of the memory region from where the allocation
15190ac398b1SYunfeng Ye  *	  is preferred (phys address)
15200ac398b1SYunfeng Ye  * @max_addr: the upper bound of the memory region from where the allocation
15210ac398b1SYunfeng Ye  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
15220ac398b1SYunfeng Ye  *	      allocate only from memory limited by memblock.current_limit value
15230ac398b1SYunfeng Ye  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
15240ac398b1SYunfeng Ye  *
15250ac398b1SYunfeng Ye  * Public function, provides additional debug information (including caller
15260ac398b1SYunfeng Ye  * info), if enabled. Does not zero allocated memory.
15270ac398b1SYunfeng Ye  *
15280ac398b1SYunfeng Ye  * Return:
15290ac398b1SYunfeng Ye  * Virtual address of allocated memory block on success, NULL on failure.
15300ac398b1SYunfeng Ye  */
15310ac398b1SYunfeng Ye void * __init memblock_alloc_exact_nid_raw(
15320ac398b1SYunfeng Ye 			phys_addr_t size, phys_addr_t align,
15330ac398b1SYunfeng Ye 			phys_addr_t min_addr, phys_addr_t max_addr,
15340ac398b1SYunfeng Ye 			int nid)
15350ac398b1SYunfeng Ye {
15360ac398b1SYunfeng Ye 	void *ptr;
15370ac398b1SYunfeng Ye 
15380ac398b1SYunfeng Ye 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
15390ac398b1SYunfeng Ye 		     __func__, (u64)size, (u64)align, nid, &min_addr,
15400ac398b1SYunfeng Ye 		     &max_addr, (void *)_RET_IP_);
15410ac398b1SYunfeng Ye 
15420ac398b1SYunfeng Ye 	ptr = memblock_alloc_internal(size, align,
15430ac398b1SYunfeng Ye 					   min_addr, max_addr, nid, true);
15440ac398b1SYunfeng Ye 	if (ptr && size > 0)
15450ac398b1SYunfeng Ye 		page_init_poison(ptr, size);
15460ac398b1SYunfeng Ye 
15470ac398b1SYunfeng Ye 	return ptr;
15480ac398b1SYunfeng Ye }
15490ac398b1SYunfeng Ye 
15500ac398b1SYunfeng Ye /**
1551eb31d559SMike Rapoport  * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
1552ea1f5f37SPavel Tatashin  * memory and without panicking
1553ea1f5f37SPavel Tatashin  * @size: size of memory block to be allocated in bytes
1554ea1f5f37SPavel Tatashin  * @align: alignment of the region and block's size
1555ea1f5f37SPavel Tatashin  * @min_addr: the lower bound of the memory region from where the allocation
1556ea1f5f37SPavel Tatashin  *	  is preferred (phys address)
1557ea1f5f37SPavel Tatashin  * @max_addr: the upper bound of the memory region from where the allocation
155897ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1559ea1f5f37SPavel Tatashin  *	      allocate only from memory limited by memblock.current_limit value
1560ea1f5f37SPavel Tatashin  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1561ea1f5f37SPavel Tatashin  *
1562ea1f5f37SPavel Tatashin  * Public function, provides additional debug information (including caller
1563ea1f5f37SPavel Tatashin  * info), if enabled. Does not zero allocated memory, does not panic if request
1564ea1f5f37SPavel Tatashin  * cannot be satisfied.
1565ea1f5f37SPavel Tatashin  *
156647cec443SMike Rapoport  * Return:
1567ea1f5f37SPavel Tatashin  * Virtual address of allocated memory block on success, NULL on failure.
1568ea1f5f37SPavel Tatashin  */
1569eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_raw(
1570ea1f5f37SPavel Tatashin 			phys_addr_t size, phys_addr_t align,
1571ea1f5f37SPavel Tatashin 			phys_addr_t min_addr, phys_addr_t max_addr,
1572ea1f5f37SPavel Tatashin 			int nid)
1573ea1f5f37SPavel Tatashin {
1574ea1f5f37SPavel Tatashin 	void *ptr;
1575ea1f5f37SPavel Tatashin 
1576d75f773cSSakari Ailus 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1577a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1578a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1579ea1f5f37SPavel Tatashin 
1580eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
15810ac398b1SYunfeng Ye 					   min_addr, max_addr, nid, false);
1582ea1f5f37SPavel Tatashin 	if (ptr && size > 0)
1583f682a97aSAlexander Duyck 		page_init_poison(ptr, size);
1584f682a97aSAlexander Duyck 
1585ea1f5f37SPavel Tatashin 	return ptr;
1586ea1f5f37SPavel Tatashin }
1587ea1f5f37SPavel Tatashin 
1588ea1f5f37SPavel Tatashin /**
1589c0dbe825SMike Rapoport  * memblock_alloc_try_nid - allocate boot memory block
159026f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
159126f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
159226f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region from where the allocation
159326f09e9bSSantosh Shilimkar  *	  is preferred (phys address)
159426f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region from where the allocation
159597ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
159626f09e9bSSantosh Shilimkar  *	      allocate only from memory limited by memblock.current_limit value
159726f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
159826f09e9bSSantosh Shilimkar  *
1599c0dbe825SMike Rapoport  * Public function, provides additional debug information (including caller
1600c0dbe825SMike Rapoport  * info), if enabled. This function zeroes the allocated memory.
160126f09e9bSSantosh Shilimkar  *
160247cec443SMike Rapoport  * Return:
160326f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
160426f09e9bSSantosh Shilimkar  */
1605eb31d559SMike Rapoport void * __init memblock_alloc_try_nid(
160626f09e9bSSantosh Shilimkar 			phys_addr_t size, phys_addr_t align,
160726f09e9bSSantosh Shilimkar 			phys_addr_t min_addr, phys_addr_t max_addr,
160826f09e9bSSantosh Shilimkar 			int nid)
160926f09e9bSSantosh Shilimkar {
161026f09e9bSSantosh Shilimkar 	void *ptr;
161126f09e9bSSantosh Shilimkar 
1612d75f773cSSakari Ailus 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1613a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1614a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1615eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
16160ac398b1SYunfeng Ye 					   min_addr, max_addr, nid, false);
1617c0dbe825SMike Rapoport 	if (ptr)
1618ea1f5f37SPavel Tatashin 		memset(ptr, 0, size);
161926f09e9bSSantosh Shilimkar 
1620c0dbe825SMike Rapoport 	return ptr;
162126f09e9bSSantosh Shilimkar }
162226f09e9bSSantosh Shilimkar 
162326f09e9bSSantosh Shilimkar /**
1624a2974133SMike Rapoport  * __memblock_free_late - free pages directly to buddy allocator
162548a833ccSMike Rapoport  * @base: phys starting address of the  boot memory block
162626f09e9bSSantosh Shilimkar  * @size: size of the boot memory block in bytes
162726f09e9bSSantosh Shilimkar  *
1628a2974133SMike Rapoport  * This is only useful when the memblock allocator has already been torn
162926f09e9bSSantosh Shilimkar  * down, but we are still initializing the system.  Pages are released directly
1630a2974133SMike Rapoport  * to the buddy allocator.
163126f09e9bSSantosh Shilimkar  */
163226f09e9bSSantosh Shilimkar void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
163326f09e9bSSantosh Shilimkar {
1634a36aab89SMike Rapoport 	phys_addr_t cursor, end;
163526f09e9bSSantosh Shilimkar 
1636a36aab89SMike Rapoport 	end = base + size - 1;
1637d75f773cSSakari Ailus 	memblock_dbg("%s: [%pa-%pa] %pS\n",
1638a36aab89SMike Rapoport 		     __func__, &base, &end, (void *)_RET_IP_);
16399099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
164026f09e9bSSantosh Shilimkar 	cursor = PFN_UP(base);
164126f09e9bSSantosh Shilimkar 	end = PFN_DOWN(base + size);
164226f09e9bSSantosh Shilimkar 
164326f09e9bSSantosh Shilimkar 	for (; cursor < end; cursor++) {
16447c2ee349SMike Rapoport 		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
1645ca79b0c2SArun KS 		totalram_pages_inc();
164626f09e9bSSantosh Shilimkar 	}
164726f09e9bSSantosh Shilimkar }
16489d1e2492SBenjamin Herrenschmidt 
16499d1e2492SBenjamin Herrenschmidt /*
16509d1e2492SBenjamin Herrenschmidt  * Remaining API functions
16519d1e2492SBenjamin Herrenschmidt  */
16529d1e2492SBenjamin Herrenschmidt 
16531f1ffb8aSDavid Gibson phys_addr_t __init_memblock memblock_phys_mem_size(void)
165495f72d1eSYinghai Lu {
16551440c4e2STejun Heo 	return memblock.memory.total_size;
165695f72d1eSYinghai Lu }
165795f72d1eSYinghai Lu 
16588907de5dSSrikar Dronamraju phys_addr_t __init_memblock memblock_reserved_size(void)
16598907de5dSSrikar Dronamraju {
16608907de5dSSrikar Dronamraju 	return memblock.reserved.total_size;
16618907de5dSSrikar Dronamraju }
16628907de5dSSrikar Dronamraju 
1663595ad9afSYinghai Lu phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1664595ad9afSYinghai Lu {
1665595ad9afSYinghai Lu 	unsigned long pages = 0;
1666595ad9afSYinghai Lu 	struct memblock_region *r;
1667595ad9afSYinghai Lu 	unsigned long start_pfn, end_pfn;
1668595ad9afSYinghai Lu 
1669595ad9afSYinghai Lu 	for_each_memblock(memory, r) {
1670595ad9afSYinghai Lu 		start_pfn = memblock_region_memory_base_pfn(r);
1671595ad9afSYinghai Lu 		end_pfn = memblock_region_memory_end_pfn(r);
1672595ad9afSYinghai Lu 		start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1673595ad9afSYinghai Lu 		end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1674595ad9afSYinghai Lu 		pages += end_pfn - start_pfn;
1675595ad9afSYinghai Lu 	}
1676595ad9afSYinghai Lu 
167716763230SFabian Frederick 	return PFN_PHYS(pages);
1678595ad9afSYinghai Lu }
1679595ad9afSYinghai Lu 
16800a93ebefSSam Ravnborg /* lowest address */
16810a93ebefSSam Ravnborg phys_addr_t __init_memblock memblock_start_of_DRAM(void)
16820a93ebefSSam Ravnborg {
16830a93ebefSSam Ravnborg 	return memblock.memory.regions[0].base;
16840a93ebefSSam Ravnborg }
16850a93ebefSSam Ravnborg 
168610d06439SYinghai Lu phys_addr_t __init_memblock memblock_end_of_DRAM(void)
168795f72d1eSYinghai Lu {
168895f72d1eSYinghai Lu 	int idx = memblock.memory.cnt - 1;
168995f72d1eSYinghai Lu 
1690e3239ff9SBenjamin Herrenschmidt 	return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
169195f72d1eSYinghai Lu }
169295f72d1eSYinghai Lu 
1693a571d4ebSDennis Chen static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
169495f72d1eSYinghai Lu {
16951c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1696136199f0SEmil Medve 	struct memblock_region *r;
169795f72d1eSYinghai Lu 
1698a571d4ebSDennis Chen 	/*
1699a571d4ebSDennis Chen 	 * translate the memory @limit size into the max address within one of
1700a571d4ebSDennis Chen 	 * the memory memblock regions, if the @limit exceeds the total size
17011c4bc43dSStefan Agner 	 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
1702a571d4ebSDennis Chen 	 */
1703136199f0SEmil Medve 	for_each_memblock(memory, r) {
1704c0ce8fefSTejun Heo 		if (limit <= r->size) {
1705c0ce8fefSTejun Heo 			max_addr = r->base + limit;
170695f72d1eSYinghai Lu 			break;
170795f72d1eSYinghai Lu 		}
1708c0ce8fefSTejun Heo 		limit -= r->size;
170995f72d1eSYinghai Lu 	}
1710c0ce8fefSTejun Heo 
1711a571d4ebSDennis Chen 	return max_addr;
1712a571d4ebSDennis Chen }
1713a571d4ebSDennis Chen 
1714a571d4ebSDennis Chen void __init memblock_enforce_memory_limit(phys_addr_t limit)
1715a571d4ebSDennis Chen {
171649aef717SColin Ian King 	phys_addr_t max_addr;
1717a571d4ebSDennis Chen 
1718a571d4ebSDennis Chen 	if (!limit)
1719a571d4ebSDennis Chen 		return;
1720a571d4ebSDennis Chen 
1721a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1722a571d4ebSDennis Chen 
1723a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
17241c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1725a571d4ebSDennis Chen 		return;
1726a571d4ebSDennis Chen 
1727c0ce8fefSTejun Heo 	/* truncate both memory and reserved regions */
1728f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.memory, max_addr,
17291c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
1730f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.reserved, max_addr,
17311c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
173295f72d1eSYinghai Lu }
173395f72d1eSYinghai Lu 
1734c9ca9b4eSAKASHI Takahiro void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1735c9ca9b4eSAKASHI Takahiro {
1736c9ca9b4eSAKASHI Takahiro 	int start_rgn, end_rgn;
1737c9ca9b4eSAKASHI Takahiro 	int i, ret;
1738c9ca9b4eSAKASHI Takahiro 
1739c9ca9b4eSAKASHI Takahiro 	if (!size)
1740c9ca9b4eSAKASHI Takahiro 		return;
1741c9ca9b4eSAKASHI Takahiro 
1742c9ca9b4eSAKASHI Takahiro 	ret = memblock_isolate_range(&memblock.memory, base, size,
1743c9ca9b4eSAKASHI Takahiro 						&start_rgn, &end_rgn);
1744c9ca9b4eSAKASHI Takahiro 	if (ret)
1745c9ca9b4eSAKASHI Takahiro 		return;
1746c9ca9b4eSAKASHI Takahiro 
1747c9ca9b4eSAKASHI Takahiro 	/* remove all the MAP regions */
1748c9ca9b4eSAKASHI Takahiro 	for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1749c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1750c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1751c9ca9b4eSAKASHI Takahiro 
1752c9ca9b4eSAKASHI Takahiro 	for (i = start_rgn - 1; i >= 0; i--)
1753c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1754c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1755c9ca9b4eSAKASHI Takahiro 
1756c9ca9b4eSAKASHI Takahiro 	/* truncate the reserved regions */
1757c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved, 0, base);
1758c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved,
17591c4bc43dSStefan Agner 			base + size, PHYS_ADDR_MAX);
1760c9ca9b4eSAKASHI Takahiro }
1761c9ca9b4eSAKASHI Takahiro 
1762a571d4ebSDennis Chen void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1763a571d4ebSDennis Chen {
1764a571d4ebSDennis Chen 	phys_addr_t max_addr;
1765a571d4ebSDennis Chen 
1766a571d4ebSDennis Chen 	if (!limit)
1767a571d4ebSDennis Chen 		return;
1768a571d4ebSDennis Chen 
1769a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1770a571d4ebSDennis Chen 
1771a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
17721c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1773a571d4ebSDennis Chen 		return;
1774a571d4ebSDennis Chen 
1775c9ca9b4eSAKASHI Takahiro 	memblock_cap_memory_range(0, max_addr);
1776a571d4ebSDennis Chen }
1777a571d4ebSDennis Chen 
1778cd79481dSYinghai Lu static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
177972d4b0b4SBenjamin Herrenschmidt {
178072d4b0b4SBenjamin Herrenschmidt 	unsigned int left = 0, right = type->cnt;
178172d4b0b4SBenjamin Herrenschmidt 
178272d4b0b4SBenjamin Herrenschmidt 	do {
178372d4b0b4SBenjamin Herrenschmidt 		unsigned int mid = (right + left) / 2;
178472d4b0b4SBenjamin Herrenschmidt 
178572d4b0b4SBenjamin Herrenschmidt 		if (addr < type->regions[mid].base)
178672d4b0b4SBenjamin Herrenschmidt 			right = mid;
178772d4b0b4SBenjamin Herrenschmidt 		else if (addr >= (type->regions[mid].base +
178872d4b0b4SBenjamin Herrenschmidt 				  type->regions[mid].size))
178972d4b0b4SBenjamin Herrenschmidt 			left = mid + 1;
179072d4b0b4SBenjamin Herrenschmidt 		else
179172d4b0b4SBenjamin Herrenschmidt 			return mid;
179272d4b0b4SBenjamin Herrenschmidt 	} while (left < right);
179372d4b0b4SBenjamin Herrenschmidt 	return -1;
179472d4b0b4SBenjamin Herrenschmidt }
179572d4b0b4SBenjamin Herrenschmidt 
1796f5a222dcSYueyi Li bool __init_memblock memblock_is_reserved(phys_addr_t addr)
179795f72d1eSYinghai Lu {
179872d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.reserved, addr) != -1;
179995f72d1eSYinghai Lu }
180072d4b0b4SBenjamin Herrenschmidt 
1801b4ad0c7eSYaowei Bai bool __init_memblock memblock_is_memory(phys_addr_t addr)
180272d4b0b4SBenjamin Herrenschmidt {
180372d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.memory, addr) != -1;
180472d4b0b4SBenjamin Herrenschmidt }
180572d4b0b4SBenjamin Herrenschmidt 
1806937f0c26SYaowei Bai bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
1807bf3d3cc5SArd Biesheuvel {
1808bf3d3cc5SArd Biesheuvel 	int i = memblock_search(&memblock.memory, addr);
1809bf3d3cc5SArd Biesheuvel 
1810bf3d3cc5SArd Biesheuvel 	if (i == -1)
1811bf3d3cc5SArd Biesheuvel 		return false;
1812bf3d3cc5SArd Biesheuvel 	return !memblock_is_nomap(&memblock.memory.regions[i]);
1813bf3d3cc5SArd Biesheuvel }
1814bf3d3cc5SArd Biesheuvel 
1815e76b63f8SYinghai Lu int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1816e76b63f8SYinghai Lu 			 unsigned long *start_pfn, unsigned long *end_pfn)
1817e76b63f8SYinghai Lu {
1818e76b63f8SYinghai Lu 	struct memblock_type *type = &memblock.memory;
181916763230SFabian Frederick 	int mid = memblock_search(type, PFN_PHYS(pfn));
1820e76b63f8SYinghai Lu 
1821e76b63f8SYinghai Lu 	if (mid == -1)
1822e76b63f8SYinghai Lu 		return -1;
1823e76b63f8SYinghai Lu 
1824f7e2f7e8SFabian Frederick 	*start_pfn = PFN_DOWN(type->regions[mid].base);
1825f7e2f7e8SFabian Frederick 	*end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
1826e76b63f8SYinghai Lu 
1827d622abf7SMike Rapoport 	return memblock_get_region_node(&type->regions[mid]);
1828e76b63f8SYinghai Lu }
1829e76b63f8SYinghai Lu 
1830eab30949SStephen Boyd /**
1831eab30949SStephen Boyd  * memblock_is_region_memory - check if a region is a subset of memory
1832eab30949SStephen Boyd  * @base: base of region to check
1833eab30949SStephen Boyd  * @size: size of region to check
1834eab30949SStephen Boyd  *
1835eab30949SStephen Boyd  * Check if the region [@base, @base + @size) is a subset of a memory block.
1836eab30949SStephen Boyd  *
183747cec443SMike Rapoport  * Return:
1838eab30949SStephen Boyd  * 0 if false, non-zero if true
1839eab30949SStephen Boyd  */
1840937f0c26SYaowei Bai bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
184172d4b0b4SBenjamin Herrenschmidt {
1842abb65272STomi Valkeinen 	int idx = memblock_search(&memblock.memory, base);
1843eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
184472d4b0b4SBenjamin Herrenschmidt 
184572d4b0b4SBenjamin Herrenschmidt 	if (idx == -1)
1846937f0c26SYaowei Bai 		return false;
1847ef415ef4SWei Yang 	return (memblock.memory.regions[idx].base +
1848eb18f1b5STejun Heo 		 memblock.memory.regions[idx].size) >= end;
184995f72d1eSYinghai Lu }
185095f72d1eSYinghai Lu 
1851eab30949SStephen Boyd /**
1852eab30949SStephen Boyd  * memblock_is_region_reserved - check if a region intersects reserved memory
1853eab30949SStephen Boyd  * @base: base of region to check
1854eab30949SStephen Boyd  * @size: size of region to check
1855eab30949SStephen Boyd  *
185647cec443SMike Rapoport  * Check if the region [@base, @base + @size) intersects a reserved
185747cec443SMike Rapoport  * memory block.
1858eab30949SStephen Boyd  *
185947cec443SMike Rapoport  * Return:
1860c5c5c9d1STang Chen  * True if they intersect, false if not.
1861eab30949SStephen Boyd  */
1862c5c5c9d1STang Chen bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
186395f72d1eSYinghai Lu {
1864eb18f1b5STejun Heo 	memblock_cap_size(base, &size);
1865c5c5c9d1STang Chen 	return memblock_overlaps_region(&memblock.reserved, base, size);
186695f72d1eSYinghai Lu }
186795f72d1eSYinghai Lu 
18686ede1fd3SYinghai Lu void __init_memblock memblock_trim_memory(phys_addr_t align)
18696ede1fd3SYinghai Lu {
18706ede1fd3SYinghai Lu 	phys_addr_t start, end, orig_start, orig_end;
1871136199f0SEmil Medve 	struct memblock_region *r;
18726ede1fd3SYinghai Lu 
1873136199f0SEmil Medve 	for_each_memblock(memory, r) {
1874136199f0SEmil Medve 		orig_start = r->base;
1875136199f0SEmil Medve 		orig_end = r->base + r->size;
18766ede1fd3SYinghai Lu 		start = round_up(orig_start, align);
18776ede1fd3SYinghai Lu 		end = round_down(orig_end, align);
18786ede1fd3SYinghai Lu 
18796ede1fd3SYinghai Lu 		if (start == orig_start && end == orig_end)
18806ede1fd3SYinghai Lu 			continue;
18816ede1fd3SYinghai Lu 
18826ede1fd3SYinghai Lu 		if (start < end) {
1883136199f0SEmil Medve 			r->base = start;
1884136199f0SEmil Medve 			r->size = end - start;
18856ede1fd3SYinghai Lu 		} else {
1886136199f0SEmil Medve 			memblock_remove_region(&memblock.memory,
1887136199f0SEmil Medve 					       r - memblock.memory.regions);
1888136199f0SEmil Medve 			r--;
18896ede1fd3SYinghai Lu 		}
18906ede1fd3SYinghai Lu 	}
18916ede1fd3SYinghai Lu }
1892e63075a3SBenjamin Herrenschmidt 
18933661ca66SYinghai Lu void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1894e63075a3SBenjamin Herrenschmidt {
1895e63075a3SBenjamin Herrenschmidt 	memblock.current_limit = limit;
1896e63075a3SBenjamin Herrenschmidt }
1897e63075a3SBenjamin Herrenschmidt 
1898fec51014SLaura Abbott phys_addr_t __init_memblock memblock_get_current_limit(void)
1899fec51014SLaura Abbott {
1900fec51014SLaura Abbott 	return memblock.current_limit;
1901fec51014SLaura Abbott }
1902fec51014SLaura Abbott 
19030262d9c8SHeiko Carstens static void __init_memblock memblock_dump(struct memblock_type *type)
19046ed311b2SBenjamin Herrenschmidt {
19055d63f81cSMiles Chen 	phys_addr_t base, end, size;
1906e1720feeSMike Rapoport 	enum memblock_flags flags;
19078c9c1701SAlexander Kuleshov 	int idx;
19088c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
19096ed311b2SBenjamin Herrenschmidt 
19100262d9c8SHeiko Carstens 	pr_info(" %s.cnt  = 0x%lx\n", type->name, type->cnt);
19116ed311b2SBenjamin Herrenschmidt 
191266e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
19137c0caeb8STejun Heo 		char nid_buf[32] = "";
19146ed311b2SBenjamin Herrenschmidt 
19157c0caeb8STejun Heo 		base = rgn->base;
19167c0caeb8STejun Heo 		size = rgn->size;
19175d63f81cSMiles Chen 		end = base + size - 1;
191866a20757STang Chen 		flags = rgn->flags;
19193f08a302SMike Rapoport #ifdef CONFIG_NEED_MULTIPLE_NODES
19207c0caeb8STejun Heo 		if (memblock_get_region_node(rgn) != MAX_NUMNODES)
19217c0caeb8STejun Heo 			snprintf(nid_buf, sizeof(nid_buf), " on node %d",
19227c0caeb8STejun Heo 				 memblock_get_region_node(rgn));
19237c0caeb8STejun Heo #endif
1924e1720feeSMike Rapoport 		pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
19250262d9c8SHeiko Carstens 			type->name, idx, &base, &end, &size, nid_buf, flags);
19266ed311b2SBenjamin Herrenschmidt 	}
19276ed311b2SBenjamin Herrenschmidt }
19286ed311b2SBenjamin Herrenschmidt 
1929*87c55870SMike Rapoport static void __init_memblock __memblock_dump_all(void)
19306ed311b2SBenjamin Herrenschmidt {
19316ed311b2SBenjamin Herrenschmidt 	pr_info("MEMBLOCK configuration:\n");
19325d63f81cSMiles Chen 	pr_info(" memory size = %pa reserved size = %pa\n",
19335d63f81cSMiles Chen 		&memblock.memory.total_size,
19345d63f81cSMiles Chen 		&memblock.reserved.total_size);
19356ed311b2SBenjamin Herrenschmidt 
19360262d9c8SHeiko Carstens 	memblock_dump(&memblock.memory);
19370262d9c8SHeiko Carstens 	memblock_dump(&memblock.reserved);
1938409efd4cSHeiko Carstens #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
193977649905SDavid Hildenbrand 	memblock_dump(&physmem);
1940409efd4cSHeiko Carstens #endif
19416ed311b2SBenjamin Herrenschmidt }
19426ed311b2SBenjamin Herrenschmidt 
1943*87c55870SMike Rapoport void __init_memblock memblock_dump_all(void)
1944*87c55870SMike Rapoport {
1945*87c55870SMike Rapoport 	if (memblock_debug)
1946*87c55870SMike Rapoport 		__memblock_dump_all();
1947*87c55870SMike Rapoport }
1948*87c55870SMike Rapoport 
19491aadc056STejun Heo void __init memblock_allow_resize(void)
19506ed311b2SBenjamin Herrenschmidt {
1951142b45a7SBenjamin Herrenschmidt 	memblock_can_resize = 1;
19526ed311b2SBenjamin Herrenschmidt }
19536ed311b2SBenjamin Herrenschmidt 
19546ed311b2SBenjamin Herrenschmidt static int __init early_memblock(char *p)
19556ed311b2SBenjamin Herrenschmidt {
19566ed311b2SBenjamin Herrenschmidt 	if (p && strstr(p, "debug"))
19576ed311b2SBenjamin Herrenschmidt 		memblock_debug = 1;
19586ed311b2SBenjamin Herrenschmidt 	return 0;
19596ed311b2SBenjamin Herrenschmidt }
19606ed311b2SBenjamin Herrenschmidt early_param("memblock", early_memblock);
19616ed311b2SBenjamin Herrenschmidt 
1962bda49a81SMike Rapoport static void __init __free_pages_memory(unsigned long start, unsigned long end)
1963bda49a81SMike Rapoport {
1964bda49a81SMike Rapoport 	int order;
1965bda49a81SMike Rapoport 
1966bda49a81SMike Rapoport 	while (start < end) {
1967bda49a81SMike Rapoport 		order = min(MAX_ORDER - 1UL, __ffs(start));
1968bda49a81SMike Rapoport 
1969bda49a81SMike Rapoport 		while (start + (1UL << order) > end)
1970bda49a81SMike Rapoport 			order--;
1971bda49a81SMike Rapoport 
1972bda49a81SMike Rapoport 		memblock_free_pages(pfn_to_page(start), start, order);
1973bda49a81SMike Rapoport 
1974bda49a81SMike Rapoport 		start += (1UL << order);
1975bda49a81SMike Rapoport 	}
1976bda49a81SMike Rapoport }
1977bda49a81SMike Rapoport 
1978bda49a81SMike Rapoport static unsigned long __init __free_memory_core(phys_addr_t start,
1979bda49a81SMike Rapoport 				 phys_addr_t end)
1980bda49a81SMike Rapoport {
1981bda49a81SMike Rapoport 	unsigned long start_pfn = PFN_UP(start);
1982bda49a81SMike Rapoport 	unsigned long end_pfn = min_t(unsigned long,
1983bda49a81SMike Rapoport 				      PFN_DOWN(end), max_low_pfn);
1984bda49a81SMike Rapoport 
1985bda49a81SMike Rapoport 	if (start_pfn >= end_pfn)
1986bda49a81SMike Rapoport 		return 0;
1987bda49a81SMike Rapoport 
1988bda49a81SMike Rapoport 	__free_pages_memory(start_pfn, end_pfn);
1989bda49a81SMike Rapoport 
1990bda49a81SMike Rapoport 	return end_pfn - start_pfn;
1991bda49a81SMike Rapoport }
1992bda49a81SMike Rapoport 
1993bda49a81SMike Rapoport static unsigned long __init free_low_memory_core_early(void)
1994bda49a81SMike Rapoport {
1995bda49a81SMike Rapoport 	unsigned long count = 0;
1996bda49a81SMike Rapoport 	phys_addr_t start, end;
1997bda49a81SMike Rapoport 	u64 i;
1998bda49a81SMike Rapoport 
1999bda49a81SMike Rapoport 	memblock_clear_hotplug(0, -1);
2000bda49a81SMike Rapoport 
2001bda49a81SMike Rapoport 	for_each_reserved_mem_region(i, &start, &end)
2002bda49a81SMike Rapoport 		reserve_bootmem_region(start, end);
2003bda49a81SMike Rapoport 
2004bda49a81SMike Rapoport 	/*
2005bda49a81SMike Rapoport 	 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
2006bda49a81SMike Rapoport 	 *  because in some case like Node0 doesn't have RAM installed
2007bda49a81SMike Rapoport 	 *  low ram will be on Node1
2008bda49a81SMike Rapoport 	 */
2009bda49a81SMike Rapoport 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
2010bda49a81SMike Rapoport 				NULL)
2011bda49a81SMike Rapoport 		count += __free_memory_core(start, end);
2012bda49a81SMike Rapoport 
2013bda49a81SMike Rapoport 	return count;
2014bda49a81SMike Rapoport }
2015bda49a81SMike Rapoport 
2016bda49a81SMike Rapoport static int reset_managed_pages_done __initdata;
2017bda49a81SMike Rapoport 
2018bda49a81SMike Rapoport void reset_node_managed_pages(pg_data_t *pgdat)
2019bda49a81SMike Rapoport {
2020bda49a81SMike Rapoport 	struct zone *z;
2021bda49a81SMike Rapoport 
2022bda49a81SMike Rapoport 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
20239705bea5SArun KS 		atomic_long_set(&z->managed_pages, 0);
2024bda49a81SMike Rapoport }
2025bda49a81SMike Rapoport 
2026bda49a81SMike Rapoport void __init reset_all_zones_managed_pages(void)
2027bda49a81SMike Rapoport {
2028bda49a81SMike Rapoport 	struct pglist_data *pgdat;
2029bda49a81SMike Rapoport 
2030bda49a81SMike Rapoport 	if (reset_managed_pages_done)
2031bda49a81SMike Rapoport 		return;
2032bda49a81SMike Rapoport 
2033bda49a81SMike Rapoport 	for_each_online_pgdat(pgdat)
2034bda49a81SMike Rapoport 		reset_node_managed_pages(pgdat);
2035bda49a81SMike Rapoport 
2036bda49a81SMike Rapoport 	reset_managed_pages_done = 1;
2037bda49a81SMike Rapoport }
2038bda49a81SMike Rapoport 
2039bda49a81SMike Rapoport /**
2040bda49a81SMike Rapoport  * memblock_free_all - release free pages to the buddy allocator
2041bda49a81SMike Rapoport  *
2042bda49a81SMike Rapoport  * Return: the number of pages actually released.
2043bda49a81SMike Rapoport  */
2044bda49a81SMike Rapoport unsigned long __init memblock_free_all(void)
2045bda49a81SMike Rapoport {
2046bda49a81SMike Rapoport 	unsigned long pages;
2047bda49a81SMike Rapoport 
2048bda49a81SMike Rapoport 	reset_all_zones_managed_pages();
2049bda49a81SMike Rapoport 
2050bda49a81SMike Rapoport 	pages = free_low_memory_core_early();
2051ca79b0c2SArun KS 	totalram_pages_add(pages);
2052bda49a81SMike Rapoport 
2053bda49a81SMike Rapoport 	return pages;
2054bda49a81SMike Rapoport }
2055bda49a81SMike Rapoport 
2056350e88baSMike Rapoport #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
20576d03b885SBenjamin Herrenschmidt 
20586d03b885SBenjamin Herrenschmidt static int memblock_debug_show(struct seq_file *m, void *private)
20596d03b885SBenjamin Herrenschmidt {
20606d03b885SBenjamin Herrenschmidt 	struct memblock_type *type = m->private;
20616d03b885SBenjamin Herrenschmidt 	struct memblock_region *reg;
20626d03b885SBenjamin Herrenschmidt 	int i;
20635d63f81cSMiles Chen 	phys_addr_t end;
20646d03b885SBenjamin Herrenschmidt 
20656d03b885SBenjamin Herrenschmidt 	for (i = 0; i < type->cnt; i++) {
20666d03b885SBenjamin Herrenschmidt 		reg = &type->regions[i];
20675d63f81cSMiles Chen 		end = reg->base + reg->size - 1;
20686d03b885SBenjamin Herrenschmidt 
20695d63f81cSMiles Chen 		seq_printf(m, "%4d: ", i);
20705d63f81cSMiles Chen 		seq_printf(m, "%pa..%pa\n", &reg->base, &end);
20716d03b885SBenjamin Herrenschmidt 	}
20726d03b885SBenjamin Herrenschmidt 	return 0;
20736d03b885SBenjamin Herrenschmidt }
20745ad35093SAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(memblock_debug);
20756d03b885SBenjamin Herrenschmidt 
20766d03b885SBenjamin Herrenschmidt static int __init memblock_init_debugfs(void)
20776d03b885SBenjamin Herrenschmidt {
20786d03b885SBenjamin Herrenschmidt 	struct dentry *root = debugfs_create_dir("memblock", NULL);
2079d9f7979cSGreg Kroah-Hartman 
20800825a6f9SJoe Perches 	debugfs_create_file("memory", 0444, root,
20810825a6f9SJoe Perches 			    &memblock.memory, &memblock_debug_fops);
20820825a6f9SJoe Perches 	debugfs_create_file("reserved", 0444, root,
20830825a6f9SJoe Perches 			    &memblock.reserved, &memblock_debug_fops);
208470210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
208577649905SDavid Hildenbrand 	debugfs_create_file("physmem", 0444, root, &physmem,
208677649905SDavid Hildenbrand 			    &memblock_debug_fops);
208770210ed9SPhilipp Hachtmann #endif
20886d03b885SBenjamin Herrenschmidt 
20896d03b885SBenjamin Herrenschmidt 	return 0;
20906d03b885SBenjamin Herrenschmidt }
20916d03b885SBenjamin Herrenschmidt __initcall(memblock_init_debugfs);
20926d03b885SBenjamin Herrenschmidt 
20936d03b885SBenjamin Herrenschmidt #endif /* CONFIG_DEBUG_FS */
2094