xref: /OK3568_Linux_fs/kernel/mm/memblock.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Procedures for maintaining information about logical memory blocks.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Peter Bergner, IBM Corp.	June 2001.
6*4882a593Smuzhiyun  * Copyright (C) 2001 Peter Bergner.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/kernel.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/init.h>
12*4882a593Smuzhiyun #include <linux/bitops.h>
13*4882a593Smuzhiyun #include <linux/poison.h>
14*4882a593Smuzhiyun #include <linux/pfn.h>
15*4882a593Smuzhiyun #include <linux/debugfs.h>
16*4882a593Smuzhiyun #include <linux/kmemleak.h>
17*4882a593Smuzhiyun #include <linux/seq_file.h>
18*4882a593Smuzhiyun #include <linux/memblock.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <asm/sections.h>
21*4882a593Smuzhiyun #include <linux/io.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include "internal.h"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define INIT_MEMBLOCK_REGIONS			128
26*4882a593Smuzhiyun #define INIT_PHYSMEM_REGIONS			4
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #ifndef INIT_MEMBLOCK_RESERVED_REGIONS
29*4882a593Smuzhiyun # define INIT_MEMBLOCK_RESERVED_REGIONS		INIT_MEMBLOCK_REGIONS
30*4882a593Smuzhiyun #endif
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /**
33*4882a593Smuzhiyun  * DOC: memblock overview
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * Memblock is a method of managing memory regions during the early
36*4882a593Smuzhiyun  * boot period when the usual kernel memory allocators are not up and
37*4882a593Smuzhiyun  * running.
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * Memblock views the system memory as collections of contiguous
40*4882a593Smuzhiyun  * regions. There are several types of these collections:
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * * ``memory`` - describes the physical memory available to the
43*4882a593Smuzhiyun  *   kernel; this may differ from the actual physical memory installed
44*4882a593Smuzhiyun  *   in the system, for instance when the memory is restricted with
45*4882a593Smuzhiyun  *   ``mem=`` command line parameter
46*4882a593Smuzhiyun  * * ``reserved`` - describes the regions that were allocated
47*4882a593Smuzhiyun  * * ``physmem`` - describes the actual physical memory available during
48*4882a593Smuzhiyun  *   boot regardless of the possible restrictions and memory hot(un)plug;
49*4882a593Smuzhiyun  *   the ``physmem`` type is only available on some architectures.
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * Each region is represented by struct memblock_region that
52*4882a593Smuzhiyun  * defines the region extents, its attributes and NUMA node id on NUMA
53*4882a593Smuzhiyun  * systems. Every memory type is described by the struct memblock_type
54*4882a593Smuzhiyun  * which contains an array of memory regions along with
55*4882a593Smuzhiyun  * the allocator metadata. The "memory" and "reserved" types are nicely
56*4882a593Smuzhiyun  * wrapped with struct memblock. This structure is statically
57*4882a593Smuzhiyun  * initialized at build time. The region arrays are initially sized to
58*4882a593Smuzhiyun  * %INIT_MEMBLOCK_REGIONS for "memory" and %INIT_MEMBLOCK_RESERVED_REGIONS
59*4882a593Smuzhiyun  * for "reserved". The region array for "physmem" is initially sized to
60*4882a593Smuzhiyun  * %INIT_PHYSMEM_REGIONS.
61*4882a593Smuzhiyun  * The memblock_allow_resize() enables automatic resizing of the region
62*4882a593Smuzhiyun  * arrays during addition of new regions. This feature should be used
63*4882a593Smuzhiyun  * with care so that memory allocated for the region array will not
64*4882a593Smuzhiyun  * overlap with areas that should be reserved, for example initrd.
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  * The early architecture setup should tell memblock what the physical
67*4882a593Smuzhiyun  * memory layout is by using memblock_add() or memblock_add_node()
68*4882a593Smuzhiyun  * functions. The first function does not assign the region to a NUMA
69*4882a593Smuzhiyun  * node and it is appropriate for UMA systems. Yet, it is possible to
70*4882a593Smuzhiyun  * use it on NUMA systems as well and assign the region to a NUMA node
71*4882a593Smuzhiyun  * later in the setup process using memblock_set_node(). The
72*4882a593Smuzhiyun  * memblock_add_node() performs such an assignment directly.
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * Once memblock is setup the memory can be allocated using one of the
75*4882a593Smuzhiyun  * API variants:
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * * memblock_phys_alloc*() - these functions return the **physical**
78*4882a593Smuzhiyun  *   address of the allocated memory
79*4882a593Smuzhiyun  * * memblock_alloc*() - these functions return the **virtual** address
80*4882a593Smuzhiyun  *   of the allocated memory.
81*4882a593Smuzhiyun  *
82*4882a593Smuzhiyun  * Note, that both API variants use implicit assumptions about allowed
83*4882a593Smuzhiyun  * memory ranges and the fallback methods. Consult the documentation
84*4882a593Smuzhiyun  * of memblock_alloc_internal() and memblock_alloc_range_nid()
85*4882a593Smuzhiyun  * functions for more elaborate description.
86*4882a593Smuzhiyun  *
87*4882a593Smuzhiyun  * As the system boot progresses, the architecture specific mem_init()
88*4882a593Smuzhiyun  * function frees all the memory to the buddy page allocator.
89*4882a593Smuzhiyun  *
90*4882a593Smuzhiyun  * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the
91*4882a593Smuzhiyun  * memblock data structures (except "physmem") will be discarded after the
92*4882a593Smuzhiyun  * system initialization completes.
93*4882a593Smuzhiyun  */
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun #ifndef CONFIG_NEED_MULTIPLE_NODES
96*4882a593Smuzhiyun struct pglist_data __refdata contig_page_data;
97*4882a593Smuzhiyun EXPORT_SYMBOL(contig_page_data);
98*4882a593Smuzhiyun #endif
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun #if defined(CONFIG_ROCKCHIP_THUNDER_BOOT) && defined(CONFIG_SMP)
101*4882a593Smuzhiyun static unsigned long defer_start __initdata;
102*4882a593Smuzhiyun static unsigned long defer_end __initdata;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun #define DEFAULT_DEFER_FREE_BLOCK_SIZE SZ_256M
105*4882a593Smuzhiyun static unsigned long defer_free_block_size __initdata =
106*4882a593Smuzhiyun 	DEFAULT_DEFER_FREE_BLOCK_SIZE;
107*4882a593Smuzhiyun 
early_defer_free_block_size(char * p)108*4882a593Smuzhiyun static int __init early_defer_free_block_size(char *p)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	defer_free_block_size = memparse(p, &p);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	pr_debug("defer_free_block_size = 0x%lx\n", defer_free_block_size);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	return 0;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun early_param("defer_free_block_size", early_defer_free_block_size);
118*4882a593Smuzhiyun #endif
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun unsigned long max_low_pfn;
121*4882a593Smuzhiyun unsigned long min_low_pfn;
122*4882a593Smuzhiyun unsigned long max_pfn;
123*4882a593Smuzhiyun unsigned long long max_possible_pfn;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
126*4882a593Smuzhiyun static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
127*4882a593Smuzhiyun #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
128*4882a593Smuzhiyun static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS];
129*4882a593Smuzhiyun #endif
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun struct memblock memblock __initdata_memblock = {
132*4882a593Smuzhiyun 	.memory.regions		= memblock_memory_init_regions,
133*4882a593Smuzhiyun 	.memory.cnt		= 1,	/* empty dummy entry */
134*4882a593Smuzhiyun 	.memory.max		= INIT_MEMBLOCK_REGIONS,
135*4882a593Smuzhiyun 	.memory.name		= "memory",
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	.reserved.regions	= memblock_reserved_init_regions,
138*4882a593Smuzhiyun 	.reserved.cnt		= 1,	/* empty dummy entry */
139*4882a593Smuzhiyun 	.reserved.max		= INIT_MEMBLOCK_RESERVED_REGIONS,
140*4882a593Smuzhiyun 	.reserved.name		= "reserved",
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	.bottom_up		= false,
143*4882a593Smuzhiyun 	.current_limit		= MEMBLOCK_ALLOC_ANYWHERE,
144*4882a593Smuzhiyun };
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
147*4882a593Smuzhiyun struct memblock_type physmem = {
148*4882a593Smuzhiyun 	.regions		= memblock_physmem_init_regions,
149*4882a593Smuzhiyun 	.cnt			= 1,	/* empty dummy entry */
150*4882a593Smuzhiyun 	.max			= INIT_PHYSMEM_REGIONS,
151*4882a593Smuzhiyun 	.name			= "physmem",
152*4882a593Smuzhiyun };
153*4882a593Smuzhiyun #endif
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun  * keep a pointer to &memblock.memory in the text section to use it in
157*4882a593Smuzhiyun  * __next_mem_range() and its helpers.
158*4882a593Smuzhiyun  *  For architectures that do not keep memblock data after init, this
159*4882a593Smuzhiyun  * pointer will be reset to NULL at memblock_discard()
160*4882a593Smuzhiyun  */
161*4882a593Smuzhiyun static __refdata struct memblock_type *memblock_memory = &memblock.memory;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun #define for_each_memblock_type(i, memblock_type, rgn)			\
164*4882a593Smuzhiyun 	for (i = 0, rgn = &memblock_type->regions[0];			\
165*4882a593Smuzhiyun 	     i < memblock_type->cnt;					\
166*4882a593Smuzhiyun 	     i++, rgn = &memblock_type->regions[i])
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun #define memblock_dbg(fmt, ...)						\
169*4882a593Smuzhiyun 	do {								\
170*4882a593Smuzhiyun 		if (memblock_debug)					\
171*4882a593Smuzhiyun 			pr_info(fmt, ##__VA_ARGS__);			\
172*4882a593Smuzhiyun 	} while (0)
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun static int memblock_debug __initdata_memblock;
175*4882a593Smuzhiyun static bool memblock_nomap_remove __initdata_memblock;
176*4882a593Smuzhiyun static bool system_has_some_mirror __initdata_memblock = false;
177*4882a593Smuzhiyun static int memblock_can_resize __initdata_memblock;
178*4882a593Smuzhiyun static int memblock_memory_in_slab __initdata_memblock = 0;
179*4882a593Smuzhiyun static int memblock_reserved_in_slab __initdata_memblock = 0;
180*4882a593Smuzhiyun 
choose_memblock_flags(void)181*4882a593Smuzhiyun static enum memblock_flags __init_memblock choose_memblock_flags(void)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
memblock_cap_size(phys_addr_t base,phys_addr_t * size)187*4882a593Smuzhiyun static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	return *size = min(*size, PHYS_ADDR_MAX - base);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /*
193*4882a593Smuzhiyun  * Address comparison utilities
194*4882a593Smuzhiyun  */
memblock_addrs_overlap(phys_addr_t base1,phys_addr_t size1,phys_addr_t base2,phys_addr_t size2)195*4882a593Smuzhiyun static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
196*4882a593Smuzhiyun 				       phys_addr_t base2, phys_addr_t size2)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
memblock_overlaps_region(struct memblock_type * type,phys_addr_t base,phys_addr_t size)201*4882a593Smuzhiyun bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
202*4882a593Smuzhiyun 					phys_addr_t base, phys_addr_t size)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	unsigned long i;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	memblock_cap_size(base, &size);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	for (i = 0; i < type->cnt; i++)
209*4882a593Smuzhiyun 		if (memblock_addrs_overlap(base, size, type->regions[i].base,
210*4882a593Smuzhiyun 					   type->regions[i].size))
211*4882a593Smuzhiyun 			break;
212*4882a593Smuzhiyun 	return i < type->cnt;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun /**
216*4882a593Smuzhiyun  * __memblock_find_range_bottom_up - find free area utility in bottom-up
217*4882a593Smuzhiyun  * @start: start of candidate range
218*4882a593Smuzhiyun  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
219*4882a593Smuzhiyun  *       %MEMBLOCK_ALLOC_ACCESSIBLE
220*4882a593Smuzhiyun  * @size: size of free area to find
221*4882a593Smuzhiyun  * @align: alignment of free area to find
222*4882a593Smuzhiyun  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
223*4882a593Smuzhiyun  * @flags: pick from blocks based on memory attributes
224*4882a593Smuzhiyun  *
225*4882a593Smuzhiyun  * Utility called from memblock_find_in_range_node(), find free area bottom-up.
226*4882a593Smuzhiyun  *
227*4882a593Smuzhiyun  * Return:
228*4882a593Smuzhiyun  * Found address on success, 0 on failure.
229*4882a593Smuzhiyun  */
230*4882a593Smuzhiyun static phys_addr_t __init_memblock
__memblock_find_range_bottom_up(phys_addr_t start,phys_addr_t end,phys_addr_t size,phys_addr_t align,int nid,enum memblock_flags flags)231*4882a593Smuzhiyun __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
232*4882a593Smuzhiyun 				phys_addr_t size, phys_addr_t align, int nid,
233*4882a593Smuzhiyun 				enum memblock_flags flags)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun 	phys_addr_t this_start, this_end, cand;
236*4882a593Smuzhiyun 	u64 i;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
239*4882a593Smuzhiyun 		this_start = clamp(this_start, start, end);
240*4882a593Smuzhiyun 		this_end = clamp(this_end, start, end);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 		cand = round_up(this_start, align);
243*4882a593Smuzhiyun 		if (cand < this_end && this_end - cand >= size)
244*4882a593Smuzhiyun 			return cand;
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	return 0;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun /**
251*4882a593Smuzhiyun  * __memblock_find_range_top_down - find free area utility, in top-down
252*4882a593Smuzhiyun  * @start: start of candidate range
253*4882a593Smuzhiyun  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
254*4882a593Smuzhiyun  *       %MEMBLOCK_ALLOC_ACCESSIBLE
255*4882a593Smuzhiyun  * @size: size of free area to find
256*4882a593Smuzhiyun  * @align: alignment of free area to find
257*4882a593Smuzhiyun  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
258*4882a593Smuzhiyun  * @flags: pick from blocks based on memory attributes
259*4882a593Smuzhiyun  *
260*4882a593Smuzhiyun  * Utility called from memblock_find_in_range_node(), find free area top-down.
261*4882a593Smuzhiyun  *
262*4882a593Smuzhiyun  * Return:
263*4882a593Smuzhiyun  * Found address on success, 0 on failure.
264*4882a593Smuzhiyun  */
265*4882a593Smuzhiyun static phys_addr_t __init_memblock
__memblock_find_range_top_down(phys_addr_t start,phys_addr_t end,phys_addr_t size,phys_addr_t align,int nid,enum memblock_flags flags)266*4882a593Smuzhiyun __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
267*4882a593Smuzhiyun 			       phys_addr_t size, phys_addr_t align, int nid,
268*4882a593Smuzhiyun 			       enum memblock_flags flags)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	phys_addr_t this_start, this_end, cand;
271*4882a593Smuzhiyun 	u64 i;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
274*4882a593Smuzhiyun 					NULL) {
275*4882a593Smuzhiyun 		this_start = clamp(this_start, start, end);
276*4882a593Smuzhiyun 		this_end = clamp(this_end, start, end);
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 		if (this_end < size)
279*4882a593Smuzhiyun 			continue;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 		cand = round_down(this_end - size, align);
282*4882a593Smuzhiyun 		if (cand >= this_start)
283*4882a593Smuzhiyun 			return cand;
284*4882a593Smuzhiyun 	}
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	return 0;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun /**
290*4882a593Smuzhiyun  * memblock_find_in_range_node - find free area in given range and node
291*4882a593Smuzhiyun  * @size: size of free area to find
292*4882a593Smuzhiyun  * @align: alignment of free area to find
293*4882a593Smuzhiyun  * @start: start of candidate range
294*4882a593Smuzhiyun  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
295*4882a593Smuzhiyun  *       %MEMBLOCK_ALLOC_ACCESSIBLE
296*4882a593Smuzhiyun  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
297*4882a593Smuzhiyun  * @flags: pick from blocks based on memory attributes
298*4882a593Smuzhiyun  *
299*4882a593Smuzhiyun  * Find @size free area aligned to @align in the specified range and node.
300*4882a593Smuzhiyun  *
301*4882a593Smuzhiyun  * Return:
302*4882a593Smuzhiyun  * Found address on success, 0 on failure.
303*4882a593Smuzhiyun  */
memblock_find_in_range_node(phys_addr_t size,phys_addr_t align,phys_addr_t start,phys_addr_t end,int nid,enum memblock_flags flags)304*4882a593Smuzhiyun static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
305*4882a593Smuzhiyun 					phys_addr_t align, phys_addr_t start,
306*4882a593Smuzhiyun 					phys_addr_t end, int nid,
307*4882a593Smuzhiyun 					enum memblock_flags flags)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	/* pump up @end */
310*4882a593Smuzhiyun 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
311*4882a593Smuzhiyun 	    end == MEMBLOCK_ALLOC_KASAN)
312*4882a593Smuzhiyun 		end = memblock.current_limit;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	/* avoid allocating the first page */
315*4882a593Smuzhiyun 	start = max_t(phys_addr_t, start, PAGE_SIZE);
316*4882a593Smuzhiyun 	end = max(start, end);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	if (memblock_bottom_up())
319*4882a593Smuzhiyun 		return __memblock_find_range_bottom_up(start, end, size, align,
320*4882a593Smuzhiyun 						       nid, flags);
321*4882a593Smuzhiyun 	else
322*4882a593Smuzhiyun 		return __memblock_find_range_top_down(start, end, size, align,
323*4882a593Smuzhiyun 						      nid, flags);
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun /**
327*4882a593Smuzhiyun  * memblock_find_in_range - find free area in given range
328*4882a593Smuzhiyun  * @start: start of candidate range
329*4882a593Smuzhiyun  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
330*4882a593Smuzhiyun  *       %MEMBLOCK_ALLOC_ACCESSIBLE
331*4882a593Smuzhiyun  * @size: size of free area to find
332*4882a593Smuzhiyun  * @align: alignment of free area to find
333*4882a593Smuzhiyun  *
334*4882a593Smuzhiyun  * Find @size free area aligned to @align in the specified range.
335*4882a593Smuzhiyun  *
336*4882a593Smuzhiyun  * Return:
337*4882a593Smuzhiyun  * Found address on success, 0 on failure.
338*4882a593Smuzhiyun  */
memblock_find_in_range(phys_addr_t start,phys_addr_t end,phys_addr_t size,phys_addr_t align)339*4882a593Smuzhiyun phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
340*4882a593Smuzhiyun 					phys_addr_t end, phys_addr_t size,
341*4882a593Smuzhiyun 					phys_addr_t align)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun 	phys_addr_t ret;
344*4882a593Smuzhiyun 	enum memblock_flags flags = choose_memblock_flags();
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun again:
347*4882a593Smuzhiyun 	ret = memblock_find_in_range_node(size, align, start, end,
348*4882a593Smuzhiyun 					    NUMA_NO_NODE, flags);
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	if (!ret && (flags & MEMBLOCK_MIRROR)) {
351*4882a593Smuzhiyun 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
352*4882a593Smuzhiyun 			&size);
353*4882a593Smuzhiyun 		flags &= ~MEMBLOCK_MIRROR;
354*4882a593Smuzhiyun 		goto again;
355*4882a593Smuzhiyun 	}
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	return ret;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
memblock_remove_region(struct memblock_type * type,unsigned long r)360*4882a593Smuzhiyun static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	type->total_size -= type->regions[r].size;
363*4882a593Smuzhiyun 	memmove(&type->regions[r], &type->regions[r + 1],
364*4882a593Smuzhiyun 		(type->cnt - (r + 1)) * sizeof(type->regions[r]));
365*4882a593Smuzhiyun 	type->cnt--;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	/* Special case for empty arrays */
368*4882a593Smuzhiyun 	if (type->cnt == 0) {
369*4882a593Smuzhiyun 		WARN_ON(type->total_size != 0);
370*4882a593Smuzhiyun 		type->cnt = 1;
371*4882a593Smuzhiyun 		type->regions[0].base = 0;
372*4882a593Smuzhiyun 		type->regions[0].size = 0;
373*4882a593Smuzhiyun 		type->regions[0].flags = 0;
374*4882a593Smuzhiyun 		memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
375*4882a593Smuzhiyun 	}
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun #ifndef CONFIG_ARCH_KEEP_MEMBLOCK
379*4882a593Smuzhiyun /**
380*4882a593Smuzhiyun  * memblock_discard - discard memory and reserved arrays if they were allocated
381*4882a593Smuzhiyun  */
memblock_discard(void)382*4882a593Smuzhiyun void __init memblock_discard(void)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun 	phys_addr_t addr, size;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	if (memblock.reserved.regions != memblock_reserved_init_regions) {
387*4882a593Smuzhiyun 		addr = __pa(memblock.reserved.regions);
388*4882a593Smuzhiyun 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
389*4882a593Smuzhiyun 				  memblock.reserved.max);
390*4882a593Smuzhiyun 		if (memblock_reserved_in_slab)
391*4882a593Smuzhiyun 			kfree(memblock.reserved.regions);
392*4882a593Smuzhiyun 		else
393*4882a593Smuzhiyun 			__memblock_free_late(addr, size);
394*4882a593Smuzhiyun 	}
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	if (memblock.memory.regions != memblock_memory_init_regions) {
397*4882a593Smuzhiyun 		addr = __pa(memblock.memory.regions);
398*4882a593Smuzhiyun 		size = PAGE_ALIGN(sizeof(struct memblock_region) *
399*4882a593Smuzhiyun 				  memblock.memory.max);
400*4882a593Smuzhiyun 		if (memblock_memory_in_slab)
401*4882a593Smuzhiyun 			kfree(memblock.memory.regions);
402*4882a593Smuzhiyun 		else
403*4882a593Smuzhiyun 			__memblock_free_late(addr, size);
404*4882a593Smuzhiyun 	}
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	memblock_memory = NULL;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun #endif
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun /**
411*4882a593Smuzhiyun  * memblock_double_array - double the size of the memblock regions array
412*4882a593Smuzhiyun  * @type: memblock type of the regions array being doubled
413*4882a593Smuzhiyun  * @new_area_start: starting address of memory range to avoid overlap with
414*4882a593Smuzhiyun  * @new_area_size: size of memory range to avoid overlap with
415*4882a593Smuzhiyun  *
416*4882a593Smuzhiyun  * Double the size of the @type regions array. If memblock is being used to
417*4882a593Smuzhiyun  * allocate memory for a new reserved regions array and there is a previously
418*4882a593Smuzhiyun  * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
419*4882a593Smuzhiyun  * waiting to be reserved, ensure the memory used by the new array does
420*4882a593Smuzhiyun  * not overlap.
421*4882a593Smuzhiyun  *
422*4882a593Smuzhiyun  * Return:
423*4882a593Smuzhiyun  * 0 on success, -1 on failure.
424*4882a593Smuzhiyun  */
memblock_double_array(struct memblock_type * type,phys_addr_t new_area_start,phys_addr_t new_area_size)425*4882a593Smuzhiyun static int __init_memblock memblock_double_array(struct memblock_type *type,
426*4882a593Smuzhiyun 						phys_addr_t new_area_start,
427*4882a593Smuzhiyun 						phys_addr_t new_area_size)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun 	struct memblock_region *new_array, *old_array;
430*4882a593Smuzhiyun 	phys_addr_t old_alloc_size, new_alloc_size;
431*4882a593Smuzhiyun 	phys_addr_t old_size, new_size, addr, new_end;
432*4882a593Smuzhiyun 	int use_slab = slab_is_available();
433*4882a593Smuzhiyun 	int *in_slab;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	/* We don't allow resizing until we know about the reserved regions
436*4882a593Smuzhiyun 	 * of memory that aren't suitable for allocation
437*4882a593Smuzhiyun 	 */
438*4882a593Smuzhiyun 	if (!memblock_can_resize)
439*4882a593Smuzhiyun 		return -1;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	/* Calculate new doubled size */
442*4882a593Smuzhiyun 	old_size = type->max * sizeof(struct memblock_region);
443*4882a593Smuzhiyun 	new_size = old_size << 1;
444*4882a593Smuzhiyun 	/*
445*4882a593Smuzhiyun 	 * We need to allocated new one align to PAGE_SIZE,
446*4882a593Smuzhiyun 	 *   so we can free them completely later.
447*4882a593Smuzhiyun 	 */
448*4882a593Smuzhiyun 	old_alloc_size = PAGE_ALIGN(old_size);
449*4882a593Smuzhiyun 	new_alloc_size = PAGE_ALIGN(new_size);
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	/* Retrieve the slab flag */
452*4882a593Smuzhiyun 	if (type == &memblock.memory)
453*4882a593Smuzhiyun 		in_slab = &memblock_memory_in_slab;
454*4882a593Smuzhiyun 	else
455*4882a593Smuzhiyun 		in_slab = &memblock_reserved_in_slab;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	/* Try to find some space for it */
458*4882a593Smuzhiyun 	if (use_slab) {
459*4882a593Smuzhiyun 		new_array = kmalloc(new_size, GFP_KERNEL);
460*4882a593Smuzhiyun 		addr = new_array ? __pa(new_array) : 0;
461*4882a593Smuzhiyun 	} else {
462*4882a593Smuzhiyun 		/* only exclude range when trying to double reserved.regions */
463*4882a593Smuzhiyun 		if (type != &memblock.reserved)
464*4882a593Smuzhiyun 			new_area_start = new_area_size = 0;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 		addr = memblock_find_in_range(new_area_start + new_area_size,
467*4882a593Smuzhiyun 						memblock.current_limit,
468*4882a593Smuzhiyun 						new_alloc_size, PAGE_SIZE);
469*4882a593Smuzhiyun 		if (!addr && new_area_size)
470*4882a593Smuzhiyun 			addr = memblock_find_in_range(0,
471*4882a593Smuzhiyun 				min(new_area_start, memblock.current_limit),
472*4882a593Smuzhiyun 				new_alloc_size, PAGE_SIZE);
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 		new_array = addr ? __va(addr) : NULL;
475*4882a593Smuzhiyun 	}
476*4882a593Smuzhiyun 	if (!addr) {
477*4882a593Smuzhiyun 		pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
478*4882a593Smuzhiyun 		       type->name, type->max, type->max * 2);
479*4882a593Smuzhiyun 		return -1;
480*4882a593Smuzhiyun 	}
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	new_end = addr + new_size - 1;
483*4882a593Smuzhiyun 	memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
484*4882a593Smuzhiyun 			type->name, type->max * 2, &addr, &new_end);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	/*
487*4882a593Smuzhiyun 	 * Found space, we now need to move the array over before we add the
488*4882a593Smuzhiyun 	 * reserved region since it may be our reserved array itself that is
489*4882a593Smuzhiyun 	 * full.
490*4882a593Smuzhiyun 	 */
491*4882a593Smuzhiyun 	memcpy(new_array, type->regions, old_size);
492*4882a593Smuzhiyun 	memset(new_array + type->max, 0, old_size);
493*4882a593Smuzhiyun 	old_array = type->regions;
494*4882a593Smuzhiyun 	type->regions = new_array;
495*4882a593Smuzhiyun 	type->max <<= 1;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	/* Free old array. We needn't free it if the array is the static one */
498*4882a593Smuzhiyun 	if (*in_slab)
499*4882a593Smuzhiyun 		kfree(old_array);
500*4882a593Smuzhiyun 	else if (old_array != memblock_memory_init_regions &&
501*4882a593Smuzhiyun 		 old_array != memblock_reserved_init_regions)
502*4882a593Smuzhiyun 		memblock_free(__pa(old_array), old_alloc_size);
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	/*
505*4882a593Smuzhiyun 	 * Reserve the new array if that comes from the memblock.  Otherwise, we
506*4882a593Smuzhiyun 	 * needn't do it
507*4882a593Smuzhiyun 	 */
508*4882a593Smuzhiyun 	if (!use_slab)
509*4882a593Smuzhiyun 		BUG_ON(memblock_reserve(addr, new_alloc_size));
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	/* Update slab flag */
512*4882a593Smuzhiyun 	*in_slab = use_slab;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	return 0;
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun /**
518*4882a593Smuzhiyun  * memblock_merge_regions - merge neighboring compatible regions
519*4882a593Smuzhiyun  * @type: memblock type to scan
520*4882a593Smuzhiyun  *
521*4882a593Smuzhiyun  * Scan @type and merge neighboring compatible regions.
522*4882a593Smuzhiyun  */
memblock_merge_regions(struct memblock_type * type)523*4882a593Smuzhiyun static void __init_memblock memblock_merge_regions(struct memblock_type *type)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun 	int i = 0;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	/* cnt never goes below 1 */
528*4882a593Smuzhiyun 	while (i < type->cnt - 1) {
529*4882a593Smuzhiyun 		struct memblock_region *this = &type->regions[i];
530*4882a593Smuzhiyun 		struct memblock_region *next = &type->regions[i + 1];
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 		if (this->base + this->size != next->base ||
533*4882a593Smuzhiyun 		    memblock_get_region_node(this) !=
534*4882a593Smuzhiyun 		    memblock_get_region_node(next) ||
535*4882a593Smuzhiyun 		    this->flags != next->flags) {
536*4882a593Smuzhiyun 			BUG_ON(this->base + this->size > next->base);
537*4882a593Smuzhiyun 			i++;
538*4882a593Smuzhiyun 			continue;
539*4882a593Smuzhiyun 		}
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 		this->size += next->size;
542*4882a593Smuzhiyun 		/* move forward from next + 1, index of which is i + 2 */
543*4882a593Smuzhiyun 		memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
544*4882a593Smuzhiyun 		type->cnt--;
545*4882a593Smuzhiyun 	}
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun /**
549*4882a593Smuzhiyun  * memblock_insert_region - insert new memblock region
550*4882a593Smuzhiyun  * @type:	memblock type to insert into
551*4882a593Smuzhiyun  * @idx:	index for the insertion point
552*4882a593Smuzhiyun  * @base:	base address of the new region
553*4882a593Smuzhiyun  * @size:	size of the new region
554*4882a593Smuzhiyun  * @nid:	node id of the new region
555*4882a593Smuzhiyun  * @flags:	flags of the new region
556*4882a593Smuzhiyun  *
557*4882a593Smuzhiyun  * Insert new memblock region [@base, @base + @size) into @type at @idx.
558*4882a593Smuzhiyun  * @type must already have extra room to accommodate the new region.
559*4882a593Smuzhiyun  */
memblock_insert_region(struct memblock_type * type,int idx,phys_addr_t base,phys_addr_t size,int nid,enum memblock_flags flags)560*4882a593Smuzhiyun static void __init_memblock memblock_insert_region(struct memblock_type *type,
561*4882a593Smuzhiyun 						   int idx, phys_addr_t base,
562*4882a593Smuzhiyun 						   phys_addr_t size,
563*4882a593Smuzhiyun 						   int nid,
564*4882a593Smuzhiyun 						   enum memblock_flags flags)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun 	struct memblock_region *rgn = &type->regions[idx];
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	BUG_ON(type->cnt >= type->max);
569*4882a593Smuzhiyun 	memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
570*4882a593Smuzhiyun 	rgn->base = base;
571*4882a593Smuzhiyun 	rgn->size = size;
572*4882a593Smuzhiyun 	rgn->flags = flags;
573*4882a593Smuzhiyun 	memblock_set_region_node(rgn, nid);
574*4882a593Smuzhiyun 	type->cnt++;
575*4882a593Smuzhiyun 	type->total_size += size;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun /**
579*4882a593Smuzhiyun  * memblock_add_range - add new memblock region
580*4882a593Smuzhiyun  * @type: memblock type to add new region into
581*4882a593Smuzhiyun  * @base: base address of the new region
582*4882a593Smuzhiyun  * @size: size of the new region
583*4882a593Smuzhiyun  * @nid: nid of the new region
584*4882a593Smuzhiyun  * @flags: flags of the new region
585*4882a593Smuzhiyun  *
586*4882a593Smuzhiyun  * Add new memblock region [@base, @base + @size) into @type.  The new region
587*4882a593Smuzhiyun  * is allowed to overlap with existing ones - overlaps don't affect already
588*4882a593Smuzhiyun  * existing regions.  @type is guaranteed to be minimal (all neighbouring
589*4882a593Smuzhiyun  * compatible regions are merged) after the addition.
590*4882a593Smuzhiyun  *
591*4882a593Smuzhiyun  * Return:
592*4882a593Smuzhiyun  * 0 on success, -errno on failure.
593*4882a593Smuzhiyun  */
memblock_add_range(struct memblock_type * type,phys_addr_t base,phys_addr_t size,int nid,enum memblock_flags flags)594*4882a593Smuzhiyun static int __init_memblock memblock_add_range(struct memblock_type *type,
595*4882a593Smuzhiyun 				phys_addr_t base, phys_addr_t size,
596*4882a593Smuzhiyun 				int nid, enum memblock_flags flags)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun 	bool insert = false;
599*4882a593Smuzhiyun 	phys_addr_t obase = base;
600*4882a593Smuzhiyun 	phys_addr_t end = base + memblock_cap_size(base, &size);
601*4882a593Smuzhiyun 	int idx, nr_new;
602*4882a593Smuzhiyun 	struct memblock_region *rgn;
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	if (!size)
605*4882a593Smuzhiyun 		return 0;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	/* special case for empty array */
608*4882a593Smuzhiyun 	if (type->regions[0].size == 0) {
609*4882a593Smuzhiyun 		WARN_ON(type->cnt != 1 || type->total_size);
610*4882a593Smuzhiyun 		type->regions[0].base = base;
611*4882a593Smuzhiyun 		type->regions[0].size = size;
612*4882a593Smuzhiyun 		type->regions[0].flags = flags;
613*4882a593Smuzhiyun 		memblock_set_region_node(&type->regions[0], nid);
614*4882a593Smuzhiyun 		type->total_size = size;
615*4882a593Smuzhiyun 		return 0;
616*4882a593Smuzhiyun 	}
617*4882a593Smuzhiyun repeat:
618*4882a593Smuzhiyun 	/*
619*4882a593Smuzhiyun 	 * The following is executed twice.  Once with %false @insert and
620*4882a593Smuzhiyun 	 * then with %true.  The first counts the number of regions needed
621*4882a593Smuzhiyun 	 * to accommodate the new area.  The second actually inserts them.
622*4882a593Smuzhiyun 	 */
623*4882a593Smuzhiyun 	base = obase;
624*4882a593Smuzhiyun 	nr_new = 0;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 	for_each_memblock_type(idx, type, rgn) {
627*4882a593Smuzhiyun 		phys_addr_t rbase = rgn->base;
628*4882a593Smuzhiyun 		phys_addr_t rend = rbase + rgn->size;
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 		if (rbase >= end)
631*4882a593Smuzhiyun 			break;
632*4882a593Smuzhiyun 		if (rend <= base)
633*4882a593Smuzhiyun 			continue;
634*4882a593Smuzhiyun 		/*
635*4882a593Smuzhiyun 		 * @rgn overlaps.  If it separates the lower part of new
636*4882a593Smuzhiyun 		 * area, insert that portion.
637*4882a593Smuzhiyun 		 */
638*4882a593Smuzhiyun 		if (rbase > base) {
639*4882a593Smuzhiyun #ifdef CONFIG_NEED_MULTIPLE_NODES
640*4882a593Smuzhiyun 			WARN_ON(nid != memblock_get_region_node(rgn));
641*4882a593Smuzhiyun #endif
642*4882a593Smuzhiyun 			WARN_ON(flags != rgn->flags);
643*4882a593Smuzhiyun 			nr_new++;
644*4882a593Smuzhiyun 			if (insert)
645*4882a593Smuzhiyun 				memblock_insert_region(type, idx++, base,
646*4882a593Smuzhiyun 						       rbase - base, nid,
647*4882a593Smuzhiyun 						       flags);
648*4882a593Smuzhiyun 		}
649*4882a593Smuzhiyun 		/* area below @rend is dealt with, forget about it */
650*4882a593Smuzhiyun 		base = min(rend, end);
651*4882a593Smuzhiyun 	}
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	/* insert the remaining portion */
654*4882a593Smuzhiyun 	if (base < end) {
655*4882a593Smuzhiyun 		nr_new++;
656*4882a593Smuzhiyun 		if (insert)
657*4882a593Smuzhiyun 			memblock_insert_region(type, idx, base, end - base,
658*4882a593Smuzhiyun 					       nid, flags);
659*4882a593Smuzhiyun 	}
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	if (!nr_new)
662*4882a593Smuzhiyun 		return 0;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	/*
665*4882a593Smuzhiyun 	 * If this was the first round, resize array and repeat for actual
666*4882a593Smuzhiyun 	 * insertions; otherwise, merge and return.
667*4882a593Smuzhiyun 	 */
668*4882a593Smuzhiyun 	if (!insert) {
669*4882a593Smuzhiyun 		while (type->cnt + nr_new > type->max)
670*4882a593Smuzhiyun 			if (memblock_double_array(type, obase, size) < 0)
671*4882a593Smuzhiyun 				return -ENOMEM;
672*4882a593Smuzhiyun 		insert = true;
673*4882a593Smuzhiyun 		goto repeat;
674*4882a593Smuzhiyun 	} else {
675*4882a593Smuzhiyun 		memblock_merge_regions(type);
676*4882a593Smuzhiyun 		return 0;
677*4882a593Smuzhiyun 	}
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun /**
681*4882a593Smuzhiyun  * memblock_add_node - add new memblock region within a NUMA node
682*4882a593Smuzhiyun  * @base: base address of the new region
683*4882a593Smuzhiyun  * @size: size of the new region
684*4882a593Smuzhiyun  * @nid: nid of the new region
685*4882a593Smuzhiyun  *
686*4882a593Smuzhiyun  * Add new memblock region [@base, @base + @size) to the "memory"
687*4882a593Smuzhiyun  * type. See memblock_add_range() description for mode details
688*4882a593Smuzhiyun  *
689*4882a593Smuzhiyun  * Return:
690*4882a593Smuzhiyun  * 0 on success, -errno on failure.
691*4882a593Smuzhiyun  */
memblock_add_node(phys_addr_t base,phys_addr_t size,int nid)692*4882a593Smuzhiyun int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
693*4882a593Smuzhiyun 				       int nid)
694*4882a593Smuzhiyun {
695*4882a593Smuzhiyun 	return memblock_add_range(&memblock.memory, base, size, nid, 0);
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun /**
699*4882a593Smuzhiyun  * memblock_add - add new memblock region
700*4882a593Smuzhiyun  * @base: base address of the new region
701*4882a593Smuzhiyun  * @size: size of the new region
702*4882a593Smuzhiyun  *
703*4882a593Smuzhiyun  * Add new memblock region [@base, @base + @size) to the "memory"
704*4882a593Smuzhiyun  * type. See memblock_add_range() description for mode details
705*4882a593Smuzhiyun  *
706*4882a593Smuzhiyun  * Return:
707*4882a593Smuzhiyun  * 0 on success, -errno on failure.
708*4882a593Smuzhiyun  */
memblock_add(phys_addr_t base,phys_addr_t size)709*4882a593Smuzhiyun int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun 	phys_addr_t end = base + size - 1;
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
714*4882a593Smuzhiyun 		     &base, &end, (void *)_RET_IP_);
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun /**
720*4882a593Smuzhiyun  * memblock_isolate_range - isolate given range into disjoint memblocks
721*4882a593Smuzhiyun  * @type: memblock type to isolate range for
722*4882a593Smuzhiyun  * @base: base of range to isolate
723*4882a593Smuzhiyun  * @size: size of range to isolate
724*4882a593Smuzhiyun  * @start_rgn: out parameter for the start of isolated region
725*4882a593Smuzhiyun  * @end_rgn: out parameter for the end of isolated region
726*4882a593Smuzhiyun  *
727*4882a593Smuzhiyun  * Walk @type and ensure that regions don't cross the boundaries defined by
728*4882a593Smuzhiyun  * [@base, @base + @size).  Crossing regions are split at the boundaries,
729*4882a593Smuzhiyun  * which may create at most two more regions.  The index of the first
730*4882a593Smuzhiyun  * region inside the range is returned in *@start_rgn and end in *@end_rgn.
731*4882a593Smuzhiyun  *
732*4882a593Smuzhiyun  * Return:
733*4882a593Smuzhiyun  * 0 on success, -errno on failure.
734*4882a593Smuzhiyun  */
memblock_isolate_range(struct memblock_type * type,phys_addr_t base,phys_addr_t size,int * start_rgn,int * end_rgn)735*4882a593Smuzhiyun static int __init_memblock memblock_isolate_range(struct memblock_type *type,
736*4882a593Smuzhiyun 					phys_addr_t base, phys_addr_t size,
737*4882a593Smuzhiyun 					int *start_rgn, int *end_rgn)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun 	phys_addr_t end = base + memblock_cap_size(base, &size);
740*4882a593Smuzhiyun 	int idx;
741*4882a593Smuzhiyun 	struct memblock_region *rgn;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	*start_rgn = *end_rgn = 0;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	if (!size)
746*4882a593Smuzhiyun 		return 0;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	/* we'll create at most two more regions */
749*4882a593Smuzhiyun 	while (type->cnt + 2 > type->max)
750*4882a593Smuzhiyun 		if (memblock_double_array(type, base, size) < 0)
751*4882a593Smuzhiyun 			return -ENOMEM;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	for_each_memblock_type(idx, type, rgn) {
754*4882a593Smuzhiyun 		phys_addr_t rbase = rgn->base;
755*4882a593Smuzhiyun 		phys_addr_t rend = rbase + rgn->size;
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 		if (rbase >= end)
758*4882a593Smuzhiyun 			break;
759*4882a593Smuzhiyun 		if (rend <= base)
760*4882a593Smuzhiyun 			continue;
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 		if (rbase < base) {
763*4882a593Smuzhiyun 			/*
764*4882a593Smuzhiyun 			 * @rgn intersects from below.  Split and continue
765*4882a593Smuzhiyun 			 * to process the next region - the new top half.
766*4882a593Smuzhiyun 			 */
767*4882a593Smuzhiyun 			rgn->base = base;
768*4882a593Smuzhiyun 			rgn->size -= base - rbase;
769*4882a593Smuzhiyun 			type->total_size -= base - rbase;
770*4882a593Smuzhiyun 			memblock_insert_region(type, idx, rbase, base - rbase,
771*4882a593Smuzhiyun 					       memblock_get_region_node(rgn),
772*4882a593Smuzhiyun 					       rgn->flags);
773*4882a593Smuzhiyun 		} else if (rend > end) {
774*4882a593Smuzhiyun 			/*
775*4882a593Smuzhiyun 			 * @rgn intersects from above.  Split and redo the
776*4882a593Smuzhiyun 			 * current region - the new bottom half.
777*4882a593Smuzhiyun 			 */
778*4882a593Smuzhiyun 			rgn->base = end;
779*4882a593Smuzhiyun 			rgn->size -= end - rbase;
780*4882a593Smuzhiyun 			type->total_size -= end - rbase;
781*4882a593Smuzhiyun 			memblock_insert_region(type, idx--, rbase, end - rbase,
782*4882a593Smuzhiyun 					       memblock_get_region_node(rgn),
783*4882a593Smuzhiyun 					       rgn->flags);
784*4882a593Smuzhiyun 		} else {
785*4882a593Smuzhiyun 			/* @rgn is fully contained, record it */
786*4882a593Smuzhiyun 			if (!*end_rgn)
787*4882a593Smuzhiyun 				*start_rgn = idx;
788*4882a593Smuzhiyun 			*end_rgn = idx + 1;
789*4882a593Smuzhiyun 		}
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	return 0;
793*4882a593Smuzhiyun }
794*4882a593Smuzhiyun 
memblock_remove_range(struct memblock_type * type,phys_addr_t base,phys_addr_t size)795*4882a593Smuzhiyun static int __init_memblock memblock_remove_range(struct memblock_type *type,
796*4882a593Smuzhiyun 					  phys_addr_t base, phys_addr_t size)
797*4882a593Smuzhiyun {
798*4882a593Smuzhiyun 	int start_rgn, end_rgn;
799*4882a593Smuzhiyun 	int i, ret;
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
802*4882a593Smuzhiyun 	if (ret)
803*4882a593Smuzhiyun 		return ret;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	for (i = end_rgn - 1; i >= start_rgn; i--)
806*4882a593Smuzhiyun 		memblock_remove_region(type, i);
807*4882a593Smuzhiyun 	return 0;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun 
memblock_remove(phys_addr_t base,phys_addr_t size)810*4882a593Smuzhiyun int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
811*4882a593Smuzhiyun {
812*4882a593Smuzhiyun 	phys_addr_t end = base + size - 1;
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
815*4882a593Smuzhiyun 		     &base, &end, (void *)_RET_IP_);
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	return memblock_remove_range(&memblock.memory, base, size);
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun /**
821*4882a593Smuzhiyun  * memblock_free - free boot memory block
822*4882a593Smuzhiyun  * @base: phys starting address of the  boot memory block
823*4882a593Smuzhiyun  * @size: size of the boot memory block in bytes
824*4882a593Smuzhiyun  *
825*4882a593Smuzhiyun  * Free boot memory block previously allocated by memblock_alloc_xx() API.
826*4882a593Smuzhiyun  * The freeing memory will not be released to the buddy allocator.
827*4882a593Smuzhiyun  */
memblock_free(phys_addr_t base,phys_addr_t size)828*4882a593Smuzhiyun int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
829*4882a593Smuzhiyun {
830*4882a593Smuzhiyun 	phys_addr_t end = base + size - 1;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
833*4882a593Smuzhiyun 		     &base, &end, (void *)_RET_IP_);
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	kmemleak_free_part_phys(base, size);
836*4882a593Smuzhiyun 	return memblock_remove_range(&memblock.reserved, base, size);
837*4882a593Smuzhiyun }
838*4882a593Smuzhiyun #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
839*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(memblock_free);
840*4882a593Smuzhiyun #endif
841*4882a593Smuzhiyun 
memblock_reserve(phys_addr_t base,phys_addr_t size)842*4882a593Smuzhiyun int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
843*4882a593Smuzhiyun {
844*4882a593Smuzhiyun 	phys_addr_t end = base + size - 1;
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
847*4882a593Smuzhiyun 		     &base, &end, (void *)_RET_IP_);
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
850*4882a593Smuzhiyun }
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
memblock_physmem_add(phys_addr_t base,phys_addr_t size)853*4882a593Smuzhiyun int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun 	phys_addr_t end = base + size - 1;
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
858*4882a593Smuzhiyun 		     &base, &end, (void *)_RET_IP_);
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0);
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun #endif
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun /**
865*4882a593Smuzhiyun  * memblock_setclr_flag - set or clear flag for a memory region
866*4882a593Smuzhiyun  * @base: base address of the region
867*4882a593Smuzhiyun  * @size: size of the region
868*4882a593Smuzhiyun  * @set: set or clear the flag
869*4882a593Smuzhiyun  * @flag: the flag to udpate
870*4882a593Smuzhiyun  *
871*4882a593Smuzhiyun  * This function isolates region [@base, @base + @size), and sets/clears flag
872*4882a593Smuzhiyun  *
873*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure.
874*4882a593Smuzhiyun  */
memblock_setclr_flag(phys_addr_t base,phys_addr_t size,int set,int flag)875*4882a593Smuzhiyun static int __init_memblock memblock_setclr_flag(phys_addr_t base,
876*4882a593Smuzhiyun 				phys_addr_t size, int set, int flag)
877*4882a593Smuzhiyun {
878*4882a593Smuzhiyun 	struct memblock_type *type = &memblock.memory;
879*4882a593Smuzhiyun 	int i, ret, start_rgn, end_rgn;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
882*4882a593Smuzhiyun 	if (ret)
883*4882a593Smuzhiyun 		return ret;
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	for (i = start_rgn; i < end_rgn; i++) {
886*4882a593Smuzhiyun 		struct memblock_region *r = &type->regions[i];
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 		if (set)
889*4882a593Smuzhiyun 			r->flags |= flag;
890*4882a593Smuzhiyun 		else
891*4882a593Smuzhiyun 			r->flags &= ~flag;
892*4882a593Smuzhiyun 	}
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 	memblock_merge_regions(type);
895*4882a593Smuzhiyun 	return 0;
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun /**
899*4882a593Smuzhiyun  * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
900*4882a593Smuzhiyun  * @base: the base phys addr of the region
901*4882a593Smuzhiyun  * @size: the size of the region
902*4882a593Smuzhiyun  *
903*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure.
904*4882a593Smuzhiyun  */
memblock_mark_hotplug(phys_addr_t base,phys_addr_t size)905*4882a593Smuzhiyun int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun /**
911*4882a593Smuzhiyun  * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
912*4882a593Smuzhiyun  * @base: the base phys addr of the region
913*4882a593Smuzhiyun  * @size: the size of the region
914*4882a593Smuzhiyun  *
915*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure.
916*4882a593Smuzhiyun  */
memblock_clear_hotplug(phys_addr_t base,phys_addr_t size)917*4882a593Smuzhiyun int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun /**
923*4882a593Smuzhiyun  * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
924*4882a593Smuzhiyun  * @base: the base phys addr of the region
925*4882a593Smuzhiyun  * @size: the size of the region
926*4882a593Smuzhiyun  *
927*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure.
928*4882a593Smuzhiyun  */
memblock_mark_mirror(phys_addr_t base,phys_addr_t size)929*4882a593Smuzhiyun int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
930*4882a593Smuzhiyun {
931*4882a593Smuzhiyun 	system_has_some_mirror = true;
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun /**
937*4882a593Smuzhiyun  * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
938*4882a593Smuzhiyun  * @base: the base phys addr of the region
939*4882a593Smuzhiyun  * @size: the size of the region
940*4882a593Smuzhiyun  *
941*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure.
942*4882a593Smuzhiyun  */
memblock_mark_nomap(phys_addr_t base,phys_addr_t size)943*4882a593Smuzhiyun int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
944*4882a593Smuzhiyun {
945*4882a593Smuzhiyun 	return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
946*4882a593Smuzhiyun }
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun /**
949*4882a593Smuzhiyun  * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
950*4882a593Smuzhiyun  * @base: the base phys addr of the region
951*4882a593Smuzhiyun  * @size: the size of the region
952*4882a593Smuzhiyun  *
953*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure.
954*4882a593Smuzhiyun  */
memblock_clear_nomap(phys_addr_t base,phys_addr_t size)955*4882a593Smuzhiyun int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
956*4882a593Smuzhiyun {
957*4882a593Smuzhiyun 	return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
958*4882a593Smuzhiyun }
959*4882a593Smuzhiyun 
should_skip_region(struct memblock_type * type,struct memblock_region * m,int nid,int flags)960*4882a593Smuzhiyun static bool should_skip_region(struct memblock_type *type,
961*4882a593Smuzhiyun 			       struct memblock_region *m,
962*4882a593Smuzhiyun 			       int nid, int flags)
963*4882a593Smuzhiyun {
964*4882a593Smuzhiyun 	int m_nid = memblock_get_region_node(m);
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	/* we never skip regions when iterating memblock.reserved or physmem */
967*4882a593Smuzhiyun 	if (type != memblock_memory)
968*4882a593Smuzhiyun 		return false;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	/* only memory regions are associated with nodes, check it */
971*4882a593Smuzhiyun 	if (nid != NUMA_NO_NODE && nid != m_nid)
972*4882a593Smuzhiyun 		return true;
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	/* skip hotpluggable memory regions if needed */
975*4882a593Smuzhiyun 	if (movable_node_is_enabled() && memblock_is_hotpluggable(m) &&
976*4882a593Smuzhiyun 	    !(flags & MEMBLOCK_HOTPLUG))
977*4882a593Smuzhiyun 		return true;
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 	/* if we want mirror memory skip non-mirror memory regions */
980*4882a593Smuzhiyun 	if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
981*4882a593Smuzhiyun 		return true;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	/* skip nomap memory unless we were asked for it explicitly */
984*4882a593Smuzhiyun 	if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
985*4882a593Smuzhiyun 		return true;
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 	return false;
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun /**
991*4882a593Smuzhiyun  * __next_mem_range - next function for for_each_free_mem_range() etc.
992*4882a593Smuzhiyun  * @idx: pointer to u64 loop variable
993*4882a593Smuzhiyun  * @nid: node selector, %NUMA_NO_NODE for all nodes
994*4882a593Smuzhiyun  * @flags: pick from blocks based on memory attributes
995*4882a593Smuzhiyun  * @type_a: pointer to memblock_type from where the range is taken
996*4882a593Smuzhiyun  * @type_b: pointer to memblock_type which excludes memory from being taken
997*4882a593Smuzhiyun  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
998*4882a593Smuzhiyun  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
999*4882a593Smuzhiyun  * @out_nid: ptr to int for nid of the range, can be %NULL
1000*4882a593Smuzhiyun  *
1001*4882a593Smuzhiyun  * Find the first area from *@idx which matches @nid, fill the out
1002*4882a593Smuzhiyun  * parameters, and update *@idx for the next iteration.  The lower 32bit of
1003*4882a593Smuzhiyun  * *@idx contains index into type_a and the upper 32bit indexes the
1004*4882a593Smuzhiyun  * areas before each region in type_b.	For example, if type_b regions
1005*4882a593Smuzhiyun  * look like the following,
1006*4882a593Smuzhiyun  *
1007*4882a593Smuzhiyun  *	0:[0-16), 1:[32-48), 2:[128-130)
1008*4882a593Smuzhiyun  *
1009*4882a593Smuzhiyun  * The upper 32bit indexes the following regions.
1010*4882a593Smuzhiyun  *
1011*4882a593Smuzhiyun  *	0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
1012*4882a593Smuzhiyun  *
1013*4882a593Smuzhiyun  * As both region arrays are sorted, the function advances the two indices
1014*4882a593Smuzhiyun  * in lockstep and returns each intersection.
1015*4882a593Smuzhiyun  */
__next_mem_range(u64 * idx,int nid,enum memblock_flags flags,struct memblock_type * type_a,struct memblock_type * type_b,phys_addr_t * out_start,phys_addr_t * out_end,int * out_nid)1016*4882a593Smuzhiyun void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
1017*4882a593Smuzhiyun 		      struct memblock_type *type_a,
1018*4882a593Smuzhiyun 		      struct memblock_type *type_b, phys_addr_t *out_start,
1019*4882a593Smuzhiyun 		      phys_addr_t *out_end, int *out_nid)
1020*4882a593Smuzhiyun {
1021*4882a593Smuzhiyun 	int idx_a = *idx & 0xffffffff;
1022*4882a593Smuzhiyun 	int idx_b = *idx >> 32;
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	if (WARN_ONCE(nid == MAX_NUMNODES,
1025*4882a593Smuzhiyun 	"Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1026*4882a593Smuzhiyun 		nid = NUMA_NO_NODE;
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun 	for (; idx_a < type_a->cnt; idx_a++) {
1029*4882a593Smuzhiyun 		struct memblock_region *m = &type_a->regions[idx_a];
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 		phys_addr_t m_start = m->base;
1032*4882a593Smuzhiyun 		phys_addr_t m_end = m->base + m->size;
1033*4882a593Smuzhiyun 		int	    m_nid = memblock_get_region_node(m);
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 		if (should_skip_region(type_a, m, nid, flags))
1036*4882a593Smuzhiyun 			continue;
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 		if (!type_b) {
1039*4882a593Smuzhiyun 			if (out_start)
1040*4882a593Smuzhiyun 				*out_start = m_start;
1041*4882a593Smuzhiyun 			if (out_end)
1042*4882a593Smuzhiyun 				*out_end = m_end;
1043*4882a593Smuzhiyun 			if (out_nid)
1044*4882a593Smuzhiyun 				*out_nid = m_nid;
1045*4882a593Smuzhiyun 			idx_a++;
1046*4882a593Smuzhiyun 			*idx = (u32)idx_a | (u64)idx_b << 32;
1047*4882a593Smuzhiyun 			return;
1048*4882a593Smuzhiyun 		}
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 		/* scan areas before each reservation */
1051*4882a593Smuzhiyun 		for (; idx_b < type_b->cnt + 1; idx_b++) {
1052*4882a593Smuzhiyun 			struct memblock_region *r;
1053*4882a593Smuzhiyun 			phys_addr_t r_start;
1054*4882a593Smuzhiyun 			phys_addr_t r_end;
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 			r = &type_b->regions[idx_b];
1057*4882a593Smuzhiyun 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1058*4882a593Smuzhiyun 			r_end = idx_b < type_b->cnt ?
1059*4882a593Smuzhiyun 				r->base : PHYS_ADDR_MAX;
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 			/*
1062*4882a593Smuzhiyun 			 * if idx_b advanced past idx_a,
1063*4882a593Smuzhiyun 			 * break out to advance idx_a
1064*4882a593Smuzhiyun 			 */
1065*4882a593Smuzhiyun 			if (r_start >= m_end)
1066*4882a593Smuzhiyun 				break;
1067*4882a593Smuzhiyun 			/* if the two regions intersect, we're done */
1068*4882a593Smuzhiyun 			if (m_start < r_end) {
1069*4882a593Smuzhiyun 				if (out_start)
1070*4882a593Smuzhiyun 					*out_start =
1071*4882a593Smuzhiyun 						max(m_start, r_start);
1072*4882a593Smuzhiyun 				if (out_end)
1073*4882a593Smuzhiyun 					*out_end = min(m_end, r_end);
1074*4882a593Smuzhiyun 				if (out_nid)
1075*4882a593Smuzhiyun 					*out_nid = m_nid;
1076*4882a593Smuzhiyun 				/*
1077*4882a593Smuzhiyun 				 * The region which ends first is
1078*4882a593Smuzhiyun 				 * advanced for the next iteration.
1079*4882a593Smuzhiyun 				 */
1080*4882a593Smuzhiyun 				if (m_end <= r_end)
1081*4882a593Smuzhiyun 					idx_a++;
1082*4882a593Smuzhiyun 				else
1083*4882a593Smuzhiyun 					idx_b++;
1084*4882a593Smuzhiyun 				*idx = (u32)idx_a | (u64)idx_b << 32;
1085*4882a593Smuzhiyun 				return;
1086*4882a593Smuzhiyun 			}
1087*4882a593Smuzhiyun 		}
1088*4882a593Smuzhiyun 	}
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	/* signal end of iteration */
1091*4882a593Smuzhiyun 	*idx = ULLONG_MAX;
1092*4882a593Smuzhiyun }
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun /**
1095*4882a593Smuzhiyun  * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1096*4882a593Smuzhiyun  *
1097*4882a593Smuzhiyun  * @idx: pointer to u64 loop variable
1098*4882a593Smuzhiyun  * @nid: node selector, %NUMA_NO_NODE for all nodes
1099*4882a593Smuzhiyun  * @flags: pick from blocks based on memory attributes
1100*4882a593Smuzhiyun  * @type_a: pointer to memblock_type from where the range is taken
1101*4882a593Smuzhiyun  * @type_b: pointer to memblock_type which excludes memory from being taken
1102*4882a593Smuzhiyun  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1103*4882a593Smuzhiyun  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1104*4882a593Smuzhiyun  * @out_nid: ptr to int for nid of the range, can be %NULL
1105*4882a593Smuzhiyun  *
1106*4882a593Smuzhiyun  * Finds the next range from type_a which is not marked as unsuitable
1107*4882a593Smuzhiyun  * in type_b.
1108*4882a593Smuzhiyun  *
1109*4882a593Smuzhiyun  * Reverse of __next_mem_range().
1110*4882a593Smuzhiyun  */
__next_mem_range_rev(u64 * idx,int nid,enum memblock_flags flags,struct memblock_type * type_a,struct memblock_type * type_b,phys_addr_t * out_start,phys_addr_t * out_end,int * out_nid)1111*4882a593Smuzhiyun void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1112*4882a593Smuzhiyun 					  enum memblock_flags flags,
1113*4882a593Smuzhiyun 					  struct memblock_type *type_a,
1114*4882a593Smuzhiyun 					  struct memblock_type *type_b,
1115*4882a593Smuzhiyun 					  phys_addr_t *out_start,
1116*4882a593Smuzhiyun 					  phys_addr_t *out_end, int *out_nid)
1117*4882a593Smuzhiyun {
1118*4882a593Smuzhiyun 	int idx_a = *idx & 0xffffffff;
1119*4882a593Smuzhiyun 	int idx_b = *idx >> 32;
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1122*4882a593Smuzhiyun 		nid = NUMA_NO_NODE;
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	if (*idx == (u64)ULLONG_MAX) {
1125*4882a593Smuzhiyun 		idx_a = type_a->cnt - 1;
1126*4882a593Smuzhiyun 		if (type_b != NULL)
1127*4882a593Smuzhiyun 			idx_b = type_b->cnt;
1128*4882a593Smuzhiyun 		else
1129*4882a593Smuzhiyun 			idx_b = 0;
1130*4882a593Smuzhiyun 	}
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	for (; idx_a >= 0; idx_a--) {
1133*4882a593Smuzhiyun 		struct memblock_region *m = &type_a->regions[idx_a];
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 		phys_addr_t m_start = m->base;
1136*4882a593Smuzhiyun 		phys_addr_t m_end = m->base + m->size;
1137*4882a593Smuzhiyun 		int m_nid = memblock_get_region_node(m);
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun 		if (should_skip_region(type_a, m, nid, flags))
1140*4882a593Smuzhiyun 			continue;
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 		if (!type_b) {
1143*4882a593Smuzhiyun 			if (out_start)
1144*4882a593Smuzhiyun 				*out_start = m_start;
1145*4882a593Smuzhiyun 			if (out_end)
1146*4882a593Smuzhiyun 				*out_end = m_end;
1147*4882a593Smuzhiyun 			if (out_nid)
1148*4882a593Smuzhiyun 				*out_nid = m_nid;
1149*4882a593Smuzhiyun 			idx_a--;
1150*4882a593Smuzhiyun 			*idx = (u32)idx_a | (u64)idx_b << 32;
1151*4882a593Smuzhiyun 			return;
1152*4882a593Smuzhiyun 		}
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 		/* scan areas before each reservation */
1155*4882a593Smuzhiyun 		for (; idx_b >= 0; idx_b--) {
1156*4882a593Smuzhiyun 			struct memblock_region *r;
1157*4882a593Smuzhiyun 			phys_addr_t r_start;
1158*4882a593Smuzhiyun 			phys_addr_t r_end;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 			r = &type_b->regions[idx_b];
1161*4882a593Smuzhiyun 			r_start = idx_b ? r[-1].base + r[-1].size : 0;
1162*4882a593Smuzhiyun 			r_end = idx_b < type_b->cnt ?
1163*4882a593Smuzhiyun 				r->base : PHYS_ADDR_MAX;
1164*4882a593Smuzhiyun 			/*
1165*4882a593Smuzhiyun 			 * if idx_b advanced past idx_a,
1166*4882a593Smuzhiyun 			 * break out to advance idx_a
1167*4882a593Smuzhiyun 			 */
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 			if (r_end <= m_start)
1170*4882a593Smuzhiyun 				break;
1171*4882a593Smuzhiyun 			/* if the two regions intersect, we're done */
1172*4882a593Smuzhiyun 			if (m_end > r_start) {
1173*4882a593Smuzhiyun 				if (out_start)
1174*4882a593Smuzhiyun 					*out_start = max(m_start, r_start);
1175*4882a593Smuzhiyun 				if (out_end)
1176*4882a593Smuzhiyun 					*out_end = min(m_end, r_end);
1177*4882a593Smuzhiyun 				if (out_nid)
1178*4882a593Smuzhiyun 					*out_nid = m_nid;
1179*4882a593Smuzhiyun 				if (m_start >= r_start)
1180*4882a593Smuzhiyun 					idx_a--;
1181*4882a593Smuzhiyun 				else
1182*4882a593Smuzhiyun 					idx_b--;
1183*4882a593Smuzhiyun 				*idx = (u32)idx_a | (u64)idx_b << 32;
1184*4882a593Smuzhiyun 				return;
1185*4882a593Smuzhiyun 			}
1186*4882a593Smuzhiyun 		}
1187*4882a593Smuzhiyun 	}
1188*4882a593Smuzhiyun 	/* signal end of iteration */
1189*4882a593Smuzhiyun 	*idx = ULLONG_MAX;
1190*4882a593Smuzhiyun }
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun /*
1193*4882a593Smuzhiyun  * Common iterator interface used to define for_each_mem_pfn_range().
1194*4882a593Smuzhiyun  */
__next_mem_pfn_range(int * idx,int nid,unsigned long * out_start_pfn,unsigned long * out_end_pfn,int * out_nid)1195*4882a593Smuzhiyun void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1196*4882a593Smuzhiyun 				unsigned long *out_start_pfn,
1197*4882a593Smuzhiyun 				unsigned long *out_end_pfn, int *out_nid)
1198*4882a593Smuzhiyun {
1199*4882a593Smuzhiyun 	struct memblock_type *type = &memblock.memory;
1200*4882a593Smuzhiyun 	struct memblock_region *r;
1201*4882a593Smuzhiyun 	int r_nid;
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 	while (++*idx < type->cnt) {
1204*4882a593Smuzhiyun 		r = &type->regions[*idx];
1205*4882a593Smuzhiyun 		r_nid = memblock_get_region_node(r);
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 		if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1208*4882a593Smuzhiyun 			continue;
1209*4882a593Smuzhiyun 		if (nid == MAX_NUMNODES || nid == r_nid)
1210*4882a593Smuzhiyun 			break;
1211*4882a593Smuzhiyun 	}
1212*4882a593Smuzhiyun 	if (*idx >= type->cnt) {
1213*4882a593Smuzhiyun 		*idx = -1;
1214*4882a593Smuzhiyun 		return;
1215*4882a593Smuzhiyun 	}
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun 	if (out_start_pfn)
1218*4882a593Smuzhiyun 		*out_start_pfn = PFN_UP(r->base);
1219*4882a593Smuzhiyun 	if (out_end_pfn)
1220*4882a593Smuzhiyun 		*out_end_pfn = PFN_DOWN(r->base + r->size);
1221*4882a593Smuzhiyun 	if (out_nid)
1222*4882a593Smuzhiyun 		*out_nid = r_nid;
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun /**
1226*4882a593Smuzhiyun  * memblock_set_node - set node ID on memblock regions
1227*4882a593Smuzhiyun  * @base: base of area to set node ID for
1228*4882a593Smuzhiyun  * @size: size of area to set node ID for
1229*4882a593Smuzhiyun  * @type: memblock type to set node ID for
1230*4882a593Smuzhiyun  * @nid: node ID to set
1231*4882a593Smuzhiyun  *
1232*4882a593Smuzhiyun  * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
1233*4882a593Smuzhiyun  * Regions which cross the area boundaries are split as necessary.
1234*4882a593Smuzhiyun  *
1235*4882a593Smuzhiyun  * Return:
1236*4882a593Smuzhiyun  * 0 on success, -errno on failure.
1237*4882a593Smuzhiyun  */
memblock_set_node(phys_addr_t base,phys_addr_t size,struct memblock_type * type,int nid)1238*4882a593Smuzhiyun int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1239*4882a593Smuzhiyun 				      struct memblock_type *type, int nid)
1240*4882a593Smuzhiyun {
1241*4882a593Smuzhiyun #ifdef CONFIG_NEED_MULTIPLE_NODES
1242*4882a593Smuzhiyun 	int start_rgn, end_rgn;
1243*4882a593Smuzhiyun 	int i, ret;
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1246*4882a593Smuzhiyun 	if (ret)
1247*4882a593Smuzhiyun 		return ret;
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 	for (i = start_rgn; i < end_rgn; i++)
1250*4882a593Smuzhiyun 		memblock_set_region_node(&type->regions[i], nid);
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 	memblock_merge_regions(type);
1253*4882a593Smuzhiyun #endif
1254*4882a593Smuzhiyun 	return 0;
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1258*4882a593Smuzhiyun /**
1259*4882a593Smuzhiyun  * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
1260*4882a593Smuzhiyun  *
1261*4882a593Smuzhiyun  * @idx: pointer to u64 loop variable
1262*4882a593Smuzhiyun  * @zone: zone in which all of the memory blocks reside
1263*4882a593Smuzhiyun  * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
1264*4882a593Smuzhiyun  * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
1265*4882a593Smuzhiyun  *
1266*4882a593Smuzhiyun  * This function is meant to be a zone/pfn specific wrapper for the
1267*4882a593Smuzhiyun  * for_each_mem_range type iterators. Specifically they are used in the
1268*4882a593Smuzhiyun  * deferred memory init routines and as such we were duplicating much of
1269*4882a593Smuzhiyun  * this logic throughout the code. So instead of having it in multiple
1270*4882a593Smuzhiyun  * locations it seemed like it would make more sense to centralize this to
1271*4882a593Smuzhiyun  * one new iterator that does everything they need.
1272*4882a593Smuzhiyun  */
1273*4882a593Smuzhiyun void __init_memblock
__next_mem_pfn_range_in_zone(u64 * idx,struct zone * zone,unsigned long * out_spfn,unsigned long * out_epfn)1274*4882a593Smuzhiyun __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
1275*4882a593Smuzhiyun 			     unsigned long *out_spfn, unsigned long *out_epfn)
1276*4882a593Smuzhiyun {
1277*4882a593Smuzhiyun 	int zone_nid = zone_to_nid(zone);
1278*4882a593Smuzhiyun 	phys_addr_t spa, epa;
1279*4882a593Smuzhiyun 	int nid;
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 	__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1282*4882a593Smuzhiyun 			 &memblock.memory, &memblock.reserved,
1283*4882a593Smuzhiyun 			 &spa, &epa, &nid);
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	while (*idx != U64_MAX) {
1286*4882a593Smuzhiyun 		unsigned long epfn = PFN_DOWN(epa);
1287*4882a593Smuzhiyun 		unsigned long spfn = PFN_UP(spa);
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 		/*
1290*4882a593Smuzhiyun 		 * Verify the end is at least past the start of the zone and
1291*4882a593Smuzhiyun 		 * that we have at least one PFN to initialize.
1292*4882a593Smuzhiyun 		 */
1293*4882a593Smuzhiyun 		if (zone->zone_start_pfn < epfn && spfn < epfn) {
1294*4882a593Smuzhiyun 			/* if we went too far just stop searching */
1295*4882a593Smuzhiyun 			if (zone_end_pfn(zone) <= spfn) {
1296*4882a593Smuzhiyun 				*idx = U64_MAX;
1297*4882a593Smuzhiyun 				break;
1298*4882a593Smuzhiyun 			}
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 			if (out_spfn)
1301*4882a593Smuzhiyun 				*out_spfn = max(zone->zone_start_pfn, spfn);
1302*4882a593Smuzhiyun 			if (out_epfn)
1303*4882a593Smuzhiyun 				*out_epfn = min(zone_end_pfn(zone), epfn);
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 			return;
1306*4882a593Smuzhiyun 		}
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 		__next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
1309*4882a593Smuzhiyun 				 &memblock.memory, &memblock.reserved,
1310*4882a593Smuzhiyun 				 &spa, &epa, &nid);
1311*4882a593Smuzhiyun 	}
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun 	/* signal end of iteration */
1314*4882a593Smuzhiyun 	if (out_spfn)
1315*4882a593Smuzhiyun 		*out_spfn = ULONG_MAX;
1316*4882a593Smuzhiyun 	if (out_epfn)
1317*4882a593Smuzhiyun 		*out_epfn = 0;
1318*4882a593Smuzhiyun }
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
1321*4882a593Smuzhiyun 
1322*4882a593Smuzhiyun /**
1323*4882a593Smuzhiyun  * memblock_alloc_range_nid - allocate boot memory block
1324*4882a593Smuzhiyun  * @size: size of memory block to be allocated in bytes
1325*4882a593Smuzhiyun  * @align: alignment of the region and block's size
1326*4882a593Smuzhiyun  * @start: the lower bound of the memory region to allocate (phys address)
1327*4882a593Smuzhiyun  * @end: the upper bound of the memory region to allocate (phys address)
1328*4882a593Smuzhiyun  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1329*4882a593Smuzhiyun  * @exact_nid: control the allocation fall back to other nodes
1330*4882a593Smuzhiyun  *
1331*4882a593Smuzhiyun  * The allocation is performed from memory region limited by
1332*4882a593Smuzhiyun  * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE.
1333*4882a593Smuzhiyun  *
1334*4882a593Smuzhiyun  * If the specified node can not hold the requested memory and @exact_nid
1335*4882a593Smuzhiyun  * is false, the allocation falls back to any node in the system.
1336*4882a593Smuzhiyun  *
1337*4882a593Smuzhiyun  * For systems with memory mirroring, the allocation is attempted first
1338*4882a593Smuzhiyun  * from the regions with mirroring enabled and then retried from any
1339*4882a593Smuzhiyun  * memory region.
1340*4882a593Smuzhiyun  *
1341*4882a593Smuzhiyun  * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for
1342*4882a593Smuzhiyun  * allocated boot memory block, so that it is never reported as leaks.
1343*4882a593Smuzhiyun  *
1344*4882a593Smuzhiyun  * Return:
1345*4882a593Smuzhiyun  * Physical address of allocated memory block on success, %0 on failure.
1346*4882a593Smuzhiyun  */
memblock_alloc_range_nid(phys_addr_t size,phys_addr_t align,phys_addr_t start,phys_addr_t end,int nid,bool exact_nid)1347*4882a593Smuzhiyun phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1348*4882a593Smuzhiyun 					phys_addr_t align, phys_addr_t start,
1349*4882a593Smuzhiyun 					phys_addr_t end, int nid,
1350*4882a593Smuzhiyun 					bool exact_nid)
1351*4882a593Smuzhiyun {
1352*4882a593Smuzhiyun 	enum memblock_flags flags = choose_memblock_flags();
1353*4882a593Smuzhiyun 	phys_addr_t found;
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1356*4882a593Smuzhiyun 		nid = NUMA_NO_NODE;
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 	if (!align) {
1359*4882a593Smuzhiyun 		/* Can't use WARNs this early in boot on powerpc */
1360*4882a593Smuzhiyun 		dump_stack();
1361*4882a593Smuzhiyun 		align = SMP_CACHE_BYTES;
1362*4882a593Smuzhiyun 	}
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun again:
1365*4882a593Smuzhiyun 	found = memblock_find_in_range_node(size, align, start, end, nid,
1366*4882a593Smuzhiyun 					    flags);
1367*4882a593Smuzhiyun 	if (found && !memblock_reserve(found, size))
1368*4882a593Smuzhiyun 		goto done;
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 	if (nid != NUMA_NO_NODE && !exact_nid) {
1371*4882a593Smuzhiyun 		found = memblock_find_in_range_node(size, align, start,
1372*4882a593Smuzhiyun 						    end, NUMA_NO_NODE,
1373*4882a593Smuzhiyun 						    flags);
1374*4882a593Smuzhiyun 		if (found && !memblock_reserve(found, size))
1375*4882a593Smuzhiyun 			goto done;
1376*4882a593Smuzhiyun 	}
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 	if (flags & MEMBLOCK_MIRROR) {
1379*4882a593Smuzhiyun 		flags &= ~MEMBLOCK_MIRROR;
1380*4882a593Smuzhiyun 		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1381*4882a593Smuzhiyun 			&size);
1382*4882a593Smuzhiyun 		goto again;
1383*4882a593Smuzhiyun 	}
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	return 0;
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun done:
1388*4882a593Smuzhiyun 	/* Skip kmemleak for kasan_init() due to high volume. */
1389*4882a593Smuzhiyun 	if (end != MEMBLOCK_ALLOC_KASAN)
1390*4882a593Smuzhiyun 		/*
1391*4882a593Smuzhiyun 		 * The min_count is set to 0 so that memblock allocated
1392*4882a593Smuzhiyun 		 * blocks are never reported as leaks. This is because many
1393*4882a593Smuzhiyun 		 * of these blocks are only referred via the physical
1394*4882a593Smuzhiyun 		 * address which is not looked up by kmemleak.
1395*4882a593Smuzhiyun 		 */
1396*4882a593Smuzhiyun 		kmemleak_alloc_phys(found, size, 0, 0);
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	return found;
1399*4882a593Smuzhiyun }
1400*4882a593Smuzhiyun 
1401*4882a593Smuzhiyun /**
1402*4882a593Smuzhiyun  * memblock_phys_alloc_range - allocate a memory block inside specified range
1403*4882a593Smuzhiyun  * @size: size of memory block to be allocated in bytes
1404*4882a593Smuzhiyun  * @align: alignment of the region and block's size
1405*4882a593Smuzhiyun  * @start: the lower bound of the memory region to allocate (physical address)
1406*4882a593Smuzhiyun  * @end: the upper bound of the memory region to allocate (physical address)
1407*4882a593Smuzhiyun  *
1408*4882a593Smuzhiyun  * Allocate @size bytes in the between @start and @end.
1409*4882a593Smuzhiyun  *
1410*4882a593Smuzhiyun  * Return: physical address of the allocated memory block on success,
1411*4882a593Smuzhiyun  * %0 on failure.
1412*4882a593Smuzhiyun  */
memblock_phys_alloc_range(phys_addr_t size,phys_addr_t align,phys_addr_t start,phys_addr_t end)1413*4882a593Smuzhiyun phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
1414*4882a593Smuzhiyun 					     phys_addr_t align,
1415*4882a593Smuzhiyun 					     phys_addr_t start,
1416*4882a593Smuzhiyun 					     phys_addr_t end)
1417*4882a593Smuzhiyun {
1418*4882a593Smuzhiyun 	memblock_dbg("%s: %llu bytes align=0x%llx from=%pa max_addr=%pa %pS\n",
1419*4882a593Smuzhiyun 		     __func__, (u64)size, (u64)align, &start, &end,
1420*4882a593Smuzhiyun 		     (void *)_RET_IP_);
1421*4882a593Smuzhiyun 	return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1422*4882a593Smuzhiyun 					false);
1423*4882a593Smuzhiyun }
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun /**
1426*4882a593Smuzhiyun  * memblock_phys_alloc_try_nid - allocate a memory block from specified MUMA node
1427*4882a593Smuzhiyun  * @size: size of memory block to be allocated in bytes
1428*4882a593Smuzhiyun  * @align: alignment of the region and block's size
1429*4882a593Smuzhiyun  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1430*4882a593Smuzhiyun  *
1431*4882a593Smuzhiyun  * Allocates memory block from the specified NUMA node. If the node
1432*4882a593Smuzhiyun  * has no available memory, attempts to allocated from any node in the
1433*4882a593Smuzhiyun  * system.
1434*4882a593Smuzhiyun  *
1435*4882a593Smuzhiyun  * Return: physical address of the allocated memory block on success,
1436*4882a593Smuzhiyun  * %0 on failure.
1437*4882a593Smuzhiyun  */
memblock_phys_alloc_try_nid(phys_addr_t size,phys_addr_t align,int nid)1438*4882a593Smuzhiyun phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1439*4882a593Smuzhiyun {
1440*4882a593Smuzhiyun 	return memblock_alloc_range_nid(size, align, 0,
1441*4882a593Smuzhiyun 					MEMBLOCK_ALLOC_ACCESSIBLE, nid, false);
1442*4882a593Smuzhiyun }
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun /**
1445*4882a593Smuzhiyun  * memblock_alloc_internal - allocate boot memory block
1446*4882a593Smuzhiyun  * @size: size of memory block to be allocated in bytes
1447*4882a593Smuzhiyun  * @align: alignment of the region and block's size
1448*4882a593Smuzhiyun  * @min_addr: the lower bound of the memory region to allocate (phys address)
1449*4882a593Smuzhiyun  * @max_addr: the upper bound of the memory region to allocate (phys address)
1450*4882a593Smuzhiyun  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1451*4882a593Smuzhiyun  * @exact_nid: control the allocation fall back to other nodes
1452*4882a593Smuzhiyun  *
1453*4882a593Smuzhiyun  * Allocates memory block using memblock_alloc_range_nid() and
1454*4882a593Smuzhiyun  * converts the returned physical address to virtual.
1455*4882a593Smuzhiyun  *
1456*4882a593Smuzhiyun  * The @min_addr limit is dropped if it can not be satisfied and the allocation
1457*4882a593Smuzhiyun  * will fall back to memory below @min_addr. Other constraints, such
1458*4882a593Smuzhiyun  * as node and mirrored memory will be handled again in
1459*4882a593Smuzhiyun  * memblock_alloc_range_nid().
1460*4882a593Smuzhiyun  *
1461*4882a593Smuzhiyun  * Return:
1462*4882a593Smuzhiyun  * Virtual address of allocated memory block on success, NULL on failure.
1463*4882a593Smuzhiyun  */
memblock_alloc_internal(phys_addr_t size,phys_addr_t align,phys_addr_t min_addr,phys_addr_t max_addr,int nid,bool exact_nid)1464*4882a593Smuzhiyun static void * __init memblock_alloc_internal(
1465*4882a593Smuzhiyun 				phys_addr_t size, phys_addr_t align,
1466*4882a593Smuzhiyun 				phys_addr_t min_addr, phys_addr_t max_addr,
1467*4882a593Smuzhiyun 				int nid, bool exact_nid)
1468*4882a593Smuzhiyun {
1469*4882a593Smuzhiyun 	phys_addr_t alloc;
1470*4882a593Smuzhiyun 
1471*4882a593Smuzhiyun 	/*
1472*4882a593Smuzhiyun 	 * Detect any accidental use of these APIs after slab is ready, as at
1473*4882a593Smuzhiyun 	 * this moment memblock may be deinitialized already and its
1474*4882a593Smuzhiyun 	 * internal data may be destroyed (after execution of memblock_free_all)
1475*4882a593Smuzhiyun 	 */
1476*4882a593Smuzhiyun 	if (WARN_ON_ONCE(slab_is_available()))
1477*4882a593Smuzhiyun 		return kzalloc_node(size, GFP_NOWAIT, nid);
1478*4882a593Smuzhiyun 
1479*4882a593Smuzhiyun 	if (max_addr > memblock.current_limit)
1480*4882a593Smuzhiyun 		max_addr = memblock.current_limit;
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun 	alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid,
1483*4882a593Smuzhiyun 					exact_nid);
1484*4882a593Smuzhiyun 
1485*4882a593Smuzhiyun 	/* retry allocation without lower limit */
1486*4882a593Smuzhiyun 	if (!alloc && min_addr)
1487*4882a593Smuzhiyun 		alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid,
1488*4882a593Smuzhiyun 						exact_nid);
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 	if (!alloc)
1491*4882a593Smuzhiyun 		return NULL;
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 	return phys_to_virt(alloc);
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun /**
1497*4882a593Smuzhiyun  * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node
1498*4882a593Smuzhiyun  * without zeroing memory
1499*4882a593Smuzhiyun  * @size: size of memory block to be allocated in bytes
1500*4882a593Smuzhiyun  * @align: alignment of the region and block's size
1501*4882a593Smuzhiyun  * @min_addr: the lower bound of the memory region from where the allocation
1502*4882a593Smuzhiyun  *	  is preferred (phys address)
1503*4882a593Smuzhiyun  * @max_addr: the upper bound of the memory region from where the allocation
1504*4882a593Smuzhiyun  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1505*4882a593Smuzhiyun  *	      allocate only from memory limited by memblock.current_limit value
1506*4882a593Smuzhiyun  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1507*4882a593Smuzhiyun  *
1508*4882a593Smuzhiyun  * Public function, provides additional debug information (including caller
1509*4882a593Smuzhiyun  * info), if enabled. Does not zero allocated memory.
1510*4882a593Smuzhiyun  *
1511*4882a593Smuzhiyun  * Return:
1512*4882a593Smuzhiyun  * Virtual address of allocated memory block on success, NULL on failure.
1513*4882a593Smuzhiyun  */
memblock_alloc_exact_nid_raw(phys_addr_t size,phys_addr_t align,phys_addr_t min_addr,phys_addr_t max_addr,int nid)1514*4882a593Smuzhiyun void * __init memblock_alloc_exact_nid_raw(
1515*4882a593Smuzhiyun 			phys_addr_t size, phys_addr_t align,
1516*4882a593Smuzhiyun 			phys_addr_t min_addr, phys_addr_t max_addr,
1517*4882a593Smuzhiyun 			int nid)
1518*4882a593Smuzhiyun {
1519*4882a593Smuzhiyun 	void *ptr;
1520*4882a593Smuzhiyun 
1521*4882a593Smuzhiyun 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1522*4882a593Smuzhiyun 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1523*4882a593Smuzhiyun 		     &max_addr, (void *)_RET_IP_);
1524*4882a593Smuzhiyun 
1525*4882a593Smuzhiyun 	ptr = memblock_alloc_internal(size, align,
1526*4882a593Smuzhiyun 					   min_addr, max_addr, nid, true);
1527*4882a593Smuzhiyun 	if (ptr && size > 0)
1528*4882a593Smuzhiyun 		page_init_poison(ptr, size);
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun 	return ptr;
1531*4882a593Smuzhiyun }
1532*4882a593Smuzhiyun 
1533*4882a593Smuzhiyun /**
1534*4882a593Smuzhiyun  * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
1535*4882a593Smuzhiyun  * memory and without panicking
1536*4882a593Smuzhiyun  * @size: size of memory block to be allocated in bytes
1537*4882a593Smuzhiyun  * @align: alignment of the region and block's size
1538*4882a593Smuzhiyun  * @min_addr: the lower bound of the memory region from where the allocation
1539*4882a593Smuzhiyun  *	  is preferred (phys address)
1540*4882a593Smuzhiyun  * @max_addr: the upper bound of the memory region from where the allocation
1541*4882a593Smuzhiyun  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1542*4882a593Smuzhiyun  *	      allocate only from memory limited by memblock.current_limit value
1543*4882a593Smuzhiyun  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1544*4882a593Smuzhiyun  *
1545*4882a593Smuzhiyun  * Public function, provides additional debug information (including caller
1546*4882a593Smuzhiyun  * info), if enabled. Does not zero allocated memory, does not panic if request
1547*4882a593Smuzhiyun  * cannot be satisfied.
1548*4882a593Smuzhiyun  *
1549*4882a593Smuzhiyun  * Return:
1550*4882a593Smuzhiyun  * Virtual address of allocated memory block on success, NULL on failure.
1551*4882a593Smuzhiyun  */
memblock_alloc_try_nid_raw(phys_addr_t size,phys_addr_t align,phys_addr_t min_addr,phys_addr_t max_addr,int nid)1552*4882a593Smuzhiyun void * __init memblock_alloc_try_nid_raw(
1553*4882a593Smuzhiyun 			phys_addr_t size, phys_addr_t align,
1554*4882a593Smuzhiyun 			phys_addr_t min_addr, phys_addr_t max_addr,
1555*4882a593Smuzhiyun 			int nid)
1556*4882a593Smuzhiyun {
1557*4882a593Smuzhiyun 	void *ptr;
1558*4882a593Smuzhiyun 
1559*4882a593Smuzhiyun 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1560*4882a593Smuzhiyun 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1561*4882a593Smuzhiyun 		     &max_addr, (void *)_RET_IP_);
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun 	ptr = memblock_alloc_internal(size, align,
1564*4882a593Smuzhiyun 					   min_addr, max_addr, nid, false);
1565*4882a593Smuzhiyun 	if (ptr && size > 0)
1566*4882a593Smuzhiyun 		page_init_poison(ptr, size);
1567*4882a593Smuzhiyun 
1568*4882a593Smuzhiyun 	return ptr;
1569*4882a593Smuzhiyun }
1570*4882a593Smuzhiyun 
1571*4882a593Smuzhiyun /**
1572*4882a593Smuzhiyun  * memblock_alloc_try_nid - allocate boot memory block
1573*4882a593Smuzhiyun  * @size: size of memory block to be allocated in bytes
1574*4882a593Smuzhiyun  * @align: alignment of the region and block's size
1575*4882a593Smuzhiyun  * @min_addr: the lower bound of the memory region from where the allocation
1576*4882a593Smuzhiyun  *	  is preferred (phys address)
1577*4882a593Smuzhiyun  * @max_addr: the upper bound of the memory region from where the allocation
1578*4882a593Smuzhiyun  *	      is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
1579*4882a593Smuzhiyun  *	      allocate only from memory limited by memblock.current_limit value
1580*4882a593Smuzhiyun  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1581*4882a593Smuzhiyun  *
1582*4882a593Smuzhiyun  * Public function, provides additional debug information (including caller
1583*4882a593Smuzhiyun  * info), if enabled. This function zeroes the allocated memory.
1584*4882a593Smuzhiyun  *
1585*4882a593Smuzhiyun  * Return:
1586*4882a593Smuzhiyun  * Virtual address of allocated memory block on success, NULL on failure.
1587*4882a593Smuzhiyun  */
memblock_alloc_try_nid(phys_addr_t size,phys_addr_t align,phys_addr_t min_addr,phys_addr_t max_addr,int nid)1588*4882a593Smuzhiyun void * __init memblock_alloc_try_nid(
1589*4882a593Smuzhiyun 			phys_addr_t size, phys_addr_t align,
1590*4882a593Smuzhiyun 			phys_addr_t min_addr, phys_addr_t max_addr,
1591*4882a593Smuzhiyun 			int nid)
1592*4882a593Smuzhiyun {
1593*4882a593Smuzhiyun 	void *ptr;
1594*4882a593Smuzhiyun 
1595*4882a593Smuzhiyun 	memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
1596*4882a593Smuzhiyun 		     __func__, (u64)size, (u64)align, nid, &min_addr,
1597*4882a593Smuzhiyun 		     &max_addr, (void *)_RET_IP_);
1598*4882a593Smuzhiyun 	ptr = memblock_alloc_internal(size, align,
1599*4882a593Smuzhiyun 					   min_addr, max_addr, nid, false);
1600*4882a593Smuzhiyun 	if (ptr)
1601*4882a593Smuzhiyun 		memset(ptr, 0, size);
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 	return ptr;
1604*4882a593Smuzhiyun }
1605*4882a593Smuzhiyun 
1606*4882a593Smuzhiyun /**
1607*4882a593Smuzhiyun  * __memblock_free_late - free pages directly to buddy allocator
1608*4882a593Smuzhiyun  * @base: phys starting address of the  boot memory block
1609*4882a593Smuzhiyun  * @size: size of the boot memory block in bytes
1610*4882a593Smuzhiyun  *
1611*4882a593Smuzhiyun  * This is only useful when the memblock allocator has already been torn
1612*4882a593Smuzhiyun  * down, but we are still initializing the system.  Pages are released directly
1613*4882a593Smuzhiyun  * to the buddy allocator.
1614*4882a593Smuzhiyun  */
__memblock_free_late(phys_addr_t base,phys_addr_t size)1615*4882a593Smuzhiyun void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1616*4882a593Smuzhiyun {
1617*4882a593Smuzhiyun 	phys_addr_t cursor, end;
1618*4882a593Smuzhiyun 
1619*4882a593Smuzhiyun 	end = base + size - 1;
1620*4882a593Smuzhiyun 	memblock_dbg("%s: [%pa-%pa] %pS\n",
1621*4882a593Smuzhiyun 		     __func__, &base, &end, (void *)_RET_IP_);
1622*4882a593Smuzhiyun 	kmemleak_free_part_phys(base, size);
1623*4882a593Smuzhiyun 	cursor = PFN_UP(base);
1624*4882a593Smuzhiyun 	end = PFN_DOWN(base + size);
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	for (; cursor < end; cursor++) {
1627*4882a593Smuzhiyun 		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
1628*4882a593Smuzhiyun 		totalram_pages_inc();
1629*4882a593Smuzhiyun 	}
1630*4882a593Smuzhiyun }
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun /*
1633*4882a593Smuzhiyun  * Remaining API functions
1634*4882a593Smuzhiyun  */
1635*4882a593Smuzhiyun 
memblock_phys_mem_size(void)1636*4882a593Smuzhiyun phys_addr_t __init_memblock memblock_phys_mem_size(void)
1637*4882a593Smuzhiyun {
1638*4882a593Smuzhiyun 	return memblock.memory.total_size;
1639*4882a593Smuzhiyun }
1640*4882a593Smuzhiyun 
memblock_reserved_size(void)1641*4882a593Smuzhiyun phys_addr_t __init_memblock memblock_reserved_size(void)
1642*4882a593Smuzhiyun {
1643*4882a593Smuzhiyun 	return memblock.reserved.total_size;
1644*4882a593Smuzhiyun }
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun /* lowest address */
memblock_start_of_DRAM(void)1647*4882a593Smuzhiyun phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1648*4882a593Smuzhiyun {
1649*4882a593Smuzhiyun 	return memblock.memory.regions[0].base;
1650*4882a593Smuzhiyun }
1651*4882a593Smuzhiyun 
memblock_end_of_DRAM(void)1652*4882a593Smuzhiyun phys_addr_t __init_memblock memblock_end_of_DRAM(void)
1653*4882a593Smuzhiyun {
1654*4882a593Smuzhiyun 	int idx = memblock.memory.cnt - 1;
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun 	return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
1657*4882a593Smuzhiyun }
1658*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(memblock_end_of_DRAM);
1659*4882a593Smuzhiyun 
__find_max_addr(phys_addr_t limit)1660*4882a593Smuzhiyun static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
1661*4882a593Smuzhiyun {
1662*4882a593Smuzhiyun 	phys_addr_t max_addr = PHYS_ADDR_MAX;
1663*4882a593Smuzhiyun 	struct memblock_region *r;
1664*4882a593Smuzhiyun 
1665*4882a593Smuzhiyun 	/*
1666*4882a593Smuzhiyun 	 * translate the memory @limit size into the max address within one of
1667*4882a593Smuzhiyun 	 * the memory memblock regions, if the @limit exceeds the total size
1668*4882a593Smuzhiyun 	 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
1669*4882a593Smuzhiyun 	 */
1670*4882a593Smuzhiyun 	for_each_mem_region(r) {
1671*4882a593Smuzhiyun 		if (limit <= r->size) {
1672*4882a593Smuzhiyun 			max_addr = r->base + limit;
1673*4882a593Smuzhiyun 			break;
1674*4882a593Smuzhiyun 		}
1675*4882a593Smuzhiyun 		limit -= r->size;
1676*4882a593Smuzhiyun 	}
1677*4882a593Smuzhiyun 
1678*4882a593Smuzhiyun 	return max_addr;
1679*4882a593Smuzhiyun }
1680*4882a593Smuzhiyun 
memblock_enforce_memory_limit(phys_addr_t limit)1681*4882a593Smuzhiyun void __init memblock_enforce_memory_limit(phys_addr_t limit)
1682*4882a593Smuzhiyun {
1683*4882a593Smuzhiyun 	phys_addr_t max_addr;
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun 	if (!limit)
1686*4882a593Smuzhiyun 		return;
1687*4882a593Smuzhiyun 
1688*4882a593Smuzhiyun 	max_addr = __find_max_addr(limit);
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	/* @limit exceeds the total size of the memory, do nothing */
1691*4882a593Smuzhiyun 	if (max_addr == PHYS_ADDR_MAX)
1692*4882a593Smuzhiyun 		return;
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun 	/* truncate both memory and reserved regions */
1695*4882a593Smuzhiyun 	memblock_remove_range(&memblock.memory, max_addr,
1696*4882a593Smuzhiyun 			      PHYS_ADDR_MAX);
1697*4882a593Smuzhiyun 	memblock_remove_range(&memblock.reserved, max_addr,
1698*4882a593Smuzhiyun 			      PHYS_ADDR_MAX);
1699*4882a593Smuzhiyun }
1700*4882a593Smuzhiyun 
memblock_cap_memory_range(phys_addr_t base,phys_addr_t size)1701*4882a593Smuzhiyun void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1702*4882a593Smuzhiyun {
1703*4882a593Smuzhiyun 	int start_rgn, end_rgn;
1704*4882a593Smuzhiyun 	int i, ret;
1705*4882a593Smuzhiyun 
1706*4882a593Smuzhiyun 	if (!size)
1707*4882a593Smuzhiyun 		return;
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 	ret = memblock_isolate_range(&memblock.memory, base, size,
1710*4882a593Smuzhiyun 						&start_rgn, &end_rgn);
1711*4882a593Smuzhiyun 	if (ret)
1712*4882a593Smuzhiyun 		return;
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun 	/* remove all the MAP regions */
1715*4882a593Smuzhiyun 	for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1716*4882a593Smuzhiyun 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1717*4882a593Smuzhiyun 			memblock_remove_region(&memblock.memory, i);
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun 	for (i = start_rgn - 1; i >= 0; i--)
1720*4882a593Smuzhiyun 		if (!memblock_is_nomap(&memblock.memory.regions[i]))
1721*4882a593Smuzhiyun 			memblock_remove_region(&memblock.memory, i);
1722*4882a593Smuzhiyun 
1723*4882a593Smuzhiyun 	/* truncate the reserved regions */
1724*4882a593Smuzhiyun 	memblock_remove_range(&memblock.reserved, 0, base);
1725*4882a593Smuzhiyun 	memblock_remove_range(&memblock.reserved,
1726*4882a593Smuzhiyun 			base + size, PHYS_ADDR_MAX);
1727*4882a593Smuzhiyun }
1728*4882a593Smuzhiyun 
memblock_mem_limit_remove_map(phys_addr_t limit)1729*4882a593Smuzhiyun void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1730*4882a593Smuzhiyun {
1731*4882a593Smuzhiyun 	phys_addr_t max_addr;
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	if (!limit)
1734*4882a593Smuzhiyun 		return;
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 	max_addr = __find_max_addr(limit);
1737*4882a593Smuzhiyun 
1738*4882a593Smuzhiyun 	/* @limit exceeds the total size of the memory, do nothing */
1739*4882a593Smuzhiyun 	if (max_addr == PHYS_ADDR_MAX)
1740*4882a593Smuzhiyun 		return;
1741*4882a593Smuzhiyun 
1742*4882a593Smuzhiyun 	memblock_cap_memory_range(0, max_addr);
1743*4882a593Smuzhiyun }
1744*4882a593Smuzhiyun 
memblock_search(struct memblock_type * type,phys_addr_t addr)1745*4882a593Smuzhiyun static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
1746*4882a593Smuzhiyun {
1747*4882a593Smuzhiyun 	unsigned int left = 0, right = type->cnt;
1748*4882a593Smuzhiyun 
1749*4882a593Smuzhiyun 	do {
1750*4882a593Smuzhiyun 		unsigned int mid = (right + left) / 2;
1751*4882a593Smuzhiyun 
1752*4882a593Smuzhiyun 		if (addr < type->regions[mid].base)
1753*4882a593Smuzhiyun 			right = mid;
1754*4882a593Smuzhiyun 		else if (addr >= (type->regions[mid].base +
1755*4882a593Smuzhiyun 				  type->regions[mid].size))
1756*4882a593Smuzhiyun 			left = mid + 1;
1757*4882a593Smuzhiyun 		else
1758*4882a593Smuzhiyun 			return mid;
1759*4882a593Smuzhiyun 	} while (left < right);
1760*4882a593Smuzhiyun 	return -1;
1761*4882a593Smuzhiyun }
1762*4882a593Smuzhiyun 
memblock_is_reserved(phys_addr_t addr)1763*4882a593Smuzhiyun bool __init_memblock memblock_is_reserved(phys_addr_t addr)
1764*4882a593Smuzhiyun {
1765*4882a593Smuzhiyun 	return memblock_search(&memblock.reserved, addr) != -1;
1766*4882a593Smuzhiyun }
1767*4882a593Smuzhiyun 
memblock_is_memory(phys_addr_t addr)1768*4882a593Smuzhiyun bool __init_memblock memblock_is_memory(phys_addr_t addr)
1769*4882a593Smuzhiyun {
1770*4882a593Smuzhiyun 	return memblock_search(&memblock.memory, addr) != -1;
1771*4882a593Smuzhiyun }
1772*4882a593Smuzhiyun 
memblock_is_map_memory(phys_addr_t addr)1773*4882a593Smuzhiyun bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
1774*4882a593Smuzhiyun {
1775*4882a593Smuzhiyun 	int i = memblock_search(&memblock.memory, addr);
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun 	if (i == -1)
1778*4882a593Smuzhiyun 		return false;
1779*4882a593Smuzhiyun 	return !memblock_is_nomap(&memblock.memory.regions[i]);
1780*4882a593Smuzhiyun }
1781*4882a593Smuzhiyun 
memblock_search_pfn_nid(unsigned long pfn,unsigned long * start_pfn,unsigned long * end_pfn)1782*4882a593Smuzhiyun int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1783*4882a593Smuzhiyun 			 unsigned long *start_pfn, unsigned long *end_pfn)
1784*4882a593Smuzhiyun {
1785*4882a593Smuzhiyun 	struct memblock_type *type = &memblock.memory;
1786*4882a593Smuzhiyun 	int mid = memblock_search(type, PFN_PHYS(pfn));
1787*4882a593Smuzhiyun 
1788*4882a593Smuzhiyun 	if (mid == -1)
1789*4882a593Smuzhiyun 		return -1;
1790*4882a593Smuzhiyun 
1791*4882a593Smuzhiyun 	*start_pfn = PFN_DOWN(type->regions[mid].base);
1792*4882a593Smuzhiyun 	*end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
1793*4882a593Smuzhiyun 
1794*4882a593Smuzhiyun 	return memblock_get_region_node(&type->regions[mid]);
1795*4882a593Smuzhiyun }
1796*4882a593Smuzhiyun 
1797*4882a593Smuzhiyun /**
1798*4882a593Smuzhiyun  * memblock_is_region_memory - check if a region is a subset of memory
1799*4882a593Smuzhiyun  * @base: base of region to check
1800*4882a593Smuzhiyun  * @size: size of region to check
1801*4882a593Smuzhiyun  *
1802*4882a593Smuzhiyun  * Check if the region [@base, @base + @size) is a subset of a memory block.
1803*4882a593Smuzhiyun  *
1804*4882a593Smuzhiyun  * Return:
1805*4882a593Smuzhiyun  * 0 if false, non-zero if true
1806*4882a593Smuzhiyun  */
memblock_is_region_memory(phys_addr_t base,phys_addr_t size)1807*4882a593Smuzhiyun bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
1808*4882a593Smuzhiyun {
1809*4882a593Smuzhiyun 	int idx = memblock_search(&memblock.memory, base);
1810*4882a593Smuzhiyun 	phys_addr_t end = base + memblock_cap_size(base, &size);
1811*4882a593Smuzhiyun 
1812*4882a593Smuzhiyun 	if (idx == -1)
1813*4882a593Smuzhiyun 		return false;
1814*4882a593Smuzhiyun 	return (memblock.memory.regions[idx].base +
1815*4882a593Smuzhiyun 		 memblock.memory.regions[idx].size) >= end;
1816*4882a593Smuzhiyun }
1817*4882a593Smuzhiyun 
1818*4882a593Smuzhiyun /**
1819*4882a593Smuzhiyun  * memblock_is_region_reserved - check if a region intersects reserved memory
1820*4882a593Smuzhiyun  * @base: base of region to check
1821*4882a593Smuzhiyun  * @size: size of region to check
1822*4882a593Smuzhiyun  *
1823*4882a593Smuzhiyun  * Check if the region [@base, @base + @size) intersects a reserved
1824*4882a593Smuzhiyun  * memory block.
1825*4882a593Smuzhiyun  *
1826*4882a593Smuzhiyun  * Return:
1827*4882a593Smuzhiyun  * True if they intersect, false if not.
1828*4882a593Smuzhiyun  */
memblock_is_region_reserved(phys_addr_t base,phys_addr_t size)1829*4882a593Smuzhiyun bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
1830*4882a593Smuzhiyun {
1831*4882a593Smuzhiyun 	return memblock_overlaps_region(&memblock.reserved, base, size);
1832*4882a593Smuzhiyun }
1833*4882a593Smuzhiyun 
memblock_trim_memory(phys_addr_t align)1834*4882a593Smuzhiyun void __init_memblock memblock_trim_memory(phys_addr_t align)
1835*4882a593Smuzhiyun {
1836*4882a593Smuzhiyun 	phys_addr_t start, end, orig_start, orig_end;
1837*4882a593Smuzhiyun 	struct memblock_region *r;
1838*4882a593Smuzhiyun 
1839*4882a593Smuzhiyun 	for_each_mem_region(r) {
1840*4882a593Smuzhiyun 		orig_start = r->base;
1841*4882a593Smuzhiyun 		orig_end = r->base + r->size;
1842*4882a593Smuzhiyun 		start = round_up(orig_start, align);
1843*4882a593Smuzhiyun 		end = round_down(orig_end, align);
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun 		if (start == orig_start && end == orig_end)
1846*4882a593Smuzhiyun 			continue;
1847*4882a593Smuzhiyun 
1848*4882a593Smuzhiyun 		if (start < end) {
1849*4882a593Smuzhiyun 			r->base = start;
1850*4882a593Smuzhiyun 			r->size = end - start;
1851*4882a593Smuzhiyun 		} else {
1852*4882a593Smuzhiyun 			memblock_remove_region(&memblock.memory,
1853*4882a593Smuzhiyun 					       r - memblock.memory.regions);
1854*4882a593Smuzhiyun 			r--;
1855*4882a593Smuzhiyun 		}
1856*4882a593Smuzhiyun 	}
1857*4882a593Smuzhiyun }
1858*4882a593Smuzhiyun 
memblock_set_current_limit(phys_addr_t limit)1859*4882a593Smuzhiyun void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1860*4882a593Smuzhiyun {
1861*4882a593Smuzhiyun 	memblock.current_limit = limit;
1862*4882a593Smuzhiyun }
1863*4882a593Smuzhiyun 
memblock_get_current_limit(void)1864*4882a593Smuzhiyun phys_addr_t __init_memblock memblock_get_current_limit(void)
1865*4882a593Smuzhiyun {
1866*4882a593Smuzhiyun 	return memblock.current_limit;
1867*4882a593Smuzhiyun }
1868*4882a593Smuzhiyun 
memblock_dump(struct memblock_type * type)1869*4882a593Smuzhiyun static void __init_memblock memblock_dump(struct memblock_type *type)
1870*4882a593Smuzhiyun {
1871*4882a593Smuzhiyun 	phys_addr_t base, end, size;
1872*4882a593Smuzhiyun 	enum memblock_flags flags;
1873*4882a593Smuzhiyun 	int idx;
1874*4882a593Smuzhiyun 	struct memblock_region *rgn;
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun 	pr_info(" %s.cnt  = 0x%lx\n", type->name, type->cnt);
1877*4882a593Smuzhiyun 
1878*4882a593Smuzhiyun 	for_each_memblock_type(idx, type, rgn) {
1879*4882a593Smuzhiyun 		char nid_buf[32] = "";
1880*4882a593Smuzhiyun 
1881*4882a593Smuzhiyun 		base = rgn->base;
1882*4882a593Smuzhiyun 		size = rgn->size;
1883*4882a593Smuzhiyun 		end = base + size - 1;
1884*4882a593Smuzhiyun 		flags = rgn->flags;
1885*4882a593Smuzhiyun #ifdef CONFIG_NEED_MULTIPLE_NODES
1886*4882a593Smuzhiyun 		if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1887*4882a593Smuzhiyun 			snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1888*4882a593Smuzhiyun 				 memblock_get_region_node(rgn));
1889*4882a593Smuzhiyun #endif
1890*4882a593Smuzhiyun 		pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
1891*4882a593Smuzhiyun 			type->name, idx, &base, &end, &size, nid_buf, flags);
1892*4882a593Smuzhiyun 	}
1893*4882a593Smuzhiyun }
1894*4882a593Smuzhiyun 
__memblock_dump_all(void)1895*4882a593Smuzhiyun static void __init_memblock __memblock_dump_all(void)
1896*4882a593Smuzhiyun {
1897*4882a593Smuzhiyun 	pr_info("MEMBLOCK configuration:\n");
1898*4882a593Smuzhiyun 	pr_info(" memory size = %pa reserved size = %pa\n",
1899*4882a593Smuzhiyun 		&memblock.memory.total_size,
1900*4882a593Smuzhiyun 		&memblock.reserved.total_size);
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun 	memblock_dump(&memblock.memory);
1903*4882a593Smuzhiyun 	memblock_dump(&memblock.reserved);
1904*4882a593Smuzhiyun #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
1905*4882a593Smuzhiyun 	memblock_dump(&physmem);
1906*4882a593Smuzhiyun #endif
1907*4882a593Smuzhiyun }
1908*4882a593Smuzhiyun 
memblock_dump_all(void)1909*4882a593Smuzhiyun void __init_memblock memblock_dump_all(void)
1910*4882a593Smuzhiyun {
1911*4882a593Smuzhiyun 	if (memblock_debug)
1912*4882a593Smuzhiyun 		__memblock_dump_all();
1913*4882a593Smuzhiyun }
1914*4882a593Smuzhiyun 
memblock_allow_resize(void)1915*4882a593Smuzhiyun void __init memblock_allow_resize(void)
1916*4882a593Smuzhiyun {
1917*4882a593Smuzhiyun 	memblock_can_resize = 1;
1918*4882a593Smuzhiyun }
1919*4882a593Smuzhiyun 
early_memblock(char * p)1920*4882a593Smuzhiyun static int __init early_memblock(char *p)
1921*4882a593Smuzhiyun {
1922*4882a593Smuzhiyun 	if (p && strstr(p, "debug"))
1923*4882a593Smuzhiyun 		memblock_debug = 1;
1924*4882a593Smuzhiyun 	return 0;
1925*4882a593Smuzhiyun }
1926*4882a593Smuzhiyun early_param("memblock", early_memblock);
1927*4882a593Smuzhiyun 
early_memblock_nomap(char * str)1928*4882a593Smuzhiyun static int __init early_memblock_nomap(char *str)
1929*4882a593Smuzhiyun {
1930*4882a593Smuzhiyun 	return kstrtobool(str, &memblock_nomap_remove);
1931*4882a593Smuzhiyun }
1932*4882a593Smuzhiyun early_param("android12_only.will_be_removed_soon.memblock_nomap_remove", early_memblock_nomap);
1933*4882a593Smuzhiyun 
memblock_is_nomap_remove(void)1934*4882a593Smuzhiyun bool __init memblock_is_nomap_remove(void)
1935*4882a593Smuzhiyun {
1936*4882a593Smuzhiyun 	return memblock_nomap_remove;
1937*4882a593Smuzhiyun }
1938*4882a593Smuzhiyun 
__free_pages_memory(unsigned long start,unsigned long end)1939*4882a593Smuzhiyun static void __init __free_pages_memory(unsigned long start, unsigned long end)
1940*4882a593Smuzhiyun {
1941*4882a593Smuzhiyun 	int order;
1942*4882a593Smuzhiyun 
1943*4882a593Smuzhiyun 	while (start < end) {
1944*4882a593Smuzhiyun 		order = min(MAX_ORDER - 1UL, __ffs(start));
1945*4882a593Smuzhiyun 
1946*4882a593Smuzhiyun 		while (start + (1UL << order) > end)
1947*4882a593Smuzhiyun 			order--;
1948*4882a593Smuzhiyun 
1949*4882a593Smuzhiyun 		memblock_free_pages(pfn_to_page(start), start, order);
1950*4882a593Smuzhiyun 
1951*4882a593Smuzhiyun 		start += (1UL << order);
1952*4882a593Smuzhiyun 	}
1953*4882a593Smuzhiyun }
1954*4882a593Smuzhiyun 
1955*4882a593Smuzhiyun #if defined(CONFIG_ROCKCHIP_THUNDER_BOOT) && defined(CONFIG_SMP)
defer_free_memblock(void * unused)1956*4882a593Smuzhiyun int __init defer_free_memblock(void *unused)
1957*4882a593Smuzhiyun {
1958*4882a593Smuzhiyun 	if (defer_start == 0)
1959*4882a593Smuzhiyun 		return 0;
1960*4882a593Smuzhiyun 
1961*4882a593Smuzhiyun 	pr_debug("start = %ld, end = %ld\n", defer_start, defer_end);
1962*4882a593Smuzhiyun 
1963*4882a593Smuzhiyun 	__free_pages_memory(defer_start, defer_end);
1964*4882a593Smuzhiyun 
1965*4882a593Smuzhiyun 	totalram_pages_add(defer_end - defer_start);
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun 	pr_info("%s: size %luM free %luM [%luM - %luM] total %luM\n", __func__,
1968*4882a593Smuzhiyun 		defer_free_block_size >> 20,
1969*4882a593Smuzhiyun 		(defer_end - defer_start) >> (20 - PAGE_SHIFT),
1970*4882a593Smuzhiyun 		defer_end >> (20 - PAGE_SHIFT),
1971*4882a593Smuzhiyun 		defer_start >> (20 - PAGE_SHIFT),
1972*4882a593Smuzhiyun 		totalram_pages() >> (20 - PAGE_SHIFT));
1973*4882a593Smuzhiyun 	return 0;
1974*4882a593Smuzhiyun }
1975*4882a593Smuzhiyun #endif
1976*4882a593Smuzhiyun 
__free_memory_core(phys_addr_t start,phys_addr_t end)1977*4882a593Smuzhiyun static unsigned long __init __free_memory_core(phys_addr_t start,
1978*4882a593Smuzhiyun 				 phys_addr_t end)
1979*4882a593Smuzhiyun {
1980*4882a593Smuzhiyun 	unsigned long start_pfn = PFN_UP(start);
1981*4882a593Smuzhiyun 	unsigned long end_pfn = min_t(unsigned long,
1982*4882a593Smuzhiyun 				      PFN_DOWN(end), max_low_pfn);
1983*4882a593Smuzhiyun 
1984*4882a593Smuzhiyun 	if (start_pfn >= end_pfn)
1985*4882a593Smuzhiyun 		return 0;
1986*4882a593Smuzhiyun 
1987*4882a593Smuzhiyun #if defined(CONFIG_ROCKCHIP_THUNDER_BOOT) && defined(CONFIG_SMP)
1988*4882a593Smuzhiyun 	if ((end - start) > defer_free_block_size) {
1989*4882a593Smuzhiyun 		defer_start = start_pfn;
1990*4882a593Smuzhiyun 		defer_end = end_pfn;
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun 		return 0;
1993*4882a593Smuzhiyun 	}
1994*4882a593Smuzhiyun #endif
1995*4882a593Smuzhiyun 
1996*4882a593Smuzhiyun 	__free_pages_memory(start_pfn, end_pfn);
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun 	return end_pfn - start_pfn;
1999*4882a593Smuzhiyun }
2000*4882a593Smuzhiyun 
free_low_memory_core_early(void)2001*4882a593Smuzhiyun static unsigned long __init free_low_memory_core_early(void)
2002*4882a593Smuzhiyun {
2003*4882a593Smuzhiyun 	unsigned long count = 0;
2004*4882a593Smuzhiyun 	phys_addr_t start, end;
2005*4882a593Smuzhiyun 	u64 i;
2006*4882a593Smuzhiyun 
2007*4882a593Smuzhiyun 	memblock_clear_hotplug(0, -1);
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 	for_each_reserved_mem_range(i, &start, &end)
2010*4882a593Smuzhiyun 		reserve_bootmem_region(start, end);
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun 	/*
2013*4882a593Smuzhiyun 	 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
2014*4882a593Smuzhiyun 	 *  because in some case like Node0 doesn't have RAM installed
2015*4882a593Smuzhiyun 	 *  low ram will be on Node1
2016*4882a593Smuzhiyun 	 */
2017*4882a593Smuzhiyun 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
2018*4882a593Smuzhiyun 				NULL)
2019*4882a593Smuzhiyun 		count += __free_memory_core(start, end);
2020*4882a593Smuzhiyun 
2021*4882a593Smuzhiyun 	return count;
2022*4882a593Smuzhiyun }
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun static int reset_managed_pages_done __initdata;
2025*4882a593Smuzhiyun 
reset_node_managed_pages(pg_data_t * pgdat)2026*4882a593Smuzhiyun void reset_node_managed_pages(pg_data_t *pgdat)
2027*4882a593Smuzhiyun {
2028*4882a593Smuzhiyun 	struct zone *z;
2029*4882a593Smuzhiyun 
2030*4882a593Smuzhiyun 	for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
2031*4882a593Smuzhiyun 		atomic_long_set(&z->managed_pages, 0);
2032*4882a593Smuzhiyun }
2033*4882a593Smuzhiyun 
reset_all_zones_managed_pages(void)2034*4882a593Smuzhiyun void __init reset_all_zones_managed_pages(void)
2035*4882a593Smuzhiyun {
2036*4882a593Smuzhiyun 	struct pglist_data *pgdat;
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 	if (reset_managed_pages_done)
2039*4882a593Smuzhiyun 		return;
2040*4882a593Smuzhiyun 
2041*4882a593Smuzhiyun 	for_each_online_pgdat(pgdat)
2042*4882a593Smuzhiyun 		reset_node_managed_pages(pgdat);
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun 	reset_managed_pages_done = 1;
2045*4882a593Smuzhiyun }
2046*4882a593Smuzhiyun 
2047*4882a593Smuzhiyun /**
2048*4882a593Smuzhiyun  * memblock_free_all - release free pages to the buddy allocator
2049*4882a593Smuzhiyun  *
2050*4882a593Smuzhiyun  * Return: the number of pages actually released.
2051*4882a593Smuzhiyun  */
memblock_free_all(void)2052*4882a593Smuzhiyun unsigned long __init memblock_free_all(void)
2053*4882a593Smuzhiyun {
2054*4882a593Smuzhiyun 	unsigned long pages;
2055*4882a593Smuzhiyun 
2056*4882a593Smuzhiyun 	reset_all_zones_managed_pages();
2057*4882a593Smuzhiyun 
2058*4882a593Smuzhiyun 	pages = free_low_memory_core_early();
2059*4882a593Smuzhiyun 	totalram_pages_add(pages);
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun 	return pages;
2062*4882a593Smuzhiyun }
2063*4882a593Smuzhiyun 
2064*4882a593Smuzhiyun #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
2065*4882a593Smuzhiyun 
memblock_debug_show(struct seq_file * m,void * private)2066*4882a593Smuzhiyun static int memblock_debug_show(struct seq_file *m, void *private)
2067*4882a593Smuzhiyun {
2068*4882a593Smuzhiyun 	struct memblock_type *type = m->private;
2069*4882a593Smuzhiyun 	struct memblock_region *reg;
2070*4882a593Smuzhiyun 	int i;
2071*4882a593Smuzhiyun 	phys_addr_t end;
2072*4882a593Smuzhiyun 
2073*4882a593Smuzhiyun 	for (i = 0; i < type->cnt; i++) {
2074*4882a593Smuzhiyun 		reg = &type->regions[i];
2075*4882a593Smuzhiyun 		end = reg->base + reg->size - 1;
2076*4882a593Smuzhiyun 
2077*4882a593Smuzhiyun 		seq_printf(m, "%4d: ", i);
2078*4882a593Smuzhiyun 		seq_printf(m, "%pa..%pa\n", &reg->base, &end);
2079*4882a593Smuzhiyun 	}
2080*4882a593Smuzhiyun 	return 0;
2081*4882a593Smuzhiyun }
2082*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(memblock_debug);
2083*4882a593Smuzhiyun 
memblock_init_debugfs(void)2084*4882a593Smuzhiyun static int __init memblock_init_debugfs(void)
2085*4882a593Smuzhiyun {
2086*4882a593Smuzhiyun 	struct dentry *root = debugfs_create_dir("memblock", NULL);
2087*4882a593Smuzhiyun 
2088*4882a593Smuzhiyun 	debugfs_create_file("memory", 0444, root,
2089*4882a593Smuzhiyun 			    &memblock.memory, &memblock_debug_fops);
2090*4882a593Smuzhiyun 	debugfs_create_file("reserved", 0444, root,
2091*4882a593Smuzhiyun 			    &memblock.reserved, &memblock_debug_fops);
2092*4882a593Smuzhiyun #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
2093*4882a593Smuzhiyun 	debugfs_create_file("physmem", 0444, root, &physmem,
2094*4882a593Smuzhiyun 			    &memblock_debug_fops);
2095*4882a593Smuzhiyun #endif
2096*4882a593Smuzhiyun 
2097*4882a593Smuzhiyun 	return 0;
2098*4882a593Smuzhiyun }
2099*4882a593Smuzhiyun __initcall(memblock_init_debugfs);
2100*4882a593Smuzhiyun 
2101*4882a593Smuzhiyun #endif /* CONFIG_DEBUG_FS */
2102