xref: /linux/include/linux/memblock.h (revision 948ef73f7ec39622ebd27bba4e94d78a983109f6)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _LINUX_MEMBLOCK_H
3 #define _LINUX_MEMBLOCK_H
4 
5 /*
6  * Logical memory blocks.
7  *
8  * Copyright (C) 2001 Peter Bergner, IBM Corp.
9  */
10 
11 #include <linux/init.h>
12 #include <linux/mm.h>
13 #include <asm/dma.h>
14 
15 extern unsigned long max_low_pfn;
16 extern unsigned long min_low_pfn;
17 
18 /*
19  * highest page
20  */
21 extern unsigned long max_pfn;
22 /*
23  * highest possible page
24  */
25 extern unsigned long long max_possible_pfn;
26 
27 /**
28  * enum memblock_flags - definition of memory region attributes
29  * @MEMBLOCK_NONE: no special request
30  * @MEMBLOCK_HOTPLUG: memory region indicated in the firmware-provided memory
31  * map during early boot as hot(un)pluggable system RAM (e.g., memory range
32  * that might get hotunplugged later). With "movable_node" set on the kernel
33  * commandline, try keeping this memory region hotunpluggable. Does not apply
34  * to memblocks added ("hotplugged") after early boot.
35  * @MEMBLOCK_MIRROR: mirrored region
36  * @MEMBLOCK_NOMAP: don't add to kernel direct mapping and treat as
37  * reserved in the memory map; refer to memblock_mark_nomap() description
38  * for further details
39  * @MEMBLOCK_DRIVER_MANAGED: memory region that is always detected and added
40  * via a driver, and never indicated in the firmware-provided memory map as
41  * system RAM. This corresponds to IORESOURCE_SYSRAM_DRIVER_MANAGED in the
42  * kernel resource tree.
43  * @MEMBLOCK_RSRV_NOINIT: reserved memory region for which struct pages are not
44  * fully initialized. Users of this flag are responsible to properly initialize
45  * struct pages of this region
46  * @MEMBLOCK_RSRV_KERN: memory region that is reserved for kernel use,
47  * either explictitly with memblock_reserve_kern() or via memblock
48  * allocation APIs. All memblock allocations set this flag.
49  * @MEMBLOCK_KHO_SCRATCH: memory region that kexec can pass to the next
50  * kernel in handover mode. During early boot, we do not know about all
51  * memory reservations yet, so we get scratch memory from the previous
52  * kernel that we know is good to use. It is the only memory that
53  * allocations may happen from in this phase.
54  */
55 enum memblock_flags {
56 	MEMBLOCK_NONE		= 0x0,	/* No special request */
57 	MEMBLOCK_HOTPLUG	= 0x1,	/* hotpluggable region */
58 	MEMBLOCK_MIRROR		= 0x2,	/* mirrored region */
59 	MEMBLOCK_NOMAP		= 0x4,	/* don't add to kernel direct mapping */
60 	MEMBLOCK_DRIVER_MANAGED = 0x8,	/* always detected via a driver */
61 	MEMBLOCK_RSRV_NOINIT	= 0x10,	/* don't initialize struct pages */
62 	MEMBLOCK_RSRV_KERN	= 0x20,	/* memory reserved for kernel use */
63 	MEMBLOCK_KHO_SCRATCH	= 0x40,	/* scratch memory for kexec handover */
64 };
65 
66 /**
67  * struct memblock_region - represents a memory region
68  * @base: base address of the region
69  * @size: size of the region
70  * @flags: memory region attributes
71  * @nid: NUMA node id
72  */
73 struct memblock_region {
74 	phys_addr_t base;
75 	phys_addr_t size;
76 	enum memblock_flags flags;
77 #ifdef CONFIG_NUMA
78 	int nid;
79 #endif
80 };
81 
82 /**
83  * struct memblock_type - collection of memory regions of certain type
84  * @cnt: number of regions
85  * @max: size of the allocated array
86  * @total_size: size of all regions
87  * @regions: array of regions
88  * @name: the memory type symbolic name
89  */
90 struct memblock_type {
91 	unsigned long cnt;
92 	unsigned long max;
93 	phys_addr_t total_size;
94 	struct memblock_region *regions;
95 	char *name;
96 };
97 
98 /**
99  * struct memblock - memblock allocator metadata
100  * @bottom_up: is bottom up direction?
101  * @current_limit: physical address of the current allocation limit
102  * @memory: usable memory regions
103  * @reserved: reserved memory regions
104  */
105 struct memblock {
106 	bool bottom_up;  /* is bottom up direction? */
107 	phys_addr_t current_limit;
108 	struct memblock_type memory;
109 	struct memblock_type reserved;
110 };
111 
112 extern struct memblock memblock;
113 
114 #ifndef CONFIG_ARCH_KEEP_MEMBLOCK
115 #define __init_memblock __meminit
116 #define __initdata_memblock __meminitdata
117 void memblock_discard(void);
118 #else
119 #define __init_memblock
120 #define __initdata_memblock
memblock_discard(void)121 static inline void memblock_discard(void) {}
122 #endif
123 
124 void memblock_allow_resize(void);
125 int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid,
126 		      enum memblock_flags flags);
127 int memblock_add(phys_addr_t base, phys_addr_t size);
128 int memblock_remove(phys_addr_t base, phys_addr_t size);
129 int memblock_phys_free(phys_addr_t base, phys_addr_t size);
130 int __memblock_reserve(phys_addr_t base, phys_addr_t size, int nid,
131 		       enum memblock_flags flags);
132 
memblock_reserve(phys_addr_t base,phys_addr_t size)133 static __always_inline int memblock_reserve(phys_addr_t base, phys_addr_t size)
134 {
135 	return __memblock_reserve(base, size, NUMA_NO_NODE, 0);
136 }
137 
memblock_reserve_kern(phys_addr_t base,phys_addr_t size)138 static __always_inline int memblock_reserve_kern(phys_addr_t base, phys_addr_t size)
139 {
140 	return __memblock_reserve(base, size, NUMA_NO_NODE, MEMBLOCK_RSRV_KERN);
141 }
142 
143 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
144 int memblock_physmem_add(phys_addr_t base, phys_addr_t size);
145 #endif
146 void memblock_trim_memory(phys_addr_t align);
147 unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
148 				     phys_addr_t base2, phys_addr_t size2);
149 bool memblock_overlaps_region(struct memblock_type *type,
150 			      phys_addr_t base, phys_addr_t size);
151 bool memblock_validate_numa_coverage(unsigned long threshold_bytes);
152 int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
153 int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
154 int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
155 int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
156 int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
157 int memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size);
158 int memblock_reserved_mark_kern(phys_addr_t base, phys_addr_t size);
159 int memblock_mark_kho_scratch(phys_addr_t base, phys_addr_t size);
160 int memblock_clear_kho_scratch(phys_addr_t base, phys_addr_t size);
161 
162 void memblock_free(void *ptr, size_t size);
163 void reset_all_zones_managed_pages(void);
164 
165 /* Low level functions */
166 void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
167 		      struct memblock_type *type_a,
168 		      struct memblock_type *type_b, phys_addr_t *out_start,
169 		      phys_addr_t *out_end, int *out_nid);
170 
171 void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags,
172 			  struct memblock_type *type_a,
173 			  struct memblock_type *type_b, phys_addr_t *out_start,
174 			  phys_addr_t *out_end, int *out_nid);
175 
176 void memblock_free_late(phys_addr_t base, phys_addr_t size);
177 
178 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
__next_physmem_range(u64 * idx,struct memblock_type * type,phys_addr_t * out_start,phys_addr_t * out_end)179 static inline void __next_physmem_range(u64 *idx, struct memblock_type *type,
180 					phys_addr_t *out_start,
181 					phys_addr_t *out_end)
182 {
183 	extern struct memblock_type physmem;
184 
185 	__next_mem_range(idx, NUMA_NO_NODE, MEMBLOCK_NONE, &physmem, type,
186 			 out_start, out_end, NULL);
187 }
188 
189 /**
190  * for_each_physmem_range - iterate through physmem areas not included in type.
191  * @i: u64 used as loop variable
192  * @type: ptr to memblock_type which excludes from the iteration, can be %NULL
193  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
194  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
195  */
196 #define for_each_physmem_range(i, type, p_start, p_end)			\
197 	for (i = 0, __next_physmem_range(&i, type, p_start, p_end);	\
198 	     i != (u64)ULLONG_MAX;					\
199 	     __next_physmem_range(&i, type, p_start, p_end))
200 #endif /* CONFIG_HAVE_MEMBLOCK_PHYS_MAP */
201 
202 /**
203  * __for_each_mem_range - iterate through memblock areas from type_a and not
204  * included in type_b. Or just type_a if type_b is NULL.
205  * @i: u64 used as loop variable
206  * @type_a: ptr to memblock_type to iterate
207  * @type_b: ptr to memblock_type which excludes from the iteration
208  * @nid: node selector, %NUMA_NO_NODE for all nodes
209  * @flags: pick from blocks based on memory attributes
210  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
211  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
212  * @p_nid: ptr to int for nid of the range, can be %NULL
213  */
214 #define __for_each_mem_range(i, type_a, type_b, nid, flags,		\
215 			   p_start, p_end, p_nid)			\
216 	for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b,	\
217 				     p_start, p_end, p_nid);		\
218 	     i != (u64)ULLONG_MAX;					\
219 	     __next_mem_range(&i, nid, flags, type_a, type_b,		\
220 			      p_start, p_end, p_nid))
221 
222 /**
223  * __for_each_mem_range_rev - reverse iterate through memblock areas from
224  * type_a and not included in type_b. Or just type_a if type_b is NULL.
225  * @i: u64 used as loop variable
226  * @type_a: ptr to memblock_type to iterate
227  * @type_b: ptr to memblock_type which excludes from the iteration
228  * @nid: node selector, %NUMA_NO_NODE for all nodes
229  * @flags: pick from blocks based on memory attributes
230  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
231  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
232  * @p_nid: ptr to int for nid of the range, can be %NULL
233  */
234 #define __for_each_mem_range_rev(i, type_a, type_b, nid, flags,		\
235 				 p_start, p_end, p_nid)			\
236 	for (i = (u64)ULLONG_MAX,					\
237 		     __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
238 					  p_start, p_end, p_nid);	\
239 	     i != (u64)ULLONG_MAX;					\
240 	     __next_mem_range_rev(&i, nid, flags, type_a, type_b,	\
241 				  p_start, p_end, p_nid))
242 
243 /**
244  * for_each_mem_range - iterate through memory areas.
245  * @i: u64 used as loop variable
246  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
247  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
248  */
249 #define for_each_mem_range(i, p_start, p_end) \
250 	__for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE,	\
251 			     MEMBLOCK_HOTPLUG | MEMBLOCK_DRIVER_MANAGED, \
252 			     p_start, p_end, NULL)
253 
254 /**
255  * for_each_mem_range_rev - reverse iterate through memblock areas from
256  * type_a and not included in type_b. Or just type_a if type_b is NULL.
257  * @i: u64 used as loop variable
258  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
259  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
260  */
261 #define for_each_mem_range_rev(i, p_start, p_end)			\
262 	__for_each_mem_range_rev(i, &memblock.memory, NULL, NUMA_NO_NODE, \
263 				 MEMBLOCK_HOTPLUG | MEMBLOCK_DRIVER_MANAGED,\
264 				 p_start, p_end, NULL)
265 
266 /**
267  * for_each_reserved_mem_range - iterate over all reserved memblock areas
268  * @i: u64 used as loop variable
269  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
270  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
271  *
272  * Walks over reserved areas of memblock. Available as soon as memblock
273  * is initialized.
274  */
275 #define for_each_reserved_mem_range(i, p_start, p_end)			\
276 	__for_each_mem_range(i, &memblock.reserved, NULL, NUMA_NO_NODE,	\
277 			     MEMBLOCK_NONE, p_start, p_end, NULL)
278 
memblock_is_hotpluggable(struct memblock_region * m)279 static inline bool memblock_is_hotpluggable(struct memblock_region *m)
280 {
281 	return m->flags & MEMBLOCK_HOTPLUG;
282 }
283 
memblock_is_mirror(struct memblock_region * m)284 static inline bool memblock_is_mirror(struct memblock_region *m)
285 {
286 	return m->flags & MEMBLOCK_MIRROR;
287 }
288 
memblock_is_nomap(struct memblock_region * m)289 static inline bool memblock_is_nomap(struct memblock_region *m)
290 {
291 	return m->flags & MEMBLOCK_NOMAP;
292 }
293 
memblock_is_reserved_noinit(struct memblock_region * m)294 static inline bool memblock_is_reserved_noinit(struct memblock_region *m)
295 {
296 	return m->flags & MEMBLOCK_RSRV_NOINIT;
297 }
298 
memblock_is_driver_managed(struct memblock_region * m)299 static inline bool memblock_is_driver_managed(struct memblock_region *m)
300 {
301 	return m->flags & MEMBLOCK_DRIVER_MANAGED;
302 }
303 
memblock_is_kho_scratch(struct memblock_region * m)304 static inline bool memblock_is_kho_scratch(struct memblock_region *m)
305 {
306 	return m->flags & MEMBLOCK_KHO_SCRATCH;
307 }
308 
309 int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
310 			    unsigned long  *end_pfn);
311 void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
312 			  unsigned long *out_end_pfn, int *out_nid);
313 
314 /**
315  * for_each_mem_pfn_range - early memory pfn range iterator
316  * @i: an integer used as loop variable
317  * @nid: node selector, %MAX_NUMNODES for all nodes
318  * @p_start: ptr to ulong for start pfn of the range, can be %NULL
319  * @p_end: ptr to ulong for end pfn of the range, can be %NULL
320  * @p_nid: ptr to int for nid of the range, can be %NULL
321  *
322  * Walks over configured memory ranges.
323  */
324 #define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid)		\
325 	for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
326 	     i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
327 
328 
329 /**
330  * for_each_free_mem_range - iterate through free memblock areas
331  * @i: u64 used as loop variable
332  * @nid: node selector, %NUMA_NO_NODE for all nodes
333  * @flags: pick from blocks based on memory attributes
334  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
335  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
336  * @p_nid: ptr to int for nid of the range, can be %NULL
337  *
338  * Walks over free (memory && !reserved) areas of memblock.  Available as
339  * soon as memblock is initialized.
340  */
341 #define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid)	\
342 	__for_each_mem_range(i, &memblock.memory, &memblock.reserved,	\
343 			     nid, flags, p_start, p_end, p_nid)
344 
345 /**
346  * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
347  * @i: u64 used as loop variable
348  * @nid: node selector, %NUMA_NO_NODE for all nodes
349  * @flags: pick from blocks based on memory attributes
350  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
351  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
352  * @p_nid: ptr to int for nid of the range, can be %NULL
353  *
354  * Walks over free (memory && !reserved) areas of memblock in reverse
355  * order.  Available as soon as memblock is initialized.
356  */
357 #define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end,	\
358 					p_nid)				\
359 	__for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
360 				 nid, flags, p_start, p_end, p_nid)
361 
362 int memblock_set_node(phys_addr_t base, phys_addr_t size,
363 		      struct memblock_type *type, int nid);
364 
365 #ifdef CONFIG_NUMA
memblock_set_region_node(struct memblock_region * r,int nid)366 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
367 {
368 	r->nid = nid;
369 }
370 
memblock_get_region_node(const struct memblock_region * r)371 static inline int memblock_get_region_node(const struct memblock_region *r)
372 {
373 	return r->nid;
374 }
375 #else
memblock_set_region_node(struct memblock_region * r,int nid)376 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
377 {
378 }
379 
memblock_get_region_node(const struct memblock_region * r)380 static inline int memblock_get_region_node(const struct memblock_region *r)
381 {
382 	return 0;
383 }
384 #endif /* CONFIG_NUMA */
385 
386 /* Flags for memblock allocation APIs */
387 #define MEMBLOCK_ALLOC_ANYWHERE	(~(phys_addr_t)0)
388 #define MEMBLOCK_ALLOC_ACCESSIBLE	0
389 /*
390  *  MEMBLOCK_ALLOC_NOLEAKTRACE avoids kmemleak tracing. It implies
391  *  MEMBLOCK_ALLOC_ACCESSIBLE
392  */
393 #define MEMBLOCK_ALLOC_NOLEAKTRACE	1
394 
395 /* We are using top down, so it is safe to use 0 here */
396 #define MEMBLOCK_LOW_LIMIT 0
397 
398 #ifndef ARCH_LOW_ADDRESS_LIMIT
399 #define ARCH_LOW_ADDRESS_LIMIT  0xffffffffUL
400 #endif
401 
402 phys_addr_t memblock_phys_alloc_range(phys_addr_t size, phys_addr_t align,
403 				      phys_addr_t start, phys_addr_t end);
404 phys_addr_t memblock_alloc_range_nid(phys_addr_t size,
405 				      phys_addr_t align, phys_addr_t start,
406 				      phys_addr_t end, int nid, bool exact_nid);
407 phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
408 
memblock_phys_alloc(phys_addr_t size,phys_addr_t align)409 static __always_inline phys_addr_t memblock_phys_alloc(phys_addr_t size,
410 						       phys_addr_t align)
411 {
412 	return memblock_phys_alloc_range(size, align, 0,
413 					 MEMBLOCK_ALLOC_ACCESSIBLE);
414 }
415 
416 void *memblock_alloc_exact_nid_raw(phys_addr_t size, phys_addr_t align,
417 				 phys_addr_t min_addr, phys_addr_t max_addr,
418 				 int nid);
419 void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align,
420 				 phys_addr_t min_addr, phys_addr_t max_addr,
421 				 int nid);
422 void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align,
423 			     phys_addr_t min_addr, phys_addr_t max_addr,
424 			     int nid);
425 
memblock_alloc(phys_addr_t size,phys_addr_t align)426 static __always_inline void *memblock_alloc(phys_addr_t size, phys_addr_t align)
427 {
428 	return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
429 				      MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
430 }
431 
432 void *__memblock_alloc_or_panic(phys_addr_t size, phys_addr_t align,
433 				const char *func);
434 
435 #define memblock_alloc_or_panic(size, align)    \
436 	 __memblock_alloc_or_panic(size, align, __func__)
437 
memblock_alloc_raw(phys_addr_t size,phys_addr_t align)438 static inline void *memblock_alloc_raw(phys_addr_t size,
439 					       phys_addr_t align)
440 {
441 	return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
442 					  MEMBLOCK_ALLOC_ACCESSIBLE,
443 					  NUMA_NO_NODE);
444 }
445 
memblock_alloc_from(phys_addr_t size,phys_addr_t align,phys_addr_t min_addr)446 static __always_inline void *memblock_alloc_from(phys_addr_t size,
447 						phys_addr_t align,
448 						phys_addr_t min_addr)
449 {
450 	return memblock_alloc_try_nid(size, align, min_addr,
451 				      MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
452 }
453 
memblock_alloc_low(phys_addr_t size,phys_addr_t align)454 static inline void *memblock_alloc_low(phys_addr_t size,
455 					       phys_addr_t align)
456 {
457 	return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
458 				      ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE);
459 }
460 
memblock_alloc_node(phys_addr_t size,phys_addr_t align,int nid)461 static inline void *memblock_alloc_node(phys_addr_t size,
462 						phys_addr_t align, int nid)
463 {
464 	return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
465 				      MEMBLOCK_ALLOC_ACCESSIBLE, nid);
466 }
467 
468 /*
469  * Set the allocation direction to bottom-up or top-down.
470  */
memblock_set_bottom_up(bool enable)471 static inline __init_memblock void memblock_set_bottom_up(bool enable)
472 {
473 	memblock.bottom_up = enable;
474 }
475 
476 /*
477  * Check if the allocation direction is bottom-up or not.
478  * if this is true, that said, memblock will allocate memory
479  * in bottom-up direction.
480  */
memblock_bottom_up(void)481 static inline __init_memblock bool memblock_bottom_up(void)
482 {
483 	return memblock.bottom_up;
484 }
485 
486 phys_addr_t memblock_phys_mem_size(void);
487 phys_addr_t memblock_reserved_size(void);
488 phys_addr_t memblock_reserved_kern_size(phys_addr_t limit, int nid);
489 unsigned long memblock_estimated_nr_free_pages(void);
490 phys_addr_t memblock_start_of_DRAM(void);
491 phys_addr_t memblock_end_of_DRAM(void);
492 void memblock_enforce_memory_limit(phys_addr_t memory_limit);
493 void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
494 void memblock_mem_limit_remove_map(phys_addr_t limit);
495 bool memblock_is_memory(phys_addr_t addr);
496 bool memblock_is_map_memory(phys_addr_t addr);
497 bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
498 bool memblock_is_reserved(phys_addr_t addr);
499 bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
500 
501 void memblock_dump_all(void);
502 
503 /**
504  * memblock_set_current_limit - Set the current allocation limit to allow
505  *                         limiting allocations to what is currently
506  *                         accessible during boot
507  * @limit: New limit value (physical address)
508  */
509 void memblock_set_current_limit(phys_addr_t limit);
510 
511 
512 phys_addr_t memblock_get_current_limit(void);
513 
514 /*
515  * pfn conversion functions
516  *
517  * While the memory MEMBLOCKs should always be page aligned, the reserved
518  * MEMBLOCKs may not be. This accessor attempt to provide a very clear
519  * idea of what they return for such non aligned MEMBLOCKs.
520  */
521 
522 /**
523  * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
524  * @reg: memblock_region structure
525  *
526  * Return: the lowest pfn intersecting with the memory region
527  */
memblock_region_memory_base_pfn(const struct memblock_region * reg)528 static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
529 {
530 	return PFN_UP(reg->base);
531 }
532 
533 /**
534  * memblock_region_memory_end_pfn - get the end pfn of the memory region
535  * @reg: memblock_region structure
536  *
537  * Return: the end_pfn of the reserved region
538  */
memblock_region_memory_end_pfn(const struct memblock_region * reg)539 static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
540 {
541 	return PFN_DOWN(reg->base + reg->size);
542 }
543 
544 /**
545  * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
546  * @reg: memblock_region structure
547  *
548  * Return: the lowest pfn intersecting with the reserved region
549  */
memblock_region_reserved_base_pfn(const struct memblock_region * reg)550 static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
551 {
552 	return PFN_DOWN(reg->base);
553 }
554 
555 /**
556  * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
557  * @reg: memblock_region structure
558  *
559  * Return: the end_pfn of the reserved region
560  */
memblock_region_reserved_end_pfn(const struct memblock_region * reg)561 static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
562 {
563 	return PFN_UP(reg->base + reg->size);
564 }
565 
566 /**
567  * for_each_mem_region - iterate over memory regions
568  * @region: loop variable
569  */
570 #define for_each_mem_region(region)					\
571 	for (region = memblock.memory.regions;				\
572 	     region < (memblock.memory.regions + memblock.memory.cnt);	\
573 	     region++)
574 
575 /**
576  * for_each_reserved_mem_region - itereate over reserved memory regions
577  * @region: loop variable
578  */
579 #define for_each_reserved_mem_region(region)				\
580 	for (region = memblock.reserved.regions;			\
581 	     region < (memblock.reserved.regions + memblock.reserved.cnt); \
582 	     region++)
583 
584 extern void *alloc_large_system_hash(const char *tablename,
585 				     unsigned long bucketsize,
586 				     unsigned long numentries,
587 				     int scale,
588 				     int flags,
589 				     unsigned int *_hash_shift,
590 				     unsigned int *_hash_mask,
591 				     unsigned long low_limit,
592 				     unsigned long high_limit);
593 
594 #define HASH_EARLY	0x00000001	/* Allocating during early boot? */
595 #define HASH_ZERO	0x00000002	/* Zero allocated hash table */
596 
597 /* Only NUMA needs hash distribution. 64bit NUMA architectures have
598  * sufficient vmalloc space.
599  */
600 #ifdef CONFIG_NUMA
601 #define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
602 extern bool hashdist;		/* Distribute hashes across NUMA nodes? */
603 #else
604 #define hashdist (false)
605 #endif
606 
607 #ifdef CONFIG_MEMTEST
608 void early_memtest(phys_addr_t start, phys_addr_t end);
609 void memtest_report_meminfo(struct seq_file *m);
610 #else
early_memtest(phys_addr_t start,phys_addr_t end)611 static inline void early_memtest(phys_addr_t start, phys_addr_t end) { }
memtest_report_meminfo(struct seq_file * m)612 static inline void memtest_report_meminfo(struct seq_file *m) { }
613 #endif
614 
615 #ifdef CONFIG_MEMBLOCK_KHO_SCRATCH
616 void memblock_set_kho_scratch_only(void);
617 void memblock_clear_kho_scratch_only(void);
618 void memmap_init_kho_scratch_pages(void);
619 #else
memblock_set_kho_scratch_only(void)620 static inline void memblock_set_kho_scratch_only(void) { }
memblock_clear_kho_scratch_only(void)621 static inline void memblock_clear_kho_scratch_only(void) { }
memmap_init_kho_scratch_pages(void)622 static inline void memmap_init_kho_scratch_pages(void) {}
623 #endif
624 
625 #endif /* _LINUX_MEMBLOCK_H */
626