1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _LINUX_MEMBLOCK_H
3 #define _LINUX_MEMBLOCK_H
7 * Logical memory blocks.
9 * Copyright (C) 2001 Peter Bergner, IBM Corp.
12 #include <linux/init.h>
16 extern unsigned long max_low_pfn;
17 extern unsigned long min_low_pfn;
22 extern unsigned long max_pfn;
24 * highest possible page
26 extern unsigned long long max_possible_pfn;
29 * enum memblock_flags - definition of memory region attributes
30 * @MEMBLOCK_NONE: no special request
31 * @MEMBLOCK_HOTPLUG: hotpluggable region
32 * @MEMBLOCK_MIRROR: mirrored region
33 * @MEMBLOCK_NOMAP: don't add to kernel direct mapping
36 MEMBLOCK_NONE = 0x0, /* No special request */
37 MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */
38 MEMBLOCK_MIRROR = 0x2, /* mirrored region */
39 MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */
43 * struct memblock_region - represents a memory region
44 * @base: base address of the region
45 * @size: size of the region
46 * @flags: memory region attributes
49 struct memblock_region {
52 enum memblock_flags flags;
53 #ifdef CONFIG_NEED_MULTIPLE_NODES
59 * struct memblock_type - collection of memory regions of certain type
60 * @cnt: number of regions
61 * @max: size of the allocated array
62 * @total_size: size of all regions
63 * @regions: array of regions
64 * @name: the memory type symbolic name
66 struct memblock_type {
69 phys_addr_t total_size;
70 struct memblock_region *regions;
75 * struct memblock - memblock allocator metadata
76 * @bottom_up: is bottom up direction?
77 * @current_limit: physical address of the current allocation limit
78 * @memory: usable memory regions
79 * @reserved: reserved memory regions
80 * @physmem: all physical memory
83 bool bottom_up; /* is bottom up direction? */
84 phys_addr_t current_limit;
85 struct memblock_type memory;
86 struct memblock_type reserved;
87 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
88 struct memblock_type physmem;
92 extern struct memblock memblock;
93 extern int memblock_debug;
95 #ifndef CONFIG_ARCH_KEEP_MEMBLOCK
96 #define __init_memblock __meminit
97 #define __initdata_memblock __meminitdata
98 void memblock_discard(void);
100 #define __init_memblock
101 #define __initdata_memblock
102 static inline void memblock_discard(void) {}
105 #define memblock_dbg(fmt, ...) \
106 if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
108 phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
109 phys_addr_t size, phys_addr_t align);
110 void memblock_allow_resize(void);
111 int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
112 int memblock_add(phys_addr_t base, phys_addr_t size);
113 int memblock_remove(phys_addr_t base, phys_addr_t size);
114 int memblock_free(phys_addr_t base, phys_addr_t size);
115 int memblock_reserve(phys_addr_t base, phys_addr_t size);
116 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
117 int memblock_physmem_add(phys_addr_t base, phys_addr_t size);
119 void memblock_trim_memory(phys_addr_t align);
120 bool memblock_overlaps_region(struct memblock_type *type,
121 phys_addr_t base, phys_addr_t size);
122 int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
123 int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
124 int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
125 int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
126 int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
128 unsigned long memblock_free_all(void);
129 void reset_node_managed_pages(pg_data_t *pgdat);
130 void reset_all_zones_managed_pages(void);
132 /* Low level functions */
133 void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
134 struct memblock_type *type_a,
135 struct memblock_type *type_b, phys_addr_t *out_start,
136 phys_addr_t *out_end, int *out_nid);
138 void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags,
139 struct memblock_type *type_a,
140 struct memblock_type *type_b, phys_addr_t *out_start,
141 phys_addr_t *out_end, int *out_nid);
143 void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start,
144 phys_addr_t *out_end);
146 void __memblock_free_late(phys_addr_t base, phys_addr_t size);
149 * for_each_mem_range - iterate through memblock areas from type_a and not
150 * included in type_b. Or just type_a if type_b is NULL.
151 * @i: u64 used as loop variable
152 * @type_a: ptr to memblock_type to iterate
153 * @type_b: ptr to memblock_type which excludes from the iteration
154 * @nid: node selector, %NUMA_NO_NODE for all nodes
155 * @flags: pick from blocks based on memory attributes
156 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
157 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
158 * @p_nid: ptr to int for nid of the range, can be %NULL
160 #define for_each_mem_range(i, type_a, type_b, nid, flags, \
161 p_start, p_end, p_nid) \
162 for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \
163 p_start, p_end, p_nid); \
164 i != (u64)ULLONG_MAX; \
165 __next_mem_range(&i, nid, flags, type_a, type_b, \
166 p_start, p_end, p_nid))
169 * for_each_mem_range_rev - reverse iterate through memblock areas from
170 * type_a and not included in type_b. Or just type_a if type_b is NULL.
171 * @i: u64 used as loop variable
172 * @type_a: ptr to memblock_type to iterate
173 * @type_b: ptr to memblock_type which excludes from the iteration
174 * @nid: node selector, %NUMA_NO_NODE for all nodes
175 * @flags: pick from blocks based on memory attributes
176 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
177 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
178 * @p_nid: ptr to int for nid of the range, can be %NULL
180 #define for_each_mem_range_rev(i, type_a, type_b, nid, flags, \
181 p_start, p_end, p_nid) \
182 for (i = (u64)ULLONG_MAX, \
183 __next_mem_range_rev(&i, nid, flags, type_a, type_b,\
184 p_start, p_end, p_nid); \
185 i != (u64)ULLONG_MAX; \
186 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
187 p_start, p_end, p_nid))
190 * for_each_reserved_mem_region - iterate over all reserved memblock areas
191 * @i: u64 used as loop variable
192 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
193 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
195 * Walks over reserved areas of memblock. Available as soon as memblock
198 #define for_each_reserved_mem_region(i, p_start, p_end) \
199 for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end); \
200 i != (u64)ULLONG_MAX; \
201 __next_reserved_mem_region(&i, p_start, p_end))
203 static inline bool memblock_is_hotpluggable(struct memblock_region *m)
205 return m->flags & MEMBLOCK_HOTPLUG;
208 static inline bool memblock_is_mirror(struct memblock_region *m)
210 return m->flags & MEMBLOCK_MIRROR;
213 static inline bool memblock_is_nomap(struct memblock_region *m)
215 return m->flags & MEMBLOCK_NOMAP;
218 int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
219 unsigned long *end_pfn);
220 void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
221 unsigned long *out_end_pfn, int *out_nid);
224 * for_each_mem_pfn_range - early memory pfn range iterator
225 * @i: an integer used as loop variable
226 * @nid: node selector, %MAX_NUMNODES for all nodes
227 * @p_start: ptr to ulong for start pfn of the range, can be %NULL
228 * @p_end: ptr to ulong for end pfn of the range, can be %NULL
229 * @p_nid: ptr to int for nid of the range, can be %NULL
231 * Walks over configured memory ranges.
233 #define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
234 for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
235 i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
237 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
238 void __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
239 unsigned long *out_spfn,
240 unsigned long *out_epfn);
242 * for_each_free_mem_range_in_zone - iterate through zone specific free
244 * @i: u64 used as loop variable
245 * @zone: zone in which all of the memory blocks reside
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
249 * Walks over free (memory && !reserved) areas of memblock in a specific
250 * zone. Available once memblock and an empty zone is initialized. The main
251 * assumption is that the zone start, end, and pgdat have been associated.
252 * This way we can use the zone to determine NUMA node, and if a given part
253 * of the memblock is valid for the zone.
255 #define for_each_free_mem_pfn_range_in_zone(i, zone, p_start, p_end) \
257 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end); \
259 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
262 * for_each_free_mem_range_in_zone_from - iterate through zone specific
263 * free memblock areas from a given point
264 * @i: u64 used as loop variable
265 * @zone: zone in which all of the memory blocks reside
266 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
267 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
269 * Walks over free (memory && !reserved) areas of memblock in a specific
270 * zone, continuing from current position. Available as soon as memblock is
273 #define for_each_free_mem_pfn_range_in_zone_from(i, zone, p_start, p_end) \
274 for (; i != U64_MAX; \
275 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
277 int __init deferred_page_init_max_threads(const struct cpumask *node_cpumask);
279 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
282 * for_each_free_mem_range - iterate through free memblock areas
283 * @i: u64 used as loop variable
284 * @nid: node selector, %NUMA_NO_NODE for all nodes
285 * @flags: pick from blocks based on memory attributes
286 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
287 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
288 * @p_nid: ptr to int for nid of the range, can be %NULL
290 * Walks over free (memory && !reserved) areas of memblock. Available as
291 * soon as memblock is initialized.
293 #define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \
294 for_each_mem_range(i, &memblock.memory, &memblock.reserved, \
295 nid, flags, p_start, p_end, p_nid)
298 * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
299 * @i: u64 used as loop variable
300 * @nid: node selector, %NUMA_NO_NODE for all nodes
301 * @flags: pick from blocks based on memory attributes
302 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
303 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
304 * @p_nid: ptr to int for nid of the range, can be %NULL
306 * Walks over free (memory && !reserved) areas of memblock in reverse
307 * order. Available as soon as memblock is initialized.
309 #define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
311 for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
312 nid, flags, p_start, p_end, p_nid)
314 int memblock_set_node(phys_addr_t base, phys_addr_t size,
315 struct memblock_type *type, int nid);
317 #ifdef CONFIG_NEED_MULTIPLE_NODES
318 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
323 static inline int memblock_get_region_node(const struct memblock_region *r)
328 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
332 static inline int memblock_get_region_node(const struct memblock_region *r)
336 #endif /* CONFIG_NEED_MULTIPLE_NODES */
338 /* Flags for memblock allocation APIs */
339 #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
340 #define MEMBLOCK_ALLOC_ACCESSIBLE 0
341 #define MEMBLOCK_ALLOC_KASAN 1
343 /* We are using top down, so it is safe to use 0 here */
344 #define MEMBLOCK_LOW_LIMIT 0
346 #ifndef ARCH_LOW_ADDRESS_LIMIT
347 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
350 phys_addr_t memblock_phys_alloc_range(phys_addr_t size, phys_addr_t align,
351 phys_addr_t start, phys_addr_t end);
352 phys_addr_t memblock_alloc_range_nid(phys_addr_t size,
353 phys_addr_t align, phys_addr_t start,
354 phys_addr_t end, int nid, bool exact_nid);
355 phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
357 static inline phys_addr_t memblock_phys_alloc(phys_addr_t size,
360 return memblock_phys_alloc_range(size, align, 0,
361 MEMBLOCK_ALLOC_ACCESSIBLE);
364 void *memblock_alloc_exact_nid_raw(phys_addr_t size, phys_addr_t align,
365 phys_addr_t min_addr, phys_addr_t max_addr,
367 void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align,
368 phys_addr_t min_addr, phys_addr_t max_addr,
370 void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align,
371 phys_addr_t min_addr, phys_addr_t max_addr,
374 static inline void * __init memblock_alloc(phys_addr_t size, phys_addr_t align)
376 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
377 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
380 static inline void * __init memblock_alloc_raw(phys_addr_t size,
383 return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
384 MEMBLOCK_ALLOC_ACCESSIBLE,
388 static inline void * __init memblock_alloc_from(phys_addr_t size,
390 phys_addr_t min_addr)
392 return memblock_alloc_try_nid(size, align, min_addr,
393 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
396 static inline void * __init memblock_alloc_low(phys_addr_t size,
399 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
400 ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE);
403 static inline void * __init memblock_alloc_node(phys_addr_t size,
404 phys_addr_t align, int nid)
406 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
407 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
410 static inline void __init memblock_free_early(phys_addr_t base,
413 memblock_free(base, size);
416 static inline void __init memblock_free_early_nid(phys_addr_t base,
417 phys_addr_t size, int nid)
419 memblock_free(base, size);
422 static inline void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
424 __memblock_free_late(base, size);
428 * Set the allocation direction to bottom-up or top-down.
430 static inline void __init memblock_set_bottom_up(bool enable)
432 memblock.bottom_up = enable;
436 * Check if the allocation direction is bottom-up or not.
437 * if this is true, that said, memblock will allocate memory
438 * in bottom-up direction.
440 static inline bool memblock_bottom_up(void)
442 return memblock.bottom_up;
445 phys_addr_t memblock_phys_mem_size(void);
446 phys_addr_t memblock_reserved_size(void);
447 phys_addr_t memblock_mem_size(unsigned long limit_pfn);
448 phys_addr_t memblock_start_of_DRAM(void);
449 phys_addr_t memblock_end_of_DRAM(void);
450 void memblock_enforce_memory_limit(phys_addr_t memory_limit);
451 void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
452 void memblock_mem_limit_remove_map(phys_addr_t limit);
453 bool memblock_is_memory(phys_addr_t addr);
454 bool memblock_is_map_memory(phys_addr_t addr);
455 bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
456 bool memblock_is_reserved(phys_addr_t addr);
457 bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
459 extern void __memblock_dump_all(void);
461 static inline void memblock_dump_all(void)
464 __memblock_dump_all();
468 * memblock_set_current_limit - Set the current allocation limit to allow
469 * limiting allocations to what is currently
470 * accessible during boot
471 * @limit: New limit value (physical address)
473 void memblock_set_current_limit(phys_addr_t limit);
476 phys_addr_t memblock_get_current_limit(void);
479 * pfn conversion functions
481 * While the memory MEMBLOCKs should always be page aligned, the reserved
482 * MEMBLOCKs may not be. This accessor attempt to provide a very clear
483 * idea of what they return for such non aligned MEMBLOCKs.
487 * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
488 * @reg: memblock_region structure
490 * Return: the lowest pfn intersecting with the memory region
492 static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
494 return PFN_UP(reg->base);
498 * memblock_region_memory_end_pfn - get the end pfn of the memory region
499 * @reg: memblock_region structure
501 * Return: the end_pfn of the reserved region
503 static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
505 return PFN_DOWN(reg->base + reg->size);
509 * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
510 * @reg: memblock_region structure
512 * Return: the lowest pfn intersecting with the reserved region
514 static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
516 return PFN_DOWN(reg->base);
520 * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
521 * @reg: memblock_region structure
523 * Return: the end_pfn of the reserved region
525 static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
527 return PFN_UP(reg->base + reg->size);
530 #define for_each_memblock(memblock_type, region) \
531 for (region = memblock.memblock_type.regions; \
532 region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \
535 #define for_each_memblock_type(i, memblock_type, rgn) \
536 for (i = 0, rgn = &memblock_type->regions[0]; \
537 i < memblock_type->cnt; \
538 i++, rgn = &memblock_type->regions[i])
540 extern void *alloc_large_system_hash(const char *tablename,
541 unsigned long bucketsize,
542 unsigned long numentries,
545 unsigned int *_hash_shift,
546 unsigned int *_hash_mask,
547 unsigned long low_limit,
548 unsigned long high_limit);
550 #define HASH_EARLY 0x00000001 /* Allocating during early boot? */
551 #define HASH_SMALL 0x00000002 /* sub-page allocation allowed, min
552 * shift passed via *_hash_shift */
553 #define HASH_ZERO 0x00000004 /* Zero allocated hash table */
555 /* Only NUMA needs hash distribution. 64bit NUMA architectures have
556 * sufficient vmalloc space.
559 #define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
560 extern int hashdist; /* Distribute hashes across NUMA nodes? */
565 #ifdef CONFIG_MEMTEST
566 extern void early_memtest(phys_addr_t start, phys_addr_t end);
568 static inline void early_memtest(phys_addr_t start, phys_addr_t end)
573 #endif /* __KERNEL__ */
575 #endif /* _LINUX_MEMBLOCK_H */