1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
5 #ifndef _LINUX_BOOTMEM_H
6 #define _LINUX_BOOTMEM_H
8 #include <linux/mmzone.h>
9 #include <linux/mm_types.h>
11 #include <asm/processor.h>
14 * simple boot-time physical memory area allocator.
17 extern unsigned long max_low_pfn;
18 extern unsigned long min_low_pfn;
23 extern unsigned long max_pfn;
25 * highest possible page
27 extern unsigned long long max_possible_pfn;
29 #ifndef CONFIG_NO_BOOTMEM
31 * struct bootmem_data - per-node information used by the bootmem allocator
32 * @node_min_pfn: the starting physical address of the node's memory
33 * @node_low_pfn: the end physical address of the directly addressable memory
34 * @node_bootmem_map: is a bitmap pointer - the bits represent all physical
35 * memory pages (including holes) on the node.
36 * @last_end_off: the offset within the page of the end of the last allocation;
37 * if 0, the page used is full
38 * @hint_idx: the PFN of the page used with the last allocation;
39 * together with using this with the @last_end_offset field,
40 * a test can be made to see if allocations can be merged
41 * with the page used for the last allocation rather than
42 * using up a full new page.
43 * @list: list entry in the linked list ordered by the memory addresses
45 typedef struct bootmem_data {
46 unsigned long node_min_pfn;
47 unsigned long node_low_pfn;
48 void *node_bootmem_map;
49 unsigned long last_end_off;
50 unsigned long hint_idx;
51 struct list_head list;
54 extern bootmem_data_t bootmem_node_data[];
57 extern unsigned long bootmem_bootmap_pages(unsigned long);
59 extern unsigned long init_bootmem_node(pg_data_t *pgdat,
60 unsigned long freepfn,
61 unsigned long startpfn,
62 unsigned long endpfn);
63 extern unsigned long init_bootmem(unsigned long addr, unsigned long memend);
65 extern unsigned long free_all_bootmem(void);
66 extern void reset_node_managed_pages(pg_data_t *pgdat);
67 extern void reset_all_zones_managed_pages(void);
69 extern void free_bootmem_node(pg_data_t *pgdat,
72 extern void free_bootmem(unsigned long physaddr, unsigned long size);
73 extern void free_bootmem_late(unsigned long physaddr, unsigned long size);
76 * Flags for reserve_bootmem (also if CONFIG_HAVE_ARCH_BOOTMEM_NODE,
77 * the architecture-specific code should honor this).
79 * If flags is BOOTMEM_DEFAULT, then the return value is always 0 (success).
80 * If flags contains BOOTMEM_EXCLUSIVE, then -EBUSY is returned if the memory
81 * already was reserved.
83 #define BOOTMEM_DEFAULT 0
84 #define BOOTMEM_EXCLUSIVE (1<<0)
86 extern int reserve_bootmem(unsigned long addr,
89 extern int reserve_bootmem_node(pg_data_t *pgdat,
90 unsigned long physaddr,
94 extern void *__alloc_bootmem(unsigned long size,
97 extern void *__alloc_bootmem_nopanic(unsigned long size,
99 unsigned long goal) __malloc;
100 extern void *__alloc_bootmem_node(pg_data_t *pgdat,
103 unsigned long goal) __malloc;
104 void *__alloc_bootmem_node_high(pg_data_t *pgdat,
107 unsigned long goal) __malloc;
108 extern void *__alloc_bootmem_node_nopanic(pg_data_t *pgdat,
111 unsigned long goal) __malloc;
112 void *___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
116 unsigned long limit) __malloc;
117 extern void *__alloc_bootmem_low(unsigned long size,
119 unsigned long goal) __malloc;
120 void *__alloc_bootmem_low_nopanic(unsigned long size,
122 unsigned long goal) __malloc;
123 extern void *__alloc_bootmem_low_node(pg_data_t *pgdat,
126 unsigned long goal) __malloc;
128 #ifdef CONFIG_NO_BOOTMEM
129 /* We are using top down, so it is safe to use 0 here */
130 #define BOOTMEM_LOW_LIMIT 0
132 #define BOOTMEM_LOW_LIMIT __pa(MAX_DMA_ADDRESS)
135 #ifndef ARCH_LOW_ADDRESS_LIMIT
136 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
139 #define alloc_bootmem(x) \
140 __alloc_bootmem(x, SMP_CACHE_BYTES, BOOTMEM_LOW_LIMIT)
141 #define alloc_bootmem_align(x, align) \
142 __alloc_bootmem(x, align, BOOTMEM_LOW_LIMIT)
143 #define alloc_bootmem_nopanic(x) \
144 __alloc_bootmem_nopanic(x, SMP_CACHE_BYTES, BOOTMEM_LOW_LIMIT)
145 #define alloc_bootmem_pages(x) \
146 __alloc_bootmem(x, PAGE_SIZE, BOOTMEM_LOW_LIMIT)
147 #define alloc_bootmem_pages_nopanic(x) \
148 __alloc_bootmem_nopanic(x, PAGE_SIZE, BOOTMEM_LOW_LIMIT)
149 #define alloc_bootmem_node(pgdat, x) \
150 __alloc_bootmem_node(pgdat, x, SMP_CACHE_BYTES, BOOTMEM_LOW_LIMIT)
151 #define alloc_bootmem_node_nopanic(pgdat, x) \
152 __alloc_bootmem_node_nopanic(pgdat, x, SMP_CACHE_BYTES, BOOTMEM_LOW_LIMIT)
153 #define alloc_bootmem_pages_node(pgdat, x) \
154 __alloc_bootmem_node(pgdat, x, PAGE_SIZE, BOOTMEM_LOW_LIMIT)
155 #define alloc_bootmem_pages_node_nopanic(pgdat, x) \
156 __alloc_bootmem_node_nopanic(pgdat, x, PAGE_SIZE, BOOTMEM_LOW_LIMIT)
158 #define alloc_bootmem_low(x) \
159 __alloc_bootmem_low(x, SMP_CACHE_BYTES, 0)
160 #define alloc_bootmem_low_pages_nopanic(x) \
161 __alloc_bootmem_low_nopanic(x, PAGE_SIZE, 0)
162 #define alloc_bootmem_low_pages(x) \
163 __alloc_bootmem_low(x, PAGE_SIZE, 0)
164 #define alloc_bootmem_low_pages_node(pgdat, x) \
165 __alloc_bootmem_low_node(pgdat, x, PAGE_SIZE, 0)
168 #if defined(CONFIG_HAVE_MEMBLOCK) && defined(CONFIG_NO_BOOTMEM)
170 /* FIXME: use MEMBLOCK_ALLOC_* variants here */
171 #define BOOTMEM_ALLOC_ACCESSIBLE 0
172 #define BOOTMEM_ALLOC_ANYWHERE (~(phys_addr_t)0)
174 /* FIXME: Move to memblock.h at a point where we remove nobootmem.c */
175 void *memblock_virt_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align,
176 phys_addr_t min_addr,
177 phys_addr_t max_addr, int nid);
178 void *memblock_virt_alloc_try_nid_nopanic(phys_addr_t size,
179 phys_addr_t align, phys_addr_t min_addr,
180 phys_addr_t max_addr, int nid);
181 void *memblock_virt_alloc_try_nid(phys_addr_t size, phys_addr_t align,
182 phys_addr_t min_addr, phys_addr_t max_addr, int nid);
183 void __memblock_free_early(phys_addr_t base, phys_addr_t size);
184 void __memblock_free_late(phys_addr_t base, phys_addr_t size);
186 static inline void * __init memblock_virt_alloc(
187 phys_addr_t size, phys_addr_t align)
189 return memblock_virt_alloc_try_nid(size, align, BOOTMEM_LOW_LIMIT,
190 BOOTMEM_ALLOC_ACCESSIBLE,
194 static inline void * __init memblock_virt_alloc_raw(
195 phys_addr_t size, phys_addr_t align)
197 return memblock_virt_alloc_try_nid_raw(size, align, BOOTMEM_LOW_LIMIT,
198 BOOTMEM_ALLOC_ACCESSIBLE,
202 static inline void * __init memblock_virt_alloc_nopanic(
203 phys_addr_t size, phys_addr_t align)
205 return memblock_virt_alloc_try_nid_nopanic(size, align,
207 BOOTMEM_ALLOC_ACCESSIBLE,
211 static inline void * __init memblock_virt_alloc_low(
212 phys_addr_t size, phys_addr_t align)
214 return memblock_virt_alloc_try_nid(size, align,
216 ARCH_LOW_ADDRESS_LIMIT,
219 static inline void * __init memblock_virt_alloc_low_nopanic(
220 phys_addr_t size, phys_addr_t align)
222 return memblock_virt_alloc_try_nid_nopanic(size, align,
224 ARCH_LOW_ADDRESS_LIMIT,
228 static inline void * __init memblock_virt_alloc_from_nopanic(
229 phys_addr_t size, phys_addr_t align, phys_addr_t min_addr)
231 return memblock_virt_alloc_try_nid_nopanic(size, align, min_addr,
232 BOOTMEM_ALLOC_ACCESSIBLE,
236 static inline void * __init memblock_virt_alloc_node(
237 phys_addr_t size, int nid)
239 return memblock_virt_alloc_try_nid(size, 0, BOOTMEM_LOW_LIMIT,
240 BOOTMEM_ALLOC_ACCESSIBLE, nid);
243 static inline void * __init memblock_virt_alloc_node_nopanic(
244 phys_addr_t size, int nid)
246 return memblock_virt_alloc_try_nid_nopanic(size, 0, BOOTMEM_LOW_LIMIT,
247 BOOTMEM_ALLOC_ACCESSIBLE,
251 static inline void __init memblock_free_early(
252 phys_addr_t base, phys_addr_t size)
254 __memblock_free_early(base, size);
257 static inline void __init memblock_free_early_nid(
258 phys_addr_t base, phys_addr_t size, int nid)
260 __memblock_free_early(base, size);
263 static inline void __init memblock_free_late(
264 phys_addr_t base, phys_addr_t size)
266 __memblock_free_late(base, size);
271 #define BOOTMEM_ALLOC_ACCESSIBLE 0
274 /* Fall back to all the existing bootmem APIs */
275 static inline void * __init memblock_virt_alloc(
276 phys_addr_t size, phys_addr_t align)
279 align = SMP_CACHE_BYTES;
280 return __alloc_bootmem(size, align, BOOTMEM_LOW_LIMIT);
283 static inline void * __init memblock_virt_alloc_raw(
284 phys_addr_t size, phys_addr_t align)
287 align = SMP_CACHE_BYTES;
288 return __alloc_bootmem_nopanic(size, align, BOOTMEM_LOW_LIMIT);
291 static inline void * __init memblock_virt_alloc_nopanic(
292 phys_addr_t size, phys_addr_t align)
295 align = SMP_CACHE_BYTES;
296 return __alloc_bootmem_nopanic(size, align, BOOTMEM_LOW_LIMIT);
299 static inline void * __init memblock_virt_alloc_low(
300 phys_addr_t size, phys_addr_t align)
303 align = SMP_CACHE_BYTES;
304 return __alloc_bootmem_low(size, align, 0);
307 static inline void * __init memblock_virt_alloc_low_nopanic(
308 phys_addr_t size, phys_addr_t align)
311 align = SMP_CACHE_BYTES;
312 return __alloc_bootmem_low_nopanic(size, align, 0);
315 static inline void * __init memblock_virt_alloc_from_nopanic(
316 phys_addr_t size, phys_addr_t align, phys_addr_t min_addr)
318 return __alloc_bootmem_nopanic(size, align, min_addr);
321 static inline void * __init memblock_virt_alloc_node(
322 phys_addr_t size, int nid)
324 return __alloc_bootmem_node(NODE_DATA(nid), size, SMP_CACHE_BYTES,
328 static inline void * __init memblock_virt_alloc_node_nopanic(
329 phys_addr_t size, int nid)
331 return __alloc_bootmem_node_nopanic(NODE_DATA(nid), size,
336 static inline void * __init memblock_virt_alloc_try_nid(phys_addr_t size,
337 phys_addr_t align, phys_addr_t min_addr, phys_addr_t max_addr, int nid)
339 return __alloc_bootmem_node_high(NODE_DATA(nid), size, align,
343 static inline void * __init memblock_virt_alloc_try_nid_raw(
344 phys_addr_t size, phys_addr_t align,
345 phys_addr_t min_addr, phys_addr_t max_addr, int nid)
347 return ___alloc_bootmem_node_nopanic(NODE_DATA(nid), size, align,
351 static inline void * __init memblock_virt_alloc_try_nid_nopanic(
352 phys_addr_t size, phys_addr_t align,
353 phys_addr_t min_addr, phys_addr_t max_addr, int nid)
355 return ___alloc_bootmem_node_nopanic(NODE_DATA(nid), size, align,
359 static inline void __init memblock_free_early(
360 phys_addr_t base, phys_addr_t size)
362 free_bootmem(base, size);
365 static inline void __init memblock_free_early_nid(
366 phys_addr_t base, phys_addr_t size, int nid)
368 free_bootmem_node(NODE_DATA(nid), base, size);
371 static inline void __init memblock_free_late(
372 phys_addr_t base, phys_addr_t size)
374 free_bootmem_late(base, size);
376 #endif /* defined(CONFIG_HAVE_MEMBLOCK) && defined(CONFIG_NO_BOOTMEM) */
378 extern void *alloc_large_system_hash(const char *tablename,
379 unsigned long bucketsize,
380 unsigned long numentries,
383 unsigned int *_hash_shift,
384 unsigned int *_hash_mask,
385 unsigned long low_limit,
386 unsigned long high_limit);
388 #define HASH_EARLY 0x00000001 /* Allocating during early boot? */
389 #define HASH_SMALL 0x00000002 /* sub-page allocation allowed, min
390 * shift passed via *_hash_shift */
391 #define HASH_ZERO 0x00000004 /* Zero allocated hash table */
393 /* Only NUMA needs hash distribution. 64bit NUMA architectures have
394 * sufficient vmalloc space.
397 #define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
398 extern int hashdist; /* Distribute hashes across NUMA nodes? */
404 #endif /* _LINUX_BOOTMEM_H */