mm/memblock: add extra "flags" to memblock to allow selection of memory based on...
[firefly-linux-kernel-4.4.55.git] / include / linux / memblock.h
1 #ifndef _LINUX_MEMBLOCK_H
2 #define _LINUX_MEMBLOCK_H
3 #ifdef __KERNEL__
4
5 #ifdef CONFIG_HAVE_MEMBLOCK
6 /*
7  * Logical memory blocks.
8  *
9  * Copyright (C) 2001 Peter Bergner, IBM Corp.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version
14  * 2 of the License, or (at your option) any later version.
15  */
16
17 #include <linux/init.h>
18 #include <linux/mm.h>
19
20 #define INIT_MEMBLOCK_REGIONS   128
21 #define INIT_PHYSMEM_REGIONS    4
22
23 /* Definition of memblock flags. */
24 enum {
25         MEMBLOCK_NONE           = 0x0,  /* No special request */
26         MEMBLOCK_HOTPLUG        = 0x1,  /* hotpluggable region */
27 };
28
29 struct memblock_region {
30         phys_addr_t base;
31         phys_addr_t size;
32         unsigned long flags;
33 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
34         int nid;
35 #endif
36 };
37
38 struct memblock_type {
39         unsigned long cnt;      /* number of regions */
40         unsigned long max;      /* size of the allocated array */
41         phys_addr_t total_size; /* size of all regions */
42         struct memblock_region *regions;
43 };
44
45 struct memblock {
46         bool bottom_up;  /* is bottom up direction? */
47         phys_addr_t current_limit;
48         struct memblock_type memory;
49         struct memblock_type reserved;
50 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
51         struct memblock_type physmem;
52 #endif
53 };
54
55 extern struct memblock memblock;
56 extern int memblock_debug;
57 #ifdef CONFIG_MOVABLE_NODE
58 /* If movable_node boot option specified */
59 extern bool movable_node_enabled;
60 #endif /* CONFIG_MOVABLE_NODE */
61
62 #define memblock_dbg(fmt, ...) \
63         if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
64
65 phys_addr_t memblock_find_in_range_node(phys_addr_t size, phys_addr_t align,
66                                             phys_addr_t start, phys_addr_t end,
67                                             int nid, ulong flags);
68 phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
69                                    phys_addr_t size, phys_addr_t align);
70 phys_addr_t get_allocated_memblock_reserved_regions_info(phys_addr_t *addr);
71 phys_addr_t get_allocated_memblock_memory_regions_info(phys_addr_t *addr);
72 void memblock_allow_resize(void);
73 int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
74 int memblock_add(phys_addr_t base, phys_addr_t size);
75 int memblock_remove(phys_addr_t base, phys_addr_t size);
76 int memblock_free(phys_addr_t base, phys_addr_t size);
77 int memblock_reserve(phys_addr_t base, phys_addr_t size);
78 void memblock_trim_memory(phys_addr_t align);
79 int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
80 int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
81
82 /* Low level functions */
83 int memblock_add_range(struct memblock_type *type,
84                        phys_addr_t base, phys_addr_t size,
85                        int nid, unsigned long flags);
86
87 int memblock_remove_range(struct memblock_type *type,
88                           phys_addr_t base,
89                           phys_addr_t size);
90
91 void __next_mem_range(u64 *idx, int nid, ulong flags,
92                       struct memblock_type *type_a,
93                       struct memblock_type *type_b, phys_addr_t *out_start,
94                       phys_addr_t *out_end, int *out_nid);
95
96 void __next_mem_range_rev(u64 *idx, int nid, ulong flags,
97                           struct memblock_type *type_a,
98                           struct memblock_type *type_b, phys_addr_t *out_start,
99                           phys_addr_t *out_end, int *out_nid);
100
101 /**
102  * for_each_mem_range - iterate through memblock areas from type_a and not
103  * included in type_b. Or just type_a if type_b is NULL.
104  * @i: u64 used as loop variable
105  * @type_a: ptr to memblock_type to iterate
106  * @type_b: ptr to memblock_type which excludes from the iteration
107  * @nid: node selector, %NUMA_NO_NODE for all nodes
108  * @flags: pick from blocks based on memory attributes
109  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
110  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
111  * @p_nid: ptr to int for nid of the range, can be %NULL
112  */
113 #define for_each_mem_range(i, type_a, type_b, nid, flags,               \
114                            p_start, p_end, p_nid)                       \
115         for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b,    \
116                                      p_start, p_end, p_nid);            \
117              i != (u64)ULLONG_MAX;                                      \
118              __next_mem_range(&i, nid, flags, type_a, type_b,           \
119                               p_start, p_end, p_nid))
120
121 /**
122  * for_each_mem_range_rev - reverse iterate through memblock areas from
123  * type_a and not included in type_b. Or just type_a if type_b is NULL.
124  * @i: u64 used as loop variable
125  * @type_a: ptr to memblock_type to iterate
126  * @type_b: ptr to memblock_type which excludes from the iteration
127  * @nid: node selector, %NUMA_NO_NODE for all nodes
128  * @flags: pick from blocks based on memory attributes
129  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
130  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
131  * @p_nid: ptr to int for nid of the range, can be %NULL
132  */
133 #define for_each_mem_range_rev(i, type_a, type_b, nid, flags,           \
134                                p_start, p_end, p_nid)                   \
135         for (i = (u64)ULLONG_MAX,                                       \
136                      __next_mem_range_rev(&i, nid, flags, type_a, type_b,\
137                                          p_start, p_end, p_nid);        \
138              i != (u64)ULLONG_MAX;                                      \
139              __next_mem_range_rev(&i, nid, flags, type_a, type_b,       \
140                                   p_start, p_end, p_nid))
141
142 #ifdef CONFIG_MOVABLE_NODE
143 static inline bool memblock_is_hotpluggable(struct memblock_region *m)
144 {
145         return m->flags & MEMBLOCK_HOTPLUG;
146 }
147
148 static inline bool movable_node_is_enabled(void)
149 {
150         return movable_node_enabled;
151 }
152 #else
153 static inline bool memblock_is_hotpluggable(struct memblock_region *m)
154 {
155         return false;
156 }
157 static inline bool movable_node_is_enabled(void)
158 {
159         return false;
160 }
161 #endif
162
163 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
164 int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
165                             unsigned long  *end_pfn);
166 void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
167                           unsigned long *out_end_pfn, int *out_nid);
168
169 /**
170  * for_each_mem_pfn_range - early memory pfn range iterator
171  * @i: an integer used as loop variable
172  * @nid: node selector, %MAX_NUMNODES for all nodes
173  * @p_start: ptr to ulong for start pfn of the range, can be %NULL
174  * @p_end: ptr to ulong for end pfn of the range, can be %NULL
175  * @p_nid: ptr to int for nid of the range, can be %NULL
176  *
177  * Walks over configured memory ranges.
178  */
179 #define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid)           \
180         for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
181              i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
182 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
183
184 /**
185  * for_each_free_mem_range - iterate through free memblock areas
186  * @i: u64 used as loop variable
187  * @nid: node selector, %NUMA_NO_NODE for all nodes
188  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
189  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
190  * @p_nid: ptr to int for nid of the range, can be %NULL
191  * @flags: pick from blocks based on memory attributes
192  *
193  * Walks over free (memory && !reserved) areas of memblock.  Available as
194  * soon as memblock is initialized.
195  */
196 #define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid)   \
197         for_each_mem_range(i, &memblock.memory, &memblock.reserved,     \
198                            nid, flags, p_start, p_end, p_nid)
199
200 /**
201  * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
202  * @i: u64 used as loop variable
203  * @nid: node selector, %NUMA_NO_NODE for all nodes
204  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
205  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
206  * @p_nid: ptr to int for nid of the range, can be %NULL
207  * @flags: pick from blocks based on memory attributes
208  *
209  * Walks over free (memory && !reserved) areas of memblock in reverse
210  * order.  Available as soon as memblock is initialized.
211  */
212 #define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end,  \
213                                         p_nid)                          \
214         for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
215                                nid, flags, p_start, p_end, p_nid)
216
217 static inline void memblock_set_region_flags(struct memblock_region *r,
218                                              unsigned long flags)
219 {
220         r->flags |= flags;
221 }
222
223 static inline void memblock_clear_region_flags(struct memblock_region *r,
224                                                unsigned long flags)
225 {
226         r->flags &= ~flags;
227 }
228
229 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
230 int memblock_set_node(phys_addr_t base, phys_addr_t size,
231                       struct memblock_type *type, int nid);
232
233 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
234 {
235         r->nid = nid;
236 }
237
238 static inline int memblock_get_region_node(const struct memblock_region *r)
239 {
240         return r->nid;
241 }
242 #else
243 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
244 {
245 }
246
247 static inline int memblock_get_region_node(const struct memblock_region *r)
248 {
249         return 0;
250 }
251 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
252
253 phys_addr_t memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid);
254 phys_addr_t memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
255
256 phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align);
257
258 #ifdef CONFIG_MOVABLE_NODE
259 /*
260  * Set the allocation direction to bottom-up or top-down.
261  */
262 static inline void __init memblock_set_bottom_up(bool enable)
263 {
264         memblock.bottom_up = enable;
265 }
266
267 /*
268  * Check if the allocation direction is bottom-up or not.
269  * if this is true, that said, memblock will allocate memory
270  * in bottom-up direction.
271  */
272 static inline bool memblock_bottom_up(void)
273 {
274         return memblock.bottom_up;
275 }
276 #else
277 static inline void __init memblock_set_bottom_up(bool enable) {}
278 static inline bool memblock_bottom_up(void) { return false; }
279 #endif
280
281 /* Flags for memblock_alloc_base() amd __memblock_alloc_base() */
282 #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
283 #define MEMBLOCK_ALLOC_ACCESSIBLE       0
284
285 phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
286                                         phys_addr_t start, phys_addr_t end,
287                                         ulong flags);
288 phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align,
289                                 phys_addr_t max_addr);
290 phys_addr_t __memblock_alloc_base(phys_addr_t size, phys_addr_t align,
291                                   phys_addr_t max_addr);
292 phys_addr_t memblock_phys_mem_size(void);
293 phys_addr_t memblock_mem_size(unsigned long limit_pfn);
294 phys_addr_t memblock_start_of_DRAM(void);
295 phys_addr_t memblock_end_of_DRAM(void);
296 void memblock_enforce_memory_limit(phys_addr_t memory_limit);
297 int memblock_is_memory(phys_addr_t addr);
298 int memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
299 int memblock_is_reserved(phys_addr_t addr);
300 int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
301
302 extern void __memblock_dump_all(void);
303
304 static inline void memblock_dump_all(void)
305 {
306         if (memblock_debug)
307                 __memblock_dump_all();
308 }
309
310 /**
311  * memblock_set_current_limit - Set the current allocation limit to allow
312  *                         limiting allocations to what is currently
313  *                         accessible during boot
314  * @limit: New limit value (physical address)
315  */
316 void memblock_set_current_limit(phys_addr_t limit);
317
318
319 phys_addr_t memblock_get_current_limit(void);
320
321 /*
322  * pfn conversion functions
323  *
324  * While the memory MEMBLOCKs should always be page aligned, the reserved
325  * MEMBLOCKs may not be. This accessor attempt to provide a very clear
326  * idea of what they return for such non aligned MEMBLOCKs.
327  */
328
329 /**
330  * memblock_region_memory_base_pfn - Return the lowest pfn intersecting with the memory region
331  * @reg: memblock_region structure
332  */
333 static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
334 {
335         return PFN_UP(reg->base);
336 }
337
338 /**
339  * memblock_region_memory_end_pfn - Return the end_pfn this region
340  * @reg: memblock_region structure
341  */
342 static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
343 {
344         return PFN_DOWN(reg->base + reg->size);
345 }
346
347 /**
348  * memblock_region_reserved_base_pfn - Return the lowest pfn intersecting with the reserved region
349  * @reg: memblock_region structure
350  */
351 static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
352 {
353         return PFN_DOWN(reg->base);
354 }
355
356 /**
357  * memblock_region_reserved_end_pfn - Return the end_pfn this region
358  * @reg: memblock_region structure
359  */
360 static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
361 {
362         return PFN_UP(reg->base + reg->size);
363 }
364
365 #define for_each_memblock(memblock_type, region)                                        \
366         for (region = memblock.memblock_type.regions;                           \
367              region < (memblock.memblock_type.regions + memblock.memblock_type.cnt);    \
368              region++)
369
370
371 #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
372 #define __init_memblock __meminit
373 #define __initdata_memblock __meminitdata
374 #else
375 #define __init_memblock
376 #define __initdata_memblock
377 #endif
378
379 #ifdef CONFIG_MEMTEST
380 extern void early_memtest(phys_addr_t start, phys_addr_t end);
381 #else
382 static inline void early_memtest(phys_addr_t start, phys_addr_t end)
383 {
384 }
385 #endif
386
387 #else
388 static inline phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align)
389 {
390         return 0;
391 }
392
393 #endif /* CONFIG_HAVE_MEMBLOCK */
394
395 #endif /* __KERNEL__ */
396
397 #endif /* _LINUX_MEMBLOCK_H */