xref: /linux/mm/memblock.c (revision 61167ad5fecdeaa037f3df1ba354dddd5f66a1ed)
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 
32450d0e74SZhou Guanghui #ifndef INIT_MEMBLOCK_MEMORY_REGIONS
33450d0e74SZhou Guanghui #define INIT_MEMBLOCK_MEMORY_REGIONS		INIT_MEMBLOCK_REGIONS
34450d0e74SZhou Guanghui #endif
35450d0e74SZhou Guanghui 
363e039c5cSMike Rapoport /**
373e039c5cSMike Rapoport  * DOC: memblock overview
383e039c5cSMike Rapoport  *
393e039c5cSMike Rapoport  * Memblock is a method of managing memory regions during the early
403e039c5cSMike Rapoport  * boot period when the usual kernel memory allocators are not up and
413e039c5cSMike Rapoport  * running.
423e039c5cSMike Rapoport  *
433e039c5cSMike Rapoport  * Memblock views the system memory as collections of contiguous
443e039c5cSMike Rapoport  * regions. There are several types of these collections:
453e039c5cSMike Rapoport  *
463e039c5cSMike Rapoport  * * ``memory`` - describes the physical memory available to the
473e039c5cSMike Rapoport  *   kernel; this may differ from the actual physical memory installed
483e039c5cSMike Rapoport  *   in the system, for instance when the memory is restricted with
493e039c5cSMike Rapoport  *   ``mem=`` command line parameter
503e039c5cSMike Rapoport  * * ``reserved`` - describes the regions that were allocated
5177649905SDavid Hildenbrand  * * ``physmem`` - describes the actual physical memory available during
5277649905SDavid Hildenbrand  *   boot regardless of the possible restrictions and memory hot(un)plug;
5377649905SDavid Hildenbrand  *   the ``physmem`` type is only available on some architectures.
543e039c5cSMike Rapoport  *
559303c9d5SMauro Carvalho Chehab  * Each region is represented by struct memblock_region that
563e039c5cSMike Rapoport  * defines the region extents, its attributes and NUMA node id on NUMA
571bf162e4SMauro Carvalho Chehab  * systems. Every memory type is described by the struct memblock_type
581bf162e4SMauro Carvalho Chehab  * which contains an array of memory regions along with
5977649905SDavid Hildenbrand  * the allocator metadata. The "memory" and "reserved" types are nicely
609303c9d5SMauro Carvalho Chehab  * wrapped with struct memblock. This structure is statically
6177649905SDavid Hildenbrand  * initialized at build time. The region arrays are initially sized to
62450d0e74SZhou Guanghui  * %INIT_MEMBLOCK_MEMORY_REGIONS for "memory" and
63450d0e74SZhou Guanghui  * %INIT_MEMBLOCK_RESERVED_REGIONS for "reserved". The region array
64450d0e74SZhou Guanghui  * for "physmem" is initially sized to %INIT_PHYSMEM_REGIONS.
656e5af9a8SCao jin  * The memblock_allow_resize() enables automatic resizing of the region
666e5af9a8SCao jin  * arrays during addition of new regions. This feature should be used
676e5af9a8SCao jin  * with care so that memory allocated for the region array will not
686e5af9a8SCao jin  * overlap with areas that should be reserved, for example initrd.
693e039c5cSMike Rapoport  *
703e039c5cSMike Rapoport  * The early architecture setup should tell memblock what the physical
716e5af9a8SCao jin  * memory layout is by using memblock_add() or memblock_add_node()
726e5af9a8SCao jin  * functions. The first function does not assign the region to a NUMA
736e5af9a8SCao jin  * node and it is appropriate for UMA systems. Yet, it is possible to
746e5af9a8SCao jin  * use it on NUMA systems as well and assign the region to a NUMA node
756e5af9a8SCao jin  * later in the setup process using memblock_set_node(). The
766e5af9a8SCao jin  * memblock_add_node() performs such an assignment directly.
773e039c5cSMike Rapoport  *
78a2974133SMike Rapoport  * Once memblock is setup the memory can be allocated using one of the
79a2974133SMike Rapoport  * API variants:
80a2974133SMike Rapoport  *
816e5af9a8SCao jin  * * memblock_phys_alloc*() - these functions return the **physical**
826e5af9a8SCao jin  *   address of the allocated memory
836e5af9a8SCao jin  * * memblock_alloc*() - these functions return the **virtual** address
846e5af9a8SCao jin  *   of the allocated memory.
85a2974133SMike Rapoport  *
86df1758d9SEthon Paul  * Note, that both API variants use implicit assumptions about allowed
87a2974133SMike Rapoport  * memory ranges and the fallback methods. Consult the documentation
886e5af9a8SCao jin  * of memblock_alloc_internal() and memblock_alloc_range_nid()
896e5af9a8SCao jin  * functions for more elaborate description.
903e039c5cSMike Rapoport  *
916e5af9a8SCao jin  * As the system boot progresses, the architecture specific mem_init()
926e5af9a8SCao jin  * function frees all the memory to the buddy page allocator.
933e039c5cSMike Rapoport  *
946e5af9a8SCao jin  * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the
9577649905SDavid Hildenbrand  * memblock data structures (except "physmem") will be discarded after the
9677649905SDavid Hildenbrand  * system initialization completes.
973e039c5cSMike Rapoport  */
983e039c5cSMike Rapoport 
99a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA
100bda49a81SMike Rapoport struct pglist_data __refdata contig_page_data;
101bda49a81SMike Rapoport EXPORT_SYMBOL(contig_page_data);
102bda49a81SMike Rapoport #endif
103bda49a81SMike Rapoport 
104bda49a81SMike Rapoport unsigned long max_low_pfn;
105bda49a81SMike Rapoport unsigned long min_low_pfn;
106bda49a81SMike Rapoport unsigned long max_pfn;
107bda49a81SMike Rapoport unsigned long long max_possible_pfn;
108bda49a81SMike Rapoport 
109450d0e74SZhou Guanghui static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_MEMORY_REGIONS] __initdata_memblock;
1108a5b403dSArd Biesheuvel static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
11170210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
11277649905SDavid Hildenbrand static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS];
11370210ed9SPhilipp Hachtmann #endif
114fe091c20STejun Heo 
115fe091c20STejun Heo struct memblock memblock __initdata_memblock = {
116fe091c20STejun Heo 	.memory.regions		= memblock_memory_init_regions,
117fe091c20STejun Heo 	.memory.cnt		= 1,	/* empty dummy entry */
118450d0e74SZhou Guanghui 	.memory.max		= INIT_MEMBLOCK_MEMORY_REGIONS,
1190262d9c8SHeiko Carstens 	.memory.name		= "memory",
120fe091c20STejun Heo 
121fe091c20STejun Heo 	.reserved.regions	= memblock_reserved_init_regions,
122fe091c20STejun Heo 	.reserved.cnt		= 1,	/* empty dummy entry */
1238a5b403dSArd Biesheuvel 	.reserved.max		= INIT_MEMBLOCK_RESERVED_REGIONS,
1240262d9c8SHeiko Carstens 	.reserved.name		= "reserved",
125fe091c20STejun Heo 
12679442ed1STang Chen 	.bottom_up		= false,
127fe091c20STejun Heo 	.current_limit		= MEMBLOCK_ALLOC_ANYWHERE,
128fe091c20STejun Heo };
12995f72d1eSYinghai Lu 
13077649905SDavid Hildenbrand #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
13177649905SDavid Hildenbrand struct memblock_type physmem = {
13277649905SDavid Hildenbrand 	.regions		= memblock_physmem_init_regions,
13377649905SDavid Hildenbrand 	.cnt			= 1,	/* empty dummy entry */
13477649905SDavid Hildenbrand 	.max			= INIT_PHYSMEM_REGIONS,
13577649905SDavid Hildenbrand 	.name			= "physmem",
13677649905SDavid Hildenbrand };
13777649905SDavid Hildenbrand #endif
13877649905SDavid Hildenbrand 
1399f3d5eaaSMike Rapoport /*
1409f3d5eaaSMike Rapoport  * keep a pointer to &memblock.memory in the text section to use it in
1419f3d5eaaSMike Rapoport  * __next_mem_range() and its helpers.
1429f3d5eaaSMike Rapoport  *  For architectures that do not keep memblock data after init, this
1439f3d5eaaSMike Rapoport  * pointer will be reset to NULL at memblock_discard()
1449f3d5eaaSMike Rapoport  */
1459f3d5eaaSMike Rapoport static __refdata struct memblock_type *memblock_memory = &memblock.memory;
1469f3d5eaaSMike Rapoport 
147cd991db8SMike Rapoport #define for_each_memblock_type(i, memblock_type, rgn)			\
148cd991db8SMike Rapoport 	for (i = 0, rgn = &memblock_type->regions[0];			\
149cd991db8SMike Rapoport 	     i < memblock_type->cnt;					\
150cd991db8SMike Rapoport 	     i++, rgn = &memblock_type->regions[i])
151cd991db8SMike Rapoport 
15287c55870SMike Rapoport #define memblock_dbg(fmt, ...)						\
15387c55870SMike Rapoport 	do {								\
15487c55870SMike Rapoport 		if (memblock_debug)					\
15587c55870SMike Rapoport 			pr_info(fmt, ##__VA_ARGS__);			\
15687c55870SMike Rapoport 	} while (0)
15787c55870SMike Rapoport 
15887c55870SMike Rapoport static int memblock_debug __initdata_memblock;
159a3f5bafcSTony Luck static bool system_has_some_mirror __initdata_memblock = false;
1601aadc056STejun Heo static int memblock_can_resize __initdata_memblock;
161181eb394SGavin Shan static int memblock_memory_in_slab __initdata_memblock = 0;
162181eb394SGavin Shan static int memblock_reserved_in_slab __initdata_memblock = 0;
16395f72d1eSYinghai Lu 
164c366ea89SMike Rapoport static enum memblock_flags __init_memblock choose_memblock_flags(void)
165a3f5bafcSTony Luck {
166a3f5bafcSTony Luck 	return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
167a3f5bafcSTony Luck }
168a3f5bafcSTony Luck 
169eb18f1b5STejun Heo /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
170eb18f1b5STejun Heo static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
171eb18f1b5STejun Heo {
1721c4bc43dSStefan Agner 	return *size = min(*size, PHYS_ADDR_MAX - base);
173eb18f1b5STejun Heo }
174eb18f1b5STejun Heo 
1756ed311b2SBenjamin Herrenschmidt /*
1766ed311b2SBenjamin Herrenschmidt  * Address comparison utilities
1776ed311b2SBenjamin Herrenschmidt  */
17810d06439SYinghai Lu static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
1792898cc4cSBenjamin Herrenschmidt 				       phys_addr_t base2, phys_addr_t size2)
18095f72d1eSYinghai Lu {
18195f72d1eSYinghai Lu 	return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
18295f72d1eSYinghai Lu }
18395f72d1eSYinghai Lu 
18495cf82ecSTang Chen bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
1852d7d3eb2SH Hartley Sweeten 					phys_addr_t base, phys_addr_t size)
1866ed311b2SBenjamin Herrenschmidt {
1876ed311b2SBenjamin Herrenschmidt 	unsigned long i;
1886ed311b2SBenjamin Herrenschmidt 
189023accf5SMike Rapoport 	memblock_cap_size(base, &size);
190023accf5SMike Rapoport 
191f14516fbSAlexander Kuleshov 	for (i = 0; i < type->cnt; i++)
192f14516fbSAlexander Kuleshov 		if (memblock_addrs_overlap(base, size, type->regions[i].base,
193f14516fbSAlexander Kuleshov 					   type->regions[i].size))
1946ed311b2SBenjamin Herrenschmidt 			break;
195c5c5c9d1STang Chen 	return i < type->cnt;
1966ed311b2SBenjamin Herrenschmidt }
1976ed311b2SBenjamin Herrenschmidt 
19847cec443SMike Rapoport /**
19979442ed1STang Chen  * __memblock_find_range_bottom_up - find free area utility in bottom-up
20079442ed1STang Chen  * @start: start of candidate range
20147cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
20247cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
20379442ed1STang Chen  * @size: size of free area to find
20479442ed1STang Chen  * @align: alignment of free area to find
205b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
206fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
20779442ed1STang Chen  *
20879442ed1STang Chen  * Utility called from memblock_find_in_range_node(), find free area bottom-up.
20979442ed1STang Chen  *
21047cec443SMike Rapoport  * Return:
21179442ed1STang Chen  * Found address on success, 0 on failure.
21279442ed1STang Chen  */
21379442ed1STang Chen static phys_addr_t __init_memblock
21479442ed1STang Chen __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
215fc6daaf9STony Luck 				phys_addr_t size, phys_addr_t align, int nid,
216e1720feeSMike Rapoport 				enum memblock_flags flags)
21779442ed1STang Chen {
21879442ed1STang Chen 	phys_addr_t this_start, this_end, cand;
21979442ed1STang Chen 	u64 i;
22079442ed1STang Chen 
221fc6daaf9STony Luck 	for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
22279442ed1STang Chen 		this_start = clamp(this_start, start, end);
22379442ed1STang Chen 		this_end = clamp(this_end, start, end);
22479442ed1STang Chen 
22579442ed1STang Chen 		cand = round_up(this_start, align);
22679442ed1STang Chen 		if (cand < this_end && this_end - cand >= size)
22779442ed1STang Chen 			return cand;
22879442ed1STang Chen 	}
22979442ed1STang Chen 
23079442ed1STang Chen 	return 0;
23179442ed1STang Chen }
23279442ed1STang Chen 
2337bd0b0f0STejun Heo /**
2341402899eSTang Chen  * __memblock_find_range_top_down - find free area utility, in top-down
2351402899eSTang Chen  * @start: start of candidate range
23647cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
23747cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
2381402899eSTang Chen  * @size: size of free area to find
2391402899eSTang Chen  * @align: alignment of free area to find
240b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
241fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2421402899eSTang Chen  *
2431402899eSTang Chen  * Utility called from memblock_find_in_range_node(), find free area top-down.
2441402899eSTang Chen  *
24547cec443SMike Rapoport  * Return:
24679442ed1STang Chen  * Found address on success, 0 on failure.
2471402899eSTang Chen  */
2481402899eSTang Chen static phys_addr_t __init_memblock
2491402899eSTang Chen __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
250fc6daaf9STony Luck 			       phys_addr_t size, phys_addr_t align, int nid,
251e1720feeSMike Rapoport 			       enum memblock_flags flags)
2521402899eSTang Chen {
2531402899eSTang Chen 	phys_addr_t this_start, this_end, cand;
2541402899eSTang Chen 	u64 i;
2551402899eSTang Chen 
256fc6daaf9STony Luck 	for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
257fc6daaf9STony Luck 					NULL) {
2581402899eSTang Chen 		this_start = clamp(this_start, start, end);
2591402899eSTang Chen 		this_end = clamp(this_end, start, end);
2601402899eSTang Chen 
2611402899eSTang Chen 		if (this_end < size)
2621402899eSTang Chen 			continue;
2631402899eSTang Chen 
2641402899eSTang Chen 		cand = round_down(this_end - size, align);
2651402899eSTang Chen 		if (cand >= this_start)
2661402899eSTang Chen 			return cand;
2671402899eSTang Chen 	}
2681402899eSTang Chen 
2691402899eSTang Chen 	return 0;
2701402899eSTang Chen }
2711402899eSTang Chen 
2721402899eSTang Chen /**
2737bd0b0f0STejun Heo  * memblock_find_in_range_node - find free area in given range and node
2747bd0b0f0STejun Heo  * @size: size of free area to find
2757bd0b0f0STejun Heo  * @align: alignment of free area to find
27687029ee9SGrygorii Strashko  * @start: start of candidate range
27747cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
27847cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
279b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
280fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2817bd0b0f0STejun Heo  *
2827bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range and node.
2837bd0b0f0STejun Heo  *
28447cec443SMike Rapoport  * Return:
28579442ed1STang Chen  * Found address on success, 0 on failure.
2866ed311b2SBenjamin Herrenschmidt  */
287c366ea89SMike Rapoport static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
28887029ee9SGrygorii Strashko 					phys_addr_t align, phys_addr_t start,
289e1720feeSMike Rapoport 					phys_addr_t end, int nid,
290e1720feeSMike Rapoport 					enum memblock_flags flags)
291f7210e6cSTang Chen {
292f7210e6cSTang Chen 	/* pump up @end */
293fed84c78SQian Cai 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
294c6975d7cSQian Cai 	    end == MEMBLOCK_ALLOC_NOLEAKTRACE)
295f7210e6cSTang Chen 		end = memblock.current_limit;
296f7210e6cSTang Chen 
297f7210e6cSTang Chen 	/* avoid allocating the first page */
298f7210e6cSTang Chen 	start = max_t(phys_addr_t, start, PAGE_SIZE);
299f7210e6cSTang Chen 	end = max(start, end);
30079442ed1STang Chen 
3012dcb3964SRoman Gushchin 	if (memblock_bottom_up())
3022dcb3964SRoman Gushchin 		return __memblock_find_range_bottom_up(start, end, size, align,
3032dcb3964SRoman Gushchin 						       nid, flags);
3042dcb3964SRoman Gushchin 	else
3052dcb3964SRoman Gushchin 		return __memblock_find_range_top_down(start, end, size, align,
3062dcb3964SRoman Gushchin 						      nid, flags);
307f7210e6cSTang Chen }
3086ed311b2SBenjamin Herrenschmidt 
3097bd0b0f0STejun Heo /**
3107bd0b0f0STejun Heo  * memblock_find_in_range - find free area in given range
3117bd0b0f0STejun Heo  * @start: start of candidate range
31247cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
31347cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
3147bd0b0f0STejun Heo  * @size: size of free area to find
3157bd0b0f0STejun Heo  * @align: alignment of free area to find
3167bd0b0f0STejun Heo  *
3177bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range.
3187bd0b0f0STejun Heo  *
31947cec443SMike Rapoport  * Return:
32079442ed1STang Chen  * Found address on success, 0 on failure.
3217bd0b0f0STejun Heo  */
322a7259df7SMike Rapoport static phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
3237bd0b0f0STejun Heo 					phys_addr_t end, phys_addr_t size,
3247bd0b0f0STejun Heo 					phys_addr_t align)
3257bd0b0f0STejun Heo {
326a3f5bafcSTony Luck 	phys_addr_t ret;
327e1720feeSMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
328a3f5bafcSTony Luck 
329a3f5bafcSTony Luck again:
330a3f5bafcSTony Luck 	ret = memblock_find_in_range_node(size, align, start, end,
331a3f5bafcSTony Luck 					    NUMA_NO_NODE, flags);
332a3f5bafcSTony Luck 
333a3f5bafcSTony Luck 	if (!ret && (flags & MEMBLOCK_MIRROR)) {
33414d9a675SMa Wupeng 		pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n",
335a3f5bafcSTony Luck 			&size);
336a3f5bafcSTony Luck 		flags &= ~MEMBLOCK_MIRROR;
337a3f5bafcSTony Luck 		goto again;
338a3f5bafcSTony Luck 	}
339a3f5bafcSTony Luck 
340a3f5bafcSTony Luck 	return ret;
3417bd0b0f0STejun Heo }
3427bd0b0f0STejun Heo 
34310d06439SYinghai Lu static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
34495f72d1eSYinghai Lu {
3451440c4e2STejun Heo 	type->total_size -= type->regions[r].size;
3467c0caeb8STejun Heo 	memmove(&type->regions[r], &type->regions[r + 1],
3477c0caeb8STejun Heo 		(type->cnt - (r + 1)) * sizeof(type->regions[r]));
348e3239ff9SBenjamin Herrenschmidt 	type->cnt--;
34995f72d1eSYinghai Lu 
3508f7a6605SBenjamin Herrenschmidt 	/* Special case for empty arrays */
3518f7a6605SBenjamin Herrenschmidt 	if (type->cnt == 0) {
3521440c4e2STejun Heo 		WARN_ON(type->total_size != 0);
3538f7a6605SBenjamin Herrenschmidt 		type->cnt = 1;
3548f7a6605SBenjamin Herrenschmidt 		type->regions[0].base = 0;
3558f7a6605SBenjamin Herrenschmidt 		type->regions[0].size = 0;
35666a20757STang Chen 		type->regions[0].flags = 0;
3577c0caeb8STejun Heo 		memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
3588f7a6605SBenjamin Herrenschmidt 	}
35995f72d1eSYinghai Lu }
36095f72d1eSYinghai Lu 
361350e88baSMike Rapoport #ifndef CONFIG_ARCH_KEEP_MEMBLOCK
3623010f876SPavel Tatashin /**
36347cec443SMike Rapoport  * memblock_discard - discard memory and reserved arrays if they were allocated
3643010f876SPavel Tatashin  */
3653010f876SPavel Tatashin void __init memblock_discard(void)
36629f67386SYinghai Lu {
3673010f876SPavel Tatashin 	phys_addr_t addr, size;
36829f67386SYinghai Lu 
3693010f876SPavel Tatashin 	if (memblock.reserved.regions != memblock_reserved_init_regions) {
3703010f876SPavel Tatashin 		addr = __pa(memblock.reserved.regions);
3713010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
37229f67386SYinghai Lu 				  memblock.reserved.max);
373c94afc46SMiaohe Lin 		if (memblock_reserved_in_slab)
374c94afc46SMiaohe Lin 			kfree(memblock.reserved.regions);
375c94afc46SMiaohe Lin 		else
376621d9739SMike Rapoport 			memblock_free_late(addr, size);
37729f67386SYinghai Lu 	}
37829f67386SYinghai Lu 
37991b540f9SPavel Tatashin 	if (memblock.memory.regions != memblock_memory_init_regions) {
3803010f876SPavel Tatashin 		addr = __pa(memblock.memory.regions);
3813010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
3825e270e25SPhilipp Hachtmann 				  memblock.memory.max);
383c94afc46SMiaohe Lin 		if (memblock_memory_in_slab)
384c94afc46SMiaohe Lin 			kfree(memblock.memory.regions);
385c94afc46SMiaohe Lin 		else
386621d9739SMike Rapoport 			memblock_free_late(addr, size);
3875e270e25SPhilipp Hachtmann 	}
3889f3d5eaaSMike Rapoport 
3899f3d5eaaSMike Rapoport 	memblock_memory = NULL;
3903010f876SPavel Tatashin }
3915e270e25SPhilipp Hachtmann #endif
3925e270e25SPhilipp Hachtmann 
39348c3b583SGreg Pearson /**
39448c3b583SGreg Pearson  * memblock_double_array - double the size of the memblock regions array
39548c3b583SGreg Pearson  * @type: memblock type of the regions array being doubled
39648c3b583SGreg Pearson  * @new_area_start: starting address of memory range to avoid overlap with
39748c3b583SGreg Pearson  * @new_area_size: size of memory range to avoid overlap with
39848c3b583SGreg Pearson  *
39948c3b583SGreg Pearson  * Double the size of the @type regions array. If memblock is being used to
40048c3b583SGreg Pearson  * allocate memory for a new reserved regions array and there is a previously
40148c3b583SGreg Pearson  * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
40248c3b583SGreg Pearson  * waiting to be reserved, ensure the memory used by the new array does
40348c3b583SGreg Pearson  * not overlap.
40448c3b583SGreg Pearson  *
40547cec443SMike Rapoport  * Return:
40648c3b583SGreg Pearson  * 0 on success, -1 on failure.
40748c3b583SGreg Pearson  */
40848c3b583SGreg Pearson static int __init_memblock memblock_double_array(struct memblock_type *type,
40948c3b583SGreg Pearson 						phys_addr_t new_area_start,
41048c3b583SGreg Pearson 						phys_addr_t new_area_size)
411142b45a7SBenjamin Herrenschmidt {
412142b45a7SBenjamin Herrenschmidt 	struct memblock_region *new_array, *old_array;
41329f67386SYinghai Lu 	phys_addr_t old_alloc_size, new_alloc_size;
414a36aab89SMike Rapoport 	phys_addr_t old_size, new_size, addr, new_end;
415142b45a7SBenjamin Herrenschmidt 	int use_slab = slab_is_available();
416181eb394SGavin Shan 	int *in_slab;
417142b45a7SBenjamin Herrenschmidt 
418142b45a7SBenjamin Herrenschmidt 	/* We don't allow resizing until we know about the reserved regions
419142b45a7SBenjamin Herrenschmidt 	 * of memory that aren't suitable for allocation
420142b45a7SBenjamin Herrenschmidt 	 */
421142b45a7SBenjamin Herrenschmidt 	if (!memblock_can_resize)
422142b45a7SBenjamin Herrenschmidt 		return -1;
423142b45a7SBenjamin Herrenschmidt 
424142b45a7SBenjamin Herrenschmidt 	/* Calculate new doubled size */
425142b45a7SBenjamin Herrenschmidt 	old_size = type->max * sizeof(struct memblock_region);
426142b45a7SBenjamin Herrenschmidt 	new_size = old_size << 1;
42729f67386SYinghai Lu 	/*
42829f67386SYinghai Lu 	 * We need to allocated new one align to PAGE_SIZE,
42929f67386SYinghai Lu 	 *   so we can free them completely later.
43029f67386SYinghai Lu 	 */
43129f67386SYinghai Lu 	old_alloc_size = PAGE_ALIGN(old_size);
43229f67386SYinghai Lu 	new_alloc_size = PAGE_ALIGN(new_size);
433142b45a7SBenjamin Herrenschmidt 
434181eb394SGavin Shan 	/* Retrieve the slab flag */
435181eb394SGavin Shan 	if (type == &memblock.memory)
436181eb394SGavin Shan 		in_slab = &memblock_memory_in_slab;
437181eb394SGavin Shan 	else
438181eb394SGavin Shan 		in_slab = &memblock_reserved_in_slab;
439181eb394SGavin Shan 
440a2974133SMike Rapoport 	/* Try to find some space for it */
441142b45a7SBenjamin Herrenschmidt 	if (use_slab) {
442142b45a7SBenjamin Herrenschmidt 		new_array = kmalloc(new_size, GFP_KERNEL);
4431f5026a7STejun Heo 		addr = new_array ? __pa(new_array) : 0;
4444e2f0775SGavin Shan 	} else {
44548c3b583SGreg Pearson 		/* only exclude range when trying to double reserved.regions */
44648c3b583SGreg Pearson 		if (type != &memblock.reserved)
44748c3b583SGreg Pearson 			new_area_start = new_area_size = 0;
44848c3b583SGreg Pearson 
44948c3b583SGreg Pearson 		addr = memblock_find_in_range(new_area_start + new_area_size,
45048c3b583SGreg Pearson 						memblock.current_limit,
45129f67386SYinghai Lu 						new_alloc_size, PAGE_SIZE);
45248c3b583SGreg Pearson 		if (!addr && new_area_size)
45348c3b583SGreg Pearson 			addr = memblock_find_in_range(0,
45448c3b583SGreg Pearson 				min(new_area_start, memblock.current_limit),
45529f67386SYinghai Lu 				new_alloc_size, PAGE_SIZE);
45648c3b583SGreg Pearson 
45715674868SSachin Kamat 		new_array = addr ? __va(addr) : NULL;
4584e2f0775SGavin Shan 	}
4591f5026a7STejun Heo 	if (!addr) {
460142b45a7SBenjamin Herrenschmidt 		pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
4610262d9c8SHeiko Carstens 		       type->name, type->max, type->max * 2);
462142b45a7SBenjamin Herrenschmidt 		return -1;
463142b45a7SBenjamin Herrenschmidt 	}
464142b45a7SBenjamin Herrenschmidt 
465a36aab89SMike Rapoport 	new_end = addr + new_size - 1;
466a36aab89SMike Rapoport 	memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
467a36aab89SMike Rapoport 			type->name, type->max * 2, &addr, &new_end);
468ea9e4376SYinghai Lu 
469fd07383bSAndrew Morton 	/*
470fd07383bSAndrew Morton 	 * Found space, we now need to move the array over before we add the
471fd07383bSAndrew Morton 	 * reserved region since it may be our reserved array itself that is
472fd07383bSAndrew Morton 	 * full.
473142b45a7SBenjamin Herrenschmidt 	 */
474142b45a7SBenjamin Herrenschmidt 	memcpy(new_array, type->regions, old_size);
475142b45a7SBenjamin Herrenschmidt 	memset(new_array + type->max, 0, old_size);
476142b45a7SBenjamin Herrenschmidt 	old_array = type->regions;
477142b45a7SBenjamin Herrenschmidt 	type->regions = new_array;
478142b45a7SBenjamin Herrenschmidt 	type->max <<= 1;
479142b45a7SBenjamin Herrenschmidt 
480fd07383bSAndrew Morton 	/* Free old array. We needn't free it if the array is the static one */
481181eb394SGavin Shan 	if (*in_slab)
482181eb394SGavin Shan 		kfree(old_array);
483181eb394SGavin Shan 	else if (old_array != memblock_memory_init_regions &&
484142b45a7SBenjamin Herrenschmidt 		 old_array != memblock_reserved_init_regions)
4854421cca0SMike Rapoport 		memblock_free(old_array, old_alloc_size);
486142b45a7SBenjamin Herrenschmidt 
487fd07383bSAndrew Morton 	/*
488fd07383bSAndrew Morton 	 * Reserve the new array if that comes from the memblock.  Otherwise, we
489fd07383bSAndrew Morton 	 * needn't do it
490181eb394SGavin Shan 	 */
491181eb394SGavin Shan 	if (!use_slab)
49229f67386SYinghai Lu 		BUG_ON(memblock_reserve(addr, new_alloc_size));
493181eb394SGavin Shan 
494181eb394SGavin Shan 	/* Update slab flag */
495181eb394SGavin Shan 	*in_slab = use_slab;
496181eb394SGavin Shan 
497142b45a7SBenjamin Herrenschmidt 	return 0;
498142b45a7SBenjamin Herrenschmidt }
499142b45a7SBenjamin Herrenschmidt 
500784656f9STejun Heo /**
501784656f9STejun Heo  * memblock_merge_regions - merge neighboring compatible regions
502784656f9STejun Heo  * @type: memblock type to scan
5032fe03412SPeng Zhang  * @start_rgn: start scanning from (@start_rgn - 1)
5042fe03412SPeng Zhang  * @end_rgn: end scanning at (@end_rgn - 1)
5052fe03412SPeng Zhang  * Scan @type and merge neighboring compatible regions in [@start_rgn - 1, @end_rgn)
506784656f9STejun Heo  */
5072fe03412SPeng Zhang static void __init_memblock memblock_merge_regions(struct memblock_type *type,
5082fe03412SPeng Zhang 						   unsigned long start_rgn,
5092fe03412SPeng Zhang 						   unsigned long end_rgn)
510784656f9STejun Heo {
511784656f9STejun Heo 	int i = 0;
5122fe03412SPeng Zhang 	if (start_rgn)
5132fe03412SPeng Zhang 		i = start_rgn - 1;
5142fe03412SPeng Zhang 	end_rgn = min(end_rgn, type->cnt - 1);
5152fe03412SPeng Zhang 	while (i < end_rgn) {
516784656f9STejun Heo 		struct memblock_region *this = &type->regions[i];
517784656f9STejun Heo 		struct memblock_region *next = &type->regions[i + 1];
518784656f9STejun Heo 
5197c0caeb8STejun Heo 		if (this->base + this->size != next->base ||
5207c0caeb8STejun Heo 		    memblock_get_region_node(this) !=
52166a20757STang Chen 		    memblock_get_region_node(next) ||
52266a20757STang Chen 		    this->flags != next->flags) {
523784656f9STejun Heo 			BUG_ON(this->base + this->size > next->base);
524784656f9STejun Heo 			i++;
525784656f9STejun Heo 			continue;
526784656f9STejun Heo 		}
527784656f9STejun Heo 
528784656f9STejun Heo 		this->size += next->size;
529c0232ae8SLin Feng 		/* move forward from next + 1, index of which is i + 2 */
530c0232ae8SLin Feng 		memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
531784656f9STejun Heo 		type->cnt--;
5322fe03412SPeng Zhang 		end_rgn--;
533784656f9STejun Heo 	}
534784656f9STejun Heo }
535784656f9STejun Heo 
536784656f9STejun Heo /**
537784656f9STejun Heo  * memblock_insert_region - insert new memblock region
538784656f9STejun Heo  * @type:	memblock type to insert into
539784656f9STejun Heo  * @idx:	index for the insertion point
540784656f9STejun Heo  * @base:	base address of the new region
541784656f9STejun Heo  * @size:	size of the new region
542209ff86dSTang Chen  * @nid:	node id of the new region
54366a20757STang Chen  * @flags:	flags of the new region
544784656f9STejun Heo  *
545784656f9STejun Heo  * Insert new memblock region [@base, @base + @size) into @type at @idx.
546412d0008SAlexander Kuleshov  * @type must already have extra room to accommodate the new region.
547784656f9STejun Heo  */
548784656f9STejun Heo static void __init_memblock memblock_insert_region(struct memblock_type *type,
549784656f9STejun Heo 						   int idx, phys_addr_t base,
55066a20757STang Chen 						   phys_addr_t size,
551e1720feeSMike Rapoport 						   int nid,
552e1720feeSMike Rapoport 						   enum memblock_flags flags)
553784656f9STejun Heo {
554784656f9STejun Heo 	struct memblock_region *rgn = &type->regions[idx];
555784656f9STejun Heo 
556784656f9STejun Heo 	BUG_ON(type->cnt >= type->max);
557784656f9STejun Heo 	memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
558784656f9STejun Heo 	rgn->base = base;
559784656f9STejun Heo 	rgn->size = size;
56066a20757STang Chen 	rgn->flags = flags;
5617c0caeb8STejun Heo 	memblock_set_region_node(rgn, nid);
562784656f9STejun Heo 	type->cnt++;
5631440c4e2STejun Heo 	type->total_size += size;
564784656f9STejun Heo }
565784656f9STejun Heo 
566784656f9STejun Heo /**
567f1af9d3aSPhilipp Hachtmann  * memblock_add_range - add new memblock region
568784656f9STejun Heo  * @type: memblock type to add new region into
569784656f9STejun Heo  * @base: base address of the new region
570784656f9STejun Heo  * @size: size of the new region
5717fb0bc3fSTejun Heo  * @nid: nid of the new region
57266a20757STang Chen  * @flags: flags of the new region
573784656f9STejun Heo  *
574784656f9STejun Heo  * Add new memblock region [@base, @base + @size) into @type.  The new region
575784656f9STejun Heo  * is allowed to overlap with existing ones - overlaps don't affect already
576784656f9STejun Heo  * existing regions.  @type is guaranteed to be minimal (all neighbouring
577784656f9STejun Heo  * compatible regions are merged) after the addition.
578784656f9STejun Heo  *
57947cec443SMike Rapoport  * Return:
580784656f9STejun Heo  * 0 on success, -errno on failure.
581784656f9STejun Heo  */
58202634a44SAnshuman Khandual static int __init_memblock memblock_add_range(struct memblock_type *type,
58366a20757STang Chen 				phys_addr_t base, phys_addr_t size,
584e1720feeSMike Rapoport 				int nid, enum memblock_flags flags)
58595f72d1eSYinghai Lu {
586784656f9STejun Heo 	bool insert = false;
587eb18f1b5STejun Heo 	phys_addr_t obase = base;
588eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
5892fe03412SPeng Zhang 	int idx, nr_new, start_rgn = -1, end_rgn;
5908c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
59195f72d1eSYinghai Lu 
592b3dc627cSTejun Heo 	if (!size)
593b3dc627cSTejun Heo 		return 0;
594b3dc627cSTejun Heo 
595784656f9STejun Heo 	/* special case for empty array */
596784656f9STejun Heo 	if (type->regions[0].size == 0) {
5971440c4e2STejun Heo 		WARN_ON(type->cnt != 1 || type->total_size);
598784656f9STejun Heo 		type->regions[0].base = base;
599784656f9STejun Heo 		type->regions[0].size = size;
60066a20757STang Chen 		type->regions[0].flags = flags;
6017fb0bc3fSTejun Heo 		memblock_set_region_node(&type->regions[0], nid);
6021440c4e2STejun Heo 		type->total_size = size;
603784656f9STejun Heo 		return 0;
604784656f9STejun Heo 	}
60528e1a8f4SJinyu Tang 
60628e1a8f4SJinyu Tang 	/*
60728e1a8f4SJinyu Tang 	 * The worst case is when new range overlaps all existing regions,
60828e1a8f4SJinyu Tang 	 * then we'll need type->cnt + 1 empty regions in @type. So if
609ad500fb2SPeng Zhang 	 * type->cnt * 2 + 1 is less than or equal to type->max, we know
61028e1a8f4SJinyu Tang 	 * that there is enough empty regions in @type, and we can insert
61128e1a8f4SJinyu Tang 	 * regions directly.
61228e1a8f4SJinyu Tang 	 */
613ad500fb2SPeng Zhang 	if (type->cnt * 2 + 1 <= type->max)
61428e1a8f4SJinyu Tang 		insert = true;
61528e1a8f4SJinyu Tang 
616784656f9STejun Heo repeat:
617784656f9STejun Heo 	/*
618784656f9STejun Heo 	 * The following is executed twice.  Once with %false @insert and
619784656f9STejun Heo 	 * then with %true.  The first counts the number of regions needed
620412d0008SAlexander Kuleshov 	 * to accommodate the new area.  The second actually inserts them.
621784656f9STejun Heo 	 */
622784656f9STejun Heo 	base = obase;
623784656f9STejun Heo 	nr_new = 0;
624784656f9STejun Heo 
62566e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
626784656f9STejun Heo 		phys_addr_t rbase = rgn->base;
627784656f9STejun Heo 		phys_addr_t rend = rbase + rgn->size;
6288f7a6605SBenjamin Herrenschmidt 
629784656f9STejun Heo 		if (rbase >= end)
6308f7a6605SBenjamin Herrenschmidt 			break;
631784656f9STejun Heo 		if (rend <= base)
632784656f9STejun Heo 			continue;
633784656f9STejun Heo 		/*
634784656f9STejun Heo 		 * @rgn overlaps.  If it separates the lower part of new
635784656f9STejun Heo 		 * area, insert that portion.
6368f7a6605SBenjamin Herrenschmidt 		 */
637784656f9STejun Heo 		if (rbase > base) {
638a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA
639c0a29498SWei Yang 			WARN_ON(nid != memblock_get_region_node(rgn));
640c0a29498SWei Yang #endif
6414fcab5f4SWei Yang 			WARN_ON(flags != rgn->flags);
642784656f9STejun Heo 			nr_new++;
6432fe03412SPeng Zhang 			if (insert) {
6442fe03412SPeng Zhang 				if (start_rgn == -1)
6452fe03412SPeng Zhang 					start_rgn = idx;
6462fe03412SPeng Zhang 				end_rgn = idx + 1;
6478c9c1701SAlexander Kuleshov 				memblock_insert_region(type, idx++, base,
64866a20757STang Chen 						       rbase - base, nid,
64966a20757STang Chen 						       flags);
650784656f9STejun Heo 			}
6512fe03412SPeng Zhang 		}
652784656f9STejun Heo 		/* area below @rend is dealt with, forget about it */
653784656f9STejun Heo 		base = min(rend, end);
6548f7a6605SBenjamin Herrenschmidt 	}
6558f7a6605SBenjamin Herrenschmidt 
656784656f9STejun Heo 	/* insert the remaining portion */
657784656f9STejun Heo 	if (base < end) {
658784656f9STejun Heo 		nr_new++;
6592fe03412SPeng Zhang 		if (insert) {
6602fe03412SPeng Zhang 			if (start_rgn == -1)
6612fe03412SPeng Zhang 				start_rgn = idx;
6622fe03412SPeng Zhang 			end_rgn = idx + 1;
6638c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, base, end - base,
66466a20757STang Chen 					       nid, flags);
6658f7a6605SBenjamin Herrenschmidt 		}
6662fe03412SPeng Zhang 	}
6678f7a6605SBenjamin Herrenschmidt 
668ef3cc4dbSnimisolo 	if (!nr_new)
669ef3cc4dbSnimisolo 		return 0;
670ef3cc4dbSnimisolo 
671784656f9STejun Heo 	/*
672784656f9STejun Heo 	 * If this was the first round, resize array and repeat for actual
673784656f9STejun Heo 	 * insertions; otherwise, merge and return.
6748f7a6605SBenjamin Herrenschmidt 	 */
675784656f9STejun Heo 	if (!insert) {
676784656f9STejun Heo 		while (type->cnt + nr_new > type->max)
67748c3b583SGreg Pearson 			if (memblock_double_array(type, obase, size) < 0)
678784656f9STejun Heo 				return -ENOMEM;
679784656f9STejun Heo 		insert = true;
680784656f9STejun Heo 		goto repeat;
68195f72d1eSYinghai Lu 	} else {
6822fe03412SPeng Zhang 		memblock_merge_regions(type, start_rgn, end_rgn);
68395f72d1eSYinghai Lu 		return 0;
68495f72d1eSYinghai Lu 	}
685784656f9STejun Heo }
68695f72d1eSYinghai Lu 
68748a833ccSMike Rapoport /**
68848a833ccSMike Rapoport  * memblock_add_node - add new memblock region within a NUMA node
68948a833ccSMike Rapoport  * @base: base address of the new region
69048a833ccSMike Rapoport  * @size: size of the new region
69148a833ccSMike Rapoport  * @nid: nid of the new region
692952eea9bSDavid Hildenbrand  * @flags: flags of the new region
69348a833ccSMike Rapoport  *
69448a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
69548a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
69648a833ccSMike Rapoport  *
69748a833ccSMike Rapoport  * Return:
69848a833ccSMike Rapoport  * 0 on success, -errno on failure.
69948a833ccSMike Rapoport  */
7007fb0bc3fSTejun Heo int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
701952eea9bSDavid Hildenbrand 				      int nid, enum memblock_flags flags)
7027fb0bc3fSTejun Heo {
70300974b9aSGeert Uytterhoeven 	phys_addr_t end = base + size - 1;
70400974b9aSGeert Uytterhoeven 
705952eea9bSDavid Hildenbrand 	memblock_dbg("%s: [%pa-%pa] nid=%d flags=%x %pS\n", __func__,
706952eea9bSDavid Hildenbrand 		     &base, &end, nid, flags, (void *)_RET_IP_);
70700974b9aSGeert Uytterhoeven 
708952eea9bSDavid Hildenbrand 	return memblock_add_range(&memblock.memory, base, size, nid, flags);
7097fb0bc3fSTejun Heo }
7107fb0bc3fSTejun Heo 
71148a833ccSMike Rapoport /**
71248a833ccSMike Rapoport  * memblock_add - add new memblock region
71348a833ccSMike Rapoport  * @base: base address of the new region
71448a833ccSMike Rapoport  * @size: size of the new region
71548a833ccSMike Rapoport  *
71648a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
71748a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
71848a833ccSMike Rapoport  *
71948a833ccSMike Rapoport  * Return:
72048a833ccSMike Rapoport  * 0 on success, -errno on failure.
72148a833ccSMike Rapoport  */
722f705ac4bSAlexander Kuleshov int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
7236a4055bcSAlexander Kuleshov {
7245d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
7255d63f81cSMiles Chen 
726a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
7275d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
7286a4055bcSAlexander Kuleshov 
729f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
73095f72d1eSYinghai Lu }
73195f72d1eSYinghai Lu 
7326a9ceb31STejun Heo /**
7336a9ceb31STejun Heo  * memblock_isolate_range - isolate given range into disjoint memblocks
7346a9ceb31STejun Heo  * @type: memblock type to isolate range for
7356a9ceb31STejun Heo  * @base: base of range to isolate
7366a9ceb31STejun Heo  * @size: size of range to isolate
7376a9ceb31STejun Heo  * @start_rgn: out parameter for the start of isolated region
7386a9ceb31STejun Heo  * @end_rgn: out parameter for the end of isolated region
7396a9ceb31STejun Heo  *
7406a9ceb31STejun Heo  * Walk @type and ensure that regions don't cross the boundaries defined by
7416a9ceb31STejun Heo  * [@base, @base + @size).  Crossing regions are split at the boundaries,
7426a9ceb31STejun Heo  * which may create at most two more regions.  The index of the first
7436a9ceb31STejun Heo  * region inside the range is returned in *@start_rgn and end in *@end_rgn.
7446a9ceb31STejun Heo  *
74547cec443SMike Rapoport  * Return:
7466a9ceb31STejun Heo  * 0 on success, -errno on failure.
7476a9ceb31STejun Heo  */
7486a9ceb31STejun Heo static int __init_memblock memblock_isolate_range(struct memblock_type *type,
7496a9ceb31STejun Heo 					phys_addr_t base, phys_addr_t size,
7506a9ceb31STejun Heo 					int *start_rgn, int *end_rgn)
7516a9ceb31STejun Heo {
752eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
7538c9c1701SAlexander Kuleshov 	int idx;
7548c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
7556a9ceb31STejun Heo 
7566a9ceb31STejun Heo 	*start_rgn = *end_rgn = 0;
7576a9ceb31STejun Heo 
758b3dc627cSTejun Heo 	if (!size)
759b3dc627cSTejun Heo 		return 0;
760b3dc627cSTejun Heo 
7616a9ceb31STejun Heo 	/* we'll create at most two more regions */
7626a9ceb31STejun Heo 	while (type->cnt + 2 > type->max)
76348c3b583SGreg Pearson 		if (memblock_double_array(type, base, size) < 0)
7646a9ceb31STejun Heo 			return -ENOMEM;
7656a9ceb31STejun Heo 
76666e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
7676a9ceb31STejun Heo 		phys_addr_t rbase = rgn->base;
7686a9ceb31STejun Heo 		phys_addr_t rend = rbase + rgn->size;
7696a9ceb31STejun Heo 
7706a9ceb31STejun Heo 		if (rbase >= end)
7716a9ceb31STejun Heo 			break;
7726a9ceb31STejun Heo 		if (rend <= base)
7736a9ceb31STejun Heo 			continue;
7746a9ceb31STejun Heo 
7756a9ceb31STejun Heo 		if (rbase < base) {
7766a9ceb31STejun Heo 			/*
7776a9ceb31STejun Heo 			 * @rgn intersects from below.  Split and continue
7786a9ceb31STejun Heo 			 * to process the next region - the new top half.
7796a9ceb31STejun Heo 			 */
7806a9ceb31STejun Heo 			rgn->base = base;
7811440c4e2STejun Heo 			rgn->size -= base - rbase;
7821440c4e2STejun Heo 			type->total_size -= base - rbase;
7838c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, rbase, base - rbase,
78466a20757STang Chen 					       memblock_get_region_node(rgn),
78566a20757STang Chen 					       rgn->flags);
7866a9ceb31STejun Heo 		} else if (rend > end) {
7876a9ceb31STejun Heo 			/*
7886a9ceb31STejun Heo 			 * @rgn intersects from above.  Split and redo the
7896a9ceb31STejun Heo 			 * current region - the new bottom half.
7906a9ceb31STejun Heo 			 */
7916a9ceb31STejun Heo 			rgn->base = end;
7921440c4e2STejun Heo 			rgn->size -= end - rbase;
7931440c4e2STejun Heo 			type->total_size -= end - rbase;
7948c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx--, rbase, end - rbase,
79566a20757STang Chen 					       memblock_get_region_node(rgn),
79666a20757STang Chen 					       rgn->flags);
7976a9ceb31STejun Heo 		} else {
7986a9ceb31STejun Heo 			/* @rgn is fully contained, record it */
7996a9ceb31STejun Heo 			if (!*end_rgn)
8008c9c1701SAlexander Kuleshov 				*start_rgn = idx;
8018c9c1701SAlexander Kuleshov 			*end_rgn = idx + 1;
8026a9ceb31STejun Heo 		}
8036a9ceb31STejun Heo 	}
8046a9ceb31STejun Heo 
8056a9ceb31STejun Heo 	return 0;
8066a9ceb31STejun Heo }
8076a9ceb31STejun Heo 
80835bd16a2SAlexander Kuleshov static int __init_memblock memblock_remove_range(struct memblock_type *type,
8098f7a6605SBenjamin Herrenschmidt 					  phys_addr_t base, phys_addr_t size)
81095f72d1eSYinghai Lu {
81171936180STejun Heo 	int start_rgn, end_rgn;
81271936180STejun Heo 	int i, ret;
81395f72d1eSYinghai Lu 
81471936180STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
81571936180STejun Heo 	if (ret)
81671936180STejun Heo 		return ret;
81795f72d1eSYinghai Lu 
81871936180STejun Heo 	for (i = end_rgn - 1; i >= start_rgn; i--)
81971936180STejun Heo 		memblock_remove_region(type, i);
82095f72d1eSYinghai Lu 	return 0;
82195f72d1eSYinghai Lu }
82295f72d1eSYinghai Lu 
823581adcbeSTejun Heo int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
82495f72d1eSYinghai Lu {
82525cf23d7SMinchan Kim 	phys_addr_t end = base + size - 1;
82625cf23d7SMinchan Kim 
827a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
82825cf23d7SMinchan Kim 		     &base, &end, (void *)_RET_IP_);
82925cf23d7SMinchan Kim 
830f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.memory, base, size);
83195f72d1eSYinghai Lu }
83295f72d1eSYinghai Lu 
8334d72868cSMike Rapoport /**
8344421cca0SMike Rapoport  * memblock_free - free boot memory allocation
83577e02cf5SLinus Torvalds  * @ptr: starting address of the  boot memory allocation
83677e02cf5SLinus Torvalds  * @size: size of the boot memory block in bytes
83777e02cf5SLinus Torvalds  *
83877e02cf5SLinus Torvalds  * Free boot memory block previously allocated by memblock_alloc_xx() API.
83977e02cf5SLinus Torvalds  * The freeing memory will not be released to the buddy allocator.
84077e02cf5SLinus Torvalds  */
8414421cca0SMike Rapoport void __init_memblock memblock_free(void *ptr, size_t size)
84277e02cf5SLinus Torvalds {
84377e02cf5SLinus Torvalds 	if (ptr)
8443ecc6834SMike Rapoport 		memblock_phys_free(__pa(ptr), size);
84577e02cf5SLinus Torvalds }
84677e02cf5SLinus Torvalds 
84777e02cf5SLinus Torvalds /**
8483ecc6834SMike Rapoport  * memblock_phys_free - free boot memory block
8494d72868cSMike Rapoport  * @base: phys starting address of the  boot memory block
8504d72868cSMike Rapoport  * @size: size of the boot memory block in bytes
8514d72868cSMike Rapoport  *
852fa81ab49SMiaoqian Lin  * Free boot memory block previously allocated by memblock_phys_alloc_xx() API.
8534d72868cSMike Rapoport  * The freeing memory will not be released to the buddy allocator.
8544d72868cSMike Rapoport  */
8553ecc6834SMike Rapoport int __init_memblock memblock_phys_free(phys_addr_t base, phys_addr_t size)
85695f72d1eSYinghai Lu {
8575d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8585d63f81cSMiles Chen 
859a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
8605d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
86124aa0788STejun Heo 
8629099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
863f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.reserved, base, size);
86495f72d1eSYinghai Lu }
86595f72d1eSYinghai Lu 
866f705ac4bSAlexander Kuleshov int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
86795f72d1eSYinghai Lu {
8685d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8695d63f81cSMiles Chen 
870a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
8715d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
87295f72d1eSYinghai Lu 
873f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
87495f72d1eSYinghai Lu }
87595f72d1eSYinghai Lu 
87602634a44SAnshuman Khandual #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
87702634a44SAnshuman Khandual int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size)
87802634a44SAnshuman Khandual {
87902634a44SAnshuman Khandual 	phys_addr_t end = base + size - 1;
88002634a44SAnshuman Khandual 
88102634a44SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
88202634a44SAnshuman Khandual 		     &base, &end, (void *)_RET_IP_);
88302634a44SAnshuman Khandual 
88477649905SDavid Hildenbrand 	return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0);
88502634a44SAnshuman Khandual }
88602634a44SAnshuman Khandual #endif
88702634a44SAnshuman Khandual 
88835fd0808STejun Heo /**
88947cec443SMike Rapoport  * memblock_setclr_flag - set or clear flag for a memory region
89047cec443SMike Rapoport  * @base: base address of the region
89147cec443SMike Rapoport  * @size: size of the region
89247cec443SMike Rapoport  * @set: set or clear the flag
8938958b249SHaitao Shi  * @flag: the flag to update
89466b16edfSTang Chen  *
8954308ce17STony Luck  * This function isolates region [@base, @base + @size), and sets/clears flag
89666b16edfSTang Chen  *
89747cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
89866b16edfSTang Chen  */
8994308ce17STony Luck static int __init_memblock memblock_setclr_flag(phys_addr_t base,
9004308ce17STony Luck 				phys_addr_t size, int set, int flag)
90166b16edfSTang Chen {
90266b16edfSTang Chen 	struct memblock_type *type = &memblock.memory;
90366b16edfSTang Chen 	int i, ret, start_rgn, end_rgn;
90466b16edfSTang Chen 
90566b16edfSTang Chen 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
90666b16edfSTang Chen 	if (ret)
90766b16edfSTang Chen 		return ret;
90866b16edfSTang Chen 
909fe145124SMike Rapoport 	for (i = start_rgn; i < end_rgn; i++) {
910fe145124SMike Rapoport 		struct memblock_region *r = &type->regions[i];
911fe145124SMike Rapoport 
9124308ce17STony Luck 		if (set)
913fe145124SMike Rapoport 			r->flags |= flag;
9144308ce17STony Luck 		else
915fe145124SMike Rapoport 			r->flags &= ~flag;
916fe145124SMike Rapoport 	}
91766b16edfSTang Chen 
9182fe03412SPeng Zhang 	memblock_merge_regions(type, start_rgn, end_rgn);
91966b16edfSTang Chen 	return 0;
92066b16edfSTang Chen }
92166b16edfSTang Chen 
92266b16edfSTang Chen /**
9234308ce17STony Luck  * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
9244308ce17STony Luck  * @base: the base phys addr of the region
9254308ce17STony Luck  * @size: the size of the region
9264308ce17STony Luck  *
92747cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
9284308ce17STony Luck  */
9294308ce17STony Luck int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
9304308ce17STony Luck {
9314308ce17STony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
9324308ce17STony Luck }
9334308ce17STony Luck 
9344308ce17STony Luck /**
93566b16edfSTang Chen  * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
93666b16edfSTang Chen  * @base: the base phys addr of the region
93766b16edfSTang Chen  * @size: the size of the region
93866b16edfSTang Chen  *
93947cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
94066b16edfSTang Chen  */
94166b16edfSTang Chen int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
94266b16edfSTang Chen {
9434308ce17STony Luck 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
94466b16edfSTang Chen }
94566b16edfSTang Chen 
94666b16edfSTang Chen /**
947a3f5bafcSTony Luck  * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
948a3f5bafcSTony Luck  * @base: the base phys addr of the region
949a3f5bafcSTony Luck  * @size: the size of the region
950a3f5bafcSTony Luck  *
95147cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
952a3f5bafcSTony Luck  */
953a3f5bafcSTony Luck int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
954a3f5bafcSTony Luck {
955902c2d91SMa Wupeng 	if (!mirrored_kernelcore)
956902c2d91SMa Wupeng 		return 0;
957902c2d91SMa Wupeng 
958a3f5bafcSTony Luck 	system_has_some_mirror = true;
959a3f5bafcSTony Luck 
960a3f5bafcSTony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
961a3f5bafcSTony Luck }
962a3f5bafcSTony Luck 
963bf3d3cc5SArd Biesheuvel /**
964bf3d3cc5SArd Biesheuvel  * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
965bf3d3cc5SArd Biesheuvel  * @base: the base phys addr of the region
966bf3d3cc5SArd Biesheuvel  * @size: the size of the region
967bf3d3cc5SArd Biesheuvel  *
9689092d4f7SMike Rapoport  * The memory regions marked with %MEMBLOCK_NOMAP will not be added to the
9699092d4f7SMike Rapoport  * direct mapping of the physical memory. These regions will still be
9709092d4f7SMike Rapoport  * covered by the memory map. The struct page representing NOMAP memory
9719092d4f7SMike Rapoport  * frames in the memory map will be PageReserved()
9729092d4f7SMike Rapoport  *
973658aafc8SMike Rapoport  * Note: if the memory being marked %MEMBLOCK_NOMAP was allocated from
974658aafc8SMike Rapoport  * memblock, the caller must inform kmemleak to ignore that memory
975658aafc8SMike Rapoport  *
97647cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
977bf3d3cc5SArd Biesheuvel  */
978bf3d3cc5SArd Biesheuvel int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
979bf3d3cc5SArd Biesheuvel {
9806c9a5455SMike Rapoport 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
981bf3d3cc5SArd Biesheuvel }
982a3f5bafcSTony Luck 
983a3f5bafcSTony Luck /**
9844c546b8aSAKASHI Takahiro  * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
9854c546b8aSAKASHI Takahiro  * @base: the base phys addr of the region
9864c546b8aSAKASHI Takahiro  * @size: the size of the region
9874c546b8aSAKASHI Takahiro  *
98847cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
9894c546b8aSAKASHI Takahiro  */
9904c546b8aSAKASHI Takahiro int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
9914c546b8aSAKASHI Takahiro {
9924c546b8aSAKASHI Takahiro 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
9934c546b8aSAKASHI Takahiro }
9944c546b8aSAKASHI Takahiro 
9959f3d5eaaSMike Rapoport static bool should_skip_region(struct memblock_type *type,
9969f3d5eaaSMike Rapoport 			       struct memblock_region *m,
9979f3d5eaaSMike Rapoport 			       int nid, int flags)
998c9a688a3SMike Rapoport {
999c9a688a3SMike Rapoport 	int m_nid = memblock_get_region_node(m);
1000c9a688a3SMike Rapoport 
10019f3d5eaaSMike Rapoport 	/* we never skip regions when iterating memblock.reserved or physmem */
10029f3d5eaaSMike Rapoport 	if (type != memblock_memory)
10039f3d5eaaSMike Rapoport 		return false;
10049f3d5eaaSMike Rapoport 
1005c9a688a3SMike Rapoport 	/* only memory regions are associated with nodes, check it */
1006c9a688a3SMike Rapoport 	if (nid != NUMA_NO_NODE && nid != m_nid)
1007c9a688a3SMike Rapoport 		return true;
1008c9a688a3SMike Rapoport 
1009c9a688a3SMike Rapoport 	/* skip hotpluggable memory regions if needed */
101079e482e9SMike Rapoport 	if (movable_node_is_enabled() && memblock_is_hotpluggable(m) &&
101179e482e9SMike Rapoport 	    !(flags & MEMBLOCK_HOTPLUG))
1012c9a688a3SMike Rapoport 		return true;
1013c9a688a3SMike Rapoport 
1014c9a688a3SMike Rapoport 	/* if we want mirror memory skip non-mirror memory regions */
1015c9a688a3SMike Rapoport 	if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1016c9a688a3SMike Rapoport 		return true;
1017c9a688a3SMike Rapoport 
1018c9a688a3SMike Rapoport 	/* skip nomap memory unless we were asked for it explicitly */
1019c9a688a3SMike Rapoport 	if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1020c9a688a3SMike Rapoport 		return true;
1021c9a688a3SMike Rapoport 
1022f7892d8eSDavid Hildenbrand 	/* skip driver-managed memory unless we were asked for it explicitly */
1023f7892d8eSDavid Hildenbrand 	if (!(flags & MEMBLOCK_DRIVER_MANAGED) && memblock_is_driver_managed(m))
1024f7892d8eSDavid Hildenbrand 		return true;
1025f7892d8eSDavid Hildenbrand 
1026c9a688a3SMike Rapoport 	return false;
1027c9a688a3SMike Rapoport }
1028c9a688a3SMike Rapoport 
10298e7a7f86SRobin Holt /**
1030a2974133SMike Rapoport  * __next_mem_range - next function for for_each_free_mem_range() etc.
103135fd0808STejun Heo  * @idx: pointer to u64 loop variable
1032b1154233SGrygorii Strashko  * @nid: node selector, %NUMA_NO_NODE for all nodes
1033fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
1034f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
1035f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
1036dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1037dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1038dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
103935fd0808STejun Heo  *
1040f1af9d3aSPhilipp Hachtmann  * Find the first area from *@idx which matches @nid, fill the out
104135fd0808STejun Heo  * parameters, and update *@idx for the next iteration.  The lower 32bit of
1042f1af9d3aSPhilipp Hachtmann  * *@idx contains index into type_a and the upper 32bit indexes the
1043f1af9d3aSPhilipp Hachtmann  * areas before each region in type_b.	For example, if type_b regions
104435fd0808STejun Heo  * look like the following,
104535fd0808STejun Heo  *
104635fd0808STejun Heo  *	0:[0-16), 1:[32-48), 2:[128-130)
104735fd0808STejun Heo  *
104835fd0808STejun Heo  * The upper 32bit indexes the following regions.
104935fd0808STejun Heo  *
105035fd0808STejun Heo  *	0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
105135fd0808STejun Heo  *
105235fd0808STejun Heo  * As both region arrays are sorted, the function advances the two indices
105335fd0808STejun Heo  * in lockstep and returns each intersection.
105435fd0808STejun Heo  */
105577649905SDavid Hildenbrand void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
1056f1af9d3aSPhilipp Hachtmann 		      struct memblock_type *type_a,
105777649905SDavid Hildenbrand 		      struct memblock_type *type_b, phys_addr_t *out_start,
105835fd0808STejun Heo 		      phys_addr_t *out_end, int *out_nid)
105935fd0808STejun Heo {
1060f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1061f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1062b1154233SGrygorii Strashko 
1063f1af9d3aSPhilipp Hachtmann 	if (WARN_ONCE(nid == MAX_NUMNODES,
1064f1af9d3aSPhilipp Hachtmann 	"Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1065560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
106635fd0808STejun Heo 
1067f1af9d3aSPhilipp Hachtmann 	for (; idx_a < type_a->cnt; idx_a++) {
1068f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1069f1af9d3aSPhilipp Hachtmann 
107035fd0808STejun Heo 		phys_addr_t m_start = m->base;
107135fd0808STejun Heo 		phys_addr_t m_end = m->base + m->size;
1072f1af9d3aSPhilipp Hachtmann 		int	    m_nid = memblock_get_region_node(m);
107335fd0808STejun Heo 
10749f3d5eaaSMike Rapoport 		if (should_skip_region(type_a, m, nid, flags))
1075bf3d3cc5SArd Biesheuvel 			continue;
1076bf3d3cc5SArd Biesheuvel 
1077f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1078f1af9d3aSPhilipp Hachtmann 			if (out_start)
1079f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1080f1af9d3aSPhilipp Hachtmann 			if (out_end)
1081f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1082f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1083f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1084f1af9d3aSPhilipp Hachtmann 			idx_a++;
1085f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1086f1af9d3aSPhilipp Hachtmann 			return;
1087f1af9d3aSPhilipp Hachtmann 		}
108835fd0808STejun Heo 
1089f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1090f1af9d3aSPhilipp Hachtmann 		for (; idx_b < type_b->cnt + 1; idx_b++) {
1091f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1092f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1093f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1094f1af9d3aSPhilipp Hachtmann 
1095f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1096f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1097f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
10981c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1099f1af9d3aSPhilipp Hachtmann 
1100f1af9d3aSPhilipp Hachtmann 			/*
1101f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1102f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1103f1af9d3aSPhilipp Hachtmann 			 */
110435fd0808STejun Heo 			if (r_start >= m_end)
110535fd0808STejun Heo 				break;
110635fd0808STejun Heo 			/* if the two regions intersect, we're done */
110735fd0808STejun Heo 			if (m_start < r_end) {
110835fd0808STejun Heo 				if (out_start)
1109f1af9d3aSPhilipp Hachtmann 					*out_start =
1110f1af9d3aSPhilipp Hachtmann 						max(m_start, r_start);
111135fd0808STejun Heo 				if (out_end)
111235fd0808STejun Heo 					*out_end = min(m_end, r_end);
111335fd0808STejun Heo 				if (out_nid)
1114f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
111535fd0808STejun Heo 				/*
1116f1af9d3aSPhilipp Hachtmann 				 * The region which ends first is
1117f1af9d3aSPhilipp Hachtmann 				 * advanced for the next iteration.
111835fd0808STejun Heo 				 */
111935fd0808STejun Heo 				if (m_end <= r_end)
1120f1af9d3aSPhilipp Hachtmann 					idx_a++;
112135fd0808STejun Heo 				else
1122f1af9d3aSPhilipp Hachtmann 					idx_b++;
1123f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
112435fd0808STejun Heo 				return;
112535fd0808STejun Heo 			}
112635fd0808STejun Heo 		}
112735fd0808STejun Heo 	}
112835fd0808STejun Heo 
112935fd0808STejun Heo 	/* signal end of iteration */
113035fd0808STejun Heo 	*idx = ULLONG_MAX;
113135fd0808STejun Heo }
113235fd0808STejun Heo 
11337bd0b0f0STejun Heo /**
1134f1af9d3aSPhilipp Hachtmann  * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1135f1af9d3aSPhilipp Hachtmann  *
11367bd0b0f0STejun Heo  * @idx: pointer to u64 loop variable
1137ad5ea8cdSAlexander Kuleshov  * @nid: node selector, %NUMA_NO_NODE for all nodes
1138fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
1139f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
1140f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
1141dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1142dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1143dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
11447bd0b0f0STejun Heo  *
114547cec443SMike Rapoport  * Finds the next range from type_a which is not marked as unsuitable
114647cec443SMike Rapoport  * in type_b.
114747cec443SMike Rapoport  *
1148f1af9d3aSPhilipp Hachtmann  * Reverse of __next_mem_range().
11497bd0b0f0STejun Heo  */
1150e1720feeSMike Rapoport void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1151e1720feeSMike Rapoport 					  enum memblock_flags flags,
1152f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_a,
1153f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_b,
11547bd0b0f0STejun Heo 					  phys_addr_t *out_start,
11557bd0b0f0STejun Heo 					  phys_addr_t *out_end, int *out_nid)
11567bd0b0f0STejun Heo {
1157f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1158f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1159b1154233SGrygorii Strashko 
1160560dca27SGrygorii Strashko 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1161560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
11627bd0b0f0STejun Heo 
11637bd0b0f0STejun Heo 	if (*idx == (u64)ULLONG_MAX) {
1164f1af9d3aSPhilipp Hachtmann 		idx_a = type_a->cnt - 1;
1165e47608abSzijun_hu 		if (type_b != NULL)
1166f1af9d3aSPhilipp Hachtmann 			idx_b = type_b->cnt;
1167e47608abSzijun_hu 		else
1168e47608abSzijun_hu 			idx_b = 0;
11697bd0b0f0STejun Heo 	}
11707bd0b0f0STejun Heo 
1171f1af9d3aSPhilipp Hachtmann 	for (; idx_a >= 0; idx_a--) {
1172f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1173f1af9d3aSPhilipp Hachtmann 
11747bd0b0f0STejun Heo 		phys_addr_t m_start = m->base;
11757bd0b0f0STejun Heo 		phys_addr_t m_end = m->base + m->size;
1176f1af9d3aSPhilipp Hachtmann 		int m_nid = memblock_get_region_node(m);
11777bd0b0f0STejun Heo 
11789f3d5eaaSMike Rapoport 		if (should_skip_region(type_a, m, nid, flags))
1179bf3d3cc5SArd Biesheuvel 			continue;
1180bf3d3cc5SArd Biesheuvel 
1181f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1182f1af9d3aSPhilipp Hachtmann 			if (out_start)
1183f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1184f1af9d3aSPhilipp Hachtmann 			if (out_end)
1185f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1186f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1187f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1188fb399b48Szijun_hu 			idx_a--;
1189f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1190f1af9d3aSPhilipp Hachtmann 			return;
1191f1af9d3aSPhilipp Hachtmann 		}
11927bd0b0f0STejun Heo 
1193f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1194f1af9d3aSPhilipp Hachtmann 		for (; idx_b >= 0; idx_b--) {
1195f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1196f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1197f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1198f1af9d3aSPhilipp Hachtmann 
1199f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1200f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1201f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
12021c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1203f1af9d3aSPhilipp Hachtmann 			/*
1204f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1205f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1206f1af9d3aSPhilipp Hachtmann 			 */
1207f1af9d3aSPhilipp Hachtmann 
12087bd0b0f0STejun Heo 			if (r_end <= m_start)
12097bd0b0f0STejun Heo 				break;
12107bd0b0f0STejun Heo 			/* if the two regions intersect, we're done */
12117bd0b0f0STejun Heo 			if (m_end > r_start) {
12127bd0b0f0STejun Heo 				if (out_start)
12137bd0b0f0STejun Heo 					*out_start = max(m_start, r_start);
12147bd0b0f0STejun Heo 				if (out_end)
12157bd0b0f0STejun Heo 					*out_end = min(m_end, r_end);
12167bd0b0f0STejun Heo 				if (out_nid)
1217f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
12187bd0b0f0STejun Heo 				if (m_start >= r_start)
1219f1af9d3aSPhilipp Hachtmann 					idx_a--;
12207bd0b0f0STejun Heo 				else
1221f1af9d3aSPhilipp Hachtmann 					idx_b--;
1222f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
12237bd0b0f0STejun Heo 				return;
12247bd0b0f0STejun Heo 			}
12257bd0b0f0STejun Heo 		}
12267bd0b0f0STejun Heo 	}
1227f1af9d3aSPhilipp Hachtmann 	/* signal end of iteration */
12287bd0b0f0STejun Heo 	*idx = ULLONG_MAX;
12297bd0b0f0STejun Heo }
12307bd0b0f0STejun Heo 
12317c0caeb8STejun Heo /*
123245e79815SChen Chang  * Common iterator interface used to define for_each_mem_pfn_range().
12337c0caeb8STejun Heo  */
12347c0caeb8STejun Heo void __init_memblock __next_mem_pfn_range(int *idx, int nid,
12357c0caeb8STejun Heo 				unsigned long *out_start_pfn,
12367c0caeb8STejun Heo 				unsigned long *out_end_pfn, int *out_nid)
12377c0caeb8STejun Heo {
12387c0caeb8STejun Heo 	struct memblock_type *type = &memblock.memory;
12397c0caeb8STejun Heo 	struct memblock_region *r;
1240d622abf7SMike Rapoport 	int r_nid;
12417c0caeb8STejun Heo 
12427c0caeb8STejun Heo 	while (++*idx < type->cnt) {
12437c0caeb8STejun Heo 		r = &type->regions[*idx];
1244d622abf7SMike Rapoport 		r_nid = memblock_get_region_node(r);
12457c0caeb8STejun Heo 
12467c0caeb8STejun Heo 		if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
12477c0caeb8STejun Heo 			continue;
1248d622abf7SMike Rapoport 		if (nid == MAX_NUMNODES || nid == r_nid)
12497c0caeb8STejun Heo 			break;
12507c0caeb8STejun Heo 	}
12517c0caeb8STejun Heo 	if (*idx >= type->cnt) {
12527c0caeb8STejun Heo 		*idx = -1;
12537c0caeb8STejun Heo 		return;
12547c0caeb8STejun Heo 	}
12557c0caeb8STejun Heo 
12567c0caeb8STejun Heo 	if (out_start_pfn)
12577c0caeb8STejun Heo 		*out_start_pfn = PFN_UP(r->base);
12587c0caeb8STejun Heo 	if (out_end_pfn)
12597c0caeb8STejun Heo 		*out_end_pfn = PFN_DOWN(r->base + r->size);
12607c0caeb8STejun Heo 	if (out_nid)
1261d622abf7SMike Rapoport 		*out_nid = r_nid;
12627c0caeb8STejun Heo }
12637c0caeb8STejun Heo 
12647c0caeb8STejun Heo /**
12657c0caeb8STejun Heo  * memblock_set_node - set node ID on memblock regions
12667c0caeb8STejun Heo  * @base: base of area to set node ID for
12677c0caeb8STejun Heo  * @size: size of area to set node ID for
1268e7e8de59STang Chen  * @type: memblock type to set node ID for
12697c0caeb8STejun Heo  * @nid: node ID to set
12707c0caeb8STejun Heo  *
1271e7e8de59STang Chen  * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
12727c0caeb8STejun Heo  * Regions which cross the area boundaries are split as necessary.
12737c0caeb8STejun Heo  *
127447cec443SMike Rapoport  * Return:
12757c0caeb8STejun Heo  * 0 on success, -errno on failure.
12767c0caeb8STejun Heo  */
12777c0caeb8STejun Heo int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1278e7e8de59STang Chen 				      struct memblock_type *type, int nid)
12797c0caeb8STejun Heo {
1280a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA
12816a9ceb31STejun Heo 	int start_rgn, end_rgn;
12826a9ceb31STejun Heo 	int i, ret;
12837c0caeb8STejun Heo 
12846a9ceb31STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
12856a9ceb31STejun Heo 	if (ret)
12866a9ceb31STejun Heo 		return ret;
12877c0caeb8STejun Heo 
12886a9ceb31STejun Heo 	for (i = start_rgn; i < end_rgn; i++)
1289e9d24ad3SWanpeng Li 		memblock_set_region_node(&type->regions[i], nid);
12907c0caeb8STejun Heo 
12912fe03412SPeng Zhang 	memblock_merge_regions(type, start_rgn, end_rgn);
12923f08a302SMike Rapoport #endif
12937c0caeb8STejun Heo 	return 0;
12947c0caeb8STejun Heo }
12953f08a302SMike Rapoport 
1296837566e7SAlexander Duyck #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1297837566e7SAlexander Duyck /**
1298837566e7SAlexander Duyck  * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
1299837566e7SAlexander Duyck  *
1300837566e7SAlexander Duyck  * @idx: pointer to u64 loop variable
1301837566e7SAlexander Duyck  * @zone: zone in which all of the memory blocks reside
1302837566e7SAlexander Duyck  * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
1303837566e7SAlexander Duyck  * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
1304837566e7SAlexander Duyck  *
1305837566e7SAlexander Duyck  * This function is meant to be a zone/pfn specific wrapper for the
1306837566e7SAlexander Duyck  * for_each_mem_range type iterators. Specifically they are used in the
1307837566e7SAlexander Duyck  * deferred memory init routines and as such we were duplicating much of
1308837566e7SAlexander Duyck  * this logic throughout the code. So instead of having it in multiple
1309837566e7SAlexander Duyck  * locations it seemed like it would make more sense to centralize this to
1310837566e7SAlexander Duyck  * one new iterator that does everything they need.
1311837566e7SAlexander Duyck  */
1312837566e7SAlexander Duyck void __init_memblock
1313837566e7SAlexander Duyck __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
1314837566e7SAlexander Duyck 			     unsigned long *out_spfn, unsigned long *out_epfn)
1315837566e7SAlexander Duyck {
1316837566e7SAlexander Duyck 	int zone_nid = zone_to_nid(zone);
1317837566e7SAlexander Duyck 	phys_addr_t spa, epa;
1318837566e7SAlexander Duyck 
1319837566e7SAlexander Duyck 	__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1320837566e7SAlexander Duyck 			 &memblock.memory, &memblock.reserved,
1321f30b002cSMiaohe Lin 			 &spa, &epa, NULL);
1322837566e7SAlexander Duyck 
1323837566e7SAlexander Duyck 	while (*idx != U64_MAX) {
1324837566e7SAlexander Duyck 		unsigned long epfn = PFN_DOWN(epa);
1325837566e7SAlexander Duyck 		unsigned long spfn = PFN_UP(spa);
1326837566e7SAlexander Duyck 
1327837566e7SAlexander Duyck 		/*
1328837566e7SAlexander Duyck 		 * Verify the end is at least past the start of the zone and
1329837566e7SAlexander Duyck 		 * that we have at least one PFN to initialize.
1330837566e7SAlexander Duyck 		 */
1331837566e7SAlexander Duyck 		if (zone->zone_start_pfn < epfn && spfn < epfn) {
1332837566e7SAlexander Duyck 			/* if we went too far just stop searching */
1333837566e7SAlexander Duyck 			if (zone_end_pfn(zone) <= spfn) {
1334837566e7SAlexander Duyck 				*idx = U64_MAX;
1335837566e7SAlexander Duyck 				break;
1336837566e7SAlexander Duyck 			}
1337837566e7SAlexander Duyck 
1338837566e7SAlexander Duyck 			if (out_spfn)
1339837566e7SAlexander Duyck 				*out_spfn = max(zone->zone_start_pfn, spfn);
1340837566e7SAlexander Duyck 			if (out_epfn)
1341837566e7SAlexander Duyck 				*out_epfn = min(zone_end_pfn(zone), epfn);
1342837566e7SAlexander Duyck 
1343837566e7SAlexander Duyck 			return;
1344837566e7SAlexander Duyck 		}
1345837566e7SAlexander Duyck 
1346837566e7SAlexander Duyck 		__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1347837566e7SAlexander Duyck 				 &memblock.memory, &memblock.reserved,
1348f30b002cSMiaohe Lin 				 &spa, &epa, NULL);
1349837566e7SAlexander Duyck 	}
1350837566e7SAlexander Duyck 
1351837566e7SAlexander Duyck 	/* signal end of iteration */
1352837566e7SAlexander Duyck 	if (out_spfn)
1353837566e7SAlexander Duyck 		*out_spfn = ULONG_MAX;
1354837566e7SAlexander Duyck 	if (out_epfn)
1355837566e7SAlexander Duyck 		*out_epfn = 0;
1356837566e7SAlexander Duyck }
1357837566e7SAlexander Duyck 
1358837566e7SAlexander Duyck #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
13597c0caeb8STejun Heo 
136092d12f95SMike Rapoport /**
136192d12f95SMike Rapoport  * memblock_alloc_range_nid - allocate boot memory block
136292d12f95SMike Rapoport  * @size: size of memory block to be allocated in bytes
136392d12f95SMike Rapoport  * @align: alignment of the region and block's size
136492d12f95SMike Rapoport  * @start: the lower bound of the memory region to allocate (phys address)
136592d12f95SMike Rapoport  * @end: the upper bound of the memory region to allocate (phys address)
136692d12f95SMike Rapoport  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
13670ac398b1SYunfeng Ye  * @exact_nid: control the allocation fall back to other nodes
136892d12f95SMike Rapoport  *
136992d12f95SMike Rapoport  * The allocation is performed from memory region limited by
137095830666SCao jin  * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE.
137192d12f95SMike Rapoport  *
13720ac398b1SYunfeng Ye  * If the specified node can not hold the requested memory and @exact_nid
13730ac398b1SYunfeng Ye  * is false, the allocation falls back to any node in the system.
137492d12f95SMike Rapoport  *
137592d12f95SMike Rapoport  * For systems with memory mirroring, the allocation is attempted first
137692d12f95SMike Rapoport  * from the regions with mirroring enabled and then retried from any
137792d12f95SMike Rapoport  * memory region.
137892d12f95SMike Rapoport  *
1379c200d900SPatrick Wang  * In addition, function using kmemleak_alloc_phys for allocated boot
1380c200d900SPatrick Wang  * memory block, it is never reported as leaks.
138192d12f95SMike Rapoport  *
138292d12f95SMike Rapoport  * Return:
138392d12f95SMike Rapoport  * Physical address of allocated memory block on success, %0 on failure.
138492d12f95SMike Rapoport  */
13858676af1fSAslan Bakirov phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
13862bfc2862SAkinobu Mita 					phys_addr_t align, phys_addr_t start,
13870ac398b1SYunfeng Ye 					phys_addr_t end, int nid,
13880ac398b1SYunfeng Ye 					bool exact_nid)
138995f72d1eSYinghai Lu {
139092d12f95SMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
13916ed311b2SBenjamin Herrenschmidt 	phys_addr_t found;
139295f72d1eSYinghai Lu 
139392d12f95SMike Rapoport 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
139492d12f95SMike Rapoport 		nid = NUMA_NO_NODE;
139592d12f95SMike Rapoport 
13962f770806SMike Rapoport 	if (!align) {
13972f770806SMike Rapoport 		/* Can't use WARNs this early in boot on powerpc */
13982f770806SMike Rapoport 		dump_stack();
13992f770806SMike Rapoport 		align = SMP_CACHE_BYTES;
14002f770806SMike Rapoport 	}
14012f770806SMike Rapoport 
140292d12f95SMike Rapoport again:
1403fc6daaf9STony Luck 	found = memblock_find_in_range_node(size, align, start, end, nid,
1404fc6daaf9STony Luck 					    flags);
140592d12f95SMike Rapoport 	if (found && !memblock_reserve(found, size))
140692d12f95SMike Rapoport 		goto done;
140792d12f95SMike Rapoport 
14080ac398b1SYunfeng Ye 	if (nid != NUMA_NO_NODE && !exact_nid) {
140992d12f95SMike Rapoport 		found = memblock_find_in_range_node(size, align, start,
141092d12f95SMike Rapoport 						    end, NUMA_NO_NODE,
141192d12f95SMike Rapoport 						    flags);
141292d12f95SMike Rapoport 		if (found && !memblock_reserve(found, size))
141392d12f95SMike Rapoport 			goto done;
141492d12f95SMike Rapoport 	}
141592d12f95SMike Rapoport 
141692d12f95SMike Rapoport 	if (flags & MEMBLOCK_MIRROR) {
141792d12f95SMike Rapoport 		flags &= ~MEMBLOCK_MIRROR;
141814d9a675SMa Wupeng 		pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n",
141992d12f95SMike Rapoport 			&size);
142092d12f95SMike Rapoport 		goto again;
142192d12f95SMike Rapoport 	}
142292d12f95SMike Rapoport 
142392d12f95SMike Rapoport 	return 0;
142492d12f95SMike Rapoport 
142592d12f95SMike Rapoport done:
1426c6975d7cSQian Cai 	/*
1427c6975d7cSQian Cai 	 * Skip kmemleak for those places like kasan_init() and
1428c6975d7cSQian Cai 	 * early_pgtable_alloc() due to high volume.
1429c6975d7cSQian Cai 	 */
1430c6975d7cSQian Cai 	if (end != MEMBLOCK_ALLOC_NOLEAKTRACE)
1431aedf95eaSCatalin Marinas 		/*
1432c200d900SPatrick Wang 		 * Memblock allocated blocks are never reported as
1433c200d900SPatrick Wang 		 * leaks. This is because many of these blocks are
1434c200d900SPatrick Wang 		 * only referred via the physical address which is
1435c200d900SPatrick Wang 		 * not looked up by kmemleak.
1436aedf95eaSCatalin Marinas 		 */
1437c200d900SPatrick Wang 		kmemleak_alloc_phys(found, size, 0);
143892d12f95SMike Rapoport 
14396ed311b2SBenjamin Herrenschmidt 	return found;
1440aedf95eaSCatalin Marinas }
144195f72d1eSYinghai Lu 
1442a2974133SMike Rapoport /**
1443a2974133SMike Rapoport  * memblock_phys_alloc_range - allocate a memory block inside specified range
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  * @start: the lower bound of the memory region to allocate (physical address)
1447a2974133SMike Rapoport  * @end: the upper bound of the memory region to allocate (physical address)
1448a2974133SMike Rapoport  *
1449a2974133SMike Rapoport  * Allocate @size bytes in the between @start and @end.
1450a2974133SMike Rapoport  *
1451a2974133SMike Rapoport  * Return: physical address of the allocated memory block on success,
1452a2974133SMike Rapoport  * %0 on failure.
1453a2974133SMike Rapoport  */
14548a770c2aSMike Rapoport phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
14558a770c2aSMike Rapoport 					     phys_addr_t align,
14568a770c2aSMike Rapoport 					     phys_addr_t start,
14578a770c2aSMike Rapoport 					     phys_addr_t end)
14582bfc2862SAkinobu Mita {
1459b5cf2d6cSFaiyaz Mohammed 	memblock_dbg("%s: %llu bytes align=0x%llx from=%pa max_addr=%pa %pS\n",
1460b5cf2d6cSFaiyaz Mohammed 		     __func__, (u64)size, (u64)align, &start, &end,
1461b5cf2d6cSFaiyaz Mohammed 		     (void *)_RET_IP_);
14620ac398b1SYunfeng Ye 	return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
14630ac398b1SYunfeng Ye 					false);
14647bd0b0f0STejun Heo }
14657bd0b0f0STejun Heo 
1466a2974133SMike Rapoport /**
146717cbe038SLevi Yun  * memblock_phys_alloc_try_nid - allocate a memory block from specified NUMA node
1468a2974133SMike Rapoport  * @size: size of memory block to be allocated in bytes
1469a2974133SMike Rapoport  * @align: alignment of the region and block's size
1470a2974133SMike Rapoport  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1471a2974133SMike Rapoport  *
1472a2974133SMike Rapoport  * Allocates memory block from the specified NUMA node. If the node
1473a2974133SMike Rapoport  * has no available memory, attempts to allocated from any node in the
1474a2974133SMike Rapoport  * system.
1475a2974133SMike Rapoport  *
1476a2974133SMike Rapoport  * Return: physical address of the allocated memory block on success,
1477a2974133SMike Rapoport  * %0 on failure.
1478a2974133SMike Rapoport  */
14799a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
14809d1e2492SBenjamin Herrenschmidt {
148133755574SMike Rapoport 	return memblock_alloc_range_nid(size, align, 0,
14820ac398b1SYunfeng Ye 					MEMBLOCK_ALLOC_ACCESSIBLE, nid, false);
148395f72d1eSYinghai Lu }
148495f72d1eSYinghai Lu 
148526f09e9bSSantosh Shilimkar /**
1486eb31d559SMike Rapoport  * memblock_alloc_internal - allocate boot memory block
148726f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
148826f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
148926f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region to allocate (phys address)
149026f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region to allocate (phys address)
149126f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
14920ac398b1SYunfeng Ye  * @exact_nid: control the allocation fall back to other nodes
149326f09e9bSSantosh Shilimkar  *
149492d12f95SMike Rapoport  * Allocates memory block using memblock_alloc_range_nid() and
149592d12f95SMike Rapoport  * converts the returned physical address to virtual.
149692d12f95SMike Rapoport  *
149726f09e9bSSantosh Shilimkar  * The @min_addr limit is dropped if it can not be satisfied and the allocation
149892d12f95SMike Rapoport  * will fall back to memory below @min_addr. Other constraints, such
149992d12f95SMike Rapoport  * as node and mirrored memory will be handled again in
150092d12f95SMike Rapoport  * memblock_alloc_range_nid().
150126f09e9bSSantosh Shilimkar  *
150247cec443SMike Rapoport  * Return:
150326f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
150426f09e9bSSantosh Shilimkar  */
1505eb31d559SMike Rapoport static void * __init memblock_alloc_internal(
150626f09e9bSSantosh Shilimkar 				phys_addr_t size, phys_addr_t align,
150726f09e9bSSantosh Shilimkar 				phys_addr_t min_addr, phys_addr_t max_addr,
15080ac398b1SYunfeng Ye 				int nid, bool exact_nid)
150926f09e9bSSantosh Shilimkar {
151026f09e9bSSantosh Shilimkar 	phys_addr_t alloc;
151126f09e9bSSantosh Shilimkar 
151226f09e9bSSantosh Shilimkar 	/*
151326f09e9bSSantosh Shilimkar 	 * Detect any accidental use of these APIs after slab is ready, as at
151426f09e9bSSantosh Shilimkar 	 * this moment memblock may be deinitialized already and its
1515c6ffc5caSMike Rapoport 	 * internal data may be destroyed (after execution of memblock_free_all)
151626f09e9bSSantosh Shilimkar 	 */
151726f09e9bSSantosh Shilimkar 	if (WARN_ON_ONCE(slab_is_available()))
151826f09e9bSSantosh Shilimkar 		return kzalloc_node(size, GFP_NOWAIT, nid);
151926f09e9bSSantosh Shilimkar 
1520f3057ad7SMike Rapoport 	if (max_addr > memblock.current_limit)
1521f3057ad7SMike Rapoport 		max_addr = memblock.current_limit;
1522f3057ad7SMike Rapoport 
15230ac398b1SYunfeng Ye 	alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid,
15240ac398b1SYunfeng Ye 					exact_nid);
15252f770806SMike Rapoport 
152692d12f95SMike Rapoport 	/* retry allocation without lower limit */
152792d12f95SMike Rapoport 	if (!alloc && min_addr)
15280ac398b1SYunfeng Ye 		alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid,
15290ac398b1SYunfeng Ye 						exact_nid);
153026f09e9bSSantosh Shilimkar 
153192d12f95SMike Rapoport 	if (!alloc)
1532a3f5bafcSTony Luck 		return NULL;
153326f09e9bSSantosh Shilimkar 
153492d12f95SMike Rapoport 	return phys_to_virt(alloc);
153526f09e9bSSantosh Shilimkar }
153626f09e9bSSantosh Shilimkar 
153726f09e9bSSantosh Shilimkar /**
15380ac398b1SYunfeng Ye  * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node
15390ac398b1SYunfeng Ye  * without zeroing memory
15400ac398b1SYunfeng Ye  * @size: size of memory block to be allocated in bytes
15410ac398b1SYunfeng Ye  * @align: alignment of the region and block's size
15420ac398b1SYunfeng Ye  * @min_addr: the lower bound of the memory region from where the allocation
15430ac398b1SYunfeng Ye  *	  is preferred (phys address)
15440ac398b1SYunfeng Ye  * @max_addr: the upper bound of the memory region from where the allocation
15450ac398b1SYunfeng Ye  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
15460ac398b1SYunfeng Ye  *	      allocate only from memory limited by memblock.current_limit value
15470ac398b1SYunfeng Ye  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
15480ac398b1SYunfeng Ye  *
15490ac398b1SYunfeng Ye  * Public function, provides additional debug information (including caller
15500ac398b1SYunfeng Ye  * info), if enabled. Does not zero allocated memory.
15510ac398b1SYunfeng Ye  *
15520ac398b1SYunfeng Ye  * Return:
15530ac398b1SYunfeng Ye  * Virtual address of allocated memory block on success, NULL on failure.
15540ac398b1SYunfeng Ye  */
15550ac398b1SYunfeng Ye void * __init memblock_alloc_exact_nid_raw(
15560ac398b1SYunfeng Ye 			phys_addr_t size, phys_addr_t align,
15570ac398b1SYunfeng Ye 			phys_addr_t min_addr, phys_addr_t max_addr,
15580ac398b1SYunfeng Ye 			int nid)
15590ac398b1SYunfeng Ye {
15600ac398b1SYunfeng Ye 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
15610ac398b1SYunfeng Ye 		     __func__, (u64)size, (u64)align, nid, &min_addr,
15620ac398b1SYunfeng Ye 		     &max_addr, (void *)_RET_IP_);
15630ac398b1SYunfeng Ye 
156408678804SMike Rapoport 	return memblock_alloc_internal(size, align, min_addr, max_addr, nid,
156508678804SMike Rapoport 				       true);
15660ac398b1SYunfeng Ye }
15670ac398b1SYunfeng Ye 
15680ac398b1SYunfeng Ye /**
1569eb31d559SMike Rapoport  * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
1570ea1f5f37SPavel Tatashin  * memory and without panicking
1571ea1f5f37SPavel Tatashin  * @size: size of memory block to be allocated in bytes
1572ea1f5f37SPavel Tatashin  * @align: alignment of the region and block's size
1573ea1f5f37SPavel Tatashin  * @min_addr: the lower bound of the memory region from where the allocation
1574ea1f5f37SPavel Tatashin  *	  is preferred (phys address)
1575ea1f5f37SPavel Tatashin  * @max_addr: the upper bound of the memory region from where the allocation
157697ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1577ea1f5f37SPavel Tatashin  *	      allocate only from memory limited by memblock.current_limit value
1578ea1f5f37SPavel Tatashin  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1579ea1f5f37SPavel Tatashin  *
1580ea1f5f37SPavel Tatashin  * Public function, provides additional debug information (including caller
1581ea1f5f37SPavel Tatashin  * info), if enabled. Does not zero allocated memory, does not panic if request
1582ea1f5f37SPavel Tatashin  * cannot be satisfied.
1583ea1f5f37SPavel Tatashin  *
158447cec443SMike Rapoport  * Return:
1585ea1f5f37SPavel Tatashin  * Virtual address of allocated memory block on success, NULL on failure.
1586ea1f5f37SPavel Tatashin  */
1587eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_raw(
1588ea1f5f37SPavel Tatashin 			phys_addr_t size, phys_addr_t align,
1589ea1f5f37SPavel Tatashin 			phys_addr_t min_addr, phys_addr_t max_addr,
1590ea1f5f37SPavel Tatashin 			int nid)
1591ea1f5f37SPavel Tatashin {
1592d75f773cSSakari Ailus 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1593a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1594a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1595ea1f5f37SPavel Tatashin 
159608678804SMike Rapoport 	return memblock_alloc_internal(size, align, min_addr, max_addr, nid,
159708678804SMike Rapoport 				       false);
1598ea1f5f37SPavel Tatashin }
1599ea1f5f37SPavel Tatashin 
1600ea1f5f37SPavel Tatashin /**
1601c0dbe825SMike Rapoport  * memblock_alloc_try_nid - allocate boot memory block
160226f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
160326f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
160426f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region from where the allocation
160526f09e9bSSantosh Shilimkar  *	  is preferred (phys address)
160626f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region from where the allocation
160797ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
160826f09e9bSSantosh Shilimkar  *	      allocate only from memory limited by memblock.current_limit value
160926f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
161026f09e9bSSantosh Shilimkar  *
1611c0dbe825SMike Rapoport  * Public function, provides additional debug information (including caller
1612c0dbe825SMike Rapoport  * info), if enabled. This function zeroes the allocated memory.
161326f09e9bSSantosh Shilimkar  *
161447cec443SMike Rapoport  * Return:
161526f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
161626f09e9bSSantosh Shilimkar  */
1617eb31d559SMike Rapoport void * __init memblock_alloc_try_nid(
161826f09e9bSSantosh Shilimkar 			phys_addr_t size, phys_addr_t align,
161926f09e9bSSantosh Shilimkar 			phys_addr_t min_addr, phys_addr_t max_addr,
162026f09e9bSSantosh Shilimkar 			int nid)
162126f09e9bSSantosh Shilimkar {
162226f09e9bSSantosh Shilimkar 	void *ptr;
162326f09e9bSSantosh Shilimkar 
1624d75f773cSSakari Ailus 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1625a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1626a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1627eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
16280ac398b1SYunfeng Ye 					   min_addr, max_addr, nid, false);
1629c0dbe825SMike Rapoport 	if (ptr)
1630ea1f5f37SPavel Tatashin 		memset(ptr, 0, size);
163126f09e9bSSantosh Shilimkar 
1632c0dbe825SMike Rapoport 	return ptr;
163326f09e9bSSantosh Shilimkar }
163426f09e9bSSantosh Shilimkar 
163526f09e9bSSantosh Shilimkar /**
1636621d9739SMike Rapoport  * memblock_free_late - free pages directly to buddy allocator
163748a833ccSMike Rapoport  * @base: phys starting address of the  boot memory block
163826f09e9bSSantosh Shilimkar  * @size: size of the boot memory block in bytes
163926f09e9bSSantosh Shilimkar  *
1640a2974133SMike Rapoport  * This is only useful when the memblock allocator has already been torn
164126f09e9bSSantosh Shilimkar  * down, but we are still initializing the system.  Pages are released directly
1642a2974133SMike Rapoport  * to the buddy allocator.
164326f09e9bSSantosh Shilimkar  */
1644621d9739SMike Rapoport void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
164526f09e9bSSantosh Shilimkar {
1646a36aab89SMike Rapoport 	phys_addr_t cursor, end;
164726f09e9bSSantosh Shilimkar 
1648a36aab89SMike Rapoport 	end = base + size - 1;
1649d75f773cSSakari Ailus 	memblock_dbg("%s: [%pa-%pa] %pS\n",
1650a36aab89SMike Rapoport 		     __func__, &base, &end, (void *)_RET_IP_);
16519099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
165226f09e9bSSantosh Shilimkar 	cursor = PFN_UP(base);
165326f09e9bSSantosh Shilimkar 	end = PFN_DOWN(base + size);
165426f09e9bSSantosh Shilimkar 
165526f09e9bSSantosh Shilimkar 	for (; cursor < end; cursor++) {
1656647037adSAaron Thompson 		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
1657ca79b0c2SArun KS 		totalram_pages_inc();
165826f09e9bSSantosh Shilimkar 	}
165926f09e9bSSantosh Shilimkar }
16609d1e2492SBenjamin Herrenschmidt 
16619d1e2492SBenjamin Herrenschmidt /*
16629d1e2492SBenjamin Herrenschmidt  * Remaining API functions
16639d1e2492SBenjamin Herrenschmidt  */
16649d1e2492SBenjamin Herrenschmidt 
16651f1ffb8aSDavid Gibson phys_addr_t __init_memblock memblock_phys_mem_size(void)
166695f72d1eSYinghai Lu {
16671440c4e2STejun Heo 	return memblock.memory.total_size;
166895f72d1eSYinghai Lu }
166995f72d1eSYinghai Lu 
16708907de5dSSrikar Dronamraju phys_addr_t __init_memblock memblock_reserved_size(void)
16718907de5dSSrikar Dronamraju {
16728907de5dSSrikar Dronamraju 	return memblock.reserved.total_size;
16738907de5dSSrikar Dronamraju }
16748907de5dSSrikar Dronamraju 
16750a93ebefSSam Ravnborg /* lowest address */
16760a93ebefSSam Ravnborg phys_addr_t __init_memblock memblock_start_of_DRAM(void)
16770a93ebefSSam Ravnborg {
16780a93ebefSSam Ravnborg 	return memblock.memory.regions[0].base;
16790a93ebefSSam Ravnborg }
16800a93ebefSSam Ravnborg 
168110d06439SYinghai Lu phys_addr_t __init_memblock memblock_end_of_DRAM(void)
168295f72d1eSYinghai Lu {
168395f72d1eSYinghai Lu 	int idx = memblock.memory.cnt - 1;
168495f72d1eSYinghai Lu 
1685e3239ff9SBenjamin Herrenschmidt 	return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
168695f72d1eSYinghai Lu }
168795f72d1eSYinghai Lu 
1688a571d4ebSDennis Chen static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
168995f72d1eSYinghai Lu {
16901c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1691136199f0SEmil Medve 	struct memblock_region *r;
169295f72d1eSYinghai Lu 
1693a571d4ebSDennis Chen 	/*
1694a571d4ebSDennis Chen 	 * translate the memory @limit size into the max address within one of
1695a571d4ebSDennis Chen 	 * the memory memblock regions, if the @limit exceeds the total size
16961c4bc43dSStefan Agner 	 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
1697a571d4ebSDennis Chen 	 */
1698cc6de168SMike Rapoport 	for_each_mem_region(r) {
1699c0ce8fefSTejun Heo 		if (limit <= r->size) {
1700c0ce8fefSTejun Heo 			max_addr = r->base + limit;
170195f72d1eSYinghai Lu 			break;
170295f72d1eSYinghai Lu 		}
1703c0ce8fefSTejun Heo 		limit -= r->size;
170495f72d1eSYinghai Lu 	}
1705c0ce8fefSTejun Heo 
1706a571d4ebSDennis Chen 	return max_addr;
1707a571d4ebSDennis Chen }
1708a571d4ebSDennis Chen 
1709a571d4ebSDennis Chen void __init memblock_enforce_memory_limit(phys_addr_t limit)
1710a571d4ebSDennis Chen {
171149aef717SColin Ian King 	phys_addr_t max_addr;
1712a571d4ebSDennis Chen 
1713a571d4ebSDennis Chen 	if (!limit)
1714a571d4ebSDennis Chen 		return;
1715a571d4ebSDennis Chen 
1716a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1717a571d4ebSDennis Chen 
1718a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
17191c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1720a571d4ebSDennis Chen 		return;
1721a571d4ebSDennis Chen 
1722c0ce8fefSTejun Heo 	/* truncate both memory and reserved regions */
1723f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.memory, max_addr,
17241c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
1725f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.reserved, max_addr,
17261c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
172795f72d1eSYinghai Lu }
172895f72d1eSYinghai Lu 
1729c9ca9b4eSAKASHI Takahiro void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1730c9ca9b4eSAKASHI Takahiro {
1731c9ca9b4eSAKASHI Takahiro 	int start_rgn, end_rgn;
1732c9ca9b4eSAKASHI Takahiro 	int i, ret;
1733c9ca9b4eSAKASHI Takahiro 
1734c9ca9b4eSAKASHI Takahiro 	if (!size)
1735c9ca9b4eSAKASHI Takahiro 		return;
1736c9ca9b4eSAKASHI Takahiro 
17375173ed72SPeng Fan 	if (!memblock_memory->total_size) {
1738e888fa7bSGeert Uytterhoeven 		pr_warn("%s: No memory registered yet\n", __func__);
1739e888fa7bSGeert Uytterhoeven 		return;
1740e888fa7bSGeert Uytterhoeven 	}
1741e888fa7bSGeert Uytterhoeven 
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 {
1864c5c5c9d1STang Chen 	return memblock_overlaps_region(&memblock.reserved, base, size);
186595f72d1eSYinghai Lu }
186695f72d1eSYinghai Lu 
18676ede1fd3SYinghai Lu void __init_memblock memblock_trim_memory(phys_addr_t align)
18686ede1fd3SYinghai Lu {
18696ede1fd3SYinghai Lu 	phys_addr_t start, end, orig_start, orig_end;
1870136199f0SEmil Medve 	struct memblock_region *r;
18716ede1fd3SYinghai Lu 
1872cc6de168SMike Rapoport 	for_each_mem_region(r) {
1873136199f0SEmil Medve 		orig_start = r->base;
1874136199f0SEmil Medve 		orig_end = r->base + r->size;
18756ede1fd3SYinghai Lu 		start = round_up(orig_start, align);
18766ede1fd3SYinghai Lu 		end = round_down(orig_end, align);
18776ede1fd3SYinghai Lu 
18786ede1fd3SYinghai Lu 		if (start == orig_start && end == orig_end)
18796ede1fd3SYinghai Lu 			continue;
18806ede1fd3SYinghai Lu 
18816ede1fd3SYinghai Lu 		if (start < end) {
1882136199f0SEmil Medve 			r->base = start;
1883136199f0SEmil Medve 			r->size = end - start;
18846ede1fd3SYinghai Lu 		} else {
1885136199f0SEmil Medve 			memblock_remove_region(&memblock.memory,
1886136199f0SEmil Medve 					       r - memblock.memory.regions);
1887136199f0SEmil Medve 			r--;
18886ede1fd3SYinghai Lu 		}
18896ede1fd3SYinghai Lu 	}
18906ede1fd3SYinghai Lu }
1891e63075a3SBenjamin Herrenschmidt 
18923661ca66SYinghai Lu void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1893e63075a3SBenjamin Herrenschmidt {
1894e63075a3SBenjamin Herrenschmidt 	memblock.current_limit = limit;
1895e63075a3SBenjamin Herrenschmidt }
1896e63075a3SBenjamin Herrenschmidt 
1897fec51014SLaura Abbott phys_addr_t __init_memblock memblock_get_current_limit(void)
1898fec51014SLaura Abbott {
1899fec51014SLaura Abbott 	return memblock.current_limit;
1900fec51014SLaura Abbott }
1901fec51014SLaura Abbott 
19020262d9c8SHeiko Carstens static void __init_memblock memblock_dump(struct memblock_type *type)
19036ed311b2SBenjamin Herrenschmidt {
19045d63f81cSMiles Chen 	phys_addr_t base, end, size;
1905e1720feeSMike Rapoport 	enum memblock_flags flags;
19068c9c1701SAlexander Kuleshov 	int idx;
19078c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
19086ed311b2SBenjamin Herrenschmidt 
19090262d9c8SHeiko Carstens 	pr_info(" %s.cnt  = 0x%lx\n", type->name, type->cnt);
19106ed311b2SBenjamin Herrenschmidt 
191166e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
19127c0caeb8STejun Heo 		char nid_buf[32] = "";
19136ed311b2SBenjamin Herrenschmidt 
19147c0caeb8STejun Heo 		base = rgn->base;
19157c0caeb8STejun Heo 		size = rgn->size;
19165d63f81cSMiles Chen 		end = base + size - 1;
191766a20757STang Chen 		flags = rgn->flags;
1918a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA
19197c0caeb8STejun Heo 		if (memblock_get_region_node(rgn) != MAX_NUMNODES)
19207c0caeb8STejun Heo 			snprintf(nid_buf, sizeof(nid_buf), " on node %d",
19217c0caeb8STejun Heo 				 memblock_get_region_node(rgn));
19227c0caeb8STejun Heo #endif
1923e1720feeSMike Rapoport 		pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
19240262d9c8SHeiko Carstens 			type->name, idx, &base, &end, &size, nid_buf, flags);
19256ed311b2SBenjamin Herrenschmidt 	}
19266ed311b2SBenjamin Herrenschmidt }
19276ed311b2SBenjamin Herrenschmidt 
192887c55870SMike Rapoport static void __init_memblock __memblock_dump_all(void)
19296ed311b2SBenjamin Herrenschmidt {
19306ed311b2SBenjamin Herrenschmidt 	pr_info("MEMBLOCK configuration:\n");
19315d63f81cSMiles Chen 	pr_info(" memory size = %pa reserved size = %pa\n",
19325d63f81cSMiles Chen 		&memblock.memory.total_size,
19335d63f81cSMiles Chen 		&memblock.reserved.total_size);
19346ed311b2SBenjamin Herrenschmidt 
19350262d9c8SHeiko Carstens 	memblock_dump(&memblock.memory);
19360262d9c8SHeiko Carstens 	memblock_dump(&memblock.reserved);
1937409efd4cSHeiko Carstens #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
193877649905SDavid Hildenbrand 	memblock_dump(&physmem);
1939409efd4cSHeiko Carstens #endif
19406ed311b2SBenjamin Herrenschmidt }
19416ed311b2SBenjamin Herrenschmidt 
194287c55870SMike Rapoport void __init_memblock memblock_dump_all(void)
194387c55870SMike Rapoport {
194487c55870SMike Rapoport 	if (memblock_debug)
194587c55870SMike Rapoport 		__memblock_dump_all();
194687c55870SMike Rapoport }
194787c55870SMike Rapoport 
19481aadc056STejun Heo void __init memblock_allow_resize(void)
19496ed311b2SBenjamin Herrenschmidt {
1950142b45a7SBenjamin Herrenschmidt 	memblock_can_resize = 1;
19516ed311b2SBenjamin Herrenschmidt }
19526ed311b2SBenjamin Herrenschmidt 
19536ed311b2SBenjamin Herrenschmidt static int __init early_memblock(char *p)
19546ed311b2SBenjamin Herrenschmidt {
19556ed311b2SBenjamin Herrenschmidt 	if (p && strstr(p, "debug"))
19566ed311b2SBenjamin Herrenschmidt 		memblock_debug = 1;
19576ed311b2SBenjamin Herrenschmidt 	return 0;
19586ed311b2SBenjamin Herrenschmidt }
19596ed311b2SBenjamin Herrenschmidt early_param("memblock", early_memblock);
19606ed311b2SBenjamin Herrenschmidt 
19614f5b0c17SMike Rapoport static void __init free_memmap(unsigned long start_pfn, unsigned long end_pfn)
19624f5b0c17SMike Rapoport {
19634f5b0c17SMike Rapoport 	struct page *start_pg, *end_pg;
19644f5b0c17SMike Rapoport 	phys_addr_t pg, pgend;
19654f5b0c17SMike Rapoport 
19664f5b0c17SMike Rapoport 	/*
19674f5b0c17SMike Rapoport 	 * Convert start_pfn/end_pfn to a struct page pointer.
19684f5b0c17SMike Rapoport 	 */
19694f5b0c17SMike Rapoport 	start_pg = pfn_to_page(start_pfn - 1) + 1;
19704f5b0c17SMike Rapoport 	end_pg = pfn_to_page(end_pfn - 1) + 1;
19714f5b0c17SMike Rapoport 
19724f5b0c17SMike Rapoport 	/*
19734f5b0c17SMike Rapoport 	 * Convert to physical addresses, and round start upwards and end
19744f5b0c17SMike Rapoport 	 * downwards.
19754f5b0c17SMike Rapoport 	 */
19764f5b0c17SMike Rapoport 	pg = PAGE_ALIGN(__pa(start_pg));
19774f5b0c17SMike Rapoport 	pgend = __pa(end_pg) & PAGE_MASK;
19784f5b0c17SMike Rapoport 
19794f5b0c17SMike Rapoport 	/*
19804f5b0c17SMike Rapoport 	 * If there are free pages between these, free the section of the
19814f5b0c17SMike Rapoport 	 * memmap array.
19824f5b0c17SMike Rapoport 	 */
19834f5b0c17SMike Rapoport 	if (pg < pgend)
19843ecc6834SMike Rapoport 		memblock_phys_free(pg, pgend - pg);
19854f5b0c17SMike Rapoport }
19864f5b0c17SMike Rapoport 
19874f5b0c17SMike Rapoport /*
19884f5b0c17SMike Rapoport  * The mem_map array can get very big.  Free the unused area of the memory map.
19894f5b0c17SMike Rapoport  */
19904f5b0c17SMike Rapoport static void __init free_unused_memmap(void)
19914f5b0c17SMike Rapoport {
19924f5b0c17SMike Rapoport 	unsigned long start, end, prev_end = 0;
19934f5b0c17SMike Rapoport 	int i;
19944f5b0c17SMike Rapoport 
19954f5b0c17SMike Rapoport 	if (!IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) ||
19964f5b0c17SMike Rapoport 	    IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
19974f5b0c17SMike Rapoport 		return;
19984f5b0c17SMike Rapoport 
19994f5b0c17SMike Rapoport 	/*
20004f5b0c17SMike Rapoport 	 * This relies on each bank being in address order.
20014f5b0c17SMike Rapoport 	 * The banks are sorted previously in bootmem_init().
20024f5b0c17SMike Rapoport 	 */
20034f5b0c17SMike Rapoport 	for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
20044f5b0c17SMike Rapoport #ifdef CONFIG_SPARSEMEM
20054f5b0c17SMike Rapoport 		/*
20064f5b0c17SMike Rapoport 		 * Take care not to free memmap entries that don't exist
20074f5b0c17SMike Rapoport 		 * due to SPARSEMEM sections which aren't present.
20084f5b0c17SMike Rapoport 		 */
20094f5b0c17SMike Rapoport 		start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
20104f5b0c17SMike Rapoport #endif
20114f5b0c17SMike Rapoport 		/*
2012e2a86800SMike Rapoport 		 * Align down here since many operations in VM subsystem
2013e2a86800SMike Rapoport 		 * presume that there are no holes in the memory map inside
2014e2a86800SMike Rapoport 		 * a pageblock
20154f5b0c17SMike Rapoport 		 */
20164f9bc69aSKefeng Wang 		start = pageblock_start_pfn(start);
20174f5b0c17SMike Rapoport 
20184f5b0c17SMike Rapoport 		/*
20194f5b0c17SMike Rapoport 		 * If we had a previous bank, and there is a space
20204f5b0c17SMike Rapoport 		 * between the current bank and the previous, free it.
20214f5b0c17SMike Rapoport 		 */
20224f5b0c17SMike Rapoport 		if (prev_end && prev_end < start)
20234f5b0c17SMike Rapoport 			free_memmap(prev_end, start);
20244f5b0c17SMike Rapoport 
20254f5b0c17SMike Rapoport 		/*
2026e2a86800SMike Rapoport 		 * Align up here since many operations in VM subsystem
2027e2a86800SMike Rapoport 		 * presume that there are no holes in the memory map inside
2028e2a86800SMike Rapoport 		 * a pageblock
20294f5b0c17SMike Rapoport 		 */
20305f7fa13fSKefeng Wang 		prev_end = pageblock_align(end);
20314f5b0c17SMike Rapoport 	}
20324f5b0c17SMike Rapoport 
20334f5b0c17SMike Rapoport #ifdef CONFIG_SPARSEMEM
2034f921f53eSMike Rapoport 	if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) {
20355f7fa13fSKefeng Wang 		prev_end = pageblock_align(end);
20364f5b0c17SMike Rapoport 		free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
2037f921f53eSMike Rapoport 	}
20384f5b0c17SMike Rapoport #endif
20394f5b0c17SMike Rapoport }
20404f5b0c17SMike Rapoport 
2041bda49a81SMike Rapoport static void __init __free_pages_memory(unsigned long start, unsigned long end)
2042bda49a81SMike Rapoport {
2043bda49a81SMike Rapoport 	int order;
2044bda49a81SMike Rapoport 
2045bda49a81SMike Rapoport 	while (start < end) {
204659f876fbSKirill A. Shutemov 		/*
204759f876fbSKirill A. Shutemov 		 * Free the pages in the largest chunks alignment allows.
204859f876fbSKirill A. Shutemov 		 *
204959f876fbSKirill A. Shutemov 		 * __ffs() behaviour is undefined for 0. start == 0 is
205059f876fbSKirill A. Shutemov 		 * MAX_ORDER-aligned, set order to MAX_ORDER for the case.
205159f876fbSKirill A. Shutemov 		 */
205259f876fbSKirill A. Shutemov 		if (start)
205323baf831SKirill A. Shutemov 			order = min_t(int, MAX_ORDER, __ffs(start));
205459f876fbSKirill A. Shutemov 		else
205559f876fbSKirill A. Shutemov 			order = MAX_ORDER;
2056bda49a81SMike Rapoport 
2057bda49a81SMike Rapoport 		while (start + (1UL << order) > end)
2058bda49a81SMike Rapoport 			order--;
2059bda49a81SMike Rapoport 
2060bda49a81SMike Rapoport 		memblock_free_pages(pfn_to_page(start), start, order);
2061bda49a81SMike Rapoport 
2062bda49a81SMike Rapoport 		start += (1UL << order);
2063bda49a81SMike Rapoport 	}
2064bda49a81SMike Rapoport }
2065bda49a81SMike Rapoport 
2066bda49a81SMike Rapoport static unsigned long __init __free_memory_core(phys_addr_t start,
2067bda49a81SMike Rapoport 				 phys_addr_t end)
2068bda49a81SMike Rapoport {
2069bda49a81SMike Rapoport 	unsigned long start_pfn = PFN_UP(start);
2070bda49a81SMike Rapoport 	unsigned long end_pfn = min_t(unsigned long,
2071bda49a81SMike Rapoport 				      PFN_DOWN(end), max_low_pfn);
2072bda49a81SMike Rapoport 
2073bda49a81SMike Rapoport 	if (start_pfn >= end_pfn)
2074bda49a81SMike Rapoport 		return 0;
2075bda49a81SMike Rapoport 
2076bda49a81SMike Rapoport 	__free_pages_memory(start_pfn, end_pfn);
2077bda49a81SMike Rapoport 
2078bda49a81SMike Rapoport 	return end_pfn - start_pfn;
2079bda49a81SMike Rapoport }
2080bda49a81SMike Rapoport 
20819092d4f7SMike Rapoport static void __init memmap_init_reserved_pages(void)
20829092d4f7SMike Rapoport {
20839092d4f7SMike Rapoport 	struct memblock_region *region;
20849092d4f7SMike Rapoport 	phys_addr_t start, end;
2085*61167ad5SYajun Deng 	int nid;
20869092d4f7SMike Rapoport 
2087*61167ad5SYajun Deng 	/*
2088*61167ad5SYajun Deng 	 * set nid on all reserved pages and also treat struct
2089*61167ad5SYajun Deng 	 * pages for the NOMAP regions as PageReserved
2090*61167ad5SYajun Deng 	 */
20919092d4f7SMike Rapoport 	for_each_mem_region(region) {
2092*61167ad5SYajun Deng 		nid = memblock_get_region_node(region);
20939092d4f7SMike Rapoport 		start = region->base;
20949092d4f7SMike Rapoport 		end = start + region->size;
2095*61167ad5SYajun Deng 
2096*61167ad5SYajun Deng 		if (memblock_is_nomap(region))
2097*61167ad5SYajun Deng 			reserve_bootmem_region(start, end, nid);
2098*61167ad5SYajun Deng 
2099*61167ad5SYajun Deng 		memblock_set_node(start, end, &memblock.reserved, nid);
21009092d4f7SMike Rapoport 	}
2101*61167ad5SYajun Deng 
2102*61167ad5SYajun Deng 	/* initialize struct pages for the reserved regions */
2103*61167ad5SYajun Deng 	for_each_reserved_mem_region(region) {
2104*61167ad5SYajun Deng 		nid = memblock_get_region_node(region);
2105*61167ad5SYajun Deng 		start = region->base;
2106*61167ad5SYajun Deng 		end = start + region->size;
2107*61167ad5SYajun Deng 
2108*61167ad5SYajun Deng 		reserve_bootmem_region(start, end, nid);
21099092d4f7SMike Rapoport 	}
21109092d4f7SMike Rapoport }
21119092d4f7SMike Rapoport 
2112bda49a81SMike Rapoport static unsigned long __init free_low_memory_core_early(void)
2113bda49a81SMike Rapoport {
2114bda49a81SMike Rapoport 	unsigned long count = 0;
2115bda49a81SMike Rapoport 	phys_addr_t start, end;
2116bda49a81SMike Rapoport 	u64 i;
2117bda49a81SMike Rapoport 
2118bda49a81SMike Rapoport 	memblock_clear_hotplug(0, -1);
2119bda49a81SMike Rapoport 
21209092d4f7SMike Rapoport 	memmap_init_reserved_pages();
2121bda49a81SMike Rapoport 
2122bda49a81SMike Rapoport 	/*
2123bda49a81SMike Rapoport 	 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
2124bda49a81SMike Rapoport 	 *  because in some case like Node0 doesn't have RAM installed
2125bda49a81SMike Rapoport 	 *  low ram will be on Node1
2126bda49a81SMike Rapoport 	 */
2127bda49a81SMike Rapoport 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
2128bda49a81SMike Rapoport 				NULL)
2129bda49a81SMike Rapoport 		count += __free_memory_core(start, end);
2130bda49a81SMike Rapoport 
2131bda49a81SMike Rapoport 	return count;
2132bda49a81SMike Rapoport }
2133bda49a81SMike Rapoport 
2134bda49a81SMike Rapoport static int reset_managed_pages_done __initdata;
2135bda49a81SMike Rapoport 
2136a668968fSHaifeng Xu static void __init reset_node_managed_pages(pg_data_t *pgdat)
2137bda49a81SMike Rapoport {
2138bda49a81SMike Rapoport 	struct zone *z;
2139bda49a81SMike Rapoport 
2140bda49a81SMike Rapoport 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
21419705bea5SArun KS 		atomic_long_set(&z->managed_pages, 0);
2142bda49a81SMike Rapoport }
2143bda49a81SMike Rapoport 
2144bda49a81SMike Rapoport void __init reset_all_zones_managed_pages(void)
2145bda49a81SMike Rapoport {
2146bda49a81SMike Rapoport 	struct pglist_data *pgdat;
2147bda49a81SMike Rapoport 
2148bda49a81SMike Rapoport 	if (reset_managed_pages_done)
2149bda49a81SMike Rapoport 		return;
2150bda49a81SMike Rapoport 
2151bda49a81SMike Rapoport 	for_each_online_pgdat(pgdat)
2152bda49a81SMike Rapoport 		reset_node_managed_pages(pgdat);
2153bda49a81SMike Rapoport 
2154bda49a81SMike Rapoport 	reset_managed_pages_done = 1;
2155bda49a81SMike Rapoport }
2156bda49a81SMike Rapoport 
2157bda49a81SMike Rapoport /**
2158bda49a81SMike Rapoport  * memblock_free_all - release free pages to the buddy allocator
2159bda49a81SMike Rapoport  */
2160097d43d8SDaeseok Youn void __init memblock_free_all(void)
2161bda49a81SMike Rapoport {
2162bda49a81SMike Rapoport 	unsigned long pages;
2163bda49a81SMike Rapoport 
21644f5b0c17SMike Rapoport 	free_unused_memmap();
2165bda49a81SMike Rapoport 	reset_all_zones_managed_pages();
2166bda49a81SMike Rapoport 
2167bda49a81SMike Rapoport 	pages = free_low_memory_core_early();
2168ca79b0c2SArun KS 	totalram_pages_add(pages);
2169bda49a81SMike Rapoport }
2170bda49a81SMike Rapoport 
2171350e88baSMike Rapoport #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
21726d03b885SBenjamin Herrenschmidt 
21736d03b885SBenjamin Herrenschmidt static int memblock_debug_show(struct seq_file *m, void *private)
21746d03b885SBenjamin Herrenschmidt {
21756d03b885SBenjamin Herrenschmidt 	struct memblock_type *type = m->private;
21766d03b885SBenjamin Herrenschmidt 	struct memblock_region *reg;
21776d03b885SBenjamin Herrenschmidt 	int i;
21785d63f81cSMiles Chen 	phys_addr_t end;
21796d03b885SBenjamin Herrenschmidt 
21806d03b885SBenjamin Herrenschmidt 	for (i = 0; i < type->cnt; i++) {
21816d03b885SBenjamin Herrenschmidt 		reg = &type->regions[i];
21825d63f81cSMiles Chen 		end = reg->base + reg->size - 1;
21836d03b885SBenjamin Herrenschmidt 
21845d63f81cSMiles Chen 		seq_printf(m, "%4d: ", i);
21855d63f81cSMiles Chen 		seq_printf(m, "%pa..%pa\n", &reg->base, &end);
21866d03b885SBenjamin Herrenschmidt 	}
21876d03b885SBenjamin Herrenschmidt 	return 0;
21886d03b885SBenjamin Herrenschmidt }
21895ad35093SAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(memblock_debug);
21906d03b885SBenjamin Herrenschmidt 
21916d03b885SBenjamin Herrenschmidt static int __init memblock_init_debugfs(void)
21926d03b885SBenjamin Herrenschmidt {
21936d03b885SBenjamin Herrenschmidt 	struct dentry *root = debugfs_create_dir("memblock", NULL);
2194d9f7979cSGreg Kroah-Hartman 
21950825a6f9SJoe Perches 	debugfs_create_file("memory", 0444, root,
21960825a6f9SJoe Perches 			    &memblock.memory, &memblock_debug_fops);
21970825a6f9SJoe Perches 	debugfs_create_file("reserved", 0444, root,
21980825a6f9SJoe Perches 			    &memblock.reserved, &memblock_debug_fops);
219970210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
220077649905SDavid Hildenbrand 	debugfs_create_file("physmem", 0444, root, &physmem,
220177649905SDavid Hildenbrand 			    &memblock_debug_fops);
220270210ed9SPhilipp Hachtmann #endif
22036d03b885SBenjamin Herrenschmidt 
22046d03b885SBenjamin Herrenschmidt 	return 0;
22056d03b885SBenjamin Herrenschmidt }
22066d03b885SBenjamin Herrenschmidt __initcall(memblock_init_debugfs);
22076d03b885SBenjamin Herrenschmidt 
22086d03b885SBenjamin Herrenschmidt #endif /* CONFIG_DEBUG_FS */
2209