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