memblock: drop __memblock_alloc_base()
[linux-2.6-microblaze.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_nid(phys_addr_t size, phys_addr_t align, int nid);
331 phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
332
333 static inline phys_addr_t memblock_phys_alloc(phys_addr_t size,
334                                               phys_addr_t align)
335 {
336         return memblock_phys_alloc_range(size, align, 0,
337                                          MEMBLOCK_ALLOC_ACCESSIBLE);
338 }
339
340 void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align,
341                                  phys_addr_t min_addr, phys_addr_t max_addr,
342                                  int nid);
343 void *memblock_alloc_try_nid_nopanic(phys_addr_t size, phys_addr_t align,
344                                      phys_addr_t min_addr, phys_addr_t max_addr,
345                                      int nid);
346 void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align,
347                              phys_addr_t min_addr, phys_addr_t max_addr,
348                              int nid);
349
350 static inline void * __init memblock_alloc(phys_addr_t size,  phys_addr_t align)
351 {
352         return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
353                                       MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
354 }
355
356 static inline void * __init memblock_alloc_raw(phys_addr_t size,
357                                                phys_addr_t align)
358 {
359         return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
360                                           MEMBLOCK_ALLOC_ACCESSIBLE,
361                                           NUMA_NO_NODE);
362 }
363
364 static inline void * __init memblock_alloc_from(phys_addr_t size,
365                                                 phys_addr_t align,
366                                                 phys_addr_t min_addr)
367 {
368         return memblock_alloc_try_nid(size, align, min_addr,
369                                       MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
370 }
371
372 static inline void * __init memblock_alloc_nopanic(phys_addr_t size,
373                                                    phys_addr_t align)
374 {
375         return memblock_alloc_try_nid_nopanic(size, align, MEMBLOCK_LOW_LIMIT,
376                                               MEMBLOCK_ALLOC_ACCESSIBLE,
377                                               NUMA_NO_NODE);
378 }
379
380 static inline void * __init memblock_alloc_low(phys_addr_t size,
381                                                phys_addr_t align)
382 {
383         return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
384                                       ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE);
385 }
386 static inline void * __init memblock_alloc_low_nopanic(phys_addr_t size,
387                                                        phys_addr_t align)
388 {
389         return memblock_alloc_try_nid_nopanic(size, align, MEMBLOCK_LOW_LIMIT,
390                                               ARCH_LOW_ADDRESS_LIMIT,
391                                               NUMA_NO_NODE);
392 }
393
394 static inline void * __init memblock_alloc_from_nopanic(phys_addr_t size,
395                                                         phys_addr_t align,
396                                                         phys_addr_t min_addr)
397 {
398         return memblock_alloc_try_nid_nopanic(size, align, min_addr,
399                                               MEMBLOCK_ALLOC_ACCESSIBLE,
400                                               NUMA_NO_NODE);
401 }
402
403 static inline void * __init memblock_alloc_node(phys_addr_t size,
404                                                 phys_addr_t align, int nid)
405 {
406         return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
407                                       MEMBLOCK_ALLOC_ACCESSIBLE, nid);
408 }
409
410 static inline void * __init memblock_alloc_node_nopanic(phys_addr_t size,
411                                                         int nid)
412 {
413         return memblock_alloc_try_nid_nopanic(size, SMP_CACHE_BYTES,
414                                               MEMBLOCK_LOW_LIMIT,
415                                               MEMBLOCK_ALLOC_ACCESSIBLE, nid);
416 }
417
418 static inline void __init memblock_free_early(phys_addr_t base,
419                                               phys_addr_t size)
420 {
421         memblock_free(base, size);
422 }
423
424 static inline void __init memblock_free_early_nid(phys_addr_t base,
425                                                   phys_addr_t size, int nid)
426 {
427         memblock_free(base, size);
428 }
429
430 static inline void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
431 {
432         __memblock_free_late(base, size);
433 }
434
435 /*
436  * Set the allocation direction to bottom-up or top-down.
437  */
438 static inline void __init memblock_set_bottom_up(bool enable)
439 {
440         memblock.bottom_up = enable;
441 }
442
443 /*
444  * Check if the allocation direction is bottom-up or not.
445  * if this is true, that said, memblock will allocate memory
446  * in bottom-up direction.
447  */
448 static inline bool memblock_bottom_up(void)
449 {
450         return memblock.bottom_up;
451 }
452
453 phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align,
454                                 phys_addr_t max_addr);
455 phys_addr_t memblock_phys_mem_size(void);
456 phys_addr_t memblock_reserved_size(void);
457 phys_addr_t memblock_mem_size(unsigned long limit_pfn);
458 phys_addr_t memblock_start_of_DRAM(void);
459 phys_addr_t memblock_end_of_DRAM(void);
460 void memblock_enforce_memory_limit(phys_addr_t memory_limit);
461 void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
462 void memblock_mem_limit_remove_map(phys_addr_t limit);
463 bool memblock_is_memory(phys_addr_t addr);
464 bool memblock_is_map_memory(phys_addr_t addr);
465 bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
466 bool memblock_is_reserved(phys_addr_t addr);
467 bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
468
469 extern void __memblock_dump_all(void);
470
471 static inline void memblock_dump_all(void)
472 {
473         if (memblock_debug)
474                 __memblock_dump_all();
475 }
476
477 /**
478  * memblock_set_current_limit - Set the current allocation limit to allow
479  *                         limiting allocations to what is currently
480  *                         accessible during boot
481  * @limit: New limit value (physical address)
482  */
483 void memblock_set_current_limit(phys_addr_t limit);
484
485
486 phys_addr_t memblock_get_current_limit(void);
487
488 /*
489  * pfn conversion functions
490  *
491  * While the memory MEMBLOCKs should always be page aligned, the reserved
492  * MEMBLOCKs may not be. This accessor attempt to provide a very clear
493  * idea of what they return for such non aligned MEMBLOCKs.
494  */
495
496 /**
497  * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
498  * @reg: memblock_region structure
499  *
500  * Return: the lowest pfn intersecting with the memory region
501  */
502 static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
503 {
504         return PFN_UP(reg->base);
505 }
506
507 /**
508  * memblock_region_memory_end_pfn - get the end pfn of the memory region
509  * @reg: memblock_region structure
510  *
511  * Return: the end_pfn of the reserved region
512  */
513 static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
514 {
515         return PFN_DOWN(reg->base + reg->size);
516 }
517
518 /**
519  * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
520  * @reg: memblock_region structure
521  *
522  * Return: the lowest pfn intersecting with the reserved region
523  */
524 static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
525 {
526         return PFN_DOWN(reg->base);
527 }
528
529 /**
530  * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
531  * @reg: memblock_region structure
532  *
533  * Return: the end_pfn of the reserved region
534  */
535 static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
536 {
537         return PFN_UP(reg->base + reg->size);
538 }
539
540 #define for_each_memblock(memblock_type, region)                                        \
541         for (region = memblock.memblock_type.regions;                                   \
542              region < (memblock.memblock_type.regions + memblock.memblock_type.cnt);    \
543              region++)
544
545 #define for_each_memblock_type(i, memblock_type, rgn)                   \
546         for (i = 0, rgn = &memblock_type->regions[0];                   \
547              i < memblock_type->cnt;                                    \
548              i++, rgn = &memblock_type->regions[i])
549
550 extern void *alloc_large_system_hash(const char *tablename,
551                                      unsigned long bucketsize,
552                                      unsigned long numentries,
553                                      int scale,
554                                      int flags,
555                                      unsigned int *_hash_shift,
556                                      unsigned int *_hash_mask,
557                                      unsigned long low_limit,
558                                      unsigned long high_limit);
559
560 #define HASH_EARLY      0x00000001      /* Allocating during early boot? */
561 #define HASH_SMALL      0x00000002      /* sub-page allocation allowed, min
562                                          * shift passed via *_hash_shift */
563 #define HASH_ZERO       0x00000004      /* Zero allocated hash table */
564
565 /* Only NUMA needs hash distribution. 64bit NUMA architectures have
566  * sufficient vmalloc space.
567  */
568 #ifdef CONFIG_NUMA
569 #define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
570 extern int hashdist;            /* Distribute hashes across NUMA nodes? */
571 #else
572 #define hashdist (0)
573 #endif
574
575 #ifdef CONFIG_MEMTEST
576 extern void early_memtest(phys_addr_t start, phys_addr_t end);
577 #else
578 static inline void early_memtest(phys_addr_t start, phys_addr_t end)
579 {
580 }
581 #endif
582
583 #endif /* __KERNEL__ */
584
585 #endif /* _LINUX_MEMBLOCK_H */