xref: /linux/mm/memblock.c (revision 2f770806fd2c3db9616965e57ba60d80f43c827d)
195f72d1eSYinghai Lu /*
295f72d1eSYinghai Lu  * Procedures for maintaining information about logical memory blocks.
395f72d1eSYinghai Lu  *
495f72d1eSYinghai Lu  * Peter Bergner, IBM Corp.	June 2001.
595f72d1eSYinghai Lu  * Copyright (C) 2001 Peter Bergner.
695f72d1eSYinghai Lu  *
795f72d1eSYinghai Lu  *      This program is free software; you can redistribute it and/or
895f72d1eSYinghai Lu  *      modify it under the terms of the GNU General Public License
995f72d1eSYinghai Lu  *      as published by the Free Software Foundation; either version
1095f72d1eSYinghai Lu  *      2 of the License, or (at your option) any later version.
1195f72d1eSYinghai Lu  */
1295f72d1eSYinghai Lu 
1395f72d1eSYinghai Lu #include <linux/kernel.h>
14142b45a7SBenjamin Herrenschmidt #include <linux/slab.h>
1595f72d1eSYinghai Lu #include <linux/init.h>
1695f72d1eSYinghai Lu #include <linux/bitops.h>
17449e8df3SBenjamin Herrenschmidt #include <linux/poison.h>
18c196f76fSBenjamin Herrenschmidt #include <linux/pfn.h>
196d03b885SBenjamin Herrenschmidt #include <linux/debugfs.h>
20514c6032SRandy Dunlap #include <linux/kmemleak.h>
216d03b885SBenjamin Herrenschmidt #include <linux/seq_file.h>
2295f72d1eSYinghai Lu #include <linux/memblock.h>
2395f72d1eSYinghai Lu 
24c4c5ad6bSChristoph Hellwig #include <asm/sections.h>
2526f09e9bSSantosh Shilimkar #include <linux/io.h>
2626f09e9bSSantosh Shilimkar 
2726f09e9bSSantosh Shilimkar #include "internal.h"
2879442ed1STang Chen 
293e039c5cSMike Rapoport /**
303e039c5cSMike Rapoport  * DOC: memblock overview
313e039c5cSMike Rapoport  *
323e039c5cSMike Rapoport  * Memblock is a method of managing memory regions during the early
333e039c5cSMike Rapoport  * boot period when the usual kernel memory allocators are not up and
343e039c5cSMike Rapoport  * running.
353e039c5cSMike Rapoport  *
363e039c5cSMike Rapoport  * Memblock views the system memory as collections of contiguous
373e039c5cSMike Rapoport  * regions. There are several types of these collections:
383e039c5cSMike Rapoport  *
393e039c5cSMike Rapoport  * * ``memory`` - describes the physical memory available to the
403e039c5cSMike Rapoport  *   kernel; this may differ from the actual physical memory installed
413e039c5cSMike Rapoport  *   in the system, for instance when the memory is restricted with
423e039c5cSMike Rapoport  *   ``mem=`` command line parameter
433e039c5cSMike Rapoport  * * ``reserved`` - describes the regions that were allocated
443e039c5cSMike Rapoport  * * ``physmap`` - describes the actual physical memory regardless of
453e039c5cSMike Rapoport  *   the possible restrictions; the ``physmap`` type is only available
463e039c5cSMike Rapoport  *   on some architectures.
473e039c5cSMike Rapoport  *
483e039c5cSMike Rapoport  * Each region is represented by :c:type:`struct memblock_region` that
493e039c5cSMike Rapoport  * defines the region extents, its attributes and NUMA node id on NUMA
503e039c5cSMike Rapoport  * systems. Every memory type is described by the :c:type:`struct
513e039c5cSMike Rapoport  * memblock_type` which contains an array of memory regions along with
523e039c5cSMike Rapoport  * the allocator metadata. The memory types are nicely wrapped with
533e039c5cSMike Rapoport  * :c:type:`struct memblock`. This structure is statically initialzed
543e039c5cSMike Rapoport  * at build time. The region arrays for the "memory" and "reserved"
553e039c5cSMike Rapoport  * types are initially sized to %INIT_MEMBLOCK_REGIONS and for the
563e039c5cSMike Rapoport  * "physmap" type to %INIT_PHYSMEM_REGIONS.
573e039c5cSMike Rapoport  * The :c:func:`memblock_allow_resize` enables automatic resizing of
583e039c5cSMike Rapoport  * the region arrays during addition of new regions. This feature
593e039c5cSMike Rapoport  * should be used with care so that memory allocated for the region
603e039c5cSMike Rapoport  * array will not overlap with areas that should be reserved, for
613e039c5cSMike Rapoport  * example initrd.
623e039c5cSMike Rapoport  *
633e039c5cSMike Rapoport  * The early architecture setup should tell memblock what the physical
643e039c5cSMike Rapoport  * memory layout is by using :c:func:`memblock_add` or
653e039c5cSMike Rapoport  * :c:func:`memblock_add_node` functions. The first function does not
663e039c5cSMike Rapoport  * assign the region to a NUMA node and it is appropriate for UMA
673e039c5cSMike Rapoport  * systems. Yet, it is possible to use it on NUMA systems as well and
683e039c5cSMike Rapoport  * assign the region to a NUMA node later in the setup process using
693e039c5cSMike Rapoport  * :c:func:`memblock_set_node`. The :c:func:`memblock_add_node`
703e039c5cSMike Rapoport  * performs such an assignment directly.
713e039c5cSMike Rapoport  *
723e039c5cSMike Rapoport  * Once memblock is setup the memory can be allocated using either
733e039c5cSMike Rapoport  * memblock or bootmem APIs.
743e039c5cSMike Rapoport  *
753e039c5cSMike Rapoport  * As the system boot progresses, the architecture specific
763e039c5cSMike Rapoport  * :c:func:`mem_init` function frees all the memory to the buddy page
773e039c5cSMike Rapoport  * allocator.
783e039c5cSMike Rapoport  *
793e039c5cSMike Rapoport  * If an architecure enables %CONFIG_ARCH_DISCARD_MEMBLOCK, the
803e039c5cSMike Rapoport  * memblock data structures will be discarded after the system
813e039c5cSMike Rapoport  * initialization compltes.
823e039c5cSMike Rapoport  */
833e039c5cSMike Rapoport 
84bda49a81SMike Rapoport #ifndef CONFIG_NEED_MULTIPLE_NODES
85bda49a81SMike Rapoport struct pglist_data __refdata contig_page_data;
86bda49a81SMike Rapoport EXPORT_SYMBOL(contig_page_data);
87bda49a81SMike Rapoport #endif
88bda49a81SMike Rapoport 
89bda49a81SMike Rapoport unsigned long max_low_pfn;
90bda49a81SMike Rapoport unsigned long min_low_pfn;
91bda49a81SMike Rapoport unsigned long max_pfn;
92bda49a81SMike Rapoport unsigned long long max_possible_pfn;
93bda49a81SMike Rapoport 
94fe091c20STejun Heo static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
95fe091c20STejun Heo static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
9670210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
9770210ed9SPhilipp Hachtmann static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
9870210ed9SPhilipp Hachtmann #endif
99fe091c20STejun Heo 
100fe091c20STejun Heo struct memblock memblock __initdata_memblock = {
101fe091c20STejun Heo 	.memory.regions		= memblock_memory_init_regions,
102fe091c20STejun Heo 	.memory.cnt		= 1,	/* empty dummy entry */
103fe091c20STejun Heo 	.memory.max		= INIT_MEMBLOCK_REGIONS,
1040262d9c8SHeiko Carstens 	.memory.name		= "memory",
105fe091c20STejun Heo 
106fe091c20STejun Heo 	.reserved.regions	= memblock_reserved_init_regions,
107fe091c20STejun Heo 	.reserved.cnt		= 1,	/* empty dummy entry */
108fe091c20STejun Heo 	.reserved.max		= INIT_MEMBLOCK_REGIONS,
1090262d9c8SHeiko Carstens 	.reserved.name		= "reserved",
110fe091c20STejun Heo 
11170210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
11270210ed9SPhilipp Hachtmann 	.physmem.regions	= memblock_physmem_init_regions,
11370210ed9SPhilipp Hachtmann 	.physmem.cnt		= 1,	/* empty dummy entry */
11470210ed9SPhilipp Hachtmann 	.physmem.max		= INIT_PHYSMEM_REGIONS,
1150262d9c8SHeiko Carstens 	.physmem.name		= "physmem",
11670210ed9SPhilipp Hachtmann #endif
11770210ed9SPhilipp Hachtmann 
11879442ed1STang Chen 	.bottom_up		= false,
119fe091c20STejun Heo 	.current_limit		= MEMBLOCK_ALLOC_ANYWHERE,
120fe091c20STejun Heo };
12195f72d1eSYinghai Lu 
12210d06439SYinghai Lu int memblock_debug __initdata_memblock;
123a3f5bafcSTony Luck static bool system_has_some_mirror __initdata_memblock = false;
1241aadc056STejun Heo static int memblock_can_resize __initdata_memblock;
125181eb394SGavin Shan static int memblock_memory_in_slab __initdata_memblock = 0;
126181eb394SGavin Shan static int memblock_reserved_in_slab __initdata_memblock = 0;
12795f72d1eSYinghai Lu 
128e1720feeSMike Rapoport enum memblock_flags __init_memblock choose_memblock_flags(void)
129a3f5bafcSTony Luck {
130a3f5bafcSTony Luck 	return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
131a3f5bafcSTony Luck }
132a3f5bafcSTony Luck 
133eb18f1b5STejun Heo /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
134eb18f1b5STejun Heo static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
135eb18f1b5STejun Heo {
1361c4bc43dSStefan Agner 	return *size = min(*size, PHYS_ADDR_MAX - base);
137eb18f1b5STejun Heo }
138eb18f1b5STejun Heo 
1396ed311b2SBenjamin Herrenschmidt /*
1406ed311b2SBenjamin Herrenschmidt  * Address comparison utilities
1416ed311b2SBenjamin Herrenschmidt  */
14210d06439SYinghai Lu static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
1432898cc4cSBenjamin Herrenschmidt 				       phys_addr_t base2, phys_addr_t size2)
14495f72d1eSYinghai Lu {
14595f72d1eSYinghai Lu 	return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
14695f72d1eSYinghai Lu }
14795f72d1eSYinghai Lu 
14895cf82ecSTang Chen bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
1492d7d3eb2SH Hartley Sweeten 					phys_addr_t base, phys_addr_t size)
1506ed311b2SBenjamin Herrenschmidt {
1516ed311b2SBenjamin Herrenschmidt 	unsigned long i;
1526ed311b2SBenjamin Herrenschmidt 
153f14516fbSAlexander Kuleshov 	for (i = 0; i < type->cnt; i++)
154f14516fbSAlexander Kuleshov 		if (memblock_addrs_overlap(base, size, type->regions[i].base,
155f14516fbSAlexander Kuleshov 					   type->regions[i].size))
1566ed311b2SBenjamin Herrenschmidt 			break;
157c5c5c9d1STang Chen 	return i < type->cnt;
1586ed311b2SBenjamin Herrenschmidt }
1596ed311b2SBenjamin Herrenschmidt 
16047cec443SMike Rapoport /**
16179442ed1STang Chen  * __memblock_find_range_bottom_up - find free area utility in bottom-up
16279442ed1STang Chen  * @start: start of candidate range
16347cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
16447cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
16579442ed1STang Chen  * @size: size of free area to find
16679442ed1STang Chen  * @align: alignment of free area to find
167b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
168fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
16979442ed1STang Chen  *
17079442ed1STang Chen  * Utility called from memblock_find_in_range_node(), find free area bottom-up.
17179442ed1STang Chen  *
17247cec443SMike Rapoport  * Return:
17379442ed1STang Chen  * Found address on success, 0 on failure.
17479442ed1STang Chen  */
17579442ed1STang Chen static phys_addr_t __init_memblock
17679442ed1STang Chen __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
177fc6daaf9STony Luck 				phys_addr_t size, phys_addr_t align, int nid,
178e1720feeSMike Rapoport 				enum memblock_flags flags)
17979442ed1STang Chen {
18079442ed1STang Chen 	phys_addr_t this_start, this_end, cand;
18179442ed1STang Chen 	u64 i;
18279442ed1STang Chen 
183fc6daaf9STony Luck 	for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
18479442ed1STang Chen 		this_start = clamp(this_start, start, end);
18579442ed1STang Chen 		this_end = clamp(this_end, start, end);
18679442ed1STang Chen 
18779442ed1STang Chen 		cand = round_up(this_start, align);
18879442ed1STang Chen 		if (cand < this_end && this_end - cand >= size)
18979442ed1STang Chen 			return cand;
19079442ed1STang Chen 	}
19179442ed1STang Chen 
19279442ed1STang Chen 	return 0;
19379442ed1STang Chen }
19479442ed1STang Chen 
1957bd0b0f0STejun Heo /**
1961402899eSTang Chen  * __memblock_find_range_top_down - find free area utility, in top-down
1971402899eSTang Chen  * @start: start of candidate range
19847cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
19947cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
2001402899eSTang Chen  * @size: size of free area to find
2011402899eSTang Chen  * @align: alignment of free area to find
202b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
203fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2041402899eSTang Chen  *
2051402899eSTang Chen  * Utility called from memblock_find_in_range_node(), find free area top-down.
2061402899eSTang Chen  *
20747cec443SMike Rapoport  * Return:
20879442ed1STang Chen  * Found address on success, 0 on failure.
2091402899eSTang Chen  */
2101402899eSTang Chen static phys_addr_t __init_memblock
2111402899eSTang Chen __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
212fc6daaf9STony Luck 			       phys_addr_t size, phys_addr_t align, int nid,
213e1720feeSMike Rapoport 			       enum memblock_flags flags)
2141402899eSTang Chen {
2151402899eSTang Chen 	phys_addr_t this_start, this_end, cand;
2161402899eSTang Chen 	u64 i;
2171402899eSTang Chen 
218fc6daaf9STony Luck 	for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
219fc6daaf9STony Luck 					NULL) {
2201402899eSTang Chen 		this_start = clamp(this_start, start, end);
2211402899eSTang Chen 		this_end = clamp(this_end, start, end);
2221402899eSTang Chen 
2231402899eSTang Chen 		if (this_end < size)
2241402899eSTang Chen 			continue;
2251402899eSTang Chen 
2261402899eSTang Chen 		cand = round_down(this_end - size, align);
2271402899eSTang Chen 		if (cand >= this_start)
2281402899eSTang Chen 			return cand;
2291402899eSTang Chen 	}
2301402899eSTang Chen 
2311402899eSTang Chen 	return 0;
2321402899eSTang Chen }
2331402899eSTang Chen 
2341402899eSTang Chen /**
2357bd0b0f0STejun Heo  * memblock_find_in_range_node - find free area in given range and node
2367bd0b0f0STejun Heo  * @size: size of free area to find
2377bd0b0f0STejun Heo  * @align: alignment of free area to find
23887029ee9SGrygorii Strashko  * @start: start of candidate range
23947cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
24047cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
241b1154233SGrygorii Strashko  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
242fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
2437bd0b0f0STejun Heo  *
2447bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range and node.
2457bd0b0f0STejun Heo  *
24679442ed1STang Chen  * When allocation direction is bottom-up, the @start should be greater
24779442ed1STang Chen  * than the end of the kernel image. Otherwise, it will be trimmed. The
24879442ed1STang Chen  * reason is that we want the bottom-up allocation just near the kernel
24979442ed1STang Chen  * image so it is highly likely that the allocated memory and the kernel
25079442ed1STang Chen  * will reside in the same node.
25179442ed1STang Chen  *
25279442ed1STang Chen  * If bottom-up allocation failed, will try to allocate memory top-down.
25379442ed1STang Chen  *
25447cec443SMike Rapoport  * Return:
25579442ed1STang Chen  * Found address on success, 0 on failure.
2566ed311b2SBenjamin Herrenschmidt  */
25787029ee9SGrygorii Strashko phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
25887029ee9SGrygorii Strashko 					phys_addr_t align, phys_addr_t start,
259e1720feeSMike Rapoport 					phys_addr_t end, int nid,
260e1720feeSMike Rapoport 					enum memblock_flags flags)
261f7210e6cSTang Chen {
2620cfb8f0cSTang Chen 	phys_addr_t kernel_end, ret;
26379442ed1STang Chen 
264f7210e6cSTang Chen 	/* pump up @end */
265f7210e6cSTang Chen 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
266f7210e6cSTang Chen 		end = memblock.current_limit;
267f7210e6cSTang Chen 
268f7210e6cSTang Chen 	/* avoid allocating the first page */
269f7210e6cSTang Chen 	start = max_t(phys_addr_t, start, PAGE_SIZE);
270f7210e6cSTang Chen 	end = max(start, end);
27179442ed1STang Chen 	kernel_end = __pa_symbol(_end);
27279442ed1STang Chen 
27379442ed1STang Chen 	/*
27479442ed1STang Chen 	 * try bottom-up allocation only when bottom-up mode
27579442ed1STang Chen 	 * is set and @end is above the kernel image.
27679442ed1STang Chen 	 */
27779442ed1STang Chen 	if (memblock_bottom_up() && end > kernel_end) {
27879442ed1STang Chen 		phys_addr_t bottom_up_start;
27979442ed1STang Chen 
28079442ed1STang Chen 		/* make sure we will allocate above the kernel */
28179442ed1STang Chen 		bottom_up_start = max(start, kernel_end);
28279442ed1STang Chen 
28379442ed1STang Chen 		/* ok, try bottom-up allocation first */
28479442ed1STang Chen 		ret = __memblock_find_range_bottom_up(bottom_up_start, end,
285fc6daaf9STony Luck 						      size, align, nid, flags);
28679442ed1STang Chen 		if (ret)
28779442ed1STang Chen 			return ret;
28879442ed1STang Chen 
28979442ed1STang Chen 		/*
29079442ed1STang Chen 		 * we always limit bottom-up allocation above the kernel,
29179442ed1STang Chen 		 * but top-down allocation doesn't have the limit, so
29279442ed1STang Chen 		 * retrying top-down allocation may succeed when bottom-up
29379442ed1STang Chen 		 * allocation failed.
29479442ed1STang Chen 		 *
29579442ed1STang Chen 		 * bottom-up allocation is expected to be fail very rarely,
29679442ed1STang Chen 		 * so we use WARN_ONCE() here to see the stack trace if
29779442ed1STang Chen 		 * fail happens.
29879442ed1STang Chen 		 */
299e3d301caSMichal Hocko 		WARN_ONCE(IS_ENABLED(CONFIG_MEMORY_HOTREMOVE),
300e3d301caSMichal Hocko 			  "memblock: bottom-up allocation failed, memory hotremove may be affected\n");
30179442ed1STang Chen 	}
302f7210e6cSTang Chen 
303fc6daaf9STony Luck 	return __memblock_find_range_top_down(start, end, size, align, nid,
304fc6daaf9STony Luck 					      flags);
305f7210e6cSTang Chen }
3066ed311b2SBenjamin Herrenschmidt 
3077bd0b0f0STejun Heo /**
3087bd0b0f0STejun Heo  * memblock_find_in_range - find free area in given range
3097bd0b0f0STejun Heo  * @start: start of candidate range
31047cec443SMike Rapoport  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
31147cec443SMike Rapoport  *       %MEMBLOCK_ALLOC_ACCESSIBLE
3127bd0b0f0STejun Heo  * @size: size of free area to find
3137bd0b0f0STejun Heo  * @align: alignment of free area to find
3147bd0b0f0STejun Heo  *
3157bd0b0f0STejun Heo  * Find @size free area aligned to @align in the specified range.
3167bd0b0f0STejun Heo  *
31747cec443SMike Rapoport  * Return:
31879442ed1STang Chen  * Found address on success, 0 on failure.
3197bd0b0f0STejun Heo  */
3207bd0b0f0STejun Heo phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
3217bd0b0f0STejun Heo 					phys_addr_t end, phys_addr_t size,
3227bd0b0f0STejun Heo 					phys_addr_t align)
3237bd0b0f0STejun Heo {
324a3f5bafcSTony Luck 	phys_addr_t ret;
325e1720feeSMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
326a3f5bafcSTony Luck 
327a3f5bafcSTony Luck again:
328a3f5bafcSTony Luck 	ret = memblock_find_in_range_node(size, align, start, end,
329a3f5bafcSTony Luck 					    NUMA_NO_NODE, flags);
330a3f5bafcSTony Luck 
331a3f5bafcSTony Luck 	if (!ret && (flags & MEMBLOCK_MIRROR)) {
332a3f5bafcSTony Luck 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
333a3f5bafcSTony Luck 			&size);
334a3f5bafcSTony Luck 		flags &= ~MEMBLOCK_MIRROR;
335a3f5bafcSTony Luck 		goto again;
336a3f5bafcSTony Luck 	}
337a3f5bafcSTony Luck 
338a3f5bafcSTony Luck 	return ret;
3397bd0b0f0STejun Heo }
3407bd0b0f0STejun Heo 
34110d06439SYinghai Lu static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
34295f72d1eSYinghai Lu {
3431440c4e2STejun Heo 	type->total_size -= type->regions[r].size;
3447c0caeb8STejun Heo 	memmove(&type->regions[r], &type->regions[r + 1],
3457c0caeb8STejun Heo 		(type->cnt - (r + 1)) * sizeof(type->regions[r]));
346e3239ff9SBenjamin Herrenschmidt 	type->cnt--;
34795f72d1eSYinghai Lu 
3488f7a6605SBenjamin Herrenschmidt 	/* Special case for empty arrays */
3498f7a6605SBenjamin Herrenschmidt 	if (type->cnt == 0) {
3501440c4e2STejun Heo 		WARN_ON(type->total_size != 0);
3518f7a6605SBenjamin Herrenschmidt 		type->cnt = 1;
3528f7a6605SBenjamin Herrenschmidt 		type->regions[0].base = 0;
3538f7a6605SBenjamin Herrenschmidt 		type->regions[0].size = 0;
35466a20757STang Chen 		type->regions[0].flags = 0;
3557c0caeb8STejun Heo 		memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
3568f7a6605SBenjamin Herrenschmidt 	}
35795f72d1eSYinghai Lu }
35895f72d1eSYinghai Lu 
359354f17e1SPhilipp Hachtmann #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
3603010f876SPavel Tatashin /**
36147cec443SMike Rapoport  * memblock_discard - discard memory and reserved arrays if they were allocated
3623010f876SPavel Tatashin  */
3633010f876SPavel Tatashin void __init memblock_discard(void)
36429f67386SYinghai Lu {
3653010f876SPavel Tatashin 	phys_addr_t addr, size;
36629f67386SYinghai Lu 
3673010f876SPavel Tatashin 	if (memblock.reserved.regions != memblock_reserved_init_regions) {
3683010f876SPavel Tatashin 		addr = __pa(memblock.reserved.regions);
3693010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
37029f67386SYinghai Lu 				  memblock.reserved.max);
3713010f876SPavel Tatashin 		__memblock_free_late(addr, size);
37229f67386SYinghai Lu 	}
37329f67386SYinghai Lu 
37491b540f9SPavel Tatashin 	if (memblock.memory.regions != memblock_memory_init_regions) {
3753010f876SPavel Tatashin 		addr = __pa(memblock.memory.regions);
3763010f876SPavel Tatashin 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
3775e270e25SPhilipp Hachtmann 				  memblock.memory.max);
3783010f876SPavel Tatashin 		__memblock_free_late(addr, size);
3795e270e25SPhilipp Hachtmann 	}
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 
430142b45a7SBenjamin Herrenschmidt 	/* Try to find some space for it.
431142b45a7SBenjamin Herrenschmidt 	 *
432142b45a7SBenjamin Herrenschmidt 	 * WARNING: We assume that either slab_is_available() and we use it or
433fd07383bSAndrew Morton 	 * we use MEMBLOCK for allocations. That means that this is unsafe to
434fd07383bSAndrew Morton 	 * use when bootmem is currently active (unless bootmem itself is
435fd07383bSAndrew Morton 	 * implemented on top of MEMBLOCK which isn't the case yet)
436142b45a7SBenjamin Herrenschmidt 	 *
437142b45a7SBenjamin Herrenschmidt 	 * This should however not be an issue for now, as we currently only
438fd07383bSAndrew Morton 	 * call into MEMBLOCK while it's still active, or much later when slab
439fd07383bSAndrew Morton 	 * is active for memory hotplug operations
440142b45a7SBenjamin Herrenschmidt 	 */
441142b45a7SBenjamin Herrenschmidt 	if (use_slab) {
442142b45a7SBenjamin Herrenschmidt 		new_array = kmalloc(new_size, GFP_KERNEL);
4431f5026a7STejun Heo 		addr = new_array ? __pa(new_array) : 0;
4444e2f0775SGavin Shan 	} else {
44548c3b583SGreg Pearson 		/* only exclude range when trying to double reserved.regions */
44648c3b583SGreg Pearson 		if (type != &memblock.reserved)
44748c3b583SGreg Pearson 			new_area_start = new_area_size = 0;
44848c3b583SGreg Pearson 
44948c3b583SGreg Pearson 		addr = memblock_find_in_range(new_area_start + new_area_size,
45048c3b583SGreg Pearson 						memblock.current_limit,
45129f67386SYinghai Lu 						new_alloc_size, PAGE_SIZE);
45248c3b583SGreg Pearson 		if (!addr && new_area_size)
45348c3b583SGreg Pearson 			addr = memblock_find_in_range(0,
45448c3b583SGreg Pearson 				min(new_area_start, memblock.current_limit),
45529f67386SYinghai Lu 				new_alloc_size, PAGE_SIZE);
45648c3b583SGreg Pearson 
45715674868SSachin Kamat 		new_array = addr ? __va(addr) : NULL;
4584e2f0775SGavin Shan 	}
4591f5026a7STejun Heo 	if (!addr) {
460142b45a7SBenjamin Herrenschmidt 		pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
4610262d9c8SHeiko Carstens 		       type->name, type->max, type->max * 2);
462142b45a7SBenjamin Herrenschmidt 		return -1;
463142b45a7SBenjamin Herrenschmidt 	}
464142b45a7SBenjamin Herrenschmidt 
465a36aab89SMike Rapoport 	new_end = addr + new_size - 1;
466a36aab89SMike Rapoport 	memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
467a36aab89SMike Rapoport 			type->name, type->max * 2, &addr, &new_end);
468ea9e4376SYinghai Lu 
469fd07383bSAndrew Morton 	/*
470fd07383bSAndrew Morton 	 * Found space, we now need to move the array over before we add the
471fd07383bSAndrew Morton 	 * reserved region since it may be our reserved array itself that is
472fd07383bSAndrew Morton 	 * full.
473142b45a7SBenjamin Herrenschmidt 	 */
474142b45a7SBenjamin Herrenschmidt 	memcpy(new_array, type->regions, old_size);
475142b45a7SBenjamin Herrenschmidt 	memset(new_array + type->max, 0, old_size);
476142b45a7SBenjamin Herrenschmidt 	old_array = type->regions;
477142b45a7SBenjamin Herrenschmidt 	type->regions = new_array;
478142b45a7SBenjamin Herrenschmidt 	type->max <<= 1;
479142b45a7SBenjamin Herrenschmidt 
480fd07383bSAndrew Morton 	/* Free old array. We needn't free it if the array is the static one */
481181eb394SGavin Shan 	if (*in_slab)
482181eb394SGavin Shan 		kfree(old_array);
483181eb394SGavin Shan 	else if (old_array != memblock_memory_init_regions &&
484142b45a7SBenjamin Herrenschmidt 		 old_array != memblock_reserved_init_regions)
48529f67386SYinghai Lu 		memblock_free(__pa(old_array), old_alloc_size);
486142b45a7SBenjamin Herrenschmidt 
487fd07383bSAndrew Morton 	/*
488fd07383bSAndrew Morton 	 * Reserve the new array if that comes from the memblock.  Otherwise, we
489fd07383bSAndrew Morton 	 * needn't do it
490181eb394SGavin Shan 	 */
491181eb394SGavin Shan 	if (!use_slab)
49229f67386SYinghai Lu 		BUG_ON(memblock_reserve(addr, new_alloc_size));
493181eb394SGavin Shan 
494181eb394SGavin Shan 	/* Update slab flag */
495181eb394SGavin Shan 	*in_slab = use_slab;
496181eb394SGavin Shan 
497142b45a7SBenjamin Herrenschmidt 	return 0;
498142b45a7SBenjamin Herrenschmidt }
499142b45a7SBenjamin Herrenschmidt 
500784656f9STejun Heo /**
501784656f9STejun Heo  * memblock_merge_regions - merge neighboring compatible regions
502784656f9STejun Heo  * @type: memblock type to scan
503784656f9STejun Heo  *
504784656f9STejun Heo  * Scan @type and merge neighboring compatible regions.
505784656f9STejun Heo  */
506784656f9STejun Heo static void __init_memblock memblock_merge_regions(struct memblock_type *type)
507784656f9STejun Heo {
508784656f9STejun Heo 	int i = 0;
509784656f9STejun Heo 
510784656f9STejun Heo 	/* cnt never goes below 1 */
511784656f9STejun Heo 	while (i < type->cnt - 1) {
512784656f9STejun Heo 		struct memblock_region *this = &type->regions[i];
513784656f9STejun Heo 		struct memblock_region *next = &type->regions[i + 1];
514784656f9STejun Heo 
5157c0caeb8STejun Heo 		if (this->base + this->size != next->base ||
5167c0caeb8STejun Heo 		    memblock_get_region_node(this) !=
51766a20757STang Chen 		    memblock_get_region_node(next) ||
51866a20757STang Chen 		    this->flags != next->flags) {
519784656f9STejun Heo 			BUG_ON(this->base + this->size > next->base);
520784656f9STejun Heo 			i++;
521784656f9STejun Heo 			continue;
522784656f9STejun Heo 		}
523784656f9STejun Heo 
524784656f9STejun Heo 		this->size += next->size;
525c0232ae8SLin Feng 		/* move forward from next + 1, index of which is i + 2 */
526c0232ae8SLin Feng 		memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
527784656f9STejun Heo 		type->cnt--;
528784656f9STejun Heo 	}
529784656f9STejun Heo }
530784656f9STejun Heo 
531784656f9STejun Heo /**
532784656f9STejun Heo  * memblock_insert_region - insert new memblock region
533784656f9STejun Heo  * @type:	memblock type to insert into
534784656f9STejun Heo  * @idx:	index for the insertion point
535784656f9STejun Heo  * @base:	base address of the new region
536784656f9STejun Heo  * @size:	size of the new region
537209ff86dSTang Chen  * @nid:	node id of the new region
53866a20757STang Chen  * @flags:	flags of the new region
539784656f9STejun Heo  *
540784656f9STejun Heo  * Insert new memblock region [@base, @base + @size) into @type at @idx.
541412d0008SAlexander Kuleshov  * @type must already have extra room to accommodate the new region.
542784656f9STejun Heo  */
543784656f9STejun Heo static void __init_memblock memblock_insert_region(struct memblock_type *type,
544784656f9STejun Heo 						   int idx, phys_addr_t base,
54566a20757STang Chen 						   phys_addr_t size,
546e1720feeSMike Rapoport 						   int nid,
547e1720feeSMike Rapoport 						   enum memblock_flags flags)
548784656f9STejun Heo {
549784656f9STejun Heo 	struct memblock_region *rgn = &type->regions[idx];
550784656f9STejun Heo 
551784656f9STejun Heo 	BUG_ON(type->cnt >= type->max);
552784656f9STejun Heo 	memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
553784656f9STejun Heo 	rgn->base = base;
554784656f9STejun Heo 	rgn->size = size;
55566a20757STang Chen 	rgn->flags = flags;
5567c0caeb8STejun Heo 	memblock_set_region_node(rgn, nid);
557784656f9STejun Heo 	type->cnt++;
5581440c4e2STejun Heo 	type->total_size += size;
559784656f9STejun Heo }
560784656f9STejun Heo 
561784656f9STejun Heo /**
562f1af9d3aSPhilipp Hachtmann  * memblock_add_range - add new memblock region
563784656f9STejun Heo  * @type: memblock type to add new region into
564784656f9STejun Heo  * @base: base address of the new region
565784656f9STejun Heo  * @size: size of the new region
5667fb0bc3fSTejun Heo  * @nid: nid of the new region
56766a20757STang Chen  * @flags: flags of the new region
568784656f9STejun Heo  *
569784656f9STejun Heo  * Add new memblock region [@base, @base + @size) into @type.  The new region
570784656f9STejun Heo  * is allowed to overlap with existing ones - overlaps don't affect already
571784656f9STejun Heo  * existing regions.  @type is guaranteed to be minimal (all neighbouring
572784656f9STejun Heo  * compatible regions are merged) after the addition.
573784656f9STejun Heo  *
57447cec443SMike Rapoport  * Return:
575784656f9STejun Heo  * 0 on success, -errno on failure.
576784656f9STejun Heo  */
577f1af9d3aSPhilipp Hachtmann int __init_memblock memblock_add_range(struct memblock_type *type,
57866a20757STang Chen 				phys_addr_t base, phys_addr_t size,
579e1720feeSMike Rapoport 				int nid, enum memblock_flags flags)
58095f72d1eSYinghai Lu {
581784656f9STejun Heo 	bool insert = false;
582eb18f1b5STejun Heo 	phys_addr_t obase = base;
583eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
5848c9c1701SAlexander Kuleshov 	int idx, nr_new;
5858c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
58695f72d1eSYinghai Lu 
587b3dc627cSTejun Heo 	if (!size)
588b3dc627cSTejun Heo 		return 0;
589b3dc627cSTejun Heo 
590784656f9STejun Heo 	/* special case for empty array */
591784656f9STejun Heo 	if (type->regions[0].size == 0) {
5921440c4e2STejun Heo 		WARN_ON(type->cnt != 1 || type->total_size);
593784656f9STejun Heo 		type->regions[0].base = base;
594784656f9STejun Heo 		type->regions[0].size = size;
59566a20757STang Chen 		type->regions[0].flags = flags;
5967fb0bc3fSTejun Heo 		memblock_set_region_node(&type->regions[0], nid);
5971440c4e2STejun Heo 		type->total_size = size;
598784656f9STejun Heo 		return 0;
599784656f9STejun Heo 	}
600784656f9STejun Heo repeat:
601784656f9STejun Heo 	/*
602784656f9STejun Heo 	 * The following is executed twice.  Once with %false @insert and
603784656f9STejun Heo 	 * then with %true.  The first counts the number of regions needed
604412d0008SAlexander Kuleshov 	 * to accommodate the new area.  The second actually inserts them.
605784656f9STejun Heo 	 */
606784656f9STejun Heo 	base = obase;
607784656f9STejun Heo 	nr_new = 0;
608784656f9STejun Heo 
60966e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
610784656f9STejun Heo 		phys_addr_t rbase = rgn->base;
611784656f9STejun Heo 		phys_addr_t rend = rbase + rgn->size;
6128f7a6605SBenjamin Herrenschmidt 
613784656f9STejun Heo 		if (rbase >= end)
6148f7a6605SBenjamin Herrenschmidt 			break;
615784656f9STejun Heo 		if (rend <= base)
616784656f9STejun Heo 			continue;
617784656f9STejun Heo 		/*
618784656f9STejun Heo 		 * @rgn overlaps.  If it separates the lower part of new
619784656f9STejun Heo 		 * area, insert that portion.
6208f7a6605SBenjamin Herrenschmidt 		 */
621784656f9STejun Heo 		if (rbase > base) {
622c0a29498SWei Yang #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
623c0a29498SWei Yang 			WARN_ON(nid != memblock_get_region_node(rgn));
624c0a29498SWei Yang #endif
6254fcab5f4SWei Yang 			WARN_ON(flags != rgn->flags);
626784656f9STejun Heo 			nr_new++;
627784656f9STejun Heo 			if (insert)
6288c9c1701SAlexander Kuleshov 				memblock_insert_region(type, idx++, base,
62966a20757STang Chen 						       rbase - base, nid,
63066a20757STang Chen 						       flags);
631784656f9STejun Heo 		}
632784656f9STejun Heo 		/* area below @rend is dealt with, forget about it */
633784656f9STejun Heo 		base = min(rend, end);
6348f7a6605SBenjamin Herrenschmidt 	}
6358f7a6605SBenjamin Herrenschmidt 
636784656f9STejun Heo 	/* insert the remaining portion */
637784656f9STejun Heo 	if (base < end) {
638784656f9STejun Heo 		nr_new++;
639784656f9STejun Heo 		if (insert)
6408c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, base, end - base,
64166a20757STang Chen 					       nid, flags);
6428f7a6605SBenjamin Herrenschmidt 	}
6438f7a6605SBenjamin Herrenschmidt 
644ef3cc4dbSnimisolo 	if (!nr_new)
645ef3cc4dbSnimisolo 		return 0;
646ef3cc4dbSnimisolo 
647784656f9STejun Heo 	/*
648784656f9STejun Heo 	 * If this was the first round, resize array and repeat for actual
649784656f9STejun Heo 	 * insertions; otherwise, merge and return.
6508f7a6605SBenjamin Herrenschmidt 	 */
651784656f9STejun Heo 	if (!insert) {
652784656f9STejun Heo 		while (type->cnt + nr_new > type->max)
65348c3b583SGreg Pearson 			if (memblock_double_array(type, obase, size) < 0)
654784656f9STejun Heo 				return -ENOMEM;
655784656f9STejun Heo 		insert = true;
656784656f9STejun Heo 		goto repeat;
65795f72d1eSYinghai Lu 	} else {
658784656f9STejun Heo 		memblock_merge_regions(type);
65995f72d1eSYinghai Lu 		return 0;
66095f72d1eSYinghai Lu 	}
661784656f9STejun Heo }
66295f72d1eSYinghai Lu 
66348a833ccSMike Rapoport /**
66448a833ccSMike Rapoport  * memblock_add_node - add new memblock region within a NUMA node
66548a833ccSMike Rapoport  * @base: base address of the new region
66648a833ccSMike Rapoport  * @size: size of the new region
66748a833ccSMike Rapoport  * @nid: nid of the new region
66848a833ccSMike Rapoport  *
66948a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
67048a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
67148a833ccSMike Rapoport  *
67248a833ccSMike Rapoport  * Return:
67348a833ccSMike Rapoport  * 0 on success, -errno on failure.
67448a833ccSMike Rapoport  */
6757fb0bc3fSTejun Heo int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
6767fb0bc3fSTejun Heo 				       int nid)
6777fb0bc3fSTejun Heo {
678f1af9d3aSPhilipp Hachtmann 	return memblock_add_range(&memblock.memory, base, size, nid, 0);
6797fb0bc3fSTejun Heo }
6807fb0bc3fSTejun Heo 
68148a833ccSMike Rapoport /**
68248a833ccSMike Rapoport  * memblock_add - add new memblock region
68348a833ccSMike Rapoport  * @base: base address of the new region
68448a833ccSMike Rapoport  * @size: size of the new region
68548a833ccSMike Rapoport  *
68648a833ccSMike Rapoport  * Add new memblock region [@base, @base + @size) to the "memory"
68748a833ccSMike Rapoport  * type. See memblock_add_range() description for mode details
68848a833ccSMike Rapoport  *
68948a833ccSMike Rapoport  * Return:
69048a833ccSMike Rapoport  * 0 on success, -errno on failure.
69148a833ccSMike Rapoport  */
692f705ac4bSAlexander Kuleshov int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
6936a4055bcSAlexander Kuleshov {
6945d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
6955d63f81cSMiles Chen 
6965d63f81cSMiles Chen 	memblock_dbg("memblock_add: [%pa-%pa] %pF\n",
6975d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
6986a4055bcSAlexander Kuleshov 
699f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
70095f72d1eSYinghai Lu }
70195f72d1eSYinghai Lu 
7026a9ceb31STejun Heo /**
7036a9ceb31STejun Heo  * memblock_isolate_range - isolate given range into disjoint memblocks
7046a9ceb31STejun Heo  * @type: memblock type to isolate range for
7056a9ceb31STejun Heo  * @base: base of range to isolate
7066a9ceb31STejun Heo  * @size: size of range to isolate
7076a9ceb31STejun Heo  * @start_rgn: out parameter for the start of isolated region
7086a9ceb31STejun Heo  * @end_rgn: out parameter for the end of isolated region
7096a9ceb31STejun Heo  *
7106a9ceb31STejun Heo  * Walk @type and ensure that regions don't cross the boundaries defined by
7116a9ceb31STejun Heo  * [@base, @base + @size).  Crossing regions are split at the boundaries,
7126a9ceb31STejun Heo  * which may create at most two more regions.  The index of the first
7136a9ceb31STejun Heo  * region inside the range is returned in *@start_rgn and end in *@end_rgn.
7146a9ceb31STejun Heo  *
71547cec443SMike Rapoport  * Return:
7166a9ceb31STejun Heo  * 0 on success, -errno on failure.
7176a9ceb31STejun Heo  */
7186a9ceb31STejun Heo static int __init_memblock memblock_isolate_range(struct memblock_type *type,
7196a9ceb31STejun Heo 					phys_addr_t base, phys_addr_t size,
7206a9ceb31STejun Heo 					int *start_rgn, int *end_rgn)
7216a9ceb31STejun Heo {
722eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
7238c9c1701SAlexander Kuleshov 	int idx;
7248c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
7256a9ceb31STejun Heo 
7266a9ceb31STejun Heo 	*start_rgn = *end_rgn = 0;
7276a9ceb31STejun Heo 
728b3dc627cSTejun Heo 	if (!size)
729b3dc627cSTejun Heo 		return 0;
730b3dc627cSTejun Heo 
7316a9ceb31STejun Heo 	/* we'll create at most two more regions */
7326a9ceb31STejun Heo 	while (type->cnt + 2 > type->max)
73348c3b583SGreg Pearson 		if (memblock_double_array(type, base, size) < 0)
7346a9ceb31STejun Heo 			return -ENOMEM;
7356a9ceb31STejun Heo 
73666e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
7376a9ceb31STejun Heo 		phys_addr_t rbase = rgn->base;
7386a9ceb31STejun Heo 		phys_addr_t rend = rbase + rgn->size;
7396a9ceb31STejun Heo 
7406a9ceb31STejun Heo 		if (rbase >= end)
7416a9ceb31STejun Heo 			break;
7426a9ceb31STejun Heo 		if (rend <= base)
7436a9ceb31STejun Heo 			continue;
7446a9ceb31STejun Heo 
7456a9ceb31STejun Heo 		if (rbase < base) {
7466a9ceb31STejun Heo 			/*
7476a9ceb31STejun Heo 			 * @rgn intersects from below.  Split and continue
7486a9ceb31STejun Heo 			 * to process the next region - the new top half.
7496a9ceb31STejun Heo 			 */
7506a9ceb31STejun Heo 			rgn->base = base;
7511440c4e2STejun Heo 			rgn->size -= base - rbase;
7521440c4e2STejun Heo 			type->total_size -= base - rbase;
7538c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx, rbase, base - rbase,
75466a20757STang Chen 					       memblock_get_region_node(rgn),
75566a20757STang Chen 					       rgn->flags);
7566a9ceb31STejun Heo 		} else if (rend > end) {
7576a9ceb31STejun Heo 			/*
7586a9ceb31STejun Heo 			 * @rgn intersects from above.  Split and redo the
7596a9ceb31STejun Heo 			 * current region - the new bottom half.
7606a9ceb31STejun Heo 			 */
7616a9ceb31STejun Heo 			rgn->base = end;
7621440c4e2STejun Heo 			rgn->size -= end - rbase;
7631440c4e2STejun Heo 			type->total_size -= end - rbase;
7648c9c1701SAlexander Kuleshov 			memblock_insert_region(type, idx--, rbase, end - rbase,
76566a20757STang Chen 					       memblock_get_region_node(rgn),
76666a20757STang Chen 					       rgn->flags);
7676a9ceb31STejun Heo 		} else {
7686a9ceb31STejun Heo 			/* @rgn is fully contained, record it */
7696a9ceb31STejun Heo 			if (!*end_rgn)
7708c9c1701SAlexander Kuleshov 				*start_rgn = idx;
7718c9c1701SAlexander Kuleshov 			*end_rgn = idx + 1;
7726a9ceb31STejun Heo 		}
7736a9ceb31STejun Heo 	}
7746a9ceb31STejun Heo 
7756a9ceb31STejun Heo 	return 0;
7766a9ceb31STejun Heo }
7776a9ceb31STejun Heo 
77835bd16a2SAlexander Kuleshov static int __init_memblock memblock_remove_range(struct memblock_type *type,
7798f7a6605SBenjamin Herrenschmidt 					  phys_addr_t base, phys_addr_t size)
78095f72d1eSYinghai Lu {
78171936180STejun Heo 	int start_rgn, end_rgn;
78271936180STejun Heo 	int i, ret;
78395f72d1eSYinghai Lu 
78471936180STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
78571936180STejun Heo 	if (ret)
78671936180STejun Heo 		return ret;
78795f72d1eSYinghai Lu 
78871936180STejun Heo 	for (i = end_rgn - 1; i >= start_rgn; i--)
78971936180STejun Heo 		memblock_remove_region(type, i);
79095f72d1eSYinghai Lu 	return 0;
79195f72d1eSYinghai Lu }
79295f72d1eSYinghai Lu 
793581adcbeSTejun Heo int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
79495f72d1eSYinghai Lu {
79525cf23d7SMinchan Kim 	phys_addr_t end = base + size - 1;
79625cf23d7SMinchan Kim 
79725cf23d7SMinchan Kim 	memblock_dbg("memblock_remove: [%pa-%pa] %pS\n",
79825cf23d7SMinchan Kim 		     &base, &end, (void *)_RET_IP_);
79925cf23d7SMinchan Kim 
800f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.memory, base, size);
80195f72d1eSYinghai Lu }
80295f72d1eSYinghai Lu 
803f1af9d3aSPhilipp Hachtmann 
804581adcbeSTejun Heo int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
80595f72d1eSYinghai Lu {
8065d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8075d63f81cSMiles Chen 
8085d63f81cSMiles Chen 	memblock_dbg("   memblock_free: [%pa-%pa] %pF\n",
8095d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
81024aa0788STejun Heo 
8119099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
812f1af9d3aSPhilipp Hachtmann 	return memblock_remove_range(&memblock.reserved, base, size);
81395f72d1eSYinghai Lu }
81495f72d1eSYinghai Lu 
815f705ac4bSAlexander Kuleshov int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
81695f72d1eSYinghai Lu {
8175d63f81cSMiles Chen 	phys_addr_t end = base + size - 1;
8185d63f81cSMiles Chen 
8195d63f81cSMiles Chen 	memblock_dbg("memblock_reserve: [%pa-%pa] %pF\n",
8205d63f81cSMiles Chen 		     &base, &end, (void *)_RET_IP_);
82195f72d1eSYinghai Lu 
822f705ac4bSAlexander Kuleshov 	return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
82395f72d1eSYinghai Lu }
82495f72d1eSYinghai Lu 
82535fd0808STejun Heo /**
82647cec443SMike Rapoport  * memblock_setclr_flag - set or clear flag for a memory region
82747cec443SMike Rapoport  * @base: base address of the region
82847cec443SMike Rapoport  * @size: size of the region
82947cec443SMike Rapoport  * @set: set or clear the flag
83047cec443SMike Rapoport  * @flag: the flag to udpate
83166b16edfSTang Chen  *
8324308ce17STony Luck  * This function isolates region [@base, @base + @size), and sets/clears flag
83366b16edfSTang Chen  *
83447cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
83566b16edfSTang Chen  */
8364308ce17STony Luck static int __init_memblock memblock_setclr_flag(phys_addr_t base,
8374308ce17STony Luck 				phys_addr_t size, int set, int flag)
83866b16edfSTang Chen {
83966b16edfSTang Chen 	struct memblock_type *type = &memblock.memory;
84066b16edfSTang Chen 	int i, ret, start_rgn, end_rgn;
84166b16edfSTang Chen 
84266b16edfSTang Chen 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
84366b16edfSTang Chen 	if (ret)
84466b16edfSTang Chen 		return ret;
84566b16edfSTang Chen 
84666b16edfSTang Chen 	for (i = start_rgn; i < end_rgn; i++)
8474308ce17STony Luck 		if (set)
8484308ce17STony Luck 			memblock_set_region_flags(&type->regions[i], flag);
8494308ce17STony Luck 		else
8504308ce17STony Luck 			memblock_clear_region_flags(&type->regions[i], flag);
85166b16edfSTang Chen 
85266b16edfSTang Chen 	memblock_merge_regions(type);
85366b16edfSTang Chen 	return 0;
85466b16edfSTang Chen }
85566b16edfSTang Chen 
85666b16edfSTang Chen /**
8574308ce17STony Luck  * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
8584308ce17STony Luck  * @base: the base phys addr of the region
8594308ce17STony Luck  * @size: the size of the region
8604308ce17STony Luck  *
86147cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
8624308ce17STony Luck  */
8634308ce17STony Luck int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
8644308ce17STony Luck {
8654308ce17STony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
8664308ce17STony Luck }
8674308ce17STony Luck 
8684308ce17STony Luck /**
86966b16edfSTang Chen  * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
87066b16edfSTang Chen  * @base: the base phys addr of the region
87166b16edfSTang Chen  * @size: the size of the region
87266b16edfSTang Chen  *
87347cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
87466b16edfSTang Chen  */
87566b16edfSTang Chen int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
87666b16edfSTang Chen {
8774308ce17STony Luck 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
87866b16edfSTang Chen }
87966b16edfSTang Chen 
88066b16edfSTang Chen /**
881a3f5bafcSTony Luck  * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
882a3f5bafcSTony Luck  * @base: the base phys addr of the region
883a3f5bafcSTony Luck  * @size: the size of the region
884a3f5bafcSTony Luck  *
88547cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
886a3f5bafcSTony Luck  */
887a3f5bafcSTony Luck int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
888a3f5bafcSTony Luck {
889a3f5bafcSTony Luck 	system_has_some_mirror = true;
890a3f5bafcSTony Luck 
891a3f5bafcSTony Luck 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
892a3f5bafcSTony Luck }
893a3f5bafcSTony Luck 
894bf3d3cc5SArd Biesheuvel /**
895bf3d3cc5SArd Biesheuvel  * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
896bf3d3cc5SArd Biesheuvel  * @base: the base phys addr of the region
897bf3d3cc5SArd Biesheuvel  * @size: the size of the region
898bf3d3cc5SArd Biesheuvel  *
89947cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
900bf3d3cc5SArd Biesheuvel  */
901bf3d3cc5SArd Biesheuvel int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
902bf3d3cc5SArd Biesheuvel {
903bf3d3cc5SArd Biesheuvel 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
904bf3d3cc5SArd Biesheuvel }
905a3f5bafcSTony Luck 
906a3f5bafcSTony Luck /**
9074c546b8aSAKASHI Takahiro  * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
9084c546b8aSAKASHI Takahiro  * @base: the base phys addr of the region
9094c546b8aSAKASHI Takahiro  * @size: the size of the region
9104c546b8aSAKASHI Takahiro  *
91147cec443SMike Rapoport  * Return: 0 on success, -errno on failure.
9124c546b8aSAKASHI Takahiro  */
9134c546b8aSAKASHI Takahiro int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
9144c546b8aSAKASHI Takahiro {
9154c546b8aSAKASHI Takahiro 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
9164c546b8aSAKASHI Takahiro }
9174c546b8aSAKASHI Takahiro 
9184c546b8aSAKASHI Takahiro /**
9198e7a7f86SRobin Holt  * __next_reserved_mem_region - next function for for_each_reserved_region()
9208e7a7f86SRobin Holt  * @idx: pointer to u64 loop variable
9218e7a7f86SRobin Holt  * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
9228e7a7f86SRobin Holt  * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
9238e7a7f86SRobin Holt  *
9248e7a7f86SRobin Holt  * Iterate over all reserved memory regions.
9258e7a7f86SRobin Holt  */
9268e7a7f86SRobin Holt void __init_memblock __next_reserved_mem_region(u64 *idx,
9278e7a7f86SRobin Holt 					   phys_addr_t *out_start,
9288e7a7f86SRobin Holt 					   phys_addr_t *out_end)
9298e7a7f86SRobin Holt {
930567d117bSAlexander Kuleshov 	struct memblock_type *type = &memblock.reserved;
9318e7a7f86SRobin Holt 
932cd33a76bSRichard Leitner 	if (*idx < type->cnt) {
933567d117bSAlexander Kuleshov 		struct memblock_region *r = &type->regions[*idx];
9348e7a7f86SRobin Holt 		phys_addr_t base = r->base;
9358e7a7f86SRobin Holt 		phys_addr_t size = r->size;
9368e7a7f86SRobin Holt 
9378e7a7f86SRobin Holt 		if (out_start)
9388e7a7f86SRobin Holt 			*out_start = base;
9398e7a7f86SRobin Holt 		if (out_end)
9408e7a7f86SRobin Holt 			*out_end = base + size - 1;
9418e7a7f86SRobin Holt 
9428e7a7f86SRobin Holt 		*idx += 1;
9438e7a7f86SRobin Holt 		return;
9448e7a7f86SRobin Holt 	}
9458e7a7f86SRobin Holt 
9468e7a7f86SRobin Holt 	/* signal end of iteration */
9478e7a7f86SRobin Holt 	*idx = ULLONG_MAX;
9488e7a7f86SRobin Holt }
9498e7a7f86SRobin Holt 
9508e7a7f86SRobin Holt /**
951f1af9d3aSPhilipp Hachtmann  * __next__mem_range - next function for for_each_free_mem_range() etc.
95235fd0808STejun Heo  * @idx: pointer to u64 loop variable
953b1154233SGrygorii Strashko  * @nid: node selector, %NUMA_NO_NODE for all nodes
954fc6daaf9STony Luck  * @flags: pick from blocks based on memory attributes
955f1af9d3aSPhilipp Hachtmann  * @type_a: pointer to memblock_type from where the range is taken
956f1af9d3aSPhilipp Hachtmann  * @type_b: pointer to memblock_type which excludes memory from being taken
957dad7557eSWanpeng Li  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
958dad7557eSWanpeng Li  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
959dad7557eSWanpeng Li  * @out_nid: ptr to int for nid of the range, can be %NULL
96035fd0808STejun Heo  *
961f1af9d3aSPhilipp Hachtmann  * Find the first area from *@idx which matches @nid, fill the out
96235fd0808STejun Heo  * parameters, and update *@idx for the next iteration.  The lower 32bit of
963f1af9d3aSPhilipp Hachtmann  * *@idx contains index into type_a and the upper 32bit indexes the
964f1af9d3aSPhilipp Hachtmann  * areas before each region in type_b.	For example, if type_b regions
96535fd0808STejun Heo  * look like the following,
96635fd0808STejun Heo  *
96735fd0808STejun Heo  *	0:[0-16), 1:[32-48), 2:[128-130)
96835fd0808STejun Heo  *
96935fd0808STejun Heo  * The upper 32bit indexes the following regions.
97035fd0808STejun Heo  *
97135fd0808STejun Heo  *	0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
97235fd0808STejun Heo  *
97335fd0808STejun Heo  * As both region arrays are sorted, the function advances the two indices
97435fd0808STejun Heo  * in lockstep and returns each intersection.
97535fd0808STejun Heo  */
976e1720feeSMike Rapoport void __init_memblock __next_mem_range(u64 *idx, int nid,
977e1720feeSMike Rapoport 				      enum memblock_flags flags,
978f1af9d3aSPhilipp Hachtmann 				      struct memblock_type *type_a,
979f1af9d3aSPhilipp Hachtmann 				      struct memblock_type *type_b,
98035fd0808STejun Heo 				      phys_addr_t *out_start,
98135fd0808STejun Heo 				      phys_addr_t *out_end, int *out_nid)
98235fd0808STejun Heo {
983f1af9d3aSPhilipp Hachtmann 	int idx_a = *idx & 0xffffffff;
984f1af9d3aSPhilipp Hachtmann 	int idx_b = *idx >> 32;
985b1154233SGrygorii Strashko 
986f1af9d3aSPhilipp Hachtmann 	if (WARN_ONCE(nid == MAX_NUMNODES,
987f1af9d3aSPhilipp Hachtmann 	"Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
988560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
98935fd0808STejun Heo 
990f1af9d3aSPhilipp Hachtmann 	for (; idx_a < type_a->cnt; idx_a++) {
991f1af9d3aSPhilipp Hachtmann 		struct memblock_region *m = &type_a->regions[idx_a];
992f1af9d3aSPhilipp Hachtmann 
99335fd0808STejun Heo 		phys_addr_t m_start = m->base;
99435fd0808STejun Heo 		phys_addr_t m_end = m->base + m->size;
995f1af9d3aSPhilipp Hachtmann 		int	    m_nid = memblock_get_region_node(m);
99635fd0808STejun Heo 
99735fd0808STejun Heo 		/* only memory regions are associated with nodes, check it */
998f1af9d3aSPhilipp Hachtmann 		if (nid != NUMA_NO_NODE && nid != m_nid)
99935fd0808STejun Heo 			continue;
100035fd0808STejun Heo 
10010a313a99SXishi Qiu 		/* skip hotpluggable memory regions if needed */
10020a313a99SXishi Qiu 		if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
10030a313a99SXishi Qiu 			continue;
10040a313a99SXishi Qiu 
1005a3f5bafcSTony Luck 		/* if we want mirror memory skip non-mirror memory regions */
1006a3f5bafcSTony Luck 		if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1007a3f5bafcSTony Luck 			continue;
1008a3f5bafcSTony Luck 
1009bf3d3cc5SArd Biesheuvel 		/* skip nomap memory unless we were asked for it explicitly */
1010bf3d3cc5SArd Biesheuvel 		if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
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 
11147bd0b0f0STejun Heo 		/* only memory regions are associated with nodes, check it */
1115f1af9d3aSPhilipp Hachtmann 		if (nid != NUMA_NO_NODE && nid != m_nid)
11167bd0b0f0STejun Heo 			continue;
11177bd0b0f0STejun Heo 
111855ac590cSTang Chen 		/* skip hotpluggable memory regions if needed */
111955ac590cSTang Chen 		if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
112055ac590cSTang Chen 			continue;
112155ac590cSTang Chen 
1122a3f5bafcSTony Luck 		/* if we want mirror memory skip non-mirror memory regions */
1123a3f5bafcSTony Luck 		if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1124a3f5bafcSTony Luck 			continue;
1125a3f5bafcSTony Luck 
1126bf3d3cc5SArd Biesheuvel 		/* skip nomap memory unless we were asked for it explicitly */
1127bf3d3cc5SArd Biesheuvel 		if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1128bf3d3cc5SArd Biesheuvel 			continue;
1129bf3d3cc5SArd Biesheuvel 
1130f1af9d3aSPhilipp Hachtmann 		if (!type_b) {
1131f1af9d3aSPhilipp Hachtmann 			if (out_start)
1132f1af9d3aSPhilipp Hachtmann 				*out_start = m_start;
1133f1af9d3aSPhilipp Hachtmann 			if (out_end)
1134f1af9d3aSPhilipp Hachtmann 				*out_end = m_end;
1135f1af9d3aSPhilipp Hachtmann 			if (out_nid)
1136f1af9d3aSPhilipp Hachtmann 				*out_nid = m_nid;
1137fb399b48Szijun_hu 			idx_a--;
1138f1af9d3aSPhilipp Hachtmann 			*idx = (u32)idx_a | (u64)idx_b << 32;
1139f1af9d3aSPhilipp Hachtmann 			return;
1140f1af9d3aSPhilipp Hachtmann 		}
11417bd0b0f0STejun Heo 
1142f1af9d3aSPhilipp Hachtmann 		/* scan areas before each reservation */
1143f1af9d3aSPhilipp Hachtmann 		for (; idx_b >= 0; idx_b--) {
1144f1af9d3aSPhilipp Hachtmann 			struct memblock_region *r;
1145f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_start;
1146f1af9d3aSPhilipp Hachtmann 			phys_addr_t r_end;
1147f1af9d3aSPhilipp Hachtmann 
1148f1af9d3aSPhilipp Hachtmann 			r = &type_b->regions[idx_b];
1149f1af9d3aSPhilipp Hachtmann 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1150f1af9d3aSPhilipp Hachtmann 			r_end = idx_b < type_b->cnt ?
11511c4bc43dSStefan Agner 				r->base : PHYS_ADDR_MAX;
1152f1af9d3aSPhilipp Hachtmann 			/*
1153f1af9d3aSPhilipp Hachtmann 			 * if idx_b advanced past idx_a,
1154f1af9d3aSPhilipp Hachtmann 			 * break out to advance idx_a
1155f1af9d3aSPhilipp Hachtmann 			 */
1156f1af9d3aSPhilipp Hachtmann 
11577bd0b0f0STejun Heo 			if (r_end <= m_start)
11587bd0b0f0STejun Heo 				break;
11597bd0b0f0STejun Heo 			/* if the two regions intersect, we're done */
11607bd0b0f0STejun Heo 			if (m_end > r_start) {
11617bd0b0f0STejun Heo 				if (out_start)
11627bd0b0f0STejun Heo 					*out_start = max(m_start, r_start);
11637bd0b0f0STejun Heo 				if (out_end)
11647bd0b0f0STejun Heo 					*out_end = min(m_end, r_end);
11657bd0b0f0STejun Heo 				if (out_nid)
1166f1af9d3aSPhilipp Hachtmann 					*out_nid = m_nid;
11677bd0b0f0STejun Heo 				if (m_start >= r_start)
1168f1af9d3aSPhilipp Hachtmann 					idx_a--;
11697bd0b0f0STejun Heo 				else
1170f1af9d3aSPhilipp Hachtmann 					idx_b--;
1171f1af9d3aSPhilipp Hachtmann 				*idx = (u32)idx_a | (u64)idx_b << 32;
11727bd0b0f0STejun Heo 				return;
11737bd0b0f0STejun Heo 			}
11747bd0b0f0STejun Heo 		}
11757bd0b0f0STejun Heo 	}
1176f1af9d3aSPhilipp Hachtmann 	/* signal end of iteration */
11777bd0b0f0STejun Heo 	*idx = ULLONG_MAX;
11787bd0b0f0STejun Heo }
11797bd0b0f0STejun Heo 
11807c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
11817c0caeb8STejun Heo /*
11827c0caeb8STejun Heo  * Common iterator interface used to define for_each_mem_range().
11837c0caeb8STejun Heo  */
11847c0caeb8STejun Heo void __init_memblock __next_mem_pfn_range(int *idx, int nid,
11857c0caeb8STejun Heo 				unsigned long *out_start_pfn,
11867c0caeb8STejun Heo 				unsigned long *out_end_pfn, int *out_nid)
11877c0caeb8STejun Heo {
11887c0caeb8STejun Heo 	struct memblock_type *type = &memblock.memory;
11897c0caeb8STejun Heo 	struct memblock_region *r;
11907c0caeb8STejun Heo 
11917c0caeb8STejun Heo 	while (++*idx < type->cnt) {
11927c0caeb8STejun Heo 		r = &type->regions[*idx];
11937c0caeb8STejun Heo 
11947c0caeb8STejun Heo 		if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
11957c0caeb8STejun Heo 			continue;
11967c0caeb8STejun Heo 		if (nid == MAX_NUMNODES || nid == r->nid)
11977c0caeb8STejun Heo 			break;
11987c0caeb8STejun Heo 	}
11997c0caeb8STejun Heo 	if (*idx >= type->cnt) {
12007c0caeb8STejun Heo 		*idx = -1;
12017c0caeb8STejun Heo 		return;
12027c0caeb8STejun Heo 	}
12037c0caeb8STejun Heo 
12047c0caeb8STejun Heo 	if (out_start_pfn)
12057c0caeb8STejun Heo 		*out_start_pfn = PFN_UP(r->base);
12067c0caeb8STejun Heo 	if (out_end_pfn)
12077c0caeb8STejun Heo 		*out_end_pfn = PFN_DOWN(r->base + r->size);
12087c0caeb8STejun Heo 	if (out_nid)
12097c0caeb8STejun Heo 		*out_nid = r->nid;
12107c0caeb8STejun Heo }
12117c0caeb8STejun Heo 
12127c0caeb8STejun Heo /**
12137c0caeb8STejun Heo  * memblock_set_node - set node ID on memblock regions
12147c0caeb8STejun Heo  * @base: base of area to set node ID for
12157c0caeb8STejun Heo  * @size: size of area to set node ID for
1216e7e8de59STang Chen  * @type: memblock type to set node ID for
12177c0caeb8STejun Heo  * @nid: node ID to set
12187c0caeb8STejun Heo  *
1219e7e8de59STang Chen  * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
12207c0caeb8STejun Heo  * Regions which cross the area boundaries are split as necessary.
12217c0caeb8STejun Heo  *
122247cec443SMike Rapoport  * Return:
12237c0caeb8STejun Heo  * 0 on success, -errno on failure.
12247c0caeb8STejun Heo  */
12257c0caeb8STejun Heo int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1226e7e8de59STang Chen 				      struct memblock_type *type, int nid)
12277c0caeb8STejun Heo {
12286a9ceb31STejun Heo 	int start_rgn, end_rgn;
12296a9ceb31STejun Heo 	int i, ret;
12307c0caeb8STejun Heo 
12316a9ceb31STejun Heo 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
12326a9ceb31STejun Heo 	if (ret)
12336a9ceb31STejun Heo 		return ret;
12347c0caeb8STejun Heo 
12356a9ceb31STejun Heo 	for (i = start_rgn; i < end_rgn; i++)
1236e9d24ad3SWanpeng Li 		memblock_set_region_node(&type->regions[i], nid);
12377c0caeb8STejun Heo 
12387c0caeb8STejun Heo 	memblock_merge_regions(type);
12397c0caeb8STejun Heo 	return 0;
12407c0caeb8STejun Heo }
12417c0caeb8STejun Heo #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
12427c0caeb8STejun Heo 
12432bfc2862SAkinobu Mita static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
12442bfc2862SAkinobu Mita 					phys_addr_t align, phys_addr_t start,
1245e1720feeSMike Rapoport 					phys_addr_t end, int nid,
1246e1720feeSMike Rapoport 					enum memblock_flags flags)
124795f72d1eSYinghai Lu {
12486ed311b2SBenjamin Herrenschmidt 	phys_addr_t found;
124995f72d1eSYinghai Lu 
1250*2f770806SMike Rapoport 	if (!align) {
1251*2f770806SMike Rapoport 		/* Can't use WARNs this early in boot on powerpc */
1252*2f770806SMike Rapoport 		dump_stack();
1253*2f770806SMike Rapoport 		align = SMP_CACHE_BYTES;
1254*2f770806SMike Rapoport 	}
1255*2f770806SMike Rapoport 
1256fc6daaf9STony Luck 	found = memblock_find_in_range_node(size, align, start, end, nid,
1257fc6daaf9STony Luck 					    flags);
1258aedf95eaSCatalin Marinas 	if (found && !memblock_reserve(found, size)) {
1259aedf95eaSCatalin Marinas 		/*
1260aedf95eaSCatalin Marinas 		 * The min_count is set to 0 so that memblock allocations are
1261aedf95eaSCatalin Marinas 		 * never reported as leaks.
1262aedf95eaSCatalin Marinas 		 */
12639099daedSCatalin Marinas 		kmemleak_alloc_phys(found, size, 0, 0);
12646ed311b2SBenjamin Herrenschmidt 		return found;
1265aedf95eaSCatalin Marinas 	}
12666ed311b2SBenjamin Herrenschmidt 	return 0;
126795f72d1eSYinghai Lu }
126895f72d1eSYinghai Lu 
12692bfc2862SAkinobu Mita phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
1270fc6daaf9STony Luck 					phys_addr_t start, phys_addr_t end,
1271e1720feeSMike Rapoport 					enum memblock_flags flags)
12722bfc2862SAkinobu Mita {
1273fc6daaf9STony Luck 	return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1274fc6daaf9STony Luck 					flags);
12752bfc2862SAkinobu Mita }
12762bfc2862SAkinobu Mita 
1277b575454fSNicholas Piggin phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
12782bfc2862SAkinobu Mita 					phys_addr_t align, phys_addr_t max_addr,
1279e1720feeSMike Rapoport 					int nid, enum memblock_flags flags)
12802bfc2862SAkinobu Mita {
1281fc6daaf9STony Luck 	return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags);
12822bfc2862SAkinobu Mita }
12832bfc2862SAkinobu Mita 
12849a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
12857bd0b0f0STejun Heo {
1286e1720feeSMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
1287a3f5bafcSTony Luck 	phys_addr_t ret;
1288a3f5bafcSTony Luck 
1289a3f5bafcSTony Luck again:
1290a3f5bafcSTony Luck 	ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE,
1291a3f5bafcSTony Luck 				      nid, flags);
1292a3f5bafcSTony Luck 
1293a3f5bafcSTony Luck 	if (!ret && (flags & MEMBLOCK_MIRROR)) {
1294a3f5bafcSTony Luck 		flags &= ~MEMBLOCK_MIRROR;
1295a3f5bafcSTony Luck 		goto again;
1296a3f5bafcSTony Luck 	}
1297a3f5bafcSTony Luck 	return ret;
12987bd0b0f0STejun Heo }
12997bd0b0f0STejun Heo 
13007bd0b0f0STejun Heo phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
13017bd0b0f0STejun Heo {
1302fc6daaf9STony Luck 	return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE,
1303fc6daaf9STony Luck 				       MEMBLOCK_NONE);
13047bd0b0f0STejun Heo }
13057bd0b0f0STejun Heo 
13066ed311b2SBenjamin Herrenschmidt phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
130795f72d1eSYinghai Lu {
13086ed311b2SBenjamin Herrenschmidt 	phys_addr_t alloc;
13096ed311b2SBenjamin Herrenschmidt 
13106ed311b2SBenjamin Herrenschmidt 	alloc = __memblock_alloc_base(size, align, max_addr);
13116ed311b2SBenjamin Herrenschmidt 
13126ed311b2SBenjamin Herrenschmidt 	if (alloc == 0)
13135d63f81cSMiles Chen 		panic("ERROR: Failed to allocate %pa bytes below %pa.\n",
13145d63f81cSMiles Chen 		      &size, &max_addr);
13156ed311b2SBenjamin Herrenschmidt 
13166ed311b2SBenjamin Herrenschmidt 	return alloc;
131795f72d1eSYinghai Lu }
131895f72d1eSYinghai Lu 
13199a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc(phys_addr_t size, phys_addr_t align)
132095f72d1eSYinghai Lu {
13216ed311b2SBenjamin Herrenschmidt 	return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
132295f72d1eSYinghai Lu }
132395f72d1eSYinghai Lu 
13249a8dd708SMike Rapoport phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
13259d1e2492SBenjamin Herrenschmidt {
13269a8dd708SMike Rapoport 	phys_addr_t res = memblock_phys_alloc_nid(size, align, nid);
13279d1e2492SBenjamin Herrenschmidt 
13289d1e2492SBenjamin Herrenschmidt 	if (res)
13299d1e2492SBenjamin Herrenschmidt 		return res;
133015fb0972STejun Heo 	return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
133195f72d1eSYinghai Lu }
133295f72d1eSYinghai Lu 
133326f09e9bSSantosh Shilimkar /**
1334eb31d559SMike Rapoport  * memblock_alloc_internal - allocate boot memory block
133526f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
133626f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
133726f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region to allocate (phys address)
133826f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region to allocate (phys address)
133926f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
134026f09e9bSSantosh Shilimkar  *
134126f09e9bSSantosh Shilimkar  * The @min_addr limit is dropped if it can not be satisfied and the allocation
134226f09e9bSSantosh Shilimkar  * will fall back to memory below @min_addr. Also, allocation may fall back
134326f09e9bSSantosh Shilimkar  * to any node in the system if the specified node can not
134426f09e9bSSantosh Shilimkar  * hold the requested memory.
134526f09e9bSSantosh Shilimkar  *
134626f09e9bSSantosh Shilimkar  * The allocation is performed from memory region limited by
134797ad1087SMike Rapoport  * memblock.current_limit if @max_addr == %MEMBLOCK_ALLOC_ACCESSIBLE.
134826f09e9bSSantosh Shilimkar  *
134926f09e9bSSantosh Shilimkar  * The phys address of allocated boot memory block is converted to virtual and
135026f09e9bSSantosh Shilimkar  * allocated memory is reset to 0.
135126f09e9bSSantosh Shilimkar  *
135226f09e9bSSantosh Shilimkar  * In addition, function sets the min_count to 0 using kmemleak_alloc for
135326f09e9bSSantosh Shilimkar  * allocated boot memory block, so that it is never reported as leaks.
135426f09e9bSSantosh Shilimkar  *
135547cec443SMike Rapoport  * Return:
135626f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
135726f09e9bSSantosh Shilimkar  */
1358eb31d559SMike Rapoport static void * __init memblock_alloc_internal(
135926f09e9bSSantosh Shilimkar 				phys_addr_t size, phys_addr_t align,
136026f09e9bSSantosh Shilimkar 				phys_addr_t min_addr, phys_addr_t max_addr,
136126f09e9bSSantosh Shilimkar 				int nid)
136226f09e9bSSantosh Shilimkar {
136326f09e9bSSantosh Shilimkar 	phys_addr_t alloc;
136426f09e9bSSantosh Shilimkar 	void *ptr;
1365e1720feeSMike Rapoport 	enum memblock_flags flags = choose_memblock_flags();
136626f09e9bSSantosh Shilimkar 
1367560dca27SGrygorii Strashko 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1368560dca27SGrygorii Strashko 		nid = NUMA_NO_NODE;
136926f09e9bSSantosh Shilimkar 
137026f09e9bSSantosh Shilimkar 	/*
137126f09e9bSSantosh Shilimkar 	 * Detect any accidental use of these APIs after slab is ready, as at
137226f09e9bSSantosh Shilimkar 	 * this moment memblock may be deinitialized already and its
1373c6ffc5caSMike Rapoport 	 * internal data may be destroyed (after execution of memblock_free_all)
137426f09e9bSSantosh Shilimkar 	 */
137526f09e9bSSantosh Shilimkar 	if (WARN_ON_ONCE(slab_is_available()))
137626f09e9bSSantosh Shilimkar 		return kzalloc_node(size, GFP_NOWAIT, nid);
137726f09e9bSSantosh Shilimkar 
1378*2f770806SMike Rapoport 	if (!align) {
1379*2f770806SMike Rapoport 		dump_stack();
1380*2f770806SMike Rapoport 		align = SMP_CACHE_BYTES;
1381*2f770806SMike Rapoport 	}
1382*2f770806SMike Rapoport 
1383f544e14fSYinghai Lu 	if (max_addr > memblock.current_limit)
1384f544e14fSYinghai Lu 		max_addr = memblock.current_limit;
138526f09e9bSSantosh Shilimkar again:
138626f09e9bSSantosh Shilimkar 	alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
1387a3f5bafcSTony Luck 					    nid, flags);
13887d41c03eSWei Yang 	if (alloc && !memblock_reserve(alloc, size))
138926f09e9bSSantosh Shilimkar 		goto done;
139026f09e9bSSantosh Shilimkar 
139126f09e9bSSantosh Shilimkar 	if (nid != NUMA_NO_NODE) {
139226f09e9bSSantosh Shilimkar 		alloc = memblock_find_in_range_node(size, align, min_addr,
1393fc6daaf9STony Luck 						    max_addr, NUMA_NO_NODE,
1394a3f5bafcSTony Luck 						    flags);
13957d41c03eSWei Yang 		if (alloc && !memblock_reserve(alloc, size))
139626f09e9bSSantosh Shilimkar 			goto done;
139726f09e9bSSantosh Shilimkar 	}
139826f09e9bSSantosh Shilimkar 
139926f09e9bSSantosh Shilimkar 	if (min_addr) {
140026f09e9bSSantosh Shilimkar 		min_addr = 0;
140126f09e9bSSantosh Shilimkar 		goto again;
140226f09e9bSSantosh Shilimkar 	}
140326f09e9bSSantosh Shilimkar 
1404a3f5bafcSTony Luck 	if (flags & MEMBLOCK_MIRROR) {
1405a3f5bafcSTony Luck 		flags &= ~MEMBLOCK_MIRROR;
1406a3f5bafcSTony Luck 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1407a3f5bafcSTony Luck 			&size);
1408a3f5bafcSTony Luck 		goto again;
1409a3f5bafcSTony Luck 	}
1410a3f5bafcSTony Luck 
1411a3f5bafcSTony Luck 	return NULL;
141226f09e9bSSantosh Shilimkar done:
141326f09e9bSSantosh Shilimkar 	ptr = phys_to_virt(alloc);
141426f09e9bSSantosh Shilimkar 
141526f09e9bSSantosh Shilimkar 	/*
141626f09e9bSSantosh Shilimkar 	 * The min_count is set to 0 so that bootmem allocated blocks
141726f09e9bSSantosh Shilimkar 	 * are never reported as leaks. This is because many of these blocks
141826f09e9bSSantosh Shilimkar 	 * are only referred via the physical address which is not
141926f09e9bSSantosh Shilimkar 	 * looked up by kmemleak.
142026f09e9bSSantosh Shilimkar 	 */
142126f09e9bSSantosh Shilimkar 	kmemleak_alloc(ptr, size, 0, 0);
142226f09e9bSSantosh Shilimkar 
142326f09e9bSSantosh Shilimkar 	return ptr;
142426f09e9bSSantosh Shilimkar }
142526f09e9bSSantosh Shilimkar 
142626f09e9bSSantosh Shilimkar /**
1427eb31d559SMike Rapoport  * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
1428ea1f5f37SPavel Tatashin  * memory and without panicking
1429ea1f5f37SPavel Tatashin  * @size: size of memory block to be allocated in bytes
1430ea1f5f37SPavel Tatashin  * @align: alignment of the region and block's size
1431ea1f5f37SPavel Tatashin  * @min_addr: the lower bound of the memory region from where the allocation
1432ea1f5f37SPavel Tatashin  *	  is preferred (phys address)
1433ea1f5f37SPavel Tatashin  * @max_addr: the upper bound of the memory region from where the allocation
143497ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1435ea1f5f37SPavel Tatashin  *	      allocate only from memory limited by memblock.current_limit value
1436ea1f5f37SPavel Tatashin  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1437ea1f5f37SPavel Tatashin  *
1438ea1f5f37SPavel Tatashin  * Public function, provides additional debug information (including caller
1439ea1f5f37SPavel Tatashin  * info), if enabled. Does not zero allocated memory, does not panic if request
1440ea1f5f37SPavel Tatashin  * cannot be satisfied.
1441ea1f5f37SPavel Tatashin  *
144247cec443SMike Rapoport  * Return:
1443ea1f5f37SPavel Tatashin  * Virtual address of allocated memory block on success, NULL on failure.
1444ea1f5f37SPavel Tatashin  */
1445eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_raw(
1446ea1f5f37SPavel Tatashin 			phys_addr_t size, phys_addr_t align,
1447ea1f5f37SPavel Tatashin 			phys_addr_t min_addr, phys_addr_t max_addr,
1448ea1f5f37SPavel Tatashin 			int nid)
1449ea1f5f37SPavel Tatashin {
1450ea1f5f37SPavel Tatashin 	void *ptr;
1451ea1f5f37SPavel Tatashin 
1452a36aab89SMike Rapoport 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1453a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1454a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1455ea1f5f37SPavel Tatashin 
1456eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
1457ea1f5f37SPavel Tatashin 					   min_addr, max_addr, nid);
1458ea1f5f37SPavel Tatashin 	if (ptr && size > 0)
1459f682a97aSAlexander Duyck 		page_init_poison(ptr, size);
1460f682a97aSAlexander Duyck 
1461ea1f5f37SPavel Tatashin 	return ptr;
1462ea1f5f37SPavel Tatashin }
1463ea1f5f37SPavel Tatashin 
1464ea1f5f37SPavel Tatashin /**
1465eb31d559SMike Rapoport  * memblock_alloc_try_nid_nopanic - allocate boot memory block
146626f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
146726f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
146826f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region from where the allocation
146926f09e9bSSantosh Shilimkar  *	  is preferred (phys address)
147026f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region from where the allocation
147197ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
147226f09e9bSSantosh Shilimkar  *	      allocate only from memory limited by memblock.current_limit value
147326f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
147426f09e9bSSantosh Shilimkar  *
1475ea1f5f37SPavel Tatashin  * Public function, provides additional debug information (including caller
1476ea1f5f37SPavel Tatashin  * info), if enabled. This function zeroes the allocated memory.
147726f09e9bSSantosh Shilimkar  *
147847cec443SMike Rapoport  * Return:
147926f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
148026f09e9bSSantosh Shilimkar  */
1481eb31d559SMike Rapoport void * __init memblock_alloc_try_nid_nopanic(
148226f09e9bSSantosh Shilimkar 				phys_addr_t size, phys_addr_t align,
148326f09e9bSSantosh Shilimkar 				phys_addr_t min_addr, phys_addr_t max_addr,
148426f09e9bSSantosh Shilimkar 				int nid)
148526f09e9bSSantosh Shilimkar {
1486ea1f5f37SPavel Tatashin 	void *ptr;
1487ea1f5f37SPavel Tatashin 
1488a36aab89SMike Rapoport 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1489a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1490a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1491ea1f5f37SPavel Tatashin 
1492eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
1493ea1f5f37SPavel Tatashin 					   min_addr, max_addr, nid);
1494ea1f5f37SPavel Tatashin 	if (ptr)
1495ea1f5f37SPavel Tatashin 		memset(ptr, 0, size);
1496ea1f5f37SPavel Tatashin 	return ptr;
149726f09e9bSSantosh Shilimkar }
149826f09e9bSSantosh Shilimkar 
149926f09e9bSSantosh Shilimkar /**
1500eb31d559SMike Rapoport  * memblock_alloc_try_nid - allocate boot memory block with panicking
150126f09e9bSSantosh Shilimkar  * @size: size of memory block to be allocated in bytes
150226f09e9bSSantosh Shilimkar  * @align: alignment of the region and block's size
150326f09e9bSSantosh Shilimkar  * @min_addr: the lower bound of the memory region from where the allocation
150426f09e9bSSantosh Shilimkar  *	  is preferred (phys address)
150526f09e9bSSantosh Shilimkar  * @max_addr: the upper bound of the memory region from where the allocation
150697ad1087SMike Rapoport  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
150726f09e9bSSantosh Shilimkar  *	      allocate only from memory limited by memblock.current_limit value
150826f09e9bSSantosh Shilimkar  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
150926f09e9bSSantosh Shilimkar  *
1510eb31d559SMike Rapoport  * Public panicking version of memblock_alloc_try_nid_nopanic()
151126f09e9bSSantosh Shilimkar  * which provides debug information (including caller info), if enabled,
151226f09e9bSSantosh Shilimkar  * and panics if the request can not be satisfied.
151326f09e9bSSantosh Shilimkar  *
151447cec443SMike Rapoport  * Return:
151526f09e9bSSantosh Shilimkar  * Virtual address of allocated memory block on success, NULL on failure.
151626f09e9bSSantosh Shilimkar  */
1517eb31d559SMike Rapoport void * __init memblock_alloc_try_nid(
151826f09e9bSSantosh Shilimkar 			phys_addr_t size, phys_addr_t align,
151926f09e9bSSantosh Shilimkar 			phys_addr_t min_addr, phys_addr_t max_addr,
152026f09e9bSSantosh Shilimkar 			int nid)
152126f09e9bSSantosh Shilimkar {
152226f09e9bSSantosh Shilimkar 	void *ptr;
152326f09e9bSSantosh Shilimkar 
1524a36aab89SMike Rapoport 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1525a36aab89SMike Rapoport 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1526a36aab89SMike Rapoport 		     &max_addr, (void *)_RET_IP_);
1527eb31d559SMike Rapoport 	ptr = memblock_alloc_internal(size, align,
152826f09e9bSSantosh Shilimkar 					   min_addr, max_addr, nid);
1529ea1f5f37SPavel Tatashin 	if (ptr) {
1530ea1f5f37SPavel Tatashin 		memset(ptr, 0, size);
153126f09e9bSSantosh Shilimkar 		return ptr;
1532ea1f5f37SPavel Tatashin 	}
153326f09e9bSSantosh Shilimkar 
1534a36aab89SMike Rapoport 	panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa\n",
1535a36aab89SMike Rapoport 	      __func__, (u64)size, (u64)align, nid, &min_addr, &max_addr);
153626f09e9bSSantosh Shilimkar 	return NULL;
153726f09e9bSSantosh Shilimkar }
153826f09e9bSSantosh Shilimkar 
153926f09e9bSSantosh Shilimkar /**
154026f09e9bSSantosh Shilimkar  * __memblock_free_early - free boot memory block
154126f09e9bSSantosh Shilimkar  * @base: phys starting address of the  boot memory block
154226f09e9bSSantosh Shilimkar  * @size: size of the boot memory block in bytes
154326f09e9bSSantosh Shilimkar  *
1544eb31d559SMike Rapoport  * Free boot memory block previously allocated by memblock_alloc_xx() API.
154526f09e9bSSantosh Shilimkar  * The freeing memory will not be released to the buddy allocator.
154626f09e9bSSantosh Shilimkar  */
154726f09e9bSSantosh Shilimkar void __init __memblock_free_early(phys_addr_t base, phys_addr_t size)
154826f09e9bSSantosh Shilimkar {
1549a36aab89SMike Rapoport 	phys_addr_t end = base + size - 1;
1550a36aab89SMike Rapoport 
1551a36aab89SMike Rapoport 	memblock_dbg("%s: [%pa-%pa] %pF\n",
1552a36aab89SMike Rapoport 		     __func__, &base, &end, (void *)_RET_IP_);
15539099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
1554f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.reserved, base, size);
155526f09e9bSSantosh Shilimkar }
155626f09e9bSSantosh Shilimkar 
155748a833ccSMike Rapoport /**
155826f09e9bSSantosh Shilimkar  * __memblock_free_late - free bootmem block pages directly to buddy allocator
155948a833ccSMike Rapoport  * @base: phys starting address of the  boot memory block
156026f09e9bSSantosh Shilimkar  * @size: size of the boot memory block in bytes
156126f09e9bSSantosh Shilimkar  *
156226f09e9bSSantosh Shilimkar  * This is only useful when the bootmem allocator has already been torn
156326f09e9bSSantosh Shilimkar  * down, but we are still initializing the system.  Pages are released directly
156426f09e9bSSantosh Shilimkar  * to the buddy allocator, no bootmem metadata is updated because it is gone.
156526f09e9bSSantosh Shilimkar  */
156626f09e9bSSantosh Shilimkar void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
156726f09e9bSSantosh Shilimkar {
1568a36aab89SMike Rapoport 	phys_addr_t cursor, end;
156926f09e9bSSantosh Shilimkar 
1570a36aab89SMike Rapoport 	end = base + size - 1;
1571a36aab89SMike Rapoport 	memblock_dbg("%s: [%pa-%pa] %pF\n",
1572a36aab89SMike Rapoport 		     __func__, &base, &end, (void *)_RET_IP_);
15739099daedSCatalin Marinas 	kmemleak_free_part_phys(base, size);
157426f09e9bSSantosh Shilimkar 	cursor = PFN_UP(base);
157526f09e9bSSantosh Shilimkar 	end = PFN_DOWN(base + size);
157626f09e9bSSantosh Shilimkar 
157726f09e9bSSantosh Shilimkar 	for (; cursor < end; cursor++) {
15787c2ee349SMike Rapoport 		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
157926f09e9bSSantosh Shilimkar 		totalram_pages++;
158026f09e9bSSantosh Shilimkar 	}
158126f09e9bSSantosh Shilimkar }
15829d1e2492SBenjamin Herrenschmidt 
15839d1e2492SBenjamin Herrenschmidt /*
15849d1e2492SBenjamin Herrenschmidt  * Remaining API functions
15859d1e2492SBenjamin Herrenschmidt  */
15869d1e2492SBenjamin Herrenschmidt 
15871f1ffb8aSDavid Gibson phys_addr_t __init_memblock memblock_phys_mem_size(void)
158895f72d1eSYinghai Lu {
15891440c4e2STejun Heo 	return memblock.memory.total_size;
159095f72d1eSYinghai Lu }
159195f72d1eSYinghai Lu 
15928907de5dSSrikar Dronamraju phys_addr_t __init_memblock memblock_reserved_size(void)
15938907de5dSSrikar Dronamraju {
15948907de5dSSrikar Dronamraju 	return memblock.reserved.total_size;
15958907de5dSSrikar Dronamraju }
15968907de5dSSrikar Dronamraju 
1597595ad9afSYinghai Lu phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1598595ad9afSYinghai Lu {
1599595ad9afSYinghai Lu 	unsigned long pages = 0;
1600595ad9afSYinghai Lu 	struct memblock_region *r;
1601595ad9afSYinghai Lu 	unsigned long start_pfn, end_pfn;
1602595ad9afSYinghai Lu 
1603595ad9afSYinghai Lu 	for_each_memblock(memory, r) {
1604595ad9afSYinghai Lu 		start_pfn = memblock_region_memory_base_pfn(r);
1605595ad9afSYinghai Lu 		end_pfn = memblock_region_memory_end_pfn(r);
1606595ad9afSYinghai Lu 		start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1607595ad9afSYinghai Lu 		end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1608595ad9afSYinghai Lu 		pages += end_pfn - start_pfn;
1609595ad9afSYinghai Lu 	}
1610595ad9afSYinghai Lu 
161116763230SFabian Frederick 	return PFN_PHYS(pages);
1612595ad9afSYinghai Lu }
1613595ad9afSYinghai Lu 
16140a93ebefSSam Ravnborg /* lowest address */
16150a93ebefSSam Ravnborg phys_addr_t __init_memblock memblock_start_of_DRAM(void)
16160a93ebefSSam Ravnborg {
16170a93ebefSSam Ravnborg 	return memblock.memory.regions[0].base;
16180a93ebefSSam Ravnborg }
16190a93ebefSSam Ravnborg 
162010d06439SYinghai Lu phys_addr_t __init_memblock memblock_end_of_DRAM(void)
162195f72d1eSYinghai Lu {
162295f72d1eSYinghai Lu 	int idx = memblock.memory.cnt - 1;
162395f72d1eSYinghai Lu 
1624e3239ff9SBenjamin Herrenschmidt 	return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
162595f72d1eSYinghai Lu }
162695f72d1eSYinghai Lu 
1627a571d4ebSDennis Chen static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
162895f72d1eSYinghai Lu {
16291c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1630136199f0SEmil Medve 	struct memblock_region *r;
163195f72d1eSYinghai Lu 
1632a571d4ebSDennis Chen 	/*
1633a571d4ebSDennis Chen 	 * translate the memory @limit size into the max address within one of
1634a571d4ebSDennis Chen 	 * the memory memblock regions, if the @limit exceeds the total size
16351c4bc43dSStefan Agner 	 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
1636a571d4ebSDennis Chen 	 */
1637136199f0SEmil Medve 	for_each_memblock(memory, r) {
1638c0ce8fefSTejun Heo 		if (limit <= r->size) {
1639c0ce8fefSTejun Heo 			max_addr = r->base + limit;
164095f72d1eSYinghai Lu 			break;
164195f72d1eSYinghai Lu 		}
1642c0ce8fefSTejun Heo 		limit -= r->size;
164395f72d1eSYinghai Lu 	}
1644c0ce8fefSTejun Heo 
1645a571d4ebSDennis Chen 	return max_addr;
1646a571d4ebSDennis Chen }
1647a571d4ebSDennis Chen 
1648a571d4ebSDennis Chen void __init memblock_enforce_memory_limit(phys_addr_t limit)
1649a571d4ebSDennis Chen {
16501c4bc43dSStefan Agner 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1651a571d4ebSDennis Chen 
1652a571d4ebSDennis Chen 	if (!limit)
1653a571d4ebSDennis Chen 		return;
1654a571d4ebSDennis Chen 
1655a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1656a571d4ebSDennis Chen 
1657a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
16581c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1659a571d4ebSDennis Chen 		return;
1660a571d4ebSDennis Chen 
1661c0ce8fefSTejun Heo 	/* truncate both memory and reserved regions */
1662f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.memory, max_addr,
16631c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
1664f1af9d3aSPhilipp Hachtmann 	memblock_remove_range(&memblock.reserved, max_addr,
16651c4bc43dSStefan Agner 			      PHYS_ADDR_MAX);
166695f72d1eSYinghai Lu }
166795f72d1eSYinghai Lu 
1668c9ca9b4eSAKASHI Takahiro void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1669c9ca9b4eSAKASHI Takahiro {
1670c9ca9b4eSAKASHI Takahiro 	int start_rgn, end_rgn;
1671c9ca9b4eSAKASHI Takahiro 	int i, ret;
1672c9ca9b4eSAKASHI Takahiro 
1673c9ca9b4eSAKASHI Takahiro 	if (!size)
1674c9ca9b4eSAKASHI Takahiro 		return;
1675c9ca9b4eSAKASHI Takahiro 
1676c9ca9b4eSAKASHI Takahiro 	ret = memblock_isolate_range(&memblock.memory, base, size,
1677c9ca9b4eSAKASHI Takahiro 						&start_rgn, &end_rgn);
1678c9ca9b4eSAKASHI Takahiro 	if (ret)
1679c9ca9b4eSAKASHI Takahiro 		return;
1680c9ca9b4eSAKASHI Takahiro 
1681c9ca9b4eSAKASHI Takahiro 	/* remove all the MAP regions */
1682c9ca9b4eSAKASHI Takahiro 	for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1683c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1684c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1685c9ca9b4eSAKASHI Takahiro 
1686c9ca9b4eSAKASHI Takahiro 	for (i = start_rgn - 1; i >= 0; i--)
1687c9ca9b4eSAKASHI Takahiro 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1688c9ca9b4eSAKASHI Takahiro 			memblock_remove_region(&memblock.memory, i);
1689c9ca9b4eSAKASHI Takahiro 
1690c9ca9b4eSAKASHI Takahiro 	/* truncate the reserved regions */
1691c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved, 0, base);
1692c9ca9b4eSAKASHI Takahiro 	memblock_remove_range(&memblock.reserved,
16931c4bc43dSStefan Agner 			base + size, PHYS_ADDR_MAX);
1694c9ca9b4eSAKASHI Takahiro }
1695c9ca9b4eSAKASHI Takahiro 
1696a571d4ebSDennis Chen void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1697a571d4ebSDennis Chen {
1698a571d4ebSDennis Chen 	phys_addr_t max_addr;
1699a571d4ebSDennis Chen 
1700a571d4ebSDennis Chen 	if (!limit)
1701a571d4ebSDennis Chen 		return;
1702a571d4ebSDennis Chen 
1703a571d4ebSDennis Chen 	max_addr = __find_max_addr(limit);
1704a571d4ebSDennis Chen 
1705a571d4ebSDennis Chen 	/* @limit exceeds the total size of the memory, do nothing */
17061c4bc43dSStefan Agner 	if (max_addr == PHYS_ADDR_MAX)
1707a571d4ebSDennis Chen 		return;
1708a571d4ebSDennis Chen 
1709c9ca9b4eSAKASHI Takahiro 	memblock_cap_memory_range(0, max_addr);
1710a571d4ebSDennis Chen }
1711a571d4ebSDennis Chen 
1712cd79481dSYinghai Lu static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
171372d4b0b4SBenjamin Herrenschmidt {
171472d4b0b4SBenjamin Herrenschmidt 	unsigned int left = 0, right = type->cnt;
171572d4b0b4SBenjamin Herrenschmidt 
171672d4b0b4SBenjamin Herrenschmidt 	do {
171772d4b0b4SBenjamin Herrenschmidt 		unsigned int mid = (right + left) / 2;
171872d4b0b4SBenjamin Herrenschmidt 
171972d4b0b4SBenjamin Herrenschmidt 		if (addr < type->regions[mid].base)
172072d4b0b4SBenjamin Herrenschmidt 			right = mid;
172172d4b0b4SBenjamin Herrenschmidt 		else if (addr >= (type->regions[mid].base +
172272d4b0b4SBenjamin Herrenschmidt 				  type->regions[mid].size))
172372d4b0b4SBenjamin Herrenschmidt 			left = mid + 1;
172472d4b0b4SBenjamin Herrenschmidt 		else
172572d4b0b4SBenjamin Herrenschmidt 			return mid;
172672d4b0b4SBenjamin Herrenschmidt 	} while (left < right);
172772d4b0b4SBenjamin Herrenschmidt 	return -1;
172872d4b0b4SBenjamin Herrenschmidt }
172972d4b0b4SBenjamin Herrenschmidt 
1730b4ad0c7eSYaowei Bai bool __init memblock_is_reserved(phys_addr_t addr)
173195f72d1eSYinghai Lu {
173272d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.reserved, addr) != -1;
173395f72d1eSYinghai Lu }
173472d4b0b4SBenjamin Herrenschmidt 
1735b4ad0c7eSYaowei Bai bool __init_memblock memblock_is_memory(phys_addr_t addr)
173672d4b0b4SBenjamin Herrenschmidt {
173772d4b0b4SBenjamin Herrenschmidt 	return memblock_search(&memblock.memory, addr) != -1;
173872d4b0b4SBenjamin Herrenschmidt }
173972d4b0b4SBenjamin Herrenschmidt 
1740937f0c26SYaowei Bai bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
1741bf3d3cc5SArd Biesheuvel {
1742bf3d3cc5SArd Biesheuvel 	int i = memblock_search(&memblock.memory, addr);
1743bf3d3cc5SArd Biesheuvel 
1744bf3d3cc5SArd Biesheuvel 	if (i == -1)
1745bf3d3cc5SArd Biesheuvel 		return false;
1746bf3d3cc5SArd Biesheuvel 	return !memblock_is_nomap(&memblock.memory.regions[i]);
1747bf3d3cc5SArd Biesheuvel }
1748bf3d3cc5SArd Biesheuvel 
1749e76b63f8SYinghai Lu #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1750e76b63f8SYinghai Lu int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1751e76b63f8SYinghai Lu 			 unsigned long *start_pfn, unsigned long *end_pfn)
1752e76b63f8SYinghai Lu {
1753e76b63f8SYinghai Lu 	struct memblock_type *type = &memblock.memory;
175416763230SFabian Frederick 	int mid = memblock_search(type, PFN_PHYS(pfn));
1755e76b63f8SYinghai Lu 
1756e76b63f8SYinghai Lu 	if (mid == -1)
1757e76b63f8SYinghai Lu 		return -1;
1758e76b63f8SYinghai Lu 
1759f7e2f7e8SFabian Frederick 	*start_pfn = PFN_DOWN(type->regions[mid].base);
1760f7e2f7e8SFabian Frederick 	*end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
1761e76b63f8SYinghai Lu 
1762e76b63f8SYinghai Lu 	return type->regions[mid].nid;
1763e76b63f8SYinghai Lu }
1764e76b63f8SYinghai Lu #endif
1765e76b63f8SYinghai Lu 
1766eab30949SStephen Boyd /**
1767eab30949SStephen Boyd  * memblock_is_region_memory - check if a region is a subset of memory
1768eab30949SStephen Boyd  * @base: base of region to check
1769eab30949SStephen Boyd  * @size: size of region to check
1770eab30949SStephen Boyd  *
1771eab30949SStephen Boyd  * Check if the region [@base, @base + @size) is a subset of a memory block.
1772eab30949SStephen Boyd  *
177347cec443SMike Rapoport  * Return:
1774eab30949SStephen Boyd  * 0 if false, non-zero if true
1775eab30949SStephen Boyd  */
1776937f0c26SYaowei Bai bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
177772d4b0b4SBenjamin Herrenschmidt {
1778abb65272STomi Valkeinen 	int idx = memblock_search(&memblock.memory, base);
1779eb18f1b5STejun Heo 	phys_addr_t end = base + memblock_cap_size(base, &size);
178072d4b0b4SBenjamin Herrenschmidt 
178172d4b0b4SBenjamin Herrenschmidt 	if (idx == -1)
1782937f0c26SYaowei Bai 		return false;
1783ef415ef4SWei Yang 	return (memblock.memory.regions[idx].base +
1784eb18f1b5STejun Heo 		 memblock.memory.regions[idx].size) >= end;
178595f72d1eSYinghai Lu }
178695f72d1eSYinghai Lu 
1787eab30949SStephen Boyd /**
1788eab30949SStephen Boyd  * memblock_is_region_reserved - check if a region intersects reserved memory
1789eab30949SStephen Boyd  * @base: base of region to check
1790eab30949SStephen Boyd  * @size: size of region to check
1791eab30949SStephen Boyd  *
179247cec443SMike Rapoport  * Check if the region [@base, @base + @size) intersects a reserved
179347cec443SMike Rapoport  * memory block.
1794eab30949SStephen Boyd  *
179547cec443SMike Rapoport  * Return:
1796c5c5c9d1STang Chen  * True if they intersect, false if not.
1797eab30949SStephen Boyd  */
1798c5c5c9d1STang Chen bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
179995f72d1eSYinghai Lu {
1800eb18f1b5STejun Heo 	memblock_cap_size(base, &size);
1801c5c5c9d1STang Chen 	return memblock_overlaps_region(&memblock.reserved, base, size);
180295f72d1eSYinghai Lu }
180395f72d1eSYinghai Lu 
18046ede1fd3SYinghai Lu void __init_memblock memblock_trim_memory(phys_addr_t align)
18056ede1fd3SYinghai Lu {
18066ede1fd3SYinghai Lu 	phys_addr_t start, end, orig_start, orig_end;
1807136199f0SEmil Medve 	struct memblock_region *r;
18086ede1fd3SYinghai Lu 
1809136199f0SEmil Medve 	for_each_memblock(memory, r) {
1810136199f0SEmil Medve 		orig_start = r->base;
1811136199f0SEmil Medve 		orig_end = r->base + r->size;
18126ede1fd3SYinghai Lu 		start = round_up(orig_start, align);
18136ede1fd3SYinghai Lu 		end = round_down(orig_end, align);
18146ede1fd3SYinghai Lu 
18156ede1fd3SYinghai Lu 		if (start == orig_start && end == orig_end)
18166ede1fd3SYinghai Lu 			continue;
18176ede1fd3SYinghai Lu 
18186ede1fd3SYinghai Lu 		if (start < end) {
1819136199f0SEmil Medve 			r->base = start;
1820136199f0SEmil Medve 			r->size = end - start;
18216ede1fd3SYinghai Lu 		} else {
1822136199f0SEmil Medve 			memblock_remove_region(&memblock.memory,
1823136199f0SEmil Medve 					       r - memblock.memory.regions);
1824136199f0SEmil Medve 			r--;
18256ede1fd3SYinghai Lu 		}
18266ede1fd3SYinghai Lu 	}
18276ede1fd3SYinghai Lu }
1828e63075a3SBenjamin Herrenschmidt 
18293661ca66SYinghai Lu void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1830e63075a3SBenjamin Herrenschmidt {
1831e63075a3SBenjamin Herrenschmidt 	memblock.current_limit = limit;
1832e63075a3SBenjamin Herrenschmidt }
1833e63075a3SBenjamin Herrenschmidt 
1834fec51014SLaura Abbott phys_addr_t __init_memblock memblock_get_current_limit(void)
1835fec51014SLaura Abbott {
1836fec51014SLaura Abbott 	return memblock.current_limit;
1837fec51014SLaura Abbott }
1838fec51014SLaura Abbott 
18390262d9c8SHeiko Carstens static void __init_memblock memblock_dump(struct memblock_type *type)
18406ed311b2SBenjamin Herrenschmidt {
18415d63f81cSMiles Chen 	phys_addr_t base, end, size;
1842e1720feeSMike Rapoport 	enum memblock_flags flags;
18438c9c1701SAlexander Kuleshov 	int idx;
18448c9c1701SAlexander Kuleshov 	struct memblock_region *rgn;
18456ed311b2SBenjamin Herrenschmidt 
18460262d9c8SHeiko Carstens 	pr_info(" %s.cnt  = 0x%lx\n", type->name, type->cnt);
18476ed311b2SBenjamin Herrenschmidt 
184866e8b438SGioh Kim 	for_each_memblock_type(idx, type, rgn) {
18497c0caeb8STejun Heo 		char nid_buf[32] = "";
18506ed311b2SBenjamin Herrenschmidt 
18517c0caeb8STejun Heo 		base = rgn->base;
18527c0caeb8STejun Heo 		size = rgn->size;
18535d63f81cSMiles Chen 		end = base + size - 1;
185466a20757STang Chen 		flags = rgn->flags;
18557c0caeb8STejun Heo #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
18567c0caeb8STejun Heo 		if (memblock_get_region_node(rgn) != MAX_NUMNODES)
18577c0caeb8STejun Heo 			snprintf(nid_buf, sizeof(nid_buf), " on node %d",
18587c0caeb8STejun Heo 				 memblock_get_region_node(rgn));
18597c0caeb8STejun Heo #endif
1860e1720feeSMike Rapoport 		pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
18610262d9c8SHeiko Carstens 			type->name, idx, &base, &end, &size, nid_buf, flags);
18626ed311b2SBenjamin Herrenschmidt 	}
18636ed311b2SBenjamin Herrenschmidt }
18646ed311b2SBenjamin Herrenschmidt 
18654ff7b82fSTejun Heo void __init_memblock __memblock_dump_all(void)
18666ed311b2SBenjamin Herrenschmidt {
18676ed311b2SBenjamin Herrenschmidt 	pr_info("MEMBLOCK configuration:\n");
18685d63f81cSMiles Chen 	pr_info(" memory size = %pa reserved size = %pa\n",
18695d63f81cSMiles Chen 		&memblock.memory.total_size,
18705d63f81cSMiles Chen 		&memblock.reserved.total_size);
18716ed311b2SBenjamin Herrenschmidt 
18720262d9c8SHeiko Carstens 	memblock_dump(&memblock.memory);
18730262d9c8SHeiko Carstens 	memblock_dump(&memblock.reserved);
1874409efd4cSHeiko Carstens #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
18750262d9c8SHeiko Carstens 	memblock_dump(&memblock.physmem);
1876409efd4cSHeiko Carstens #endif
18776ed311b2SBenjamin Herrenschmidt }
18786ed311b2SBenjamin Herrenschmidt 
18791aadc056STejun Heo void __init memblock_allow_resize(void)
18806ed311b2SBenjamin Herrenschmidt {
1881142b45a7SBenjamin Herrenschmidt 	memblock_can_resize = 1;
18826ed311b2SBenjamin Herrenschmidt }
18836ed311b2SBenjamin Herrenschmidt 
18846ed311b2SBenjamin Herrenschmidt static int __init early_memblock(char *p)
18856ed311b2SBenjamin Herrenschmidt {
18866ed311b2SBenjamin Herrenschmidt 	if (p && strstr(p, "debug"))
18876ed311b2SBenjamin Herrenschmidt 		memblock_debug = 1;
18886ed311b2SBenjamin Herrenschmidt 	return 0;
18896ed311b2SBenjamin Herrenschmidt }
18906ed311b2SBenjamin Herrenschmidt early_param("memblock", early_memblock);
18916ed311b2SBenjamin Herrenschmidt 
1892bda49a81SMike Rapoport static void __init __free_pages_memory(unsigned long start, unsigned long end)
1893bda49a81SMike Rapoport {
1894bda49a81SMike Rapoport 	int order;
1895bda49a81SMike Rapoport 
1896bda49a81SMike Rapoport 	while (start < end) {
1897bda49a81SMike Rapoport 		order = min(MAX_ORDER - 1UL, __ffs(start));
1898bda49a81SMike Rapoport 
1899bda49a81SMike Rapoport 		while (start + (1UL << order) > end)
1900bda49a81SMike Rapoport 			order--;
1901bda49a81SMike Rapoport 
1902bda49a81SMike Rapoport 		memblock_free_pages(pfn_to_page(start), start, order);
1903bda49a81SMike Rapoport 
1904bda49a81SMike Rapoport 		start += (1UL << order);
1905bda49a81SMike Rapoport 	}
1906bda49a81SMike Rapoport }
1907bda49a81SMike Rapoport 
1908bda49a81SMike Rapoport static unsigned long __init __free_memory_core(phys_addr_t start,
1909bda49a81SMike Rapoport 				 phys_addr_t end)
1910bda49a81SMike Rapoport {
1911bda49a81SMike Rapoport 	unsigned long start_pfn = PFN_UP(start);
1912bda49a81SMike Rapoport 	unsigned long end_pfn = min_t(unsigned long,
1913bda49a81SMike Rapoport 				      PFN_DOWN(end), max_low_pfn);
1914bda49a81SMike Rapoport 
1915bda49a81SMike Rapoport 	if (start_pfn >= end_pfn)
1916bda49a81SMike Rapoport 		return 0;
1917bda49a81SMike Rapoport 
1918bda49a81SMike Rapoport 	__free_pages_memory(start_pfn, end_pfn);
1919bda49a81SMike Rapoport 
1920bda49a81SMike Rapoport 	return end_pfn - start_pfn;
1921bda49a81SMike Rapoport }
1922bda49a81SMike Rapoport 
1923bda49a81SMike Rapoport static unsigned long __init free_low_memory_core_early(void)
1924bda49a81SMike Rapoport {
1925bda49a81SMike Rapoport 	unsigned long count = 0;
1926bda49a81SMike Rapoport 	phys_addr_t start, end;
1927bda49a81SMike Rapoport 	u64 i;
1928bda49a81SMike Rapoport 
1929bda49a81SMike Rapoport 	memblock_clear_hotplug(0, -1);
1930bda49a81SMike Rapoport 
1931bda49a81SMike Rapoport 	for_each_reserved_mem_region(i, &start, &end)
1932bda49a81SMike Rapoport 		reserve_bootmem_region(start, end);
1933bda49a81SMike Rapoport 
1934bda49a81SMike Rapoport 	/*
1935bda49a81SMike Rapoport 	 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
1936bda49a81SMike Rapoport 	 *  because in some case like Node0 doesn't have RAM installed
1937bda49a81SMike Rapoport 	 *  low ram will be on Node1
1938bda49a81SMike Rapoport 	 */
1939bda49a81SMike Rapoport 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
1940bda49a81SMike Rapoport 				NULL)
1941bda49a81SMike Rapoport 		count += __free_memory_core(start, end);
1942bda49a81SMike Rapoport 
1943bda49a81SMike Rapoport 	return count;
1944bda49a81SMike Rapoport }
1945bda49a81SMike Rapoport 
1946bda49a81SMike Rapoport static int reset_managed_pages_done __initdata;
1947bda49a81SMike Rapoport 
1948bda49a81SMike Rapoport void reset_node_managed_pages(pg_data_t *pgdat)
1949bda49a81SMike Rapoport {
1950bda49a81SMike Rapoport 	struct zone *z;
1951bda49a81SMike Rapoport 
1952bda49a81SMike Rapoport 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
1953bda49a81SMike Rapoport 		z->managed_pages = 0;
1954bda49a81SMike Rapoport }
1955bda49a81SMike Rapoport 
1956bda49a81SMike Rapoport void __init reset_all_zones_managed_pages(void)
1957bda49a81SMike Rapoport {
1958bda49a81SMike Rapoport 	struct pglist_data *pgdat;
1959bda49a81SMike Rapoport 
1960bda49a81SMike Rapoport 	if (reset_managed_pages_done)
1961bda49a81SMike Rapoport 		return;
1962bda49a81SMike Rapoport 
1963bda49a81SMike Rapoport 	for_each_online_pgdat(pgdat)
1964bda49a81SMike Rapoport 		reset_node_managed_pages(pgdat);
1965bda49a81SMike Rapoport 
1966bda49a81SMike Rapoport 	reset_managed_pages_done = 1;
1967bda49a81SMike Rapoport }
1968bda49a81SMike Rapoport 
1969bda49a81SMike Rapoport /**
1970bda49a81SMike Rapoport  * memblock_free_all - release free pages to the buddy allocator
1971bda49a81SMike Rapoport  *
1972bda49a81SMike Rapoport  * Return: the number of pages actually released.
1973bda49a81SMike Rapoport  */
1974bda49a81SMike Rapoport unsigned long __init memblock_free_all(void)
1975bda49a81SMike Rapoport {
1976bda49a81SMike Rapoport 	unsigned long pages;
1977bda49a81SMike Rapoport 
1978bda49a81SMike Rapoport 	reset_all_zones_managed_pages();
1979bda49a81SMike Rapoport 
1980bda49a81SMike Rapoport 	pages = free_low_memory_core_early();
1981bda49a81SMike Rapoport 	totalram_pages += pages;
1982bda49a81SMike Rapoport 
1983bda49a81SMike Rapoport 	return pages;
1984bda49a81SMike Rapoport }
1985bda49a81SMike Rapoport 
1986c378ddd5STejun Heo #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
19876d03b885SBenjamin Herrenschmidt 
19886d03b885SBenjamin Herrenschmidt static int memblock_debug_show(struct seq_file *m, void *private)
19896d03b885SBenjamin Herrenschmidt {
19906d03b885SBenjamin Herrenschmidt 	struct memblock_type *type = m->private;
19916d03b885SBenjamin Herrenschmidt 	struct memblock_region *reg;
19926d03b885SBenjamin Herrenschmidt 	int i;
19935d63f81cSMiles Chen 	phys_addr_t end;
19946d03b885SBenjamin Herrenschmidt 
19956d03b885SBenjamin Herrenschmidt 	for (i = 0; i < type->cnt; i++) {
19966d03b885SBenjamin Herrenschmidt 		reg = &type->regions[i];
19975d63f81cSMiles Chen 		end = reg->base + reg->size - 1;
19986d03b885SBenjamin Herrenschmidt 
19995d63f81cSMiles Chen 		seq_printf(m, "%4d: ", i);
20005d63f81cSMiles Chen 		seq_printf(m, "%pa..%pa\n", &reg->base, &end);
20016d03b885SBenjamin Herrenschmidt 	}
20026d03b885SBenjamin Herrenschmidt 	return 0;
20036d03b885SBenjamin Herrenschmidt }
20045ad35093SAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(memblock_debug);
20056d03b885SBenjamin Herrenschmidt 
20066d03b885SBenjamin Herrenschmidt static int __init memblock_init_debugfs(void)
20076d03b885SBenjamin Herrenschmidt {
20086d03b885SBenjamin Herrenschmidt 	struct dentry *root = debugfs_create_dir("memblock", NULL);
20096d03b885SBenjamin Herrenschmidt 	if (!root)
20106d03b885SBenjamin Herrenschmidt 		return -ENXIO;
20110825a6f9SJoe Perches 	debugfs_create_file("memory", 0444, root,
20120825a6f9SJoe Perches 			    &memblock.memory, &memblock_debug_fops);
20130825a6f9SJoe Perches 	debugfs_create_file("reserved", 0444, root,
20140825a6f9SJoe Perches 			    &memblock.reserved, &memblock_debug_fops);
201570210ed9SPhilipp Hachtmann #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
20160825a6f9SJoe Perches 	debugfs_create_file("physmem", 0444, root,
20170825a6f9SJoe Perches 			    &memblock.physmem, &memblock_debug_fops);
201870210ed9SPhilipp Hachtmann #endif
20196d03b885SBenjamin Herrenschmidt 
20206d03b885SBenjamin Herrenschmidt 	return 0;
20216d03b885SBenjamin Herrenschmidt }
20226d03b885SBenjamin Herrenschmidt __initcall(memblock_init_debugfs);
20236d03b885SBenjamin Herrenschmidt 
20246d03b885SBenjamin Herrenschmidt #endif /* CONFIG_DEBUG_FS */
2025