9d46a7204975083a0d0863a39c846407904f277a
[linux-2.6-block.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
19 #define INIT_MEMBLOCK_REGIONS   128
20 #define INIT_PHYSMEM_REGIONS    4
21
22 /**
23  * enum memblock_flags - definition of memory region attributes
24  * @MEMBLOCK_NONE: no special request
25  * @MEMBLOCK_HOTPLUG: hotpluggable region
26  * @MEMBLOCK_MIRROR: mirrored region
27  * @MEMBLOCK_NOMAP: don't add to kernel direct mapping
28  */
29 enum memblock_flags {
30         MEMBLOCK_NONE           = 0x0,  /* No special request */
31         MEMBLOCK_HOTPLUG        = 0x1,  /* hotpluggable region */
32         MEMBLOCK_MIRROR         = 0x2,  /* mirrored region */
33         MEMBLOCK_NOMAP          = 0x4,  /* don't add to kernel direct mapping */
34 };
35
36 /**
37  * struct memblock_region - represents a memory region
38  * @base: physical address of the region
39  * @size: size of the region
40  * @flags: memory region attributes
41  * @nid: NUMA node id
42  */
43 struct memblock_region {
44         phys_addr_t base;
45         phys_addr_t size;
46         enum memblock_flags flags;
47 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
48         int nid;
49 #endif
50 };
51
52 /**
53  * struct memblock_type - collection of memory regions of certain type
54  * @cnt: number of regions
55  * @max: size of the allocated array
56  * @total_size: size of all regions
57  * @regions: array of regions
58  * @name: the memory type symbolic name
59  */
60 struct memblock_type {
61         unsigned long cnt;
62         unsigned long max;
63         phys_addr_t total_size;
64         struct memblock_region *regions;
65         char *name;
66 };
67
68 /**
69  * struct memblock - memblock allocator metadata
70  * @bottom_up: is bottom up direction?
71  * @current_limit: physical address of the current allocation limit
72  * @memory: usabe memory regions
73  * @reserved: reserved memory regions
74  * @physmem: all physical memory
75  */
76 struct memblock {
77         bool bottom_up;  /* is bottom up direction? */
78         phys_addr_t current_limit;
79         struct memblock_type memory;
80         struct memblock_type reserved;
81 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
82         struct memblock_type physmem;
83 #endif
84 };
85
86 extern struct memblock memblock;
87 extern int memblock_debug;
88
89 #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
90 #define __init_memblock __meminit
91 #define __initdata_memblock __meminitdata
92 void memblock_discard(void);
93 #else
94 #define __init_memblock
95 #define __initdata_memblock
96 #endif
97
98 #define memblock_dbg(fmt, ...) \
99         if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
100
101 phys_addr_t memblock_find_in_range_node(phys_addr_t size, phys_addr_t align,
102                                         phys_addr_t start, phys_addr_t end,
103                                         int nid, enum memblock_flags flags);
104 phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
105                                    phys_addr_t size, phys_addr_t align);
106 void memblock_allow_resize(void);
107 int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
108 int memblock_add(phys_addr_t base, phys_addr_t size);
109 int memblock_remove(phys_addr_t base, phys_addr_t size);
110 int memblock_free(phys_addr_t base, phys_addr_t size);
111 int memblock_reserve(phys_addr_t base, phys_addr_t size);
112 void memblock_trim_memory(phys_addr_t align);
113 bool memblock_overlaps_region(struct memblock_type *type,
114                               phys_addr_t base, phys_addr_t size);
115 int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
116 int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
117 int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
118 int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
119 int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
120 enum memblock_flags choose_memblock_flags(void);
121
122 /* Low level functions */
123 int memblock_add_range(struct memblock_type *type,
124                        phys_addr_t base, phys_addr_t size,
125                        int nid, enum memblock_flags flags);
126
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);
131
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);
136
137 void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start,
138                                 phys_addr_t *out_end);
139
140 void __memblock_free_early(phys_addr_t base, phys_addr_t size);
141 void __memblock_free_late(phys_addr_t base, phys_addr_t size);
142
143 /**
144  * for_each_mem_range - iterate through memblock areas from type_a and not
145  * included in type_b. Or just type_a if type_b is NULL.
146  * @i: u64 used as loop variable
147  * @type_a: ptr to memblock_type to iterate
148  * @type_b: ptr to memblock_type which excludes from the iteration
149  * @nid: node selector, %NUMA_NO_NODE for all nodes
150  * @flags: pick from blocks based on memory attributes
151  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
152  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
153  * @p_nid: ptr to int for nid of the range, can be %NULL
154  */
155 #define for_each_mem_range(i, type_a, type_b, nid, flags,               \
156                            p_start, p_end, p_nid)                       \
157         for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b,    \
158                                      p_start, p_end, p_nid);            \
159              i != (u64)ULLONG_MAX;                                      \
160              __next_mem_range(&i, nid, flags, type_a, type_b,           \
161                               p_start, p_end, p_nid))
162
163 /**
164  * for_each_mem_range_rev - reverse iterate through memblock areas from
165  * type_a and not 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
174  */
175 #define for_each_mem_range_rev(i, type_a, type_b, nid, flags,           \
176                                p_start, p_end, p_nid)                   \
177         for (i = (u64)ULLONG_MAX,                                       \
178                      __next_mem_range_rev(&i, nid, flags, type_a, type_b,\
179                                           p_start, p_end, p_nid);       \
180              i != (u64)ULLONG_MAX;                                      \
181              __next_mem_range_rev(&i, nid, flags, type_a, type_b,       \
182                                   p_start, p_end, p_nid))
183
184 /**
185  * for_each_reserved_mem_region - iterate over all reserved memblock areas
186  * @i: u64 used as loop variable
187  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
188  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
189  *
190  * Walks over reserved areas of memblock. Available as soon as memblock
191  * is initialized.
192  */
193 #define for_each_reserved_mem_region(i, p_start, p_end)                 \
194         for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end);   \
195              i != (u64)ULLONG_MAX;                                      \
196              __next_reserved_mem_region(&i, p_start, p_end))
197
198 static inline bool memblock_is_hotpluggable(struct memblock_region *m)
199 {
200         return m->flags & MEMBLOCK_HOTPLUG;
201 }
202
203 static inline bool memblock_is_mirror(struct memblock_region *m)
204 {
205         return m->flags & MEMBLOCK_MIRROR;
206 }
207
208 static inline bool memblock_is_nomap(struct memblock_region *m)
209 {
210         return m->flags & MEMBLOCK_NOMAP;
211 }
212
213 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
214 int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
215                             unsigned long  *end_pfn);
216 void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
217                           unsigned long *out_end_pfn, int *out_nid);
218
219 /**
220  * for_each_mem_pfn_range - early memory pfn range iterator
221  * @i: an integer used as loop variable
222  * @nid: node selector, %MAX_NUMNODES for all nodes
223  * @p_start: ptr to ulong for start pfn of the range, can be %NULL
224  * @p_end: ptr to ulong for end pfn of the range, can be %NULL
225  * @p_nid: ptr to int for nid of the range, can be %NULL
226  *
227  * Walks over configured memory ranges.
228  */
229 #define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid)           \
230         for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
231              i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
232 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
233
234 /**
235  * for_each_free_mem_range - iterate through free memblock areas
236  * @i: u64 used as loop variable
237  * @nid: node selector, %NUMA_NO_NODE for all nodes
238  * @flags: pick from blocks based on memory attributes
239  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
240  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
241  * @p_nid: ptr to int for nid of the range, can be %NULL
242  *
243  * Walks over free (memory && !reserved) areas of memblock.  Available as
244  * soon as memblock is initialized.
245  */
246 #define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid)   \
247         for_each_mem_range(i, &memblock.memory, &memblock.reserved,     \
248                            nid, flags, p_start, p_end, p_nid)
249
250 /**
251  * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
252  * @i: u64 used as loop variable
253  * @nid: node selector, %NUMA_NO_NODE for all nodes
254  * @flags: pick from blocks based on memory attributes
255  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
256  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
257  * @p_nid: ptr to int for nid of the range, can be %NULL
258  *
259  * Walks over free (memory && !reserved) areas of memblock in reverse
260  * order.  Available as soon as memblock is initialized.
261  */
262 #define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end,  \
263                                         p_nid)                          \
264         for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
265                                nid, flags, p_start, p_end, p_nid)
266
267 static inline void memblock_set_region_flags(struct memblock_region *r,
268                                              enum memblock_flags flags)
269 {
270         r->flags |= flags;
271 }
272
273 static inline void memblock_clear_region_flags(struct memblock_region *r,
274                                                enum memblock_flags flags)
275 {
276         r->flags &= ~flags;
277 }
278
279 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
280 int memblock_set_node(phys_addr_t base, phys_addr_t size,
281                       struct memblock_type *type, int nid);
282
283 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
284 {
285         r->nid = nid;
286 }
287
288 static inline int memblock_get_region_node(const struct memblock_region *r)
289 {
290         return r->nid;
291 }
292 #else
293 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
294 {
295 }
296
297 static inline int memblock_get_region_node(const struct memblock_region *r)
298 {
299         return 0;
300 }
301 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
302
303 phys_addr_t memblock_phys_alloc_nid(phys_addr_t size, phys_addr_t align, int nid);
304 phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
305
306 phys_addr_t memblock_phys_alloc(phys_addr_t size, phys_addr_t align);
307
308 /*
309  * Set the allocation direction to bottom-up or top-down.
310  */
311 static inline void __init memblock_set_bottom_up(bool enable)
312 {
313         memblock.bottom_up = enable;
314 }
315
316 /*
317  * Check if the allocation direction is bottom-up or not.
318  * if this is true, that said, memblock will allocate memory
319  * in bottom-up direction.
320  */
321 static inline bool memblock_bottom_up(void)
322 {
323         return memblock.bottom_up;
324 }
325
326 /* Flags for memblock_alloc_base() amd __memblock_alloc_base() */
327 #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
328 #define MEMBLOCK_ALLOC_ACCESSIBLE       0
329
330 phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
331                                         phys_addr_t start, phys_addr_t end,
332                                         enum memblock_flags flags);
333 phys_addr_t memblock_alloc_base_nid(phys_addr_t size,
334                                         phys_addr_t align, phys_addr_t max_addr,
335                                         int nid, enum memblock_flags flags);
336 phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align,
337                                 phys_addr_t max_addr);
338 phys_addr_t __memblock_alloc_base(phys_addr_t size, phys_addr_t align,
339                                   phys_addr_t max_addr);
340 phys_addr_t memblock_phys_mem_size(void);
341 phys_addr_t memblock_reserved_size(void);
342 phys_addr_t memblock_mem_size(unsigned long limit_pfn);
343 phys_addr_t memblock_start_of_DRAM(void);
344 phys_addr_t memblock_end_of_DRAM(void);
345 void memblock_enforce_memory_limit(phys_addr_t memory_limit);
346 void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
347 void memblock_mem_limit_remove_map(phys_addr_t limit);
348 bool memblock_is_memory(phys_addr_t addr);
349 bool memblock_is_map_memory(phys_addr_t addr);
350 bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
351 bool memblock_is_reserved(phys_addr_t addr);
352 bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
353
354 extern void __memblock_dump_all(void);
355
356 static inline void memblock_dump_all(void)
357 {
358         if (memblock_debug)
359                 __memblock_dump_all();
360 }
361
362 /**
363  * memblock_set_current_limit - Set the current allocation limit to allow
364  *                         limiting allocations to what is currently
365  *                         accessible during boot
366  * @limit: New limit value (physical address)
367  */
368 void memblock_set_current_limit(phys_addr_t limit);
369
370
371 phys_addr_t memblock_get_current_limit(void);
372
373 /*
374  * pfn conversion functions
375  *
376  * While the memory MEMBLOCKs should always be page aligned, the reserved
377  * MEMBLOCKs may not be. This accessor attempt to provide a very clear
378  * idea of what they return for such non aligned MEMBLOCKs.
379  */
380
381 /**
382  * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
383  * @reg: memblock_region structure
384  *
385  * Return: the lowest pfn intersecting with the memory region
386  */
387 static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
388 {
389         return PFN_UP(reg->base);
390 }
391
392 /**
393  * memblock_region_memory_end_pfn - get the end pfn of the memory region
394  * @reg: memblock_region structure
395  *
396  * Return: the end_pfn of the reserved region
397  */
398 static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
399 {
400         return PFN_DOWN(reg->base + reg->size);
401 }
402
403 /**
404  * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
405  * @reg: memblock_region structure
406  *
407  * Return: the lowest pfn intersecting with the reserved region
408  */
409 static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
410 {
411         return PFN_DOWN(reg->base);
412 }
413
414 /**
415  * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
416  * @reg: memblock_region structure
417  *
418  * Return: the end_pfn of the reserved region
419  */
420 static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
421 {
422         return PFN_UP(reg->base + reg->size);
423 }
424
425 #define for_each_memblock(memblock_type, region)                                        \
426         for (region = memblock.memblock_type.regions;                                   \
427              region < (memblock.memblock_type.regions + memblock.memblock_type.cnt);    \
428              region++)
429
430 #define for_each_memblock_type(i, memblock_type, rgn)                   \
431         for (i = 0, rgn = &memblock_type->regions[0];                   \
432              i < memblock_type->cnt;                                    \
433              i++, rgn = &memblock_type->regions[i])
434
435 #ifdef CONFIG_MEMTEST
436 extern void early_memtest(phys_addr_t start, phys_addr_t end);
437 #else
438 static inline void early_memtest(phys_addr_t start, phys_addr_t end)
439 {
440 }
441 #endif
442
443 #endif /* __KERNEL__ */
444
445 #endif /* _LINUX_MEMBLOCK_H */