xref: /linux/mm/memblock.c (revision a7259df7670240ee03b0cfce8a3e5d3773911e24)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
295f72d1eSYinghai Lu /*
395f72d1eSYinghai Lu  * Procedures for maintaining information about logical memory blocks.
495f72d1eSYinghai Lu  *
595f72d1eSYinghai Lu  * Peter Bergner, IBM Corp.	June 2001.
695f72d1eSYinghai Lu  * Copyright (C) 2001 Peter Bergner.
795f72d1eSYinghai Lu  */
895f72d1eSYinghai Lu 
995f72d1eSYinghai Lu #include <linux/kernel.h>
10142b45a7SBenjamin Herrenschmidt #include <linux/slab.h>
1195f72d1eSYinghai Lu #include <linux/init.h>
1295f72d1eSYinghai Lu #include <linux/bitops.h>
13449e8df3SBenjamin Herrenschmidt #include <linux/poison.h>
14c196f76fSBenjamin Herrenschmidt #include <linux/pfn.h>
156d03b885SBenjamin Herrenschmidt #include <linux/debugfs.h>
16514c6032SRandy Dunlap #include <linux/kmemleak.h>
176d03b885SBenjamin Herrenschmidt #include <linux/seq_file.h>
1895f72d1eSYinghai Lu #include <linux/memblock.h>
1995f72d1eSYinghai Lu 
20c4c5ad6bSChristoph Hellwig #include <asm/sections.h>
2126f09e9bSSantosh Shilimkar #include <linux/io.h>
2226f09e9bSSantosh Shilimkar 
2326f09e9bSSantosh Shilimkar #include "internal.h"
2479442ed1STang Chen 
258a5b403dSArd Biesheuvel #define INIT_MEMBLOCK_REGIONS			128
268a5b403dSArd Biesheuvel #define INIT_PHYSMEM_REGIONS			4
278a5b403dSArd Biesheuvel 
288a5b403dSArd Biesheuvel #ifndef INIT_MEMBLOCK_RESERVED_REGIONS
298a5b403dSArd Biesheuvel # define INIT_MEMBLOCK_RESERVED_REGIONS		INIT_MEMBLOCK_REGIONS
308a5b403dSArd Biesheuvel #endif
318a5b403dSArd Biesheuvel 
323e039c5cSMike Rapoport /**
333e039c5cSMike Rapoport  * DOC: memblock overview
343e039c5cSMike Rapoport  *
353e039c5cSMike Rapoport  * Memblock is a method of managing memory regions during the early
363e039c5cSMike Rapoport  * boot period when the usual kernel memory allocators are not up and
373e039c5cSMike Rapoport  * running.
383e039c5cSMike Rapoport  *
393e039c5cSMike Rapoport  * Memblock views the system memory as collections of contiguous
403e039c5cSMike Rapoport  * regions. There are several types of these collections:
413e039c5cSMike Rapoport  *
423e039c5cSMike Rapoport  * * ``memory`` - describes the physical memory available to the
433e039c5cSMike Rapoport  *   kernel; this may differ from the actual physical memory installed
443e039c5cSMike Rapoport  *   in the system, for instance when the memory is restricted with
453e039c5cSMike Rapoport  *   ``mem=`` command line parameter
463e039c5cSMike Rapoport  * * ``reserved`` - describes the regions that were allocated
4777649905SDavid Hildenbrand  * * ``physmem`` - describes the actual physical memory available during
4877649905SDavid Hildenbrand  *   boot regardless of the possible restrictions and memory hot(un)plug;
4977649905SDavid Hildenbrand  *   the ``physmem`` type is only available on some architectures.
503e039c5cSMike Rapoport  *
519303c9d5SMauro Carvalho Chehab  * Each region is represented by struct memblock_region that
523e039c5cSMike Rapoport  * defines the region extents, its attributes and NUMA node id on NUMA
531bf162e4SMauro Carvalho Chehab  * systems. Every memory type is described by the struct memblock_type
541bf162e4SMauro Carvalho Chehab  * which contains an array of memory regions along with
5577649905SDavid Hildenbrand  * the allocator metadata. The "memory" and "reserved" types are nicely
569303c9d5SMauro Carvalho Chehab  * wrapped with struct memblock. This structure is statically
5777649905SDavid Hildenbrand  * initialized at build time. The region arrays are initially sized to
5877649905SDavid Hildenbrand  * %INIT_MEMBLOCK_REGIONS for "memory" and %INIT_MEMBLOCK_RESERVED_REGIONS
5977649905SDavid Hildenbrand  * for "reserved". The region array for "physmem" is initially sized to
6077649905SDavid Hildenbrand  * %INIT_PHYSMEM_REGIONS.
616e5af9a8SCao jin  * The memblock_allow_resize() enables automatic resizing of the region
626e5af9a8SCao jin  * arrays during addition of new regions. This feature should be used
636e5af9a8SCao jin  * with care so that memory allocated for the region array will not
646e5af9a8SCao jin  * overlap with areas that should be reserved, for example initrd.
653e039c5cSMike Rapoport  *
663e039c5cSMike Rapoport  * The early architecture setup should tell memblock what the physical
676e5af9a8SCao jin  * memory layout is by using memblock_add() or memblock_add_node()
686e5af9a8SCao jin  * functions. The first function does not assign the region to a NUMA
696e5af9a8SCao jin  * node and it is appropriate for UMA systems. Yet, it is possible to
706e5af9a8SCao jin  * use it on NUMA systems as well and assign the region to a NUMA node
716e5af9a8SCao jin  * later in the setup process using memblock_set_node(). The
726e5af9a8SCao jin  * memblock_add_node() performs such an assignment directly.
733e039c5cSMike Rapoport  *
74a2974133SMike Rapoport  * Once memblock is setup the memory can be allocated using one of the
75a2974133SMike Rapoport  * API variants:
76a2974133SMike Rapoport  *
776e5af9a8SCao jin  * * memblock_phys_alloc*() - these functions return the **physical**
786e5af9a8SCao jin  *   address of the allocated memory
796e5af9a8SCao jin  * * memblock_alloc*() - these functions return the **virtual** address
806e5af9a8SCao jin  *   of the allocated memory.
81a2974133SMike Rapoport  *
82df1758d9SEthon Paul  * Note, that both API variants use implicit assumptions about allowed
83a2974133SMike Rapoport  * memory ranges and the fallback methods. Consult the documentation
846e5af9a8SCao jin  * of memblock_alloc_internal() and memblock_alloc_range_nid()
856e5af9a8SCao jin  * functions for more elaborate description.
863e039c5cSMike Rapoport  *
876e5af9a8SCao jin  * As the system boot progresses, the architecture specific mem_init()
886e5af9a8SCao jin  * function frees all the memory to the buddy page allocator.
893e039c5cSMike Rapoport  *
906e5af9a8SCao jin  * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the
9177649905SDavid Hildenbrand  * memblock data structures (except "physmem") will be discarded after the
9277649905SDavid Hildenbrand  * system initialization completes.
933e039c5cSMike Rapoport  */
943e039c5cSMike Rapoport 
95a9ee6cf5SMike Rapoport #ifndef CONFIG_NUMA
96bda49a81SMike Rapoport struct pglist_data __refdata contig_page_data;
97bda49a81SMike Rapoport EXPORT_SYMBOL(contig_page_data);
98bda49a81SMike Rapoport #endif
99bda49a81SMike Rapoport 
100bda49a81SMike Rapoport unsigned long max_low_pfn;
101bda49a81SMike Rapoport unsigned long min_low_pfn;
102bda49a81SMike Rapoport unsigned long max_pfn;
103bda49a81SMike Rapoport unsigned long long max_possible_pfn;
104bda49a81SMike Rapoport 
105fe091c20STejun Heo static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
1068a5b403dSArd Biesheuvel static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
10770210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
10877649905SDavid Hildenbrand static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS];
10970210ed9SPhilipp Hachtmann #endif
110fe091c20STejun Heo 
111fe091c20STejun Heo struct memblock memblock __initdata_memblock = {
112fe091c20STejun Heo 	.memory.regions		= memblock_memory_init_regions,
113fe091c20STejun Heo 	.memory.cnt		= 1,	/* empty dummy entry */
114fe091c20STejun Heo 	.memory.max		= INIT_MEMBLOCK_REGIONS,
1150262d9c8SHeiko Carstens 	.memory.name		= "memory",
116fe091c20STejun Heo 
117fe091c20STejun Heo 	.reserved.regions	= memblock_reserved_init_regions,
118fe091c20STejun Heo 	.reserved.cnt		= 1,	/* empty dummy entry */
1198a5b403dSArd Biesheuvel 	.reserved.max		= INIT_MEMBLOCK_RESERVED_REGIONS,
1200262d9c8SHeiko Carstens 	.reserved.name		= "reserved",
121fe091c20STejun Heo 
12279442ed1STang Chen 	.bottom_up		= false,
123fe091c20STejun Heo 	.current_limit		= MEMBLOCK_ALLOC_ANYWHERE,
124fe091c20STejun Heo };
12595f72d1eSYinghai Lu 
12677649905SDavid Hildenbrand #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
12777649905SDavid Hildenbrand struct memblock_type physmem = {
12877649905SDavid Hildenbrand 	.regions		= memblock_physmem_init_regions,
12977649905SDavid Hildenbrand 	.cnt			= 1,	/* empty dummy entry */
13077649905SDavid Hildenbrand 	.max			= INIT_PHYSMEM_REGIONS,
13177649905SDavid Hildenbrand 	.name			= "physmem",
13277649905SDavid Hildenbrand };
13377649905SDavid Hildenbrand #endif
13477649905SDavid Hildenbrand 
1359f3d5eaaSMike Rapoport /*
1369f3d5eaaSMike Rapoport  * keep a pointer to &memblock.memory in the text section to use it in
1379f3d5eaaSMike Rapoport  * __next_mem_range() and its helpers.
1389f3d5eaaSMike Rapoport  *  For architectures that do not keep memblock data after init, this
1399f3d5eaaSMike Rapoport  * pointer will be reset to NULL at memblock_discard()
1409f3d5eaaSMike Rapoport  */
1419f3d5eaaSMike Rapoport static __refdata struct memblock_type *memblock_memory = &memblock.memory;
1429f3d5eaaSMike Rapoport 
143cd991db8SMike Rapoport #define for_each_memblock_type(i, memblock_type, rgn)			\
144cd991db8SMike Rapoport 	for (i = 0, rgn = &memblock_type->regions[0];			\
145cd991db8SMike Rapoport 	     i < memblock_type->cnt;					\
146cd991db8SMike Rapoport 	     i++, rgn = &memblock_type->regions[i])
147cd991db8SMike Rapoport 
14887c55870SMike Rapoport #define memblock_dbg(fmt, ...)						\
14987c55870SMike Rapoport 	do {								\
15087c55870SMike Rapoport 		if (memblock_debug)					\
15187c55870SMike Rapoport 			pr_info(fmt, ##__VA_ARGS__);			\
15287c55870SMike Rapoport 	} while (0)
15387c55870SMike Rapoport 
15487c55870SMike Rapoport static int memblock_debug __initdata_memblock;
155a3f5bafcSTony Luck static bool system_has_some_mirror __initdata_memblock = false;
1561aadc056STejun Heo static int memblock_can_resize __initdata_memblock;
157181eb394SGavin Shan static int memblock_memory_in_slab __initdata_memblock = 0;
158181eb394SGavin Shan static int memblock_reserved_in_slab __initdata_memblock = 0;
15995f72d1eSYinghai Lu 
160c366ea89SMike Rapoport static enum memblock_flags __init_memblock choose_memblock_flags(void)
161a3f5bafcSTony Luck {
162a3f5bafcSTony Luck 	return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
163a3f5bafcSTony Luck }
164a3f5bafcSTony Luck 
165eb18f1b5STejun Heo /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
166eb18f1b5STejun Heo static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
167eb18f1b5STejun Heo {
1681c4bc43dSStefan Agner 	return *size = min(*size, PHYS_ADDR_MAX - base);
169eb18f1b5STejun Heo }
170eb18f1b5STejun Heo 
1716ed311b2SBenjamin Herrenschmidt /*
1726ed311b2SBenjamin Herrenschmidt  * Address comparison utilities
1736ed311b2SBenjamin Herrenschmidt  */
17410d06439SYinghai Lu static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
1752898cc4cSBenjamin Herrenschmidt 				       phys_addr_t base2, phys_addr_t size2)
17695f72d1eSYinghai Lu {
17795f72d1eSYinghai Lu 	return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
17895f72d1eSYinghai Lu }
17995f72d1eSYinghai Lu 
18095cf82ecSTang Chen bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
1812d7d3eb2SH Hartley Sweeten 					phys_addr_t base, phys_addr_t size)
1826ed311b2SBenjamin Herrenschmidt {
1836ed311b2SBenjamin Herrenschmidt 	unsigned long i;
1846ed311b2SBenjamin Herrenschmidt 
185023accf5SMike Rapoport 	memblock_cap_size(base, &size);
186023accf5SMike Rapoport 
187f14516fbSAlexander Kuleshov 	for (i = 0; i < type->cnt; i++)
188f14516fbSAlexander Kuleshov 		if (memblock_addrs_overlap(base, size, type->regions[i].base,
189f14516fbSAlexander Kuleshov 					   type->regions[i].size))
1906ed311b2SBenjamin Herrenschmidt 			break;
191c5c5c9d1STang Chen 	return i < type->cnt;
1926ed311b2SBenjamin Herrenschmidt }
1936ed311b2SBenjamin Herrenschmidt 
19447cec443SMike Rapoport /**
19579442ed1STang Chen  * __memblock_find_range_bottom_up - find free area utility in bottom-up
19679442ed1STang Chen  * @start: start of candidate range
19747cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
19847cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
19979442ed1STang Chen  * @size: size of free area to find
20079442ed1STang Chen  * @align: alignment of free area to find
201b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
202fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
20379442ed1STang Chen  *
20479442ed1STang Chen  * Utility called from memblock_find_in_range_node(), find free area bottom-up.
20579442ed1STang Chen  *
20647cec443SMike Rapoport  * Return:
20779442ed1STang Chen  * Found address on success, 0 on failure.
20879442ed1STang Chen  */
20979442ed1STang Chen static phys_addr_t __init_memblock
21079442ed1STang Chen __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
211fc6daaf9STony Luck 				phys_addr_t size, phys_addr_t align, int nid,
212e1720feeSMike Rapoport 				enum memblock_flags flags)
21379442ed1STang Chen {
21479442ed1STang Chen 	phys_addr_t this_start, this_end, cand;
21579442ed1STang Chen 	u64 i;
21679442ed1STang Chen 
217fc6daaf9STony Luck 	for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
21879442ed1STang Chen 		this_start = clamp(this_start, start, end);
21979442ed1STang Chen 		this_end = clamp(this_end, start, end);
22079442ed1STang Chen 
22179442ed1STang Chen 		cand = round_up(this_start, align);
22279442ed1STang Chen 		if (cand < this_end && this_end - cand >= size)
22379442ed1STang Chen 			return cand;
22479442ed1STang Chen 	}
22579442ed1STang Chen 
22679442ed1STang Chen 	return 0;
22779442ed1STang Chen }
22879442ed1STang Chen 
2297bd0b0f0STejun Heo /**
2301402899eSTang Chen  * __memblock_find_range_top_down - find free area utility, in top-down
2311402899eSTang Chen  * @start: start of candidate range
23247cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
23347cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
2341402899eSTang Chen  * @size: size of free area to find
2351402899eSTang Chen  * @align: alignment of free area to find
236b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
237fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2381402899eSTang Chen  *
2391402899eSTang Chen  * Utility called from memblock_find_in_range_node(), find free area top-down.
2401402899eSTang Chen  *
24147cec443SMike Rapoport  * Return:
24279442ed1STang Chen  * Found address on success, 0 on failure.
2431402899eSTang Chen  */
2441402899eSTang Chen static phys_addr_t __init_memblock
2451402899eSTang Chen __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
246fc6daaf9STony Luck 			       phys_addr_t size, phys_addr_t align, int nid,
247e1720feeSMike Rapoport 			       enum memblock_flags flags)
2481402899eSTang Chen {
2491402899eSTang Chen 	phys_addr_t this_start, this_end, cand;
2501402899eSTang Chen 	u64 i;
2511402899eSTang Chen 
252fc6daaf9STony Luck 	for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
253fc6daaf9STony Luck 					NULL) {
2541402899eSTang Chen 		this_start = clamp(this_start, start, end);
2551402899eSTang Chen 		this_end = clamp(this_end, start, end);
2561402899eSTang Chen 
2571402899eSTang Chen 		if (this_end < size)
2581402899eSTang Chen 			continue;
2591402899eSTang Chen 
2601402899eSTang Chen 		cand = round_down(this_end - size, align);
2611402899eSTang Chen 		if (cand >= this_start)
2621402899eSTang Chen 			return cand;
2631402899eSTang Chen 	}
2641402899eSTang Chen 
2651402899eSTang Chen 	return 0;
2661402899eSTang Chen }
2671402899eSTang Chen 
2681402899eSTang Chen /**
2697bd0b0f0STejun Heo  * memblock_find_in_range_node - find free area in given range and node
2707bd0b0f0STejun Heo  * @size: size of free area to find
2717bd0b0f0STejun Heo  * @align: alignment of free area to find
27287029ee9SGrygorii Strashko  * @start: start of candidate range
27347cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
27447cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
275b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
276fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2777bd0b0f0STejun Heo  *
2787bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range and node.
2797bd0b0f0STejun Heo  *
28047cec443SMike Rapoport  * Return:
28179442ed1STang Chen  * Found address on success, 0 on failure.
2826ed311b2SBenjamin Herrenschmidt  */
283c366ea89SMike Rapoport static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
28487029ee9SGrygorii Strashko 					phys_addr_t align, phys_addr_t start,
285e1720feeSMike Rapoport 					phys_addr_t end, int nid,
286e1720feeSMike Rapoport 					enum memblock_flags flags)
287f7210e6cSTang Chen {
288f7210e6cSTang Chen 	/* pump up @end */
289fed84c78SQian Cai 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
290fed84c78SQian Cai 	    end == MEMBLOCK_ALLOC_KASAN)
291f7210e6cSTang Chen 		end = memblock.current_limit;
292f7210e6cSTang Chen 
293f7210e6cSTang Chen 	/* avoid allocating the first page */
294f7210e6cSTang Chen 	start = max_t(phys_addr_t, start, PAGE_SIZE);
295f7210e6cSTang Chen 	end = max(start, end);
29679442ed1STang Chen 
2972dcb3964SRoman Gushchin 	if (memblock_bottom_up())
2982dcb3964SRoman Gushchin 		return __memblock_find_range_bottom_up(start, end, size, align,
2992dcb3964SRoman Gushchin 						       nid, flags);
3002dcb3964SRoman Gushchin 	else
3012dcb3964SRoman Gushchin 		return __memblock_find_range_top_down(start, end, size, align,
3022dcb3964SRoman Gushchin 						      nid, flags);
303f7210e6cSTang Chen }
3046ed311b2SBenjamin Herrenschmidt 
3057bd0b0f0STejun Heo /**
3067bd0b0f0STejun Heo  * memblock_find_in_range - find free area in given range
3077bd0b0f0STejun Heo  * @start: start of candidate range
30847cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
30947cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
3107bd0b0f0STejun Heo  * @size: size of free area to find
3117bd0b0f0STejun Heo  * @align: alignment of free area to find
3127bd0b0f0STejun Heo  *
3137bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range.
3147bd0b0f0STejun Heo  *
31547cec443SMike Rapoport  * Return:
31679442ed1STang Chen  * Found address on success, 0 on failure.
3177bd0b0f0STejun Heo  */
318*a7259df7SMike Rapoport static phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
3197bd0b0f0STejun Heo 					phys_addr_t end, phys_addr_t size,
3207bd0b0f0STejun Heo 					phys_addr_t align)
3217bd0b0f0STejun Heo {
322a3f5bafcSTony Luck 	phys_addr_t ret;
323e1720feeSMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
324a3f5bafcSTony Luck 
325a3f5bafcSTony Luck again:
326a3f5bafcSTony Luck 	ret = memblock_find_in_range_node(size, align, start, end,
327a3f5bafcSTony Luck 					    NUMA_NO_NODE, flags);
328a3f5bafcSTony Luck 
329a3f5bafcSTony Luck 	if (!ret && (flags & MEMBLOCK_MIRROR)) {
330a3f5bafcSTony Luck 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
331a3f5bafcSTony Luck 			&size);
332a3f5bafcSTony Luck 		flags &= ~MEMBLOCK_MIRROR;
333a3f5bafcSTony Luck 		goto again;
334a3f5bafcSTony Luck 	}
335a3f5bafcSTony Luck 
336a3f5bafcSTony Luck 	return ret;
3377bd0b0f0STejun Heo }
3387bd0b0f0STejun Heo 
33910d06439SYinghai Lu static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
34095f72d1eSYinghai Lu {
3411440c4e2STejun Heo 	type->total_size -= type->regions[r].size;
3427c0caeb8STejun Heo 	memmove(&type->regions[r], &type->regions[r + 1],
3437c0caeb8STejun Heo 		(type->cnt - (r + 1)) * sizeof(type->regions[r]));
344e3239ff9SBenjamin Herrenschmidt 	type->cnt--;
34595f72d1eSYinghai Lu 
3468f7a6605SBenjamin Herrenschmidt 	/* Special case for empty arrays */
3478f7a6605SBenjamin Herrenschmidt 	if (type->cnt == 0) {
3481440c4e2STejun Heo 		WARN_ON(type->total_size != 0);
3498f7a6605SBenjamin Herrenschmidt 		type->cnt = 1;
3508f7a6605SBenjamin Herrenschmidt 		type->regions[0].base = 0;
3518f7a6605SBenjamin Herrenschmidt 		type->regions[0].size = 0;
35266a20757STang Chen 		type->regions[0].flags = 0;
3537c0caeb8STejun Heo 		memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
3548f7a6605SBenjamin Herrenschmidt 	}
35595f72d1eSYinghai Lu }
35695f72d1eSYinghai Lu 
357350e88baSMike Rapoport #ifndef CONFIG_ARCH_KEEP_MEMBLOCK
3583010f876SPavel Tatashin /**
35947cec443SMike Rapoport  * memblock_discard - discard memory and reserved arrays if they were allocated
3603010f876SPavel Tatashin  */
3613010f876SPavel Tatashin void __init memblock_discard(void)
36229f67386SYinghai Lu {
3633010f876SPavel Tatashin 	phys_addr_t addr, size;
36429f67386SYinghai Lu 
3653010f876SPavel Tatashin 	if (memblock.reserved.regions != memblock_reserved_init_regions) {
3663010f876SPavel Tatashin 		addr = __pa(memblock.reserved.regions);
3673010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
36829f67386SYinghai Lu 				  memblock.reserved.max);
3693010f876SPavel Tatashin 		__memblock_free_late(addr, size);
37029f67386SYinghai Lu 	}
37129f67386SYinghai Lu 
37291b540f9SPavel Tatashin 	if (memblock.memory.regions != memblock_memory_init_regions) {
3733010f876SPavel Tatashin 		addr = __pa(memblock.memory.regions);
3743010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
3755e270e25SPhilipp Hachtmann 				  memblock.memory.max);
3763010f876SPavel Tatashin 		__memblock_free_late(addr, size);
3775e270e25SPhilipp Hachtmann 	}
3789f3d5eaaSMike Rapoport 
3799f3d5eaaSMike Rapoport 	memblock_memory = NULL;
3803010f876SPavel Tatashin }
3815e270e25SPhilipp Hachtmann #endif
3825e270e25SPhilipp Hachtmann 
38348c3b583SGreg Pearson /**
38448c3b583SGreg Pearson  * memblock_double_array - double the size of the memblock regions array
38548c3b583SGreg Pearson  * @type: memblock type of the regions array being doubled
38648c3b583SGreg Pearson  * @new_area_start: starting address of memory range to avoid overlap with
38748c3b583SGreg Pearson  * @new_area_size: size of memory range to avoid overlap with
38848c3b583SGreg Pearson  *
38948c3b583SGreg Pearson  * Double the size of the @type regions array. If memblock is being used to
39048c3b583SGreg Pearson  * allocate memory for a new reserved regions array and there is a previously
39148c3b583SGreg Pearson  * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
39248c3b583SGreg Pearson  * waiting to be reserved, ensure the memory used by the new array does
39348c3b583SGreg Pearson  * not overlap.
39448c3b583SGreg Pearson  *
39547cec443SMike Rapoport  * Return:
39648c3b583SGreg Pearson  * 0 on success, -1 on failure.
39748c3b583SGreg Pearson  */
39848c3b583SGreg Pearson static int __init_memblock memblock_double_array(struct memblock_type *type,
39948c3b583SGreg Pearson 						phys_addr_t new_area_start,
40048c3b583SGreg Pearson 						phys_addr_t new_area_size)
401142b45a7SBenjamin Herrenschmidt {
402142b45a7SBenjamin Herrenschmidt 	struct memblock_region *new_array, *old_array;
40329f67386SYinghai Lu 	phys_addr_t old_alloc_size, new_alloc_size;
404a36aab89SMike Rapoport 	phys_addr_t old_size, new_size, addr, new_end;
405142b45a7SBenjamin Herrenschmidt 	int use_slab = slab_is_available();
406181eb394SGavin Shan 	int *in_slab;
407142b45a7SBenjamin Herrenschmidt 
408142b45a7SBenjamin Herrenschmidt 	/* We don't allow resizing until we know about the reserved regions
409142b45a7SBenjamin Herrenschmidt 	 * of memory that aren't suitable for allocation
410142b45a7SBenjamin Herrenschmidt 	 */
411142b45a7SBenjamin Herrenschmidt 	if (!memblock_can_resize)
412142b45a7SBenjamin Herrenschmidt 		return -1;
413142b45a7SBenjamin Herrenschmidt 
414142b45a7SBenjamin Herrenschmidt 	/* Calculate new doubled size */
415142b45a7SBenjamin Herrenschmidt 	old_size = type->max * sizeof(struct memblock_region);
416142b45a7SBenjamin Herrenschmidt 	new_size = old_size << 1;
41729f67386SYinghai Lu 	/*
41829f67386SYinghai Lu 	 * We need to allocated new one align to PAGE_SIZE,
41929f67386SYinghai Lu 	 *   so we can free them completely later.
42029f67386SYinghai Lu 	 */
42129f67386SYinghai Lu 	old_alloc_size = PAGE_ALIGN(old_size);
42229f67386SYinghai Lu 	new_alloc_size = PAGE_ALIGN(new_size);
423142b45a7SBenjamin Herrenschmidt 
424181eb394SGavin Shan 	/* Retrieve the slab flag */
425181eb394SGavin Shan 	if (type == &memblock.memory)
426181eb394SGavin Shan 		in_slab = &memblock_memory_in_slab;
427181eb394SGavin Shan 	else
428181eb394SGavin Shan 		in_slab = &memblock_reserved_in_slab;
429181eb394SGavin Shan 
430a2974133SMike Rapoport 	/* Try to find some space for it */
431142b45a7SBenjamin Herrenschmidt 	if (use_slab) {
432142b45a7SBenjamin Herrenschmidt 		new_array = kmalloc(new_size, GFP_KERNEL);
4331f5026a7STejun Heo 		addr = new_array ? __pa(new_array) : 0;
4344e2f0775SGavin Shan 	} else {
43548c3b583SGreg Pearson 		/* only exclude range when trying to double reserved.regions */
43648c3b583SGreg Pearson 		if (type != &memblock.reserved)
43748c3b583SGreg Pearson 			new_area_start = new_area_size = 0;
43848c3b583SGreg Pearson 
43948c3b583SGreg Pearson 		addr = memblock_find_in_range(new_area_start + new_area_size,
44048c3b583SGreg Pearson 						memblock.current_limit,
44129f67386SYinghai Lu 						new_alloc_size, PAGE_SIZE);
44248c3b583SGreg Pearson 		if (!addr && new_area_size)
44348c3b583SGreg Pearson 			addr = memblock_find_in_range(0,
44448c3b583SGreg Pearson 				min(new_area_start, memblock.current_limit),
44529f67386SYinghai Lu 				new_alloc_size, PAGE_SIZE);
44648c3b583SGreg Pearson 
44715674868SSachin Kamat 		new_array = addr ? __va(addr) : NULL;
4484e2f0775SGavin Shan 	}
4491f5026a7STejun Heo 	if (!addr) {
450142b45a7SBenjamin Herrenschmidt 		pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
4510262d9c8SHeiko Carstens 		       type->name, type->max, type->max * 2);
452142b45a7SBenjamin Herrenschmidt 		return -1;
453142b45a7SBenjamin Herrenschmidt 	}
454142b45a7SBenjamin Herrenschmidt 
455a36aab89SMike Rapoport 	new_end = addr + new_size - 1;
456a36aab89SMike Rapoport 	memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
457a36aab89SMike Rapoport 			type->name, type->max * 2, &addr, &new_end);
458ea9e4376SYinghai Lu 
459fd07383bSAndrew Morton 	/*
460fd07383bSAndrew Morton 	 * Found space, we now need to move the array over before we add the
461fd07383bSAndrew Morton 	 * reserved region since it may be our reserved array itself that is
462fd07383bSAndrew Morton 	 * full.
463142b45a7SBenjamin Herrenschmidt 	 */
464142b45a7SBenjamin Herrenschmidt 	memcpy(new_array, type->regions, old_size);
465142b45a7SBenjamin Herrenschmidt 	memset(new_array + type->max, 0, old_size);
466142b45a7SBenjamin Herrenschmidt 	old_array = type->regions;
467142b45a7SBenjamin Herrenschmidt 	type->regions = new_array;
468142b45a7SBenjamin Herrenschmidt 	type->max <<= 1;
469142b45a7SBenjamin Herrenschmidt 
470fd07383bSAndrew Morton 	/* Free old array. We needn't free it if the array is the static one */
471181eb394SGavin Shan 	if (*in_slab)
472181eb394SGavin Shan 		kfree(old_array);
473181eb394SGavin Shan 	else if (old_array != memblock_memory_init_regions &&
474142b45a7SBenjamin Herrenschmidt 		 old_array != memblock_reserved_init_regions)
47529f67386SYinghai Lu 		memblock_free(__pa(old_array), old_alloc_size);
476142b45a7SBenjamin Herrenschmidt 
477fd07383bSAndrew Morton 	/*
478fd07383bSAndrew Morton 	 * Reserve the new array if that comes from the memblock.  Otherwise, we
479fd07383bSAndrew Morton 	 * needn't do it
480181eb394SGavin Shan 	 */
481181eb394SGavin Shan 	if (!use_slab)
48229f67386SYinghai Lu 		BUG_ON(memblock_reserve(addr, new_alloc_size));
483181eb394SGavin Shan 
484181eb394SGavin Shan 	/* Update slab flag */
485181eb394SGavin Shan 	*in_slab = use_slab;
486181eb394SGavin Shan 
487142b45a7SBenjamin Herrenschmidt 	return 0;
488142b45a7SBenjamin Herrenschmidt }
489142b45a7SBenjamin Herrenschmidt 
490784656f9STejun Heo /**
491784656f9STejun Heo  * memblock_merge_regions - merge neighboring compatible regions
492784656f9STejun Heo  * @type: memblock type to scan
493784656f9STejun Heo  *
494784656f9STejun Heo  * Scan @type and merge neighboring compatible regions.
495784656f9STejun Heo  */
496784656f9STejun Heo static void __init_memblock memblock_merge_regions(struct memblock_type *type)
497784656f9STejun Heo {
498784656f9STejun Heo 	int i = 0;
499784656f9STejun Heo 
500784656f9STejun Heo 	/* cnt never goes below 1 */
501784656f9STejun Heo 	while (i < type->cnt - 1) {
502784656f9STejun Heo 		struct memblock_region *this = &type->regions[i];
503784656f9STejun Heo 		struct memblock_region *next = &type->regions[i + 1];
504784656f9STejun Heo 
5057c0caeb8STejun Heo 		if (this->base + this->size != next->base ||
5067c0caeb8STejun Heo 		    memblock_get_region_node(this) !=
50766a20757STang Chen 		    memblock_get_region_node(next) ||
50866a20757STang Chen 		    this->flags != next->flags) {
509784656f9STejun Heo 			BUG_ON(this->base + this->size > next->base);
510784656f9STejun Heo 			i++;
511784656f9STejun Heo 			continue;
512784656f9STejun Heo 		}
513784656f9STejun Heo 
514784656f9STejun Heo 		this->size += next->size;
515c0232ae8SLin Feng 		/* move forward from next + 1, index of which is i + 2 */
516c0232ae8SLin Feng 		memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
517784656f9STejun Heo 		type->cnt--;
518784656f9STejun Heo 	}
519784656f9STejun Heo }
520784656f9STejun Heo 
521784656f9STejun Heo /**
522784656f9STejun Heo  * memblock_insert_region - insert new memblock region
523784656f9STejun Heo  * @type:	memblock type to insert into
524784656f9STejun Heo  * @idx:	index for the insertion point
525784656f9STejun Heo  * @base:	base address of the new region
526784656f9STejun Heo  * @size:	size of the new region
527209ff86dSTang Chen  * @nid:	node id of the new region
52866a20757STang Chen  * @flags:	flags of the new region
529784656f9STejun Heo  *
530784656f9STejun Heo  * Insert new memblock region [@base, @base + @size) into @type at @idx.
531412d0008SAlexander Kuleshov  * @type must already have extra room to accommodate the new region.
532784656f9STejun Heo  */
533784656f9STejun Heo static void __init_memblock memblock_insert_region(struct memblock_type *type,
534784656f9STejun Heo 						   int idx, phys_addr_t base,
53566a20757STang Chen 						   phys_addr_t size,
536e1720feeSMike Rapoport 						   int nid,
537e1720feeSMike Rapoport 						   enum memblock_flags flags)
538784656f9STejun Heo {
539784656f9STejun Heo 	struct memblock_region *rgn = &type->regions[idx];
540784656f9STejun Heo 
541784656f9STejun Heo 	BUG_ON(type->cnt >= type->max);
542784656f9STejun Heo 	memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
543784656f9STejun Heo 	rgn->base = base;
544784656f9STejun Heo 	rgn->size = size;
54566a20757STang Chen 	rgn->flags = flags;
5467c0caeb8STejun Heo 	memblock_set_region_node(rgn, nid);
547784656f9STejun Heo 	type->cnt++;
5481440c4e2STejun Heo 	type->total_size += size;
549784656f9STejun Heo }
550784656f9STejun Heo 
551784656f9STejun Heo /**
552f1af9d3aSPhilipp Hachtmann  * memblock_add_range - add new memblock region
553784656f9STejun Heo  * @type: memblock type to add new region into
554784656f9STejun Heo  * @base: base address of the new region
555784656f9STejun Heo  * @size: size of the new region
5567fb0bc3fSTejun Heo  * @nid: nid of the new region
55766a20757STang Chen  * @flags: flags of the new region
558784656f9STejun Heo  *
559784656f9STejun Heo  * Add new memblock region [@base, @base + @size) into @type.  The new region
560784656f9STejun Heo  * is allowed to overlap with existing ones - overlaps don't affect already
561784656f9STejun Heo  * existing regions.  @type is guaranteed to be minimal (all neighbouring
562784656f9STejun Heo  * compatible regions are merged) after the addition.
563784656f9STejun Heo  *
56447cec443SMike Rapoport  * Return:
565784656f9STejun Heo  * 0 on success, -errno on failure.
566784656f9STejun Heo  */
56702634a44SAnshuman Khandual static int __init_memblock memblock_add_range(struct memblock_type *type,
56866a20757STang Chen 				phys_addr_t base, phys_addr_t size,
569e1720feeSMike Rapoport 				int nid, enum memblock_flags flags)
57095f72d1eSYinghai Lu {
571784656f9STejun Heo 	bool insert = false;
572eb18f1b5STejun Heo 	phys_addr_t obase = base;
573eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
5748c9c1701SAlexander Kuleshov 	int idx, nr_new;
5758c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
57695f72d1eSYinghai Lu 
577b3dc627cSTejun Heo 	if (!size)
578b3dc627cSTejun Heo 		return 0;
579b3dc627cSTejun Heo 
580784656f9STejun Heo 	/* special case for empty array */
581784656f9STejun Heo 	if (type->regions[0].size == 0) {
5821440c4e2STejun Heo 		WARN_ON(type->cnt != 1 || type->total_size);
583784656f9STejun Heo 		type->regions[0].base = base;
584784656f9STejun Heo 		type->regions[0].size = size;
58566a20757STang Chen 		type->regions[0].flags = flags;
5867fb0bc3fSTejun Heo 		memblock_set_region_node(&type->regions[0], nid);
5871440c4e2STejun Heo 		type->total_size = size;
588784656f9STejun Heo 		return 0;
589784656f9STejun Heo 	}
590784656f9STejun Heo repeat:
591784656f9STejun Heo 	/*
592784656f9STejun Heo 	 * The following is executed twice.  Once with %false @insert and
593784656f9STejun Heo 	 * then with %true.  The first counts the number of regions needed
594412d0008SAlexander Kuleshov 	 * to accommodate the new area.  The second actually inserts them.
595784656f9STejun Heo 	 */
596784656f9STejun Heo 	base = obase;
597784656f9STejun Heo 	nr_new = 0;
598784656f9STejun Heo 
59966e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
600784656f9STejun Heo 		phys_addr_t rbase = rgn->base;
601784656f9STejun Heo 		phys_addr_t rend = rbase + rgn->size;
6028f7a6605SBenjamin Herrenschmidt 
603784656f9STejun Heo 		if (rbase >= end)
6048f7a6605SBenjamin Herrenschmidt 			break;
605784656f9STejun Heo 		if (rend <= base)
606784656f9STejun Heo 			continue;
607784656f9STejun Heo 		/*
608784656f9STejun Heo 		 * @rgn overlaps.  If it separates the lower part of new
609784656f9STejun Heo 		 * area, insert that portion.
6108f7a6605SBenjamin Herrenschmidt 		 */
611784656f9STejun Heo 		if (rbase > base) {
612a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA
613c0a29498SWei Yang 			WARN_ON(nid != memblock_get_region_node(rgn));
614c0a29498SWei Yang #endif
6154fcab5f4SWei Yang 			WARN_ON(flags != rgn->flags);
616784656f9STejun Heo 			nr_new++;
617784656f9STejun Heo 			if (insert)
6188c9c1701SAlexander Kuleshov 				memblock_insert_region(type, idx++, base,
61966a20757STang Chen 						       rbase - base, nid,
62066a20757STang Chen 						       flags);
621784656f9STejun Heo 		}
622784656f9STejun Heo 		/* area below @rend is dealt with, forget about it */
623784656f9STejun Heo 		base = min(rend, end);
6248f7a6605SBenjamin Herrenschmidt 	}
6258f7a6605SBenjamin Herrenschmidt 
626784656f9STejun Heo 	/* insert the remaining portion */
627784656f9STejun Heo 	if (base < end) {
628784656f9STejun Heo 		nr_new++;
629784656f9STejun Heo 		if (insert)
6308c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, base, end - base,
63166a20757STang Chen 					       nid, flags);
6328f7a6605SBenjamin Herrenschmidt 	}
6338f7a6605SBenjamin Herrenschmidt 
634ef3cc4dbSnimisolo 	if (!nr_new)
635ef3cc4dbSnimisolo 		return 0;
636ef3cc4dbSnimisolo 
637784656f9STejun Heo 	/*
638784656f9STejun Heo 	 * If this was the first round, resize array and repeat for actual
639784656f9STejun Heo 	 * insertions; otherwise, merge and return.
6408f7a6605SBenjamin Herrenschmidt 	 */
641784656f9STejun Heo 	if (!insert) {
642784656f9STejun Heo 		while (type->cnt + nr_new > type->max)
64348c3b583SGreg Pearson 			if (memblock_double_array(type, obase, size) < 0)
644784656f9STejun Heo 				return -ENOMEM;
645784656f9STejun Heo 		insert = true;
646784656f9STejun Heo 		goto repeat;
64795f72d1eSYinghai Lu 	} else {
648784656f9STejun Heo 		memblock_merge_regions(type);
64995f72d1eSYinghai Lu 		return 0;
65095f72d1eSYinghai Lu 	}
651784656f9STejun Heo }
65295f72d1eSYinghai Lu 
65348a833ccSMike Rapoport /**
65448a833ccSMike Rapoport  * memblock_add_node - add new memblock region within a NUMA node
65548a833ccSMike Rapoport  * @base: base address of the new region
65648a833ccSMike Rapoport  * @size: size of the new region
65748a833ccSMike Rapoport  * @nid: nid of the new region
65848a833ccSMike Rapoport  *
65948a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
66048a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
66148a833ccSMike Rapoport  *
66248a833ccSMike Rapoport  * Return:
66348a833ccSMike Rapoport  * 0 on success, -errno on failure.
66448a833ccSMike Rapoport  */
6657fb0bc3fSTejun Heo int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
6667fb0bc3fSTejun Heo 				       int nid)
6677fb0bc3fSTejun Heo {
668f1af9d3aSPhilipp Hachtmann 	return memblock_add_range(&memblock.memory, base, size, nid, 0);
6697fb0bc3fSTejun Heo }
6707fb0bc3fSTejun Heo 
67148a833ccSMike Rapoport /**
67248a833ccSMike Rapoport  * memblock_add - add new memblock region
67348a833ccSMike Rapoport  * @base: base address of the new region
67448a833ccSMike Rapoport  * @size: size of the new region
67548a833ccSMike Rapoport  *
67648a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
67748a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
67848a833ccSMike Rapoport  *
67948a833ccSMike Rapoport  * Return:
68048a833ccSMike Rapoport  * 0 on success, -errno on failure.
68148a833ccSMike Rapoport  */
682f705ac4bSAlexander Kuleshov int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
6836a4055bcSAlexander Kuleshov {
6845d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
6855d63f81cSMiles Chen 
686a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
6875d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
6886a4055bcSAlexander Kuleshov 
689f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
69095f72d1eSYinghai Lu }
69195f72d1eSYinghai Lu 
6926a9ceb31STejun Heo /**
6936a9ceb31STejun Heo  * memblock_isolate_range - isolate given range into disjoint memblocks
6946a9ceb31STejun Heo  * @type: memblock type to isolate range for
6956a9ceb31STejun Heo  * @base: base of range to isolate
6966a9ceb31STejun Heo  * @size: size of range to isolate
6976a9ceb31STejun Heo  * @start_rgn: out parameter for the start of isolated region
6986a9ceb31STejun Heo  * @end_rgn: out parameter for the end of isolated region
6996a9ceb31STejun Heo  *
7006a9ceb31STejun Heo  * Walk @type and ensure that regions don't cross the boundaries defined by
7016a9ceb31STejun Heo  * [@base, @base + @size).  Crossing regions are split at the boundaries,
7026a9ceb31STejun Heo  * which may create at most two more regions.  The index of the first
7036a9ceb31STejun Heo  * region inside the range is returned in *@start_rgn and end in *@end_rgn.
7046a9ceb31STejun Heo  *
70547cec443SMike Rapoport  * Return:
7066a9ceb31STejun Heo  * 0 on success, -errno on failure.
7076a9ceb31STejun Heo  */
7086a9ceb31STejun Heo static int __init_memblock memblock_isolate_range(struct memblock_type *type,
7096a9ceb31STejun Heo 					phys_addr_t base, phys_addr_t size,
7106a9ceb31STejun Heo 					int *start_rgn, int *end_rgn)
7116a9ceb31STejun Heo {
712eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
7138c9c1701SAlexander Kuleshov 	int idx;
7148c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
7156a9ceb31STejun Heo 
7166a9ceb31STejun Heo 	*start_rgn = *end_rgn = 0;
7176a9ceb31STejun Heo 
718b3dc627cSTejun Heo 	if (!size)
719b3dc627cSTejun Heo 		return 0;
720b3dc627cSTejun Heo 
7216a9ceb31STejun Heo 	/* we'll create at most two more regions */
7226a9ceb31STejun Heo 	while (type->cnt + 2 > type->max)
72348c3b583SGreg Pearson 		if (memblock_double_array(type, base, size) < 0)
7246a9ceb31STejun Heo 			return -ENOMEM;
7256a9ceb31STejun Heo 
72666e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
7276a9ceb31STejun Heo 		phys_addr_t rbase = rgn->base;
7286a9ceb31STejun Heo 		phys_addr_t rend = rbase + rgn->size;
7296a9ceb31STejun Heo 
7306a9ceb31STejun Heo 		if (rbase >= end)
7316a9ceb31STejun Heo 			break;
7326a9ceb31STejun Heo 		if (rend <= base)
7336a9ceb31STejun Heo 			continue;
7346a9ceb31STejun Heo 
7356a9ceb31STejun Heo 		if (rbase < base) {
7366a9ceb31STejun Heo 			/*
7376a9ceb31STejun Heo 			 * @rgn intersects from below.  Split and continue
7386a9ceb31STejun Heo 			 * to process the next region - the new top half.
7396a9ceb31STejun Heo 			 */
7406a9ceb31STejun Heo 			rgn->base = base;
7411440c4e2STejun Heo 			rgn->size -= base - rbase;
7421440c4e2STejun Heo 			type->total_size -= base - rbase;
7438c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, rbase, base - rbase,
74466a20757STang Chen 					       memblock_get_region_node(rgn),
74566a20757STang Chen 					       rgn->flags);
7466a9ceb31STejun Heo 		} else if (rend > end) {
7476a9ceb31STejun Heo 			/*
7486a9ceb31STejun Heo 			 * @rgn intersects from above.  Split and redo the
7496a9ceb31STejun Heo 			 * current region - the new bottom half.
7506a9ceb31STejun Heo 			 */
7516a9ceb31STejun Heo 			rgn->base = end;
7521440c4e2STejun Heo 			rgn->size -= end - rbase;
7531440c4e2STejun Heo 			type->total_size -= end - rbase;
7548c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx--, rbase, end - rbase,
75566a20757STang Chen 					       memblock_get_region_node(rgn),
75666a20757STang Chen 					       rgn->flags);
7576a9ceb31STejun Heo 		} else {
7586a9ceb31STejun Heo 			/* @rgn is fully contained, record it */
7596a9ceb31STejun Heo 			if (!*end_rgn)
7608c9c1701SAlexander Kuleshov 				*start_rgn = idx;
7618c9c1701SAlexander Kuleshov 			*end_rgn = idx + 1;
7626a9ceb31STejun Heo 		}
7636a9ceb31STejun Heo 	}
7646a9ceb31STejun Heo 
7656a9ceb31STejun Heo 	return 0;
7666a9ceb31STejun Heo }
7676a9ceb31STejun Heo 
76835bd16a2SAlexander Kuleshov static int __init_memblock memblock_remove_range(struct memblock_type *type,
7698f7a6605SBenjamin Herrenschmidt 					  phys_addr_t base, phys_addr_t size)
77095f72d1eSYinghai Lu {
77171936180STejun Heo 	int start_rgn, end_rgn;
77271936180STejun Heo 	int i, ret;
77395f72d1eSYinghai Lu 
77471936180STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
77571936180STejun Heo 	if (ret)
77671936180STejun Heo 		return ret;
77795f72d1eSYinghai Lu 
77871936180STejun Heo 	for (i = end_rgn - 1; i >= start_rgn; i--)
77971936180STejun Heo 		memblock_remove_region(type, i);
78095f72d1eSYinghai Lu 	return 0;
78195f72d1eSYinghai Lu }
78295f72d1eSYinghai Lu 
783581adcbeSTejun Heo int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
78495f72d1eSYinghai Lu {
78525cf23d7SMinchan Kim 	phys_addr_t end = base + size - 1;
78625cf23d7SMinchan Kim 
787a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
78825cf23d7SMinchan Kim 		     &base, &end, (void *)_RET_IP_);
78925cf23d7SMinchan Kim 
790f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.memory, base, size);
79195f72d1eSYinghai Lu }
79295f72d1eSYinghai Lu 
7934d72868cSMike Rapoport /**
7944d72868cSMike Rapoport  * memblock_free - free boot memory block
7954d72868cSMike Rapoport  * @base: phys starting address of the  boot memory block
7964d72868cSMike Rapoport  * @size: size of the boot memory block in bytes
7974d72868cSMike Rapoport  *
7984d72868cSMike Rapoport  * Free boot memory block previously allocated by memblock_alloc_xx() API.
7994d72868cSMike Rapoport  * The freeing memory will not be released to the buddy allocator.
8004d72868cSMike Rapoport  */
801581adcbeSTejun Heo int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
80295f72d1eSYinghai Lu {
8035d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8045d63f81cSMiles Chen 
805a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
8065d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
80724aa0788STejun Heo 
8089099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
809f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.reserved, base, size);
81095f72d1eSYinghai Lu }
81195f72d1eSYinghai Lu 
812f705ac4bSAlexander Kuleshov int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
81395f72d1eSYinghai Lu {
8145d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8155d63f81cSMiles Chen 
816a090d711SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
8175d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
81895f72d1eSYinghai Lu 
819f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
82095f72d1eSYinghai Lu }
82195f72d1eSYinghai Lu 
82202634a44SAnshuman Khandual #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
82302634a44SAnshuman Khandual int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size)
82402634a44SAnshuman Khandual {
82502634a44SAnshuman Khandual 	phys_addr_t end = base + size - 1;
82602634a44SAnshuman Khandual 
82702634a44SAnshuman Khandual 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
82802634a44SAnshuman Khandual 		     &base, &end, (void *)_RET_IP_);
82902634a44SAnshuman Khandual 
83077649905SDavid Hildenbrand 	return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0);
83102634a44SAnshuman Khandual }
83202634a44SAnshuman Khandual #endif
83302634a44SAnshuman Khandual 
83435fd0808STejun Heo /**
83547cec443SMike Rapoport  * memblock_setclr_flag - set or clear flag for a memory region
83647cec443SMike Rapoport  * @base: base address of the region
83747cec443SMike Rapoport  * @size: size of the region
83847cec443SMike Rapoport  * @set: set or clear the flag
8398958b249SHaitao Shi  * @flag: the flag to update
84066b16edfSTang Chen  *
8414308ce17STony Luck  * This function isolates region [@base, @base + @size), and sets/clears flag
84266b16edfSTang Chen  *
84347cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
84466b16edfSTang Chen  */
8454308ce17STony Luck static int __init_memblock memblock_setclr_flag(phys_addr_t base,
8464308ce17STony Luck 				phys_addr_t size, int set, int flag)
84766b16edfSTang Chen {
84866b16edfSTang Chen 	struct memblock_type *type = &memblock.memory;
84966b16edfSTang Chen 	int i, ret, start_rgn, end_rgn;
85066b16edfSTang Chen 
85166b16edfSTang Chen 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
85266b16edfSTang Chen 	if (ret)
85366b16edfSTang Chen 		return ret;
85466b16edfSTang Chen 
855fe145124SMike Rapoport 	for (i = start_rgn; i < end_rgn; i++) {
856fe145124SMike Rapoport 		struct memblock_region *r = &type->regions[i];
857fe145124SMike Rapoport 
8584308ce17STony Luck 		if (set)
859fe145124SMike Rapoport 			r->flags |= flag;
8604308ce17STony Luck 		else
861fe145124SMike Rapoport 			r->flags &= ~flag;
862fe145124SMike Rapoport 	}
86366b16edfSTang Chen 
86466b16edfSTang Chen 	memblock_merge_regions(type);
86566b16edfSTang Chen 	return 0;
86666b16edfSTang Chen }
86766b16edfSTang Chen 
86866b16edfSTang Chen /**
8694308ce17STony Luck  * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
8704308ce17STony Luck  * @base: the base phys addr of the region
8714308ce17STony Luck  * @size: the size of the region
8724308ce17STony Luck  *
87347cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
8744308ce17STony Luck  */
8754308ce17STony Luck int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
8764308ce17STony Luck {
8774308ce17STony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
8784308ce17STony Luck }
8794308ce17STony Luck 
8804308ce17STony Luck /**
88166b16edfSTang Chen  * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
88266b16edfSTang Chen  * @base: the base phys addr of the region
88366b16edfSTang Chen  * @size: the size of the region
88466b16edfSTang Chen  *
88547cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
88666b16edfSTang Chen  */
88766b16edfSTang Chen int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
88866b16edfSTang Chen {
8894308ce17STony Luck 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
89066b16edfSTang Chen }
89166b16edfSTang Chen 
89266b16edfSTang Chen /**
893a3f5bafcSTony Luck  * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
894a3f5bafcSTony Luck  * @base: the base phys addr of the region
895a3f5bafcSTony Luck  * @size: the size of the region
896a3f5bafcSTony Luck  *
89747cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
898a3f5bafcSTony Luck  */
899a3f5bafcSTony Luck int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
900a3f5bafcSTony Luck {
901a3f5bafcSTony Luck 	system_has_some_mirror = true;
902a3f5bafcSTony Luck 
903a3f5bafcSTony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
904a3f5bafcSTony Luck }
905a3f5bafcSTony Luck 
906bf3d3cc5SArd Biesheuvel /**
907bf3d3cc5SArd Biesheuvel  * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
908bf3d3cc5SArd Biesheuvel  * @base: the base phys addr of the region
909bf3d3cc5SArd Biesheuvel  * @size: the size of the region
910bf3d3cc5SArd Biesheuvel  *
9119092d4f7SMike Rapoport  * The memory regions marked with %MEMBLOCK_NOMAP will not be added to the
9129092d4f7SMike Rapoport  * direct mapping of the physical memory. These regions will still be
9139092d4f7SMike Rapoport  * covered by the memory map. The struct page representing NOMAP memory
9149092d4f7SMike Rapoport  * frames in the memory map will be PageReserved()
9159092d4f7SMike Rapoport  *
91647cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
917bf3d3cc5SArd Biesheuvel  */
918bf3d3cc5SArd Biesheuvel int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
919bf3d3cc5SArd Biesheuvel {
920bf3d3cc5SArd Biesheuvel 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
921bf3d3cc5SArd Biesheuvel }
922a3f5bafcSTony Luck 
923a3f5bafcSTony Luck /**
9244c546b8aSAKASHI Takahiro  * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
9254c546b8aSAKASHI Takahiro  * @base: the base phys addr of the region
9264c546b8aSAKASHI Takahiro  * @size: the size of the region
9274c546b8aSAKASHI Takahiro  *
92847cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
9294c546b8aSAKASHI Takahiro  */
9304c546b8aSAKASHI Takahiro int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
9314c546b8aSAKASHI Takahiro {
9324c546b8aSAKASHI Takahiro 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
9334c546b8aSAKASHI Takahiro }
9344c546b8aSAKASHI Takahiro 
9359f3d5eaaSMike Rapoport static bool should_skip_region(struct memblock_type *type,
9369f3d5eaaSMike Rapoport 			       struct memblock_region *m,
9379f3d5eaaSMike Rapoport 			       int nid, int flags)
938c9a688a3SMike Rapoport {
939c9a688a3SMike Rapoport 	int m_nid = memblock_get_region_node(m);
940c9a688a3SMike Rapoport 
9419f3d5eaaSMike Rapoport 	/* we never skip regions when iterating memblock.reserved or physmem */
9429f3d5eaaSMike Rapoport 	if (type != memblock_memory)
9439f3d5eaaSMike Rapoport 		return false;
9449f3d5eaaSMike Rapoport 
945c9a688a3SMike Rapoport 	/* only memory regions are associated with nodes, check it */
946c9a688a3SMike Rapoport 	if (nid != NUMA_NO_NODE && nid != m_nid)
947c9a688a3SMike Rapoport 		return true;
948c9a688a3SMike Rapoport 
949c9a688a3SMike Rapoport 	/* skip hotpluggable memory regions if needed */
95079e482e9SMike Rapoport 	if (movable_node_is_enabled() && memblock_is_hotpluggable(m) &&
95179e482e9SMike Rapoport 	    !(flags & MEMBLOCK_HOTPLUG))
952c9a688a3SMike Rapoport 		return true;
953c9a688a3SMike Rapoport 
954c9a688a3SMike Rapoport 	/* if we want mirror memory skip non-mirror memory regions */
955c9a688a3SMike Rapoport 	if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
956c9a688a3SMike Rapoport 		return true;
957c9a688a3SMike Rapoport 
958c9a688a3SMike Rapoport 	/* skip nomap memory unless we were asked for it explicitly */
959c9a688a3SMike Rapoport 	if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
960c9a688a3SMike Rapoport 		return true;
961c9a688a3SMike Rapoport 
962c9a688a3SMike Rapoport 	return false;
963c9a688a3SMike Rapoport }
964c9a688a3SMike Rapoport 
9658e7a7f86SRobin Holt /**
966a2974133SMike Rapoport  * __next_mem_range - next function for for_each_free_mem_range() etc.
96735fd0808STejun Heo  * @idx: pointer to u64 loop variable
968b1154233SGrygorii Strashko  * @nid: node selector, %NUMA_NO_NODE for all nodes
969fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
970f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
971f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
972dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
973dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
974dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
97535fd0808STejun Heo  *
976f1af9d3aSPhilipp Hachtmann  * Find the first area from *@idx which matches @nid, fill the out
97735fd0808STejun Heo  * parameters, and update *@idx for the next iteration.  The lower 32bit of
978f1af9d3aSPhilipp Hachtmann  * *@idx contains index into type_a and the upper 32bit indexes the
979f1af9d3aSPhilipp Hachtmann  * areas before each region in type_b.	For example, if type_b regions
98035fd0808STejun Heo  * look like the following,
98135fd0808STejun Heo  *
98235fd0808STejun Heo  *	0:[0-16), 1:[32-48), 2:[128-130)
98335fd0808STejun Heo  *
98435fd0808STejun Heo  * The upper 32bit indexes the following regions.
98535fd0808STejun Heo  *
98635fd0808STejun Heo  *	0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
98735fd0808STejun Heo  *
98835fd0808STejun Heo  * As both region arrays are sorted, the function advances the two indices
98935fd0808STejun Heo  * in lockstep and returns each intersection.
99035fd0808STejun Heo  */
99177649905SDavid Hildenbrand void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
992f1af9d3aSPhilipp Hachtmann 		      struct memblock_type *type_a,
99377649905SDavid Hildenbrand 		      struct memblock_type *type_b, phys_addr_t *out_start,
99435fd0808STejun Heo 		      phys_addr_t *out_end, int *out_nid)
99535fd0808STejun Heo {
996f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
997f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
998b1154233SGrygorii Strashko 
999f1af9d3aSPhilipp Hachtmann 	if (WARN_ONCE(nid == MAX_NUMNODES,
1000f1af9d3aSPhilipp Hachtmann 	"Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1001560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
100235fd0808STejun Heo 
1003f1af9d3aSPhilipp Hachtmann 	for (; idx_a < type_a->cnt; idx_a++) {
1004f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1005f1af9d3aSPhilipp Hachtmann 
100635fd0808STejun Heo 		phys_addr_t m_start = m->base;
100735fd0808STejun Heo 		phys_addr_t m_end = m->base + m->size;
1008f1af9d3aSPhilipp Hachtmann 		int	    m_nid = memblock_get_region_node(m);
100935fd0808STejun Heo 
10109f3d5eaaSMike Rapoport 		if (should_skip_region(type_a, m, nid, flags))
1011bf3d3cc5SArd Biesheuvel 			continue;
1012bf3d3cc5SArd Biesheuvel 
1013f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1014f1af9d3aSPhilipp Hachtmann 			if (out_start)
1015f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1016f1af9d3aSPhilipp Hachtmann 			if (out_end)
1017f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1018f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1019f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1020f1af9d3aSPhilipp Hachtmann 			idx_a++;
1021f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1022f1af9d3aSPhilipp Hachtmann 			return;
1023f1af9d3aSPhilipp Hachtmann 		}
102435fd0808STejun Heo 
1025f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1026f1af9d3aSPhilipp Hachtmann 		for (; idx_b < type_b->cnt + 1; idx_b++) {
1027f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1028f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1029f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1030f1af9d3aSPhilipp Hachtmann 
1031f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1032f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1033f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
10341c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1035f1af9d3aSPhilipp Hachtmann 
1036f1af9d3aSPhilipp Hachtmann 			/*
1037f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1038f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1039f1af9d3aSPhilipp Hachtmann 			 */
104035fd0808STejun Heo 			if (r_start >= m_end)
104135fd0808STejun Heo 				break;
104235fd0808STejun Heo 			/* if the two regions intersect, we're done */
104335fd0808STejun Heo 			if (m_start < r_end) {
104435fd0808STejun Heo 				if (out_start)
1045f1af9d3aSPhilipp Hachtmann 					*out_start =
1046f1af9d3aSPhilipp Hachtmann 						max(m_start, r_start);
104735fd0808STejun Heo 				if (out_end)
104835fd0808STejun Heo 					*out_end = min(m_end, r_end);
104935fd0808STejun Heo 				if (out_nid)
1050f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
105135fd0808STejun Heo 				/*
1052f1af9d3aSPhilipp Hachtmann 				 * The region which ends first is
1053f1af9d3aSPhilipp Hachtmann 				 * advanced for the next iteration.
105435fd0808STejun Heo 				 */
105535fd0808STejun Heo 				if (m_end <= r_end)
1056f1af9d3aSPhilipp Hachtmann 					idx_a++;
105735fd0808STejun Heo 				else
1058f1af9d3aSPhilipp Hachtmann 					idx_b++;
1059f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
106035fd0808STejun Heo 				return;
106135fd0808STejun Heo 			}
106235fd0808STejun Heo 		}
106335fd0808STejun Heo 	}
106435fd0808STejun Heo 
106535fd0808STejun Heo 	/* signal end of iteration */
106635fd0808STejun Heo 	*idx = ULLONG_MAX;
106735fd0808STejun Heo }
106835fd0808STejun Heo 
10697bd0b0f0STejun Heo /**
1070f1af9d3aSPhilipp Hachtmann  * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1071f1af9d3aSPhilipp Hachtmann  *
10727bd0b0f0STejun Heo  * @idx: pointer to u64 loop variable
1073ad5ea8cdSAlexander Kuleshov  * @nid: node selector, %NUMA_NO_NODE for all nodes
1074fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
1075f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
1076f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
1077dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1078dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1079dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
10807bd0b0f0STejun Heo  *
108147cec443SMike Rapoport  * Finds the next range from type_a which is not marked as unsuitable
108247cec443SMike Rapoport  * in type_b.
108347cec443SMike Rapoport  *
1084f1af9d3aSPhilipp Hachtmann  * Reverse of __next_mem_range().
10857bd0b0f0STejun Heo  */
1086e1720feeSMike Rapoport void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1087e1720feeSMike Rapoport 					  enum memblock_flags flags,
1088f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_a,
1089f1af9d3aSPhilipp Hachtmann 					  struct memblock_type *type_b,
10907bd0b0f0STejun Heo 					  phys_addr_t *out_start,
10917bd0b0f0STejun Heo 					  phys_addr_t *out_end, int *out_nid)
10927bd0b0f0STejun Heo {
1093f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
1094f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
1095b1154233SGrygorii Strashko 
1096560dca27SGrygorii Strashko 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1097560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
10987bd0b0f0STejun Heo 
10997bd0b0f0STejun Heo 	if (*idx == (u64)ULLONG_MAX) {
1100f1af9d3aSPhilipp Hachtmann 		idx_a = type_a->cnt - 1;
1101e47608abSzijun_hu 		if (type_b != NULL)
1102f1af9d3aSPhilipp Hachtmann 			idx_b = type_b->cnt;
1103e47608abSzijun_hu 		else
1104e47608abSzijun_hu 			idx_b = 0;
11057bd0b0f0STejun Heo 	}
11067bd0b0f0STejun Heo 
1107f1af9d3aSPhilipp Hachtmann 	for (; idx_a >= 0; idx_a--) {
1108f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
1109f1af9d3aSPhilipp Hachtmann 
11107bd0b0f0STejun Heo 		phys_addr_t m_start = m->base;
11117bd0b0f0STejun Heo 		phys_addr_t m_end = m->base + m->size;
1112f1af9d3aSPhilipp Hachtmann 		int m_nid = memblock_get_region_node(m);
11137bd0b0f0STejun Heo 
11149f3d5eaaSMike Rapoport 		if (should_skip_region(type_a, m, nid, flags))
1115bf3d3cc5SArd Biesheuvel 			continue;
1116bf3d3cc5SArd Biesheuvel 
1117f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1118f1af9d3aSPhilipp Hachtmann 			if (out_start)
1119f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1120f1af9d3aSPhilipp Hachtmann 			if (out_end)
1121f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1122f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1123f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1124fb399b48Szijun_hu 			idx_a--;
1125f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1126f1af9d3aSPhilipp Hachtmann 			return;
1127f1af9d3aSPhilipp Hachtmann 		}
11287bd0b0f0STejun Heo 
1129f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1130f1af9d3aSPhilipp Hachtmann 		for (; idx_b >= 0; idx_b--) {
1131f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1132f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1133f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1134f1af9d3aSPhilipp Hachtmann 
1135f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1136f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1137f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
11381c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1139f1af9d3aSPhilipp Hachtmann 			/*
1140f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1141f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1142f1af9d3aSPhilipp Hachtmann 			 */
1143f1af9d3aSPhilipp Hachtmann 
11447bd0b0f0STejun Heo 			if (r_end <= m_start)
11457bd0b0f0STejun Heo 				break;
11467bd0b0f0STejun Heo 			/* if the two regions intersect, we're done */
11477bd0b0f0STejun Heo 			if (m_end > r_start) {
11487bd0b0f0STejun Heo 				if (out_start)
11497bd0b0f0STejun Heo 					*out_start = max(m_start, r_start);
11507bd0b0f0STejun Heo 				if (out_end)
11517bd0b0f0STejun Heo 					*out_end = min(m_end, r_end);
11527bd0b0f0STejun Heo 				if (out_nid)
1153f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
11547bd0b0f0STejun Heo 				if (m_start >= r_start)
1155f1af9d3aSPhilipp Hachtmann 					idx_a--;
11567bd0b0f0STejun Heo 				else
1157f1af9d3aSPhilipp Hachtmann 					idx_b--;
1158f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
11597bd0b0f0STejun Heo 				return;
11607bd0b0f0STejun Heo 			}
11617bd0b0f0STejun Heo 		}
11627bd0b0f0STejun Heo 	}
1163f1af9d3aSPhilipp Hachtmann 	/* signal end of iteration */
11647bd0b0f0STejun Heo 	*idx = ULLONG_MAX;
11657bd0b0f0STejun Heo }
11667bd0b0f0STejun Heo 
11677c0caeb8STejun Heo /*
116845e79815SChen Chang  * Common iterator interface used to define for_each_mem_pfn_range().
11697c0caeb8STejun Heo  */
11707c0caeb8STejun Heo void __init_memblock __next_mem_pfn_range(int *idx, int nid,
11717c0caeb8STejun Heo 				unsigned long *out_start_pfn,
11727c0caeb8STejun Heo 				unsigned long *out_end_pfn, int *out_nid)
11737c0caeb8STejun Heo {
11747c0caeb8STejun Heo 	struct memblock_type *type = &memblock.memory;
11757c0caeb8STejun Heo 	struct memblock_region *r;
1176d622abf7SMike Rapoport 	int r_nid;
11777c0caeb8STejun Heo 
11787c0caeb8STejun Heo 	while (++*idx < type->cnt) {
11797c0caeb8STejun Heo 		r = &type->regions[*idx];
1180d622abf7SMike Rapoport 		r_nid = memblock_get_region_node(r);
11817c0caeb8STejun Heo 
11827c0caeb8STejun Heo 		if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
11837c0caeb8STejun Heo 			continue;
1184d622abf7SMike Rapoport 		if (nid == MAX_NUMNODES || nid == r_nid)
11857c0caeb8STejun Heo 			break;
11867c0caeb8STejun Heo 	}
11877c0caeb8STejun Heo 	if (*idx >= type->cnt) {
11887c0caeb8STejun Heo 		*idx = -1;
11897c0caeb8STejun Heo 		return;
11907c0caeb8STejun Heo 	}
11917c0caeb8STejun Heo 
11927c0caeb8STejun Heo 	if (out_start_pfn)
11937c0caeb8STejun Heo 		*out_start_pfn = PFN_UP(r->base);
11947c0caeb8STejun Heo 	if (out_end_pfn)
11957c0caeb8STejun Heo 		*out_end_pfn = PFN_DOWN(r->base + r->size);
11967c0caeb8STejun Heo 	if (out_nid)
1197d622abf7SMike Rapoport 		*out_nid = r_nid;
11987c0caeb8STejun Heo }
11997c0caeb8STejun Heo 
12007c0caeb8STejun Heo /**
12017c0caeb8STejun Heo  * memblock_set_node - set node ID on memblock regions
12027c0caeb8STejun Heo  * @base: base of area to set node ID for
12037c0caeb8STejun Heo  * @size: size of area to set node ID for
1204e7e8de59STang Chen  * @type: memblock type to set node ID for
12057c0caeb8STejun Heo  * @nid: node ID to set
12067c0caeb8STejun Heo  *
1207e7e8de59STang Chen  * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
12087c0caeb8STejun Heo  * Regions which cross the area boundaries are split as necessary.
12097c0caeb8STejun Heo  *
121047cec443SMike Rapoport  * Return:
12117c0caeb8STejun Heo  * 0 on success, -errno on failure.
12127c0caeb8STejun Heo  */
12137c0caeb8STejun Heo int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1214e7e8de59STang Chen 				      struct memblock_type *type, int nid)
12157c0caeb8STejun Heo {
1216a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA
12176a9ceb31STejun Heo 	int start_rgn, end_rgn;
12186a9ceb31STejun Heo 	int i, ret;
12197c0caeb8STejun Heo 
12206a9ceb31STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
12216a9ceb31STejun Heo 	if (ret)
12226a9ceb31STejun Heo 		return ret;
12237c0caeb8STejun Heo 
12246a9ceb31STejun Heo 	for (i = start_rgn; i < end_rgn; i++)
1225e9d24ad3SWanpeng Li 		memblock_set_region_node(&type->regions[i], nid);
12267c0caeb8STejun Heo 
12277c0caeb8STejun Heo 	memblock_merge_regions(type);
12283f08a302SMike Rapoport #endif
12297c0caeb8STejun Heo 	return 0;
12307c0caeb8STejun Heo }
12313f08a302SMike Rapoport 
1232837566e7SAlexander Duyck #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1233837566e7SAlexander Duyck /**
1234837566e7SAlexander Duyck  * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
1235837566e7SAlexander Duyck  *
1236837566e7SAlexander Duyck  * @idx: pointer to u64 loop variable
1237837566e7SAlexander Duyck  * @zone: zone in which all of the memory blocks reside
1238837566e7SAlexander Duyck  * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
1239837566e7SAlexander Duyck  * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
1240837566e7SAlexander Duyck  *
1241837566e7SAlexander Duyck  * This function is meant to be a zone/pfn specific wrapper for the
1242837566e7SAlexander Duyck  * for_each_mem_range type iterators. Specifically they are used in the
1243837566e7SAlexander Duyck  * deferred memory init routines and as such we were duplicating much of
1244837566e7SAlexander Duyck  * this logic throughout the code. So instead of having it in multiple
1245837566e7SAlexander Duyck  * locations it seemed like it would make more sense to centralize this to
1246837566e7SAlexander Duyck  * one new iterator that does everything they need.
1247837566e7SAlexander Duyck  */
1248837566e7SAlexander Duyck void __init_memblock
1249837566e7SAlexander Duyck __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
1250837566e7SAlexander Duyck 			     unsigned long *out_spfn, unsigned long *out_epfn)
1251837566e7SAlexander Duyck {
1252837566e7SAlexander Duyck 	int zone_nid = zone_to_nid(zone);
1253837566e7SAlexander Duyck 	phys_addr_t spa, epa;
1254837566e7SAlexander Duyck 	int nid;
1255837566e7SAlexander Duyck 
1256837566e7SAlexander Duyck 	__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1257837566e7SAlexander Duyck 			 &memblock.memory, &memblock.reserved,
1258837566e7SAlexander Duyck 			 &spa, &epa, &nid);
1259837566e7SAlexander Duyck 
1260837566e7SAlexander Duyck 	while (*idx != U64_MAX) {
1261837566e7SAlexander Duyck 		unsigned long epfn = PFN_DOWN(epa);
1262837566e7SAlexander Duyck 		unsigned long spfn = PFN_UP(spa);
1263837566e7SAlexander Duyck 
1264837566e7SAlexander Duyck 		/*
1265837566e7SAlexander Duyck 		 * Verify the end is at least past the start of the zone and
1266837566e7SAlexander Duyck 		 * that we have at least one PFN to initialize.
1267837566e7SAlexander Duyck 		 */
1268837566e7SAlexander Duyck 		if (zone->zone_start_pfn < epfn && spfn < epfn) {
1269837566e7SAlexander Duyck 			/* if we went too far just stop searching */
1270837566e7SAlexander Duyck 			if (zone_end_pfn(zone) <= spfn) {
1271837566e7SAlexander Duyck 				*idx = U64_MAX;
1272837566e7SAlexander Duyck 				break;
1273837566e7SAlexander Duyck 			}
1274837566e7SAlexander Duyck 
1275837566e7SAlexander Duyck 			if (out_spfn)
1276837566e7SAlexander Duyck 				*out_spfn = max(zone->zone_start_pfn, spfn);
1277837566e7SAlexander Duyck 			if (out_epfn)
1278837566e7SAlexander Duyck 				*out_epfn = min(zone_end_pfn(zone), epfn);
1279837566e7SAlexander Duyck 
1280837566e7SAlexander Duyck 			return;
1281837566e7SAlexander Duyck 		}
1282837566e7SAlexander Duyck 
1283837566e7SAlexander Duyck 		__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1284837566e7SAlexander Duyck 				 &memblock.memory, &memblock.reserved,
1285837566e7SAlexander Duyck 				 &spa, &epa, &nid);
1286837566e7SAlexander Duyck 	}
1287837566e7SAlexander Duyck 
1288837566e7SAlexander Duyck 	/* signal end of iteration */
1289837566e7SAlexander Duyck 	if (out_spfn)
1290837566e7SAlexander Duyck 		*out_spfn = ULONG_MAX;
1291837566e7SAlexander Duyck 	if (out_epfn)
1292837566e7SAlexander Duyck 		*out_epfn = 0;
1293837566e7SAlexander Duyck }
1294837566e7SAlexander Duyck 
1295837566e7SAlexander Duyck #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
12967c0caeb8STejun Heo 
129792d12f95SMike Rapoport /**
129892d12f95SMike Rapoport  * memblock_alloc_range_nid - allocate boot memory block
129992d12f95SMike Rapoport  * @size: size of memory block to be allocated in bytes
130092d12f95SMike Rapoport  * @align: alignment of the region and block's size
130192d12f95SMike Rapoport  * @start: the lower bound of the memory region to allocate (phys address)
130292d12f95SMike Rapoport  * @end: the upper bound of the memory region to allocate (phys address)
130392d12f95SMike Rapoport  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
13040ac398b1SYunfeng Ye  * @exact_nid: control the allocation fall back to other nodes
130592d12f95SMike Rapoport  *
130692d12f95SMike Rapoport  * The allocation is performed from memory region limited by
130795830666SCao jin  * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE.
130892d12f95SMike Rapoport  *
13090ac398b1SYunfeng Ye  * If the specified node can not hold the requested memory and @exact_nid
13100ac398b1SYunfeng Ye  * is false, the allocation falls back to any node in the system.
131192d12f95SMike Rapoport  *
131292d12f95SMike Rapoport  * For systems with memory mirroring, the allocation is attempted first
131392d12f95SMike Rapoport  * from the regions with mirroring enabled and then retried from any
131492d12f95SMike Rapoport  * memory region.
131592d12f95SMike Rapoport  *
131692d12f95SMike Rapoport  * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for
131792d12f95SMike Rapoport  * allocated boot memory block, so that it is never reported as leaks.
131892d12f95SMike Rapoport  *
131992d12f95SMike Rapoport  * Return:
132092d12f95SMike Rapoport  * Physical address of allocated memory block on success, %0 on failure.
132192d12f95SMike Rapoport  */
13228676af1fSAslan Bakirov phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
13232bfc2862SAkinobu Mita 					phys_addr_t align, phys_addr_t start,
13240ac398b1SYunfeng Ye 					phys_addr_t end, int nid,
13250ac398b1SYunfeng Ye 					bool exact_nid)
132695f72d1eSYinghai Lu {
132792d12f95SMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
13286ed311b2SBenjamin Herrenschmidt 	phys_addr_t found;
132995f72d1eSYinghai Lu 
133092d12f95SMike Rapoport 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
133192d12f95SMike Rapoport 		nid = NUMA_NO_NODE;
133292d12f95SMike Rapoport 
13332f770806SMike Rapoport 	if (!align) {
13342f770806SMike Rapoport 		/* Can't use WARNs this early in boot on powerpc */
13352f770806SMike Rapoport 		dump_stack();
13362f770806SMike Rapoport 		align = SMP_CACHE_BYTES;
13372f770806SMike Rapoport 	}
13382f770806SMike Rapoport 
133992d12f95SMike Rapoport again:
1340fc6daaf9STony Luck 	found = memblock_find_in_range_node(size, align, start, end, nid,
1341fc6daaf9STony Luck 					    flags);
134292d12f95SMike Rapoport 	if (found && !memblock_reserve(found, size))
134392d12f95SMike Rapoport 		goto done;
134492d12f95SMike Rapoport 
13450ac398b1SYunfeng Ye 	if (nid != NUMA_NO_NODE && !exact_nid) {
134692d12f95SMike Rapoport 		found = memblock_find_in_range_node(size, align, start,
134792d12f95SMike Rapoport 						    end, NUMA_NO_NODE,
134892d12f95SMike Rapoport 						    flags);
134992d12f95SMike Rapoport 		if (found && !memblock_reserve(found, size))
135092d12f95SMike Rapoport 			goto done;
135192d12f95SMike Rapoport 	}
135292d12f95SMike Rapoport 
135392d12f95SMike Rapoport 	if (flags & MEMBLOCK_MIRROR) {
135492d12f95SMike Rapoport 		flags &= ~MEMBLOCK_MIRROR;
135592d12f95SMike Rapoport 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
135692d12f95SMike Rapoport 			&size);
135792d12f95SMike Rapoport 		goto again;
135892d12f95SMike Rapoport 	}
135992d12f95SMike Rapoport 
136092d12f95SMike Rapoport 	return 0;
136192d12f95SMike Rapoport 
136292d12f95SMike Rapoport done:
136392d12f95SMike Rapoport 	/* Skip kmemleak for kasan_init() due to high volume. */
136492d12f95SMike Rapoport 	if (end != MEMBLOCK_ALLOC_KASAN)
1365aedf95eaSCatalin Marinas 		/*
136692d12f95SMike Rapoport 		 * The min_count is set to 0 so that memblock allocated
136792d12f95SMike Rapoport 		 * blocks are never reported as leaks. This is because many
136892d12f95SMike Rapoport 		 * of these blocks are only referred via the physical
136992d12f95SMike Rapoport 		 * address which is not looked up by kmemleak.
1370aedf95eaSCatalin Marinas 		 */
13719099daedSCatalin Marinas 		kmemleak_alloc_phys(found, size, 0, 0);
137292d12f95SMike Rapoport 
13736ed311b2SBenjamin Herrenschmidt 	return found;
1374aedf95eaSCatalin Marinas }
137595f72d1eSYinghai Lu 
1376a2974133SMike Rapoport /**
1377a2974133SMike Rapoport  * memblock_phys_alloc_range - allocate a memory block inside specified range
1378a2974133SMike Rapoport  * @size: size of memory block to be allocated in bytes
1379a2974133SMike Rapoport  * @align: alignment of the region and block's size
1380a2974133SMike Rapoport  * @start: the lower bound of the memory region to allocate (physical address)
1381a2974133SMike Rapoport  * @end: the upper bound of the memory region to allocate (physical address)
1382a2974133SMike Rapoport  *
1383a2974133SMike Rapoport  * Allocate @size bytes in the between @start and @end.
1384a2974133SMike Rapoport  *
1385a2974133SMike Rapoport  * Return: physical address of the allocated memory block on success,
1386a2974133SMike Rapoport  * %0 on failure.
1387a2974133SMike Rapoport  */
13888a770c2aSMike Rapoport phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
13898a770c2aSMike Rapoport 					     phys_addr_t align,
13908a770c2aSMike Rapoport 					     phys_addr_t start,
13918a770c2aSMike Rapoport 					     phys_addr_t end)
13922bfc2862SAkinobu Mita {
1393b5cf2d6cSFaiyaz Mohammed 	memblock_dbg("%s: %llu bytes align=0x%llx from=%pa max_addr=%pa %pS\n",
1394b5cf2d6cSFaiyaz Mohammed 		     __func__, (u64)size, (u64)align, &start, &end,
1395b5cf2d6cSFaiyaz Mohammed 		     (void *)_RET_IP_);
13960ac398b1SYunfeng Ye 	return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
13970ac398b1SYunfeng Ye 					false);
13987bd0b0f0STejun Heo }
13997bd0b0f0STejun Heo 
1400a2974133SMike Rapoport /**
140117cbe038SLevi Yun  * memblock_phys_alloc_try_nid - allocate a memory block from specified NUMA node
1402a2974133SMike Rapoport  * @size: size of memory block to be allocated in bytes
1403a2974133SMike Rapoport  * @align: alignment of the region and block's size
1404a2974133SMike Rapoport  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1405a2974133SMike Rapoport  *
1406a2974133SMike Rapoport  * Allocates memory block from the specified NUMA node. If the node
1407a2974133SMike Rapoport  * has no available memory, attempts to allocated from any node in the
1408a2974133SMike Rapoport  * system.
1409a2974133SMike Rapoport  *
1410a2974133SMike Rapoport  * Return: physical address of the allocated memory block on success,
1411a2974133SMike Rapoport  * %0 on failure.
1412a2974133SMike Rapoport  */
14139a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
14149d1e2492SBenjamin Herrenschmidt {
141533755574SMike Rapoport 	return memblock_alloc_range_nid(size, align, 0,
14160ac398b1SYunfeng Ye 					MEMBLOCK_ALLOC_ACCESSIBLE, nid, false);
141795f72d1eSYinghai Lu }
141895f72d1eSYinghai Lu 
141926f09e9bSSantosh Shilimkar /**
1420eb31d559SMike Rapoport  * memblock_alloc_internal - allocate boot memory block
142126f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
142226f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
142326f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region to allocate (phys address)
142426f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region to allocate (phys address)
142526f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
14260ac398b1SYunfeng Ye  * @exact_nid: control the allocation fall back to other nodes
142726f09e9bSSantosh Shilimkar  *
142892d12f95SMike Rapoport  * Allocates memory block using memblock_alloc_range_nid() and
142992d12f95SMike Rapoport  * converts the returned physical address to virtual.
143092d12f95SMike Rapoport  *
143126f09e9bSSantosh Shilimkar  * The @min_addr limit is dropped if it can not be satisfied and the allocation
143292d12f95SMike Rapoport  * will fall back to memory below @min_addr. Other constraints, such
143392d12f95SMike Rapoport  * as node and mirrored memory will be handled again in
143492d12f95SMike Rapoport  * memblock_alloc_range_nid().
143526f09e9bSSantosh Shilimkar  *
143647cec443SMike Rapoport  * Return:
143726f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
143826f09e9bSSantosh Shilimkar  */
1439eb31d559SMike Rapoport static void * __init memblock_alloc_internal(
144026f09e9bSSantosh Shilimkar 				phys_addr_t size, phys_addr_t align,
144126f09e9bSSantosh Shilimkar 				phys_addr_t min_addr, phys_addr_t max_addr,
14420ac398b1SYunfeng Ye 				int nid, bool exact_nid)
144326f09e9bSSantosh Shilimkar {
144426f09e9bSSantosh Shilimkar 	phys_addr_t alloc;
144526f09e9bSSantosh Shilimkar 
144626f09e9bSSantosh Shilimkar 	/*
144726f09e9bSSantosh Shilimkar 	 * Detect any accidental use of these APIs after slab is ready, as at
144826f09e9bSSantosh Shilimkar 	 * this moment memblock may be deinitialized already and its
1449c6ffc5caSMike Rapoport 	 * internal data may be destroyed (after execution of memblock_free_all)
145026f09e9bSSantosh Shilimkar 	 */
145126f09e9bSSantosh Shilimkar 	if (WARN_ON_ONCE(slab_is_available()))
145226f09e9bSSantosh Shilimkar 		return kzalloc_node(size, GFP_NOWAIT, nid);
145326f09e9bSSantosh Shilimkar 
1454f3057ad7SMike Rapoport 	if (max_addr > memblock.current_limit)
1455f3057ad7SMike Rapoport 		max_addr = memblock.current_limit;
1456f3057ad7SMike Rapoport 
14570ac398b1SYunfeng Ye 	alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid,
14580ac398b1SYunfeng Ye 					exact_nid);
14592f770806SMike Rapoport 
146092d12f95SMike Rapoport 	/* retry allocation without lower limit */
146192d12f95SMike Rapoport 	if (!alloc && min_addr)
14620ac398b1SYunfeng Ye 		alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid,
14630ac398b1SYunfeng Ye 						exact_nid);
146426f09e9bSSantosh Shilimkar 
146592d12f95SMike Rapoport 	if (!alloc)
1466a3f5bafcSTony Luck 		return NULL;
146726f09e9bSSantosh Shilimkar 
146892d12f95SMike Rapoport 	return phys_to_virt(alloc);
146926f09e9bSSantosh Shilimkar }
147026f09e9bSSantosh Shilimkar 
147126f09e9bSSantosh Shilimkar /**
14720ac398b1SYunfeng Ye  * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node
14730ac398b1SYunfeng Ye  * without zeroing memory
14740ac398b1SYunfeng Ye  * @size: size of memory block to be allocated in bytes
14750ac398b1SYunfeng Ye  * @align: alignment of the region and block's size
14760ac398b1SYunfeng Ye  * @min_addr: the lower bound of the memory region from where the allocation
14770ac398b1SYunfeng Ye  *	  is preferred (phys address)
14780ac398b1SYunfeng Ye  * @max_addr: the upper bound of the memory region from where the allocation
14790ac398b1SYunfeng Ye  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
14800ac398b1SYunfeng Ye  *	      allocate only from memory limited by memblock.current_limit value
14810ac398b1SYunfeng Ye  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
14820ac398b1SYunfeng Ye  *
14830ac398b1SYunfeng Ye  * Public function, provides additional debug information (including caller
14840ac398b1SYunfeng Ye  * info), if enabled. Does not zero allocated memory.
14850ac398b1SYunfeng Ye  *
14860ac398b1SYunfeng Ye  * Return:
14870ac398b1SYunfeng Ye  * Virtual address of allocated memory block on success, NULL on failure.
14880ac398b1SYunfeng Ye  */
14890ac398b1SYunfeng Ye void * __init memblock_alloc_exact_nid_raw(
14900ac398b1SYunfeng Ye 			phys_addr_t size, phys_addr_t align,
14910ac398b1SYunfeng Ye 			phys_addr_t min_addr, phys_addr_t max_addr,
14920ac398b1SYunfeng Ye 			int nid)
14930ac398b1SYunfeng Ye {
14940ac398b1SYunfeng Ye 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
14950ac398b1SYunfeng Ye 		     __func__, (u64)size, (u64)align, nid, &min_addr,
14960ac398b1SYunfeng Ye 		     &max_addr, (void *)_RET_IP_);
14970ac398b1SYunfeng Ye 
149808678804SMike Rapoport 	return memblock_alloc_internal(size, align, min_addr, max_addr, nid,
149908678804SMike Rapoport 				       true);
15000ac398b1SYunfeng Ye }
15010ac398b1SYunfeng Ye 
15020ac398b1SYunfeng Ye /**
1503eb31d559SMike Rapoport  * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
1504ea1f5f37SPavel Tatashin  * memory and without panicking
1505ea1f5f37SPavel Tatashin  * @size: size of memory block to be allocated in bytes
1506ea1f5f37SPavel Tatashin  * @align: alignment of the region and block's size
1507ea1f5f37SPavel Tatashin  * @min_addr: the lower bound of the memory region from where the allocation
1508ea1f5f37SPavel Tatashin  *	  is preferred (phys address)
1509ea1f5f37SPavel Tatashin  * @max_addr: the upper bound of the memory region from where the allocation
151097ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1511ea1f5f37SPavel Tatashin  *	      allocate only from memory limited by memblock.current_limit value
1512ea1f5f37SPavel Tatashin  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1513ea1f5f37SPavel Tatashin  *
1514ea1f5f37SPavel Tatashin  * Public function, provides additional debug information (including caller
1515ea1f5f37SPavel Tatashin  * info), if enabled. Does not zero allocated memory, does not panic if request
1516ea1f5f37SPavel Tatashin  * cannot be satisfied.
1517ea1f5f37SPavel Tatashin  *
151847cec443SMike Rapoport  * Return:
1519ea1f5f37SPavel Tatashin  * Virtual address of allocated memory block on success, NULL on failure.
1520ea1f5f37SPavel Tatashin  */
1521eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_raw(
1522ea1f5f37SPavel Tatashin 			phys_addr_t size, phys_addr_t align,
1523ea1f5f37SPavel Tatashin 			phys_addr_t min_addr, phys_addr_t max_addr,
1524ea1f5f37SPavel Tatashin 			int nid)
1525ea1f5f37SPavel Tatashin {
1526d75f773cSSakari Ailus 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1527a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1528a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1529ea1f5f37SPavel Tatashin 
153008678804SMike Rapoport 	return memblock_alloc_internal(size, align, min_addr, max_addr, nid,
153108678804SMike Rapoport 				       false);
1532ea1f5f37SPavel Tatashin }
1533ea1f5f37SPavel Tatashin 
1534ea1f5f37SPavel Tatashin /**
1535c0dbe825SMike Rapoport  * memblock_alloc_try_nid - allocate boot memory block
153626f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
153726f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
153826f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region from where the allocation
153926f09e9bSSantosh Shilimkar  *	  is preferred (phys address)
154026f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region from where the allocation
154197ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
154226f09e9bSSantosh Shilimkar  *	      allocate only from memory limited by memblock.current_limit value
154326f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
154426f09e9bSSantosh Shilimkar  *
1545c0dbe825SMike Rapoport  * Public function, provides additional debug information (including caller
1546c0dbe825SMike Rapoport  * info), if enabled. This function zeroes the allocated memory.
154726f09e9bSSantosh Shilimkar  *
154847cec443SMike Rapoport  * Return:
154926f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
155026f09e9bSSantosh Shilimkar  */
1551eb31d559SMike Rapoport void * __init memblock_alloc_try_nid(
155226f09e9bSSantosh Shilimkar 			phys_addr_t size, phys_addr_t align,
155326f09e9bSSantosh Shilimkar 			phys_addr_t min_addr, phys_addr_t max_addr,
155426f09e9bSSantosh Shilimkar 			int nid)
155526f09e9bSSantosh Shilimkar {
155626f09e9bSSantosh Shilimkar 	void *ptr;
155726f09e9bSSantosh Shilimkar 
1558d75f773cSSakari Ailus 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1559a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1560a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1561eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
15620ac398b1SYunfeng Ye 					   min_addr, max_addr, nid, false);
1563c0dbe825SMike Rapoport 	if (ptr)
1564ea1f5f37SPavel Tatashin 		memset(ptr, 0, size);
156526f09e9bSSantosh Shilimkar 
1566c0dbe825SMike Rapoport 	return ptr;
156726f09e9bSSantosh Shilimkar }
156826f09e9bSSantosh Shilimkar 
156926f09e9bSSantosh Shilimkar /**
1570a2974133SMike Rapoport  * __memblock_free_late - free pages directly to buddy allocator
157148a833ccSMike Rapoport  * @base: phys starting address of the  boot memory block
157226f09e9bSSantosh Shilimkar  * @size: size of the boot memory block in bytes
157326f09e9bSSantosh Shilimkar  *
1574a2974133SMike Rapoport  * This is only useful when the memblock allocator has already been torn
157526f09e9bSSantosh Shilimkar  * down, but we are still initializing the system.  Pages are released directly
1576a2974133SMike Rapoport  * to the buddy allocator.
157726f09e9bSSantosh Shilimkar  */
157826f09e9bSSantosh Shilimkar void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
157926f09e9bSSantosh Shilimkar {
1580a36aab89SMike Rapoport 	phys_addr_t cursor, end;
158126f09e9bSSantosh Shilimkar 
1582a36aab89SMike Rapoport 	end = base + size - 1;
1583d75f773cSSakari Ailus 	memblock_dbg("%s: [%pa-%pa] %pS\n",
1584a36aab89SMike Rapoport 		     __func__, &base, &end, (void *)_RET_IP_);
15859099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
158626f09e9bSSantosh Shilimkar 	cursor = PFN_UP(base);
158726f09e9bSSantosh Shilimkar 	end = PFN_DOWN(base + size);
158826f09e9bSSantosh Shilimkar 
158926f09e9bSSantosh Shilimkar 	for (; cursor < end; cursor++) {
15907c2ee349SMike Rapoport 		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
1591ca79b0c2SArun KS 		totalram_pages_inc();
159226f09e9bSSantosh Shilimkar 	}
159326f09e9bSSantosh Shilimkar }
15949d1e2492SBenjamin Herrenschmidt 
15959d1e2492SBenjamin Herrenschmidt /*
15969d1e2492SBenjamin Herrenschmidt  * Remaining API functions
15979d1e2492SBenjamin Herrenschmidt  */
15989d1e2492SBenjamin Herrenschmidt 
15991f1ffb8aSDavid Gibson phys_addr_t __init_memblock memblock_phys_mem_size(void)
160095f72d1eSYinghai Lu {
16011440c4e2STejun Heo 	return memblock.memory.total_size;
160295f72d1eSYinghai Lu }
160395f72d1eSYinghai Lu 
16048907de5dSSrikar Dronamraju phys_addr_t __init_memblock memblock_reserved_size(void)
16058907de5dSSrikar Dronamraju {
16068907de5dSSrikar Dronamraju 	return memblock.reserved.total_size;
16078907de5dSSrikar Dronamraju }
16088907de5dSSrikar Dronamraju 
16090a93ebefSSam Ravnborg /* lowest address */
16100a93ebefSSam Ravnborg phys_addr_t __init_memblock memblock_start_of_DRAM(void)
16110a93ebefSSam Ravnborg {
16120a93ebefSSam Ravnborg 	return memblock.memory.regions[0].base;
16130a93ebefSSam Ravnborg }
16140a93ebefSSam Ravnborg 
161510d06439SYinghai Lu phys_addr_t __init_memblock memblock_end_of_DRAM(void)
161695f72d1eSYinghai Lu {
161795f72d1eSYinghai Lu 	int idx = memblock.memory.cnt - 1;
161895f72d1eSYinghai Lu 
1619e3239ff9SBenjamin Herrenschmidt 	return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
162095f72d1eSYinghai Lu }
162195f72d1eSYinghai Lu 
1622a571d4ebSDennis Chen static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
162395f72d1eSYinghai Lu {
16241c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1625136199f0SEmil Medve 	struct memblock_region *r;
162695f72d1eSYinghai Lu 
1627a571d4ebSDennis Chen 	/*
1628a571d4ebSDennis Chen 	 * translate the memory @limit size into the max address within one of
1629a571d4ebSDennis Chen 	 * the memory memblock regions, if the @limit exceeds the total size
16301c4bc43dSStefan Agner 	 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
1631a571d4ebSDennis Chen 	 */
1632cc6de168SMike Rapoport 	for_each_mem_region(r) {
1633c0ce8fefSTejun Heo 		if (limit <= r->size) {
1634c0ce8fefSTejun Heo 			max_addr = r->base + limit;
163595f72d1eSYinghai Lu 			break;
163695f72d1eSYinghai Lu 		}
1637c0ce8fefSTejun Heo 		limit -= r->size;
163895f72d1eSYinghai Lu 	}
1639c0ce8fefSTejun Heo 
1640a571d4ebSDennis Chen 	return max_addr;
1641a571d4ebSDennis Chen }
1642a571d4ebSDennis Chen 
1643a571d4ebSDennis Chen void __init memblock_enforce_memory_limit(phys_addr_t limit)
1644a571d4ebSDennis Chen {
164549aef717SColin Ian King 	phys_addr_t max_addr;
1646a571d4ebSDennis Chen 
1647a571d4ebSDennis Chen 	if (!limit)
1648a571d4ebSDennis Chen 		return;
1649a571d4ebSDennis Chen 
1650a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1651a571d4ebSDennis Chen 
1652a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
16531c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1654a571d4ebSDennis Chen 		return;
1655a571d4ebSDennis Chen 
1656c0ce8fefSTejun Heo 	/* truncate both memory and reserved regions */
1657f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.memory, max_addr,
16581c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
1659f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.reserved, max_addr,
16601c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
166195f72d1eSYinghai Lu }
166295f72d1eSYinghai Lu 
1663c9ca9b4eSAKASHI Takahiro void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1664c9ca9b4eSAKASHI Takahiro {
1665c9ca9b4eSAKASHI Takahiro 	int start_rgn, end_rgn;
1666c9ca9b4eSAKASHI Takahiro 	int i, ret;
1667c9ca9b4eSAKASHI Takahiro 
1668c9ca9b4eSAKASHI Takahiro 	if (!size)
1669c9ca9b4eSAKASHI Takahiro 		return;
1670c9ca9b4eSAKASHI Takahiro 
1671c9ca9b4eSAKASHI Takahiro 	ret = memblock_isolate_range(&memblock.memory, base, size,
1672c9ca9b4eSAKASHI Takahiro 						&start_rgn, &end_rgn);
1673c9ca9b4eSAKASHI Takahiro 	if (ret)
1674c9ca9b4eSAKASHI Takahiro 		return;
1675c9ca9b4eSAKASHI Takahiro 
1676c9ca9b4eSAKASHI Takahiro 	/* remove all the MAP regions */
1677c9ca9b4eSAKASHI Takahiro 	for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1678c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1679c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1680c9ca9b4eSAKASHI Takahiro 
1681c9ca9b4eSAKASHI Takahiro 	for (i = start_rgn - 1; i >= 0; i--)
1682c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1683c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1684c9ca9b4eSAKASHI Takahiro 
1685c9ca9b4eSAKASHI Takahiro 	/* truncate the reserved regions */
1686c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved, 0, base);
1687c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved,
16881c4bc43dSStefan Agner 			base + size, PHYS_ADDR_MAX);
1689c9ca9b4eSAKASHI Takahiro }
1690c9ca9b4eSAKASHI Takahiro 
1691a571d4ebSDennis Chen void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1692a571d4ebSDennis Chen {
1693a571d4ebSDennis Chen 	phys_addr_t max_addr;
1694a571d4ebSDennis Chen 
1695a571d4ebSDennis Chen 	if (!limit)
1696a571d4ebSDennis Chen 		return;
1697a571d4ebSDennis Chen 
1698a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1699a571d4ebSDennis Chen 
1700a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
17011c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1702a571d4ebSDennis Chen 		return;
1703a571d4ebSDennis Chen 
1704c9ca9b4eSAKASHI Takahiro 	memblock_cap_memory_range(0, max_addr);
1705a571d4ebSDennis Chen }
1706a571d4ebSDennis Chen 
1707cd79481dSYinghai Lu static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
170872d4b0b4SBenjamin Herrenschmidt {
170972d4b0b4SBenjamin Herrenschmidt 	unsigned int left = 0, right = type->cnt;
171072d4b0b4SBenjamin Herrenschmidt 
171172d4b0b4SBenjamin Herrenschmidt 	do {
171272d4b0b4SBenjamin Herrenschmidt 		unsigned int mid = (right + left) / 2;
171372d4b0b4SBenjamin Herrenschmidt 
171472d4b0b4SBenjamin Herrenschmidt 		if (addr < type->regions[mid].base)
171572d4b0b4SBenjamin Herrenschmidt 			right = mid;
171672d4b0b4SBenjamin Herrenschmidt 		else if (addr >= (type->regions[mid].base +
171772d4b0b4SBenjamin Herrenschmidt 				  type->regions[mid].size))
171872d4b0b4SBenjamin Herrenschmidt 			left = mid + 1;
171972d4b0b4SBenjamin Herrenschmidt 		else
172072d4b0b4SBenjamin Herrenschmidt 			return mid;
172172d4b0b4SBenjamin Herrenschmidt 	} while (left < right);
172272d4b0b4SBenjamin Herrenschmidt 	return -1;
172372d4b0b4SBenjamin Herrenschmidt }
172472d4b0b4SBenjamin Herrenschmidt 
1725f5a222dcSYueyi Li bool __init_memblock memblock_is_reserved(phys_addr_t addr)
172695f72d1eSYinghai Lu {
172772d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.reserved, addr) != -1;
172895f72d1eSYinghai Lu }
172972d4b0b4SBenjamin Herrenschmidt 
1730b4ad0c7eSYaowei Bai bool __init_memblock memblock_is_memory(phys_addr_t addr)
173172d4b0b4SBenjamin Herrenschmidt {
173272d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.memory, addr) != -1;
173372d4b0b4SBenjamin Herrenschmidt }
173472d4b0b4SBenjamin Herrenschmidt 
1735937f0c26SYaowei Bai bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
1736bf3d3cc5SArd Biesheuvel {
1737bf3d3cc5SArd Biesheuvel 	int i = memblock_search(&memblock.memory, addr);
1738bf3d3cc5SArd Biesheuvel 
1739bf3d3cc5SArd Biesheuvel 	if (i == -1)
1740bf3d3cc5SArd Biesheuvel 		return false;
1741bf3d3cc5SArd Biesheuvel 	return !memblock_is_nomap(&memblock.memory.regions[i]);
1742bf3d3cc5SArd Biesheuvel }
1743bf3d3cc5SArd Biesheuvel 
1744e76b63f8SYinghai Lu int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1745e76b63f8SYinghai Lu 			 unsigned long *start_pfn, unsigned long *end_pfn)
1746e76b63f8SYinghai Lu {
1747e76b63f8SYinghai Lu 	struct memblock_type *type = &memblock.memory;
174816763230SFabian Frederick 	int mid = memblock_search(type, PFN_PHYS(pfn));
1749e76b63f8SYinghai Lu 
1750e76b63f8SYinghai Lu 	if (mid == -1)
1751e76b63f8SYinghai Lu 		return -1;
1752e76b63f8SYinghai Lu 
1753f7e2f7e8SFabian Frederick 	*start_pfn = PFN_DOWN(type->regions[mid].base);
1754f7e2f7e8SFabian Frederick 	*end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
1755e76b63f8SYinghai Lu 
1756d622abf7SMike Rapoport 	return memblock_get_region_node(&type->regions[mid]);
1757e76b63f8SYinghai Lu }
1758e76b63f8SYinghai Lu 
1759eab30949SStephen Boyd /**
1760eab30949SStephen Boyd  * memblock_is_region_memory - check if a region is a subset of memory
1761eab30949SStephen Boyd  * @base: base of region to check
1762eab30949SStephen Boyd  * @size: size of region to check
1763eab30949SStephen Boyd  *
1764eab30949SStephen Boyd  * Check if the region [@base, @base + @size) is a subset of a memory block.
1765eab30949SStephen Boyd  *
176647cec443SMike Rapoport  * Return:
1767eab30949SStephen Boyd  * 0 if false, non-zero if true
1768eab30949SStephen Boyd  */
1769937f0c26SYaowei Bai bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
177072d4b0b4SBenjamin Herrenschmidt {
1771abb65272STomi Valkeinen 	int idx = memblock_search(&memblock.memory, base);
1772eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
177372d4b0b4SBenjamin Herrenschmidt 
177472d4b0b4SBenjamin Herrenschmidt 	if (idx == -1)
1775937f0c26SYaowei Bai 		return false;
1776ef415ef4SWei Yang 	return (memblock.memory.regions[idx].base +
1777eb18f1b5STejun Heo 		 memblock.memory.regions[idx].size) >= end;
177895f72d1eSYinghai Lu }
177995f72d1eSYinghai Lu 
1780eab30949SStephen Boyd /**
1781eab30949SStephen Boyd  * memblock_is_region_reserved - check if a region intersects reserved memory
1782eab30949SStephen Boyd  * @base: base of region to check
1783eab30949SStephen Boyd  * @size: size of region to check
1784eab30949SStephen Boyd  *
178547cec443SMike Rapoport  * Check if the region [@base, @base + @size) intersects a reserved
178647cec443SMike Rapoport  * memory block.
1787eab30949SStephen Boyd  *
178847cec443SMike Rapoport  * Return:
1789c5c5c9d1STang Chen  * True if they intersect, false if not.
1790eab30949SStephen Boyd  */
1791c5c5c9d1STang Chen bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
179295f72d1eSYinghai Lu {
1793c5c5c9d1STang Chen 	return memblock_overlaps_region(&memblock.reserved, base, size);
179495f72d1eSYinghai Lu }
179595f72d1eSYinghai Lu 
17966ede1fd3SYinghai Lu void __init_memblock memblock_trim_memory(phys_addr_t align)
17976ede1fd3SYinghai Lu {
17986ede1fd3SYinghai Lu 	phys_addr_t start, end, orig_start, orig_end;
1799136199f0SEmil Medve 	struct memblock_region *r;
18006ede1fd3SYinghai Lu 
1801cc6de168SMike Rapoport 	for_each_mem_region(r) {
1802136199f0SEmil Medve 		orig_start = r->base;
1803136199f0SEmil Medve 		orig_end = r->base + r->size;
18046ede1fd3SYinghai Lu 		start = round_up(orig_start, align);
18056ede1fd3SYinghai Lu 		end = round_down(orig_end, align);
18066ede1fd3SYinghai Lu 
18076ede1fd3SYinghai Lu 		if (start == orig_start && end == orig_end)
18086ede1fd3SYinghai Lu 			continue;
18096ede1fd3SYinghai Lu 
18106ede1fd3SYinghai Lu 		if (start < end) {
1811136199f0SEmil Medve 			r->base = start;
1812136199f0SEmil Medve 			r->size = end - start;
18136ede1fd3SYinghai Lu 		} else {
1814136199f0SEmil Medve 			memblock_remove_region(&memblock.memory,
1815136199f0SEmil Medve 					       r - memblock.memory.regions);
1816136199f0SEmil Medve 			r--;
18176ede1fd3SYinghai Lu 		}
18186ede1fd3SYinghai Lu 	}
18196ede1fd3SYinghai Lu }
1820e63075a3SBenjamin Herrenschmidt 
18213661ca66SYinghai Lu void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1822e63075a3SBenjamin Herrenschmidt {
1823e63075a3SBenjamin Herrenschmidt 	memblock.current_limit = limit;
1824e63075a3SBenjamin Herrenschmidt }
1825e63075a3SBenjamin Herrenschmidt 
1826fec51014SLaura Abbott phys_addr_t __init_memblock memblock_get_current_limit(void)
1827fec51014SLaura Abbott {
1828fec51014SLaura Abbott 	return memblock.current_limit;
1829fec51014SLaura Abbott }
1830fec51014SLaura Abbott 
18310262d9c8SHeiko Carstens static void __init_memblock memblock_dump(struct memblock_type *type)
18326ed311b2SBenjamin Herrenschmidt {
18335d63f81cSMiles Chen 	phys_addr_t base, end, size;
1834e1720feeSMike Rapoport 	enum memblock_flags flags;
18358c9c1701SAlexander Kuleshov 	int idx;
18368c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
18376ed311b2SBenjamin Herrenschmidt 
18380262d9c8SHeiko Carstens 	pr_info(" %s.cnt  = 0x%lx\n", type->name, type->cnt);
18396ed311b2SBenjamin Herrenschmidt 
184066e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
18417c0caeb8STejun Heo 		char nid_buf[32] = "";
18426ed311b2SBenjamin Herrenschmidt 
18437c0caeb8STejun Heo 		base = rgn->base;
18447c0caeb8STejun Heo 		size = rgn->size;
18455d63f81cSMiles Chen 		end = base + size - 1;
184666a20757STang Chen 		flags = rgn->flags;
1847a9ee6cf5SMike Rapoport #ifdef CONFIG_NUMA
18487c0caeb8STejun Heo 		if (memblock_get_region_node(rgn) != MAX_NUMNODES)
18497c0caeb8STejun Heo 			snprintf(nid_buf, sizeof(nid_buf), " on node %d",
18507c0caeb8STejun Heo 				 memblock_get_region_node(rgn));
18517c0caeb8STejun Heo #endif
1852e1720feeSMike Rapoport 		pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
18530262d9c8SHeiko Carstens 			type->name, idx, &base, &end, &size, nid_buf, flags);
18546ed311b2SBenjamin Herrenschmidt 	}
18556ed311b2SBenjamin Herrenschmidt }
18566ed311b2SBenjamin Herrenschmidt 
185787c55870SMike Rapoport static void __init_memblock __memblock_dump_all(void)
18586ed311b2SBenjamin Herrenschmidt {
18596ed311b2SBenjamin Herrenschmidt 	pr_info("MEMBLOCK configuration:\n");
18605d63f81cSMiles Chen 	pr_info(" memory size = %pa reserved size = %pa\n",
18615d63f81cSMiles Chen 		&memblock.memory.total_size,
18625d63f81cSMiles Chen 		&memblock.reserved.total_size);
18636ed311b2SBenjamin Herrenschmidt 
18640262d9c8SHeiko Carstens 	memblock_dump(&memblock.memory);
18650262d9c8SHeiko Carstens 	memblock_dump(&memblock.reserved);
1866409efd4cSHeiko Carstens #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
186777649905SDavid Hildenbrand 	memblock_dump(&physmem);
1868409efd4cSHeiko Carstens #endif
18696ed311b2SBenjamin Herrenschmidt }
18706ed311b2SBenjamin Herrenschmidt 
187187c55870SMike Rapoport void __init_memblock memblock_dump_all(void)
187287c55870SMike Rapoport {
187387c55870SMike Rapoport 	if (memblock_debug)
187487c55870SMike Rapoport 		__memblock_dump_all();
187587c55870SMike Rapoport }
187687c55870SMike Rapoport 
18771aadc056STejun Heo void __init memblock_allow_resize(void)
18786ed311b2SBenjamin Herrenschmidt {
1879142b45a7SBenjamin Herrenschmidt 	memblock_can_resize = 1;
18806ed311b2SBenjamin Herrenschmidt }
18816ed311b2SBenjamin Herrenschmidt 
18826ed311b2SBenjamin Herrenschmidt static int __init early_memblock(char *p)
18836ed311b2SBenjamin Herrenschmidt {
18846ed311b2SBenjamin Herrenschmidt 	if (p && strstr(p, "debug"))
18856ed311b2SBenjamin Herrenschmidt 		memblock_debug = 1;
18866ed311b2SBenjamin Herrenschmidt 	return 0;
18876ed311b2SBenjamin Herrenschmidt }
18886ed311b2SBenjamin Herrenschmidt early_param("memblock", early_memblock);
18896ed311b2SBenjamin Herrenschmidt 
18904f5b0c17SMike Rapoport static void __init free_memmap(unsigned long start_pfn, unsigned long end_pfn)
18914f5b0c17SMike Rapoport {
18924f5b0c17SMike Rapoport 	struct page *start_pg, *end_pg;
18934f5b0c17SMike Rapoport 	phys_addr_t pg, pgend;
18944f5b0c17SMike Rapoport 
18954f5b0c17SMike Rapoport 	/*
18964f5b0c17SMike Rapoport 	 * Convert start_pfn/end_pfn to a struct page pointer.
18974f5b0c17SMike Rapoport 	 */
18984f5b0c17SMike Rapoport 	start_pg = pfn_to_page(start_pfn - 1) + 1;
18994f5b0c17SMike Rapoport 	end_pg = pfn_to_page(end_pfn - 1) + 1;
19004f5b0c17SMike Rapoport 
19014f5b0c17SMike Rapoport 	/*
19024f5b0c17SMike Rapoport 	 * Convert to physical addresses, and round start upwards and end
19034f5b0c17SMike Rapoport 	 * downwards.
19044f5b0c17SMike Rapoport 	 */
19054f5b0c17SMike Rapoport 	pg = PAGE_ALIGN(__pa(start_pg));
19064f5b0c17SMike Rapoport 	pgend = __pa(end_pg) & PAGE_MASK;
19074f5b0c17SMike Rapoport 
19084f5b0c17SMike Rapoport 	/*
19094f5b0c17SMike Rapoport 	 * If there are free pages between these, free the section of the
19104f5b0c17SMike Rapoport 	 * memmap array.
19114f5b0c17SMike Rapoport 	 */
19124f5b0c17SMike Rapoport 	if (pg < pgend)
19134f5b0c17SMike Rapoport 		memblock_free(pg, pgend - pg);
19144f5b0c17SMike Rapoport }
19154f5b0c17SMike Rapoport 
19164f5b0c17SMike Rapoport /*
19174f5b0c17SMike Rapoport  * The mem_map array can get very big.  Free the unused area of the memory map.
19184f5b0c17SMike Rapoport  */
19194f5b0c17SMike Rapoport static void __init free_unused_memmap(void)
19204f5b0c17SMike Rapoport {
19214f5b0c17SMike Rapoport 	unsigned long start, end, prev_end = 0;
19224f5b0c17SMike Rapoport 	int i;
19234f5b0c17SMike Rapoport 
19244f5b0c17SMike Rapoport 	if (!IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) ||
19254f5b0c17SMike Rapoport 	    IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
19264f5b0c17SMike Rapoport 		return;
19274f5b0c17SMike Rapoport 
19284f5b0c17SMike Rapoport 	/*
19294f5b0c17SMike Rapoport 	 * This relies on each bank being in address order.
19304f5b0c17SMike Rapoport 	 * The banks are sorted previously in bootmem_init().
19314f5b0c17SMike Rapoport 	 */
19324f5b0c17SMike Rapoport 	for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
19334f5b0c17SMike Rapoport #ifdef CONFIG_SPARSEMEM
19344f5b0c17SMike Rapoport 		/*
19354f5b0c17SMike Rapoport 		 * Take care not to free memmap entries that don't exist
19364f5b0c17SMike Rapoport 		 * due to SPARSEMEM sections which aren't present.
19374f5b0c17SMike Rapoport 		 */
19384f5b0c17SMike Rapoport 		start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
19394f5b0c17SMike Rapoport #endif
19404f5b0c17SMike Rapoport 		/*
1941e2a86800SMike Rapoport 		 * Align down here since many operations in VM subsystem
1942e2a86800SMike Rapoport 		 * presume that there are no holes in the memory map inside
1943e2a86800SMike Rapoport 		 * a pageblock
19444f5b0c17SMike Rapoport 		 */
1945e2a86800SMike Rapoport 		start = round_down(start, pageblock_nr_pages);
19464f5b0c17SMike Rapoport 
19474f5b0c17SMike Rapoport 		/*
19484f5b0c17SMike Rapoport 		 * If we had a previous bank, and there is a space
19494f5b0c17SMike Rapoport 		 * between the current bank and the previous, free it.
19504f5b0c17SMike Rapoport 		 */
19514f5b0c17SMike Rapoport 		if (prev_end && prev_end < start)
19524f5b0c17SMike Rapoport 			free_memmap(prev_end, start);
19534f5b0c17SMike Rapoport 
19544f5b0c17SMike Rapoport 		/*
1955e2a86800SMike Rapoport 		 * Align up here since many operations in VM subsystem
1956e2a86800SMike Rapoport 		 * presume that there are no holes in the memory map inside
1957e2a86800SMike Rapoport 		 * a pageblock
19584f5b0c17SMike Rapoport 		 */
1959e2a86800SMike Rapoport 		prev_end = ALIGN(end, pageblock_nr_pages);
19604f5b0c17SMike Rapoport 	}
19614f5b0c17SMike Rapoport 
19624f5b0c17SMike Rapoport #ifdef CONFIG_SPARSEMEM
1963f921f53eSMike Rapoport 	if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) {
1964f921f53eSMike Rapoport 		prev_end = ALIGN(end, pageblock_nr_pages);
19654f5b0c17SMike Rapoport 		free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
1966f921f53eSMike Rapoport 	}
19674f5b0c17SMike Rapoport #endif
19684f5b0c17SMike Rapoport }
19694f5b0c17SMike Rapoport 
1970bda49a81SMike Rapoport static void __init __free_pages_memory(unsigned long start, unsigned long end)
1971bda49a81SMike Rapoport {
1972bda49a81SMike Rapoport 	int order;
1973bda49a81SMike Rapoport 
1974bda49a81SMike Rapoport 	while (start < end) {
1975bda49a81SMike Rapoport 		order = min(MAX_ORDER - 1UL, __ffs(start));
1976bda49a81SMike Rapoport 
1977bda49a81SMike Rapoport 		while (start + (1UL << order) > end)
1978bda49a81SMike Rapoport 			order--;
1979bda49a81SMike Rapoport 
1980bda49a81SMike Rapoport 		memblock_free_pages(pfn_to_page(start), start, order);
1981bda49a81SMike Rapoport 
1982bda49a81SMike Rapoport 		start += (1UL << order);
1983bda49a81SMike Rapoport 	}
1984bda49a81SMike Rapoport }
1985bda49a81SMike Rapoport 
1986bda49a81SMike Rapoport static unsigned long __init __free_memory_core(phys_addr_t start,
1987bda49a81SMike Rapoport 				 phys_addr_t end)
1988bda49a81SMike Rapoport {
1989bda49a81SMike Rapoport 	unsigned long start_pfn = PFN_UP(start);
1990bda49a81SMike Rapoport 	unsigned long end_pfn = min_t(unsigned long,
1991bda49a81SMike Rapoport 				      PFN_DOWN(end), max_low_pfn);
1992bda49a81SMike Rapoport 
1993bda49a81SMike Rapoport 	if (start_pfn >= end_pfn)
1994bda49a81SMike Rapoport 		return 0;
1995bda49a81SMike Rapoport 
1996bda49a81SMike Rapoport 	__free_pages_memory(start_pfn, end_pfn);
1997bda49a81SMike Rapoport 
1998bda49a81SMike Rapoport 	return end_pfn - start_pfn;
1999bda49a81SMike Rapoport }
2000bda49a81SMike Rapoport 
20019092d4f7SMike Rapoport static void __init memmap_init_reserved_pages(void)
20029092d4f7SMike Rapoport {
20039092d4f7SMike Rapoport 	struct memblock_region *region;
20049092d4f7SMike Rapoport 	phys_addr_t start, end;
20059092d4f7SMike Rapoport 	u64 i;
20069092d4f7SMike Rapoport 
20079092d4f7SMike Rapoport 	/* initialize struct pages for the reserved regions */
20089092d4f7SMike Rapoport 	for_each_reserved_mem_range(i, &start, &end)
20099092d4f7SMike Rapoport 		reserve_bootmem_region(start, end);
20109092d4f7SMike Rapoport 
20119092d4f7SMike Rapoport 	/* and also treat struct pages for the NOMAP regions as PageReserved */
20129092d4f7SMike Rapoport 	for_each_mem_region(region) {
20139092d4f7SMike Rapoport 		if (memblock_is_nomap(region)) {
20149092d4f7SMike Rapoport 			start = region->base;
20159092d4f7SMike Rapoport 			end = start + region->size;
20169092d4f7SMike Rapoport 			reserve_bootmem_region(start, end);
20179092d4f7SMike Rapoport 		}
20189092d4f7SMike Rapoport 	}
20199092d4f7SMike Rapoport }
20209092d4f7SMike Rapoport 
2021bda49a81SMike Rapoport static unsigned long __init free_low_memory_core_early(void)
2022bda49a81SMike Rapoport {
2023bda49a81SMike Rapoport 	unsigned long count = 0;
2024bda49a81SMike Rapoport 	phys_addr_t start, end;
2025bda49a81SMike Rapoport 	u64 i;
2026bda49a81SMike Rapoport 
2027bda49a81SMike Rapoport 	memblock_clear_hotplug(0, -1);
2028bda49a81SMike Rapoport 
20299092d4f7SMike Rapoport 	memmap_init_reserved_pages();
2030bda49a81SMike Rapoport 
2031bda49a81SMike Rapoport 	/*
2032bda49a81SMike Rapoport 	 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
2033bda49a81SMike Rapoport 	 *  because in some case like Node0 doesn't have RAM installed
2034bda49a81SMike Rapoport 	 *  low ram will be on Node1
2035bda49a81SMike Rapoport 	 */
2036bda49a81SMike Rapoport 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
2037bda49a81SMike Rapoport 				NULL)
2038bda49a81SMike Rapoport 		count += __free_memory_core(start, end);
2039bda49a81SMike Rapoport 
2040bda49a81SMike Rapoport 	return count;
2041bda49a81SMike Rapoport }
2042bda49a81SMike Rapoport 
2043bda49a81SMike Rapoport static int reset_managed_pages_done __initdata;
2044bda49a81SMike Rapoport 
2045bda49a81SMike Rapoport void reset_node_managed_pages(pg_data_t *pgdat)
2046bda49a81SMike Rapoport {
2047bda49a81SMike Rapoport 	struct zone *z;
2048bda49a81SMike Rapoport 
2049bda49a81SMike Rapoport 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
20509705bea5SArun KS 		atomic_long_set(&z->managed_pages, 0);
2051bda49a81SMike Rapoport }
2052bda49a81SMike Rapoport 
2053bda49a81SMike Rapoport void __init reset_all_zones_managed_pages(void)
2054bda49a81SMike Rapoport {
2055bda49a81SMike Rapoport 	struct pglist_data *pgdat;
2056bda49a81SMike Rapoport 
2057bda49a81SMike Rapoport 	if (reset_managed_pages_done)
2058bda49a81SMike Rapoport 		return;
2059bda49a81SMike Rapoport 
2060bda49a81SMike Rapoport 	for_each_online_pgdat(pgdat)
2061bda49a81SMike Rapoport 		reset_node_managed_pages(pgdat);
2062bda49a81SMike Rapoport 
2063bda49a81SMike Rapoport 	reset_managed_pages_done = 1;
2064bda49a81SMike Rapoport }
2065bda49a81SMike Rapoport 
2066bda49a81SMike Rapoport /**
2067bda49a81SMike Rapoport  * memblock_free_all - release free pages to the buddy allocator
2068bda49a81SMike Rapoport  */
2069097d43d8SDaeseok Youn void __init memblock_free_all(void)
2070bda49a81SMike Rapoport {
2071bda49a81SMike Rapoport 	unsigned long pages;
2072bda49a81SMike Rapoport 
20734f5b0c17SMike Rapoport 	free_unused_memmap();
2074bda49a81SMike Rapoport 	reset_all_zones_managed_pages();
2075bda49a81SMike Rapoport 
2076bda49a81SMike Rapoport 	pages = free_low_memory_core_early();
2077ca79b0c2SArun KS 	totalram_pages_add(pages);
2078bda49a81SMike Rapoport }
2079bda49a81SMike Rapoport 
2080350e88baSMike Rapoport #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
20816d03b885SBenjamin Herrenschmidt 
20826d03b885SBenjamin Herrenschmidt static int memblock_debug_show(struct seq_file *m, void *private)
20836d03b885SBenjamin Herrenschmidt {
20846d03b885SBenjamin Herrenschmidt 	struct memblock_type *type = m->private;
20856d03b885SBenjamin Herrenschmidt 	struct memblock_region *reg;
20866d03b885SBenjamin Herrenschmidt 	int i;
20875d63f81cSMiles Chen 	phys_addr_t end;
20886d03b885SBenjamin Herrenschmidt 
20896d03b885SBenjamin Herrenschmidt 	for (i = 0; i < type->cnt; i++) {
20906d03b885SBenjamin Herrenschmidt 		reg = &type->regions[i];
20915d63f81cSMiles Chen 		end = reg->base + reg->size - 1;
20926d03b885SBenjamin Herrenschmidt 
20935d63f81cSMiles Chen 		seq_printf(m, "%4d: ", i);
20945d63f81cSMiles Chen 		seq_printf(m, "%pa..%pa\n", &reg->base, &end);
20956d03b885SBenjamin Herrenschmidt 	}
20966d03b885SBenjamin Herrenschmidt 	return 0;
20976d03b885SBenjamin Herrenschmidt }
20985ad35093SAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(memblock_debug);
20996d03b885SBenjamin Herrenschmidt 
21006d03b885SBenjamin Herrenschmidt static int __init memblock_init_debugfs(void)
21016d03b885SBenjamin Herrenschmidt {
21026d03b885SBenjamin Herrenschmidt 	struct dentry *root = debugfs_create_dir("memblock", NULL);
2103d9f7979cSGreg Kroah-Hartman 
21040825a6f9SJoe Perches 	debugfs_create_file("memory", 0444, root,
21050825a6f9SJoe Perches 			    &memblock.memory, &memblock_debug_fops);
21060825a6f9SJoe Perches 	debugfs_create_file("reserved", 0444, root,
21070825a6f9SJoe Perches 			    &memblock.reserved, &memblock_debug_fops);
210870210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
210977649905SDavid Hildenbrand 	debugfs_create_file("physmem", 0444, root, &physmem,
211077649905SDavid Hildenbrand 			    &memblock_debug_fops);
211170210ed9SPhilipp Hachtmann #endif
21126d03b885SBenjamin Herrenschmidt 
21136d03b885SBenjamin Herrenschmidt 	return 0;
21146d03b885SBenjamin Herrenschmidt }
21156d03b885SBenjamin Herrenschmidt __initcall(memblock_init_debugfs);
21166d03b885SBenjamin Herrenschmidt 
21176d03b885SBenjamin Herrenschmidt #endif /* CONFIG_DEBUG_FS */
2118