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