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 and treat as
34 * reserved in the memory map; refer to memblock_mark_nomap() description
38 MEMBLOCK_NONE = 0x0, /* No special request */
39 MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */
40 MEMBLOCK_MIRROR = 0x2, /* mirrored region */
41 MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */
45 * struct memblock_region - represents a memory region
46 * @base: base address of the region
47 * @size: size of the region
48 * @flags: memory region attributes
51 struct memblock_region {
54 enum memblock_flags flags;
61 * struct memblock_type - collection of memory regions of certain type
62 * @cnt: number of regions
63 * @max: size of the allocated array
64 * @total_size: size of all regions
65 * @regions: array of regions
66 * @name: the memory type symbolic name
68 struct memblock_type {
71 phys_addr_t total_size;
72 struct memblock_region *regions;
77 * struct memblock - memblock allocator metadata
78 * @bottom_up: is bottom up direction?
79 * @current_limit: physical address of the current allocation limit
80 * @memory: usable memory regions
81 * @reserved: reserved memory regions
84 bool bottom_up; /* is bottom up direction? */
85 phys_addr_t current_limit;
86 struct memblock_type memory;
87 struct memblock_type reserved;
90 extern struct memblock memblock;
92 #ifndef CONFIG_ARCH_KEEP_MEMBLOCK
93 #define __init_memblock __meminit
94 #define __initdata_memblock __meminitdata
95 void memblock_discard(void);
97 #define __init_memblock
98 #define __initdata_memblock
99 static inline void memblock_discard(void) {}
102 phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
103 phys_addr_t size, phys_addr_t align);
104 void memblock_allow_resize(void);
105 int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
106 int memblock_add(phys_addr_t base, phys_addr_t size);
107 int memblock_remove(phys_addr_t base, phys_addr_t size);
108 int memblock_free(phys_addr_t base, phys_addr_t size);
109 int memblock_reserve(phys_addr_t base, phys_addr_t size);
110 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
111 int memblock_physmem_add(phys_addr_t base, phys_addr_t size);
113 void memblock_trim_memory(phys_addr_t align);
114 bool memblock_overlaps_region(struct memblock_type *type,
115 phys_addr_t base, phys_addr_t size);
116 int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
117 int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
118 int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
119 int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
120 int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
122 void memblock_free_all(void);
123 void reset_node_managed_pages(pg_data_t *pgdat);
124 void reset_all_zones_managed_pages(void);
126 /* Low level functions */
127 void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
128 struct memblock_type *type_a,
129 struct memblock_type *type_b, phys_addr_t *out_start,
130 phys_addr_t *out_end, int *out_nid);
132 void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags,
133 struct memblock_type *type_a,
134 struct memblock_type *type_b, phys_addr_t *out_start,
135 phys_addr_t *out_end, int *out_nid);
137 void __memblock_free_late(phys_addr_t base, phys_addr_t size);
139 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
140 static inline void __next_physmem_range(u64 *idx, struct memblock_type *type,
141 phys_addr_t *out_start,
142 phys_addr_t *out_end)
144 extern struct memblock_type physmem;
146 __next_mem_range(idx, NUMA_NO_NODE, MEMBLOCK_NONE, &physmem, type,
147 out_start, out_end, NULL);
151 * for_each_physmem_range - iterate through physmem areas not included in type.
152 * @i: u64 used as loop variable
153 * @type: ptr to memblock_type which excludes from the iteration, can be %NULL
154 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
155 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
157 #define for_each_physmem_range(i, type, p_start, p_end) \
158 for (i = 0, __next_physmem_range(&i, type, p_start, p_end); \
159 i != (u64)ULLONG_MAX; \
160 __next_physmem_range(&i, type, p_start, p_end))
161 #endif /* CONFIG_HAVE_MEMBLOCK_PHYS_MAP */
164 * __for_each_mem_range - iterate through memblock areas from type_a and not
165 * included in type_b. Or just type_a if type_b is NULL.
166 * @i: u64 used as loop variable
167 * @type_a: ptr to memblock_type to iterate
168 * @type_b: ptr to memblock_type which excludes from the iteration
169 * @nid: node selector, %NUMA_NO_NODE for all nodes
170 * @flags: pick from blocks based on memory attributes
171 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
172 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
173 * @p_nid: ptr to int for nid of the range, can be %NULL
175 #define __for_each_mem_range(i, type_a, type_b, nid, flags, \
176 p_start, p_end, p_nid) \
177 for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \
178 p_start, p_end, p_nid); \
179 i != (u64)ULLONG_MAX; \
180 __next_mem_range(&i, nid, flags, type_a, type_b, \
181 p_start, p_end, p_nid))
184 * __for_each_mem_range_rev - reverse iterate through memblock areas from
185 * type_a and not included in type_b. Or just type_a if type_b is NULL.
186 * @i: u64 used as loop variable
187 * @type_a: ptr to memblock_type to iterate
188 * @type_b: ptr to memblock_type which excludes from the iteration
189 * @nid: node selector, %NUMA_NO_NODE for all nodes
190 * @flags: pick from blocks based on memory attributes
191 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
192 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
193 * @p_nid: ptr to int for nid of the range, can be %NULL
195 #define __for_each_mem_range_rev(i, type_a, type_b, nid, flags, \
196 p_start, p_end, p_nid) \
197 for (i = (u64)ULLONG_MAX, \
198 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
199 p_start, p_end, p_nid); \
200 i != (u64)ULLONG_MAX; \
201 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
202 p_start, p_end, p_nid))
205 * for_each_mem_range - iterate through memory areas.
206 * @i: u64 used as loop variable
207 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
208 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
210 #define for_each_mem_range(i, p_start, p_end) \
211 __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE, \
212 MEMBLOCK_NONE, p_start, p_end, NULL)
215 * for_each_mem_range_rev - reverse iterate through memblock areas from
216 * type_a and not included in type_b. Or just type_a if type_b is NULL.
217 * @i: u64 used as loop variable
218 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
219 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
221 #define for_each_mem_range_rev(i, p_start, p_end) \
222 __for_each_mem_range_rev(i, &memblock.memory, NULL, NUMA_NO_NODE, \
223 MEMBLOCK_NONE, p_start, p_end, NULL)
226 * for_each_reserved_mem_range - iterate over all reserved memblock areas
227 * @i: u64 used as loop variable
228 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
229 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
231 * Walks over reserved areas of memblock. Available as soon as memblock
234 #define for_each_reserved_mem_range(i, p_start, p_end) \
235 __for_each_mem_range(i, &memblock.reserved, NULL, NUMA_NO_NODE, \
236 MEMBLOCK_NONE, p_start, p_end, NULL)
238 static inline bool memblock_is_hotpluggable(struct memblock_region *m)
240 return m->flags & MEMBLOCK_HOTPLUG;
243 static inline bool memblock_is_mirror(struct memblock_region *m)
245 return m->flags & MEMBLOCK_MIRROR;
248 static inline bool memblock_is_nomap(struct memblock_region *m)
250 return m->flags & MEMBLOCK_NOMAP;
253 int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
254 unsigned long *end_pfn);
255 void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
256 unsigned long *out_end_pfn, int *out_nid);
259 * for_each_mem_pfn_range - early memory pfn range iterator
260 * @i: an integer used as loop variable
261 * @nid: node selector, %MAX_NUMNODES for all nodes
262 * @p_start: ptr to ulong for start pfn of the range, can be %NULL
263 * @p_end: ptr to ulong for end pfn of the range, can be %NULL
264 * @p_nid: ptr to int for nid of the range, can be %NULL
266 * Walks over configured memory ranges.
268 #define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
269 for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
270 i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
272 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
273 void __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
274 unsigned long *out_spfn,
275 unsigned long *out_epfn);
277 * for_each_free_mem_pfn_range_in_zone - iterate through zone specific free
279 * @i: u64 used as loop variable
280 * @zone: zone in which all of the memory blocks reside
281 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
282 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
284 * Walks over free (memory && !reserved) areas of memblock in a specific
285 * zone. Available once memblock and an empty zone is initialized. The main
286 * assumption is that the zone start, end, and pgdat have been associated.
287 * This way we can use the zone to determine NUMA node, and if a given part
288 * of the memblock is valid for the zone.
290 #define for_each_free_mem_pfn_range_in_zone(i, zone, p_start, p_end) \
292 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end); \
294 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
297 * for_each_free_mem_pfn_range_in_zone_from - iterate through zone specific
298 * free memblock areas from a given point
299 * @i: u64 used as loop variable
300 * @zone: zone in which all of the memory blocks reside
301 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
302 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
304 * Walks over free (memory && !reserved) areas of memblock in a specific
305 * zone, continuing from current position. Available as soon as memblock is
308 #define for_each_free_mem_pfn_range_in_zone_from(i, zone, p_start, p_end) \
309 for (; i != U64_MAX; \
310 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
312 int __init deferred_page_init_max_threads(const struct cpumask *node_cpumask);
314 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
317 * for_each_free_mem_range - iterate through free memblock areas
318 * @i: u64 used as loop variable
319 * @nid: node selector, %NUMA_NO_NODE for all nodes
320 * @flags: pick from blocks based on memory attributes
321 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
322 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
323 * @p_nid: ptr to int for nid of the range, can be %NULL
325 * Walks over free (memory && !reserved) areas of memblock. Available as
326 * soon as memblock is initialized.
328 #define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \
329 __for_each_mem_range(i, &memblock.memory, &memblock.reserved, \
330 nid, flags, p_start, p_end, p_nid)
333 * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
334 * @i: u64 used as loop variable
335 * @nid: node selector, %NUMA_NO_NODE for all nodes
336 * @flags: pick from blocks based on memory attributes
337 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
338 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
339 * @p_nid: ptr to int for nid of the range, can be %NULL
341 * Walks over free (memory && !reserved) areas of memblock in reverse
342 * order. Available as soon as memblock is initialized.
344 #define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
346 __for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
347 nid, flags, p_start, p_end, p_nid)
349 int memblock_set_node(phys_addr_t base, phys_addr_t size,
350 struct memblock_type *type, int nid);
353 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
358 static inline int memblock_get_region_node(const struct memblock_region *r)
363 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
367 static inline int memblock_get_region_node(const struct memblock_region *r)
371 #endif /* CONFIG_NUMA */
373 /* Flags for memblock allocation APIs */
374 #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
375 #define MEMBLOCK_ALLOC_ACCESSIBLE 0
376 #define MEMBLOCK_ALLOC_KASAN 1
378 /* We are using top down, so it is safe to use 0 here */
379 #define MEMBLOCK_LOW_LIMIT 0
381 #ifndef ARCH_LOW_ADDRESS_LIMIT
382 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
385 phys_addr_t memblock_phys_alloc_range(phys_addr_t size, phys_addr_t align,
386 phys_addr_t start, phys_addr_t end);
387 phys_addr_t memblock_alloc_range_nid(phys_addr_t size,
388 phys_addr_t align, phys_addr_t start,
389 phys_addr_t end, int nid, bool exact_nid);
390 phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
392 static inline phys_addr_t memblock_phys_alloc(phys_addr_t size,
395 return memblock_phys_alloc_range(size, align, 0,
396 MEMBLOCK_ALLOC_ACCESSIBLE);
399 void *memblock_alloc_exact_nid_raw(phys_addr_t size, phys_addr_t align,
400 phys_addr_t min_addr, phys_addr_t max_addr,
402 void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align,
403 phys_addr_t min_addr, phys_addr_t max_addr,
405 void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align,
406 phys_addr_t min_addr, phys_addr_t max_addr,
409 static __always_inline void *memblock_alloc(phys_addr_t size, phys_addr_t align)
411 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
412 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
415 static inline void *memblock_alloc_raw(phys_addr_t size,
418 return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
419 MEMBLOCK_ALLOC_ACCESSIBLE,
423 static inline void *memblock_alloc_from(phys_addr_t size,
425 phys_addr_t min_addr)
427 return memblock_alloc_try_nid(size, align, min_addr,
428 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
431 static inline void *memblock_alloc_low(phys_addr_t size,
434 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
435 ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE);
438 static inline void *memblock_alloc_node(phys_addr_t size,
439 phys_addr_t align, int nid)
441 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
442 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
445 static inline void memblock_free_early(phys_addr_t base,
448 memblock_free(base, size);
451 static inline void memblock_free_early_nid(phys_addr_t base,
452 phys_addr_t size, int nid)
454 memblock_free(base, size);
457 static inline void memblock_free_late(phys_addr_t base, phys_addr_t size)
459 __memblock_free_late(base, size);
463 * Set the allocation direction to bottom-up or top-down.
465 static inline __init_memblock void memblock_set_bottom_up(bool enable)
467 memblock.bottom_up = enable;
471 * Check if the allocation direction is bottom-up or not.
472 * if this is true, that said, memblock will allocate memory
473 * in bottom-up direction.
475 static inline __init_memblock bool memblock_bottom_up(void)
477 return memblock.bottom_up;
480 phys_addr_t memblock_phys_mem_size(void);
481 phys_addr_t memblock_reserved_size(void);
482 phys_addr_t memblock_start_of_DRAM(void);
483 phys_addr_t memblock_end_of_DRAM(void);
484 void memblock_enforce_memory_limit(phys_addr_t memory_limit);
485 void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
486 void memblock_mem_limit_remove_map(phys_addr_t limit);
487 bool memblock_is_memory(phys_addr_t addr);
488 bool memblock_is_map_memory(phys_addr_t addr);
489 bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
490 bool memblock_is_reserved(phys_addr_t addr);
491 bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
493 void memblock_dump_all(void);
496 * memblock_set_current_limit - Set the current allocation limit to allow
497 * limiting allocations to what is currently
498 * accessible during boot
499 * @limit: New limit value (physical address)
501 void memblock_set_current_limit(phys_addr_t limit);
504 phys_addr_t memblock_get_current_limit(void);
507 * pfn conversion functions
509 * While the memory MEMBLOCKs should always be page aligned, the reserved
510 * MEMBLOCKs may not be. This accessor attempt to provide a very clear
511 * idea of what they return for such non aligned MEMBLOCKs.
515 * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
516 * @reg: memblock_region structure
518 * Return: the lowest pfn intersecting with the memory region
520 static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
522 return PFN_UP(reg->base);
526 * memblock_region_memory_end_pfn - get the end pfn of the memory region
527 * @reg: memblock_region structure
529 * Return: the end_pfn of the reserved region
531 static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
533 return PFN_DOWN(reg->base + reg->size);
537 * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
538 * @reg: memblock_region structure
540 * Return: the lowest pfn intersecting with the reserved region
542 static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
544 return PFN_DOWN(reg->base);
548 * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
549 * @reg: memblock_region structure
551 * Return: the end_pfn of the reserved region
553 static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
555 return PFN_UP(reg->base + reg->size);
559 * for_each_mem_region - itereate over memory regions
560 * @region: loop variable
562 #define for_each_mem_region(region) \
563 for (region = memblock.memory.regions; \
564 region < (memblock.memory.regions + memblock.memory.cnt); \
568 * for_each_reserved_mem_region - itereate over reserved memory regions
569 * @region: loop variable
571 #define for_each_reserved_mem_region(region) \
572 for (region = memblock.reserved.regions; \
573 region < (memblock.reserved.regions + memblock.reserved.cnt); \
576 extern void *alloc_large_system_hash(const char *tablename,
577 unsigned long bucketsize,
578 unsigned long numentries,
581 unsigned int *_hash_shift,
582 unsigned int *_hash_mask,
583 unsigned long low_limit,
584 unsigned long high_limit);
586 #define HASH_EARLY 0x00000001 /* Allocating during early boot? */
587 #define HASH_SMALL 0x00000002 /* sub-page allocation allowed, min
588 * shift passed via *_hash_shift */
589 #define HASH_ZERO 0x00000004 /* Zero allocated hash table */
591 /* Only NUMA needs hash distribution. 64bit NUMA architectures have
592 * sufficient vmalloc space.
595 #define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
596 extern int hashdist; /* Distribute hashes across NUMA nodes? */
601 #ifdef CONFIG_MEMTEST
602 extern void early_memtest(phys_addr_t start, phys_addr_t end);
604 static inline void early_memtest(phys_addr_t start, phys_addr_t end)
609 #endif /* __KERNEL__ */
611 #endif /* _LINUX_MEMBLOCK_H */