]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/memblock.h
mm/memblock: add extra "flags" to memblock to allow selection of memory based on...
[mirror_ubuntu-artful-kernel.git] / include / linux / memblock.h
CommitLineData
95f72d1e
YL
1#ifndef _LINUX_MEMBLOCK_H
2#define _LINUX_MEMBLOCK_H
3#ifdef __KERNEL__
4
f0b37fad 5#ifdef CONFIG_HAVE_MEMBLOCK
95f72d1e
YL
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
37d8d4bf 20#define INIT_MEMBLOCK_REGIONS 128
70210ed9 21#define INIT_PHYSMEM_REGIONS 4
95f72d1e 22
66b16edf 23/* Definition of memblock flags. */
fc6daaf9
TL
24enum {
25 MEMBLOCK_NONE = 0x0, /* No special request */
26 MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */
27};
66b16edf 28
e3239ff9 29struct memblock_region {
2898cc4c
BH
30 phys_addr_t base;
31 phys_addr_t size;
66a20757 32 unsigned long flags;
7c0caeb8
TH
33#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
34 int nid;
35#endif
95f72d1e
YL
36};
37
e3239ff9 38struct memblock_type {
bf23c51f
BH
39 unsigned long cnt; /* number of regions */
40 unsigned long max; /* size of the allocated array */
1440c4e2 41 phys_addr_t total_size; /* size of all regions */
bf23c51f 42 struct memblock_region *regions;
95f72d1e
YL
43};
44
45struct memblock {
79442ed1 46 bool bottom_up; /* is bottom up direction? */
2898cc4c 47 phys_addr_t current_limit;
e3239ff9
BH
48 struct memblock_type memory;
49 struct memblock_type reserved;
70210ed9
PH
50#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
51 struct memblock_type physmem;
52#endif
95f72d1e
YL
53};
54
55extern struct memblock memblock;
5e63cf43 56extern int memblock_debug;
55ac590c
TC
57#ifdef CONFIG_MOVABLE_NODE
58/* If movable_node boot option specified */
59extern bool movable_node_enabled;
60#endif /* CONFIG_MOVABLE_NODE */
5e63cf43
YL
61
62#define memblock_dbg(fmt, ...) \
63 if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
95f72d1e 64
87029ee9
GS
65phys_addr_t memblock_find_in_range_node(phys_addr_t size, phys_addr_t align,
66 phys_addr_t start, phys_addr_t end,
fc6daaf9 67 int nid, ulong flags);
fc769a8e
TH
68phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
69 phys_addr_t size, phys_addr_t align);
29f67386 70phys_addr_t get_allocated_memblock_reserved_regions_info(phys_addr_t *addr);
5e270e25 71phys_addr_t get_allocated_memblock_memory_regions_info(phys_addr_t *addr);
1aadc056 72void memblock_allow_resize(void);
7fb0bc3f 73int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
581adcbe
TH
74int memblock_add(phys_addr_t base, phys_addr_t size);
75int memblock_remove(phys_addr_t base, phys_addr_t size);
76int memblock_free(phys_addr_t base, phys_addr_t size);
77int memblock_reserve(phys_addr_t base, phys_addr_t size);
6ede1fd3 78void memblock_trim_memory(phys_addr_t align);
66b16edf
TC
79int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
80int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
f1af9d3a
PH
81
82/* Low level functions */
83int memblock_add_range(struct memblock_type *type,
84 phys_addr_t base, phys_addr_t size,
85 int nid, unsigned long flags);
86
87int memblock_remove_range(struct memblock_type *type,
88 phys_addr_t base,
89 phys_addr_t size);
90
fc6daaf9
TL
91void __next_mem_range(u64 *idx, int nid, ulong flags,
92 struct memblock_type *type_a,
f1af9d3a
PH
93 struct memblock_type *type_b, phys_addr_t *out_start,
94 phys_addr_t *out_end, int *out_nid);
95
fc6daaf9
TL
96void __next_mem_range_rev(u64 *idx, int nid, ulong flags,
97 struct memblock_type *type_a,
f1af9d3a
PH
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
fc6daaf9 108 * @flags: pick from blocks based on memory attributes
f1af9d3a
PH
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 */
fc6daaf9 113#define for_each_mem_range(i, type_a, type_b, nid, flags, \
f1af9d3a 114 p_start, p_end, p_nid) \
fc6daaf9 115 for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \
f1af9d3a
PH
116 p_start, p_end, p_nid); \
117 i != (u64)ULLONG_MAX; \
fc6daaf9 118 __next_mem_range(&i, nid, flags, type_a, type_b, \
f1af9d3a
PH
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
fc6daaf9 128 * @flags: pick from blocks based on memory attributes
f1af9d3a
PH
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 */
fc6daaf9 133#define for_each_mem_range_rev(i, type_a, type_b, nid, flags, \
f1af9d3a
PH
134 p_start, p_end, p_nid) \
135 for (i = (u64)ULLONG_MAX, \
fc6daaf9 136 __next_mem_range_rev(&i, nid, flags, type_a, type_b,\
f1af9d3a
PH
137 p_start, p_end, p_nid); \
138 i != (u64)ULLONG_MAX; \
fc6daaf9 139 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
f1af9d3a
PH
140 p_start, p_end, p_nid))
141
55ac590c
TC
142#ifdef CONFIG_MOVABLE_NODE
143static inline bool memblock_is_hotpluggable(struct memblock_region *m)
144{
145 return m->flags & MEMBLOCK_HOTPLUG;
146}
147
148static inline bool movable_node_is_enabled(void)
149{
150 return movable_node_enabled;
151}
152#else
153static inline bool memblock_is_hotpluggable(struct memblock_region *m)
154{
155 return false;
156}
157static inline bool movable_node_is_enabled(void)
158{
159 return false;
160}
161#endif
e63075a3 162
0ee332c1 163#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
e76b63f8
YL
164int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
165 unsigned long *end_pfn);
0ee332c1
TH
166void __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 *
f2d52fe5 177 * Walks over configured memory ranges.
0ee332c1
TH
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
35fd0808
TH
184/**
185 * for_each_free_mem_range - iterate through free memblock areas
186 * @i: u64 used as loop variable
b1154233 187 * @nid: node selector, %NUMA_NO_NODE for all nodes
35fd0808
TH
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
fc6daaf9 191 * @flags: pick from blocks based on memory attributes
35fd0808
TH
192 *
193 * Walks over free (memory && !reserved) areas of memblock. Available as
194 * soon as memblock is initialized.
195 */
fc6daaf9 196#define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \
f1af9d3a 197 for_each_mem_range(i, &memblock.memory, &memblock.reserved, \
fc6daaf9 198 nid, flags, p_start, p_end, p_nid)
7bd0b0f0
TH
199
200/**
201 * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
202 * @i: u64 used as loop variable
b1154233 203 * @nid: node selector, %NUMA_NO_NODE for all nodes
7bd0b0f0
TH
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
fc6daaf9 207 * @flags: pick from blocks based on memory attributes
7bd0b0f0
TH
208 *
209 * Walks over free (memory && !reserved) areas of memblock in reverse
210 * order. Available as soon as memblock is initialized.
211 */
fc6daaf9
TL
212#define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
213 p_nid) \
f1af9d3a 214 for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
fc6daaf9 215 nid, flags, p_start, p_end, p_nid)
7bd0b0f0 216
66b16edf
TC
217static inline void memblock_set_region_flags(struct memblock_region *r,
218 unsigned long flags)
219{
220 r->flags |= flags;
221}
222
223static inline void memblock_clear_region_flags(struct memblock_region *r,
224 unsigned long flags)
225{
226 r->flags &= ~flags;
227}
228
7c0caeb8 229#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
e7e8de59
TC
230int memblock_set_node(phys_addr_t base, phys_addr_t size,
231 struct memblock_type *type, int nid);
7c0caeb8
TH
232
233static inline void memblock_set_region_node(struct memblock_region *r, int nid)
234{
235 r->nid = nid;
236}
237
238static inline int memblock_get_region_node(const struct memblock_region *r)
239{
240 return r->nid;
241}
242#else
243static inline void memblock_set_region_node(struct memblock_region *r, int nid)
244{
245}
246
247static inline int memblock_get_region_node(const struct memblock_region *r)
248{
249 return 0;
250}
251#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
252
581adcbe
TH
253phys_addr_t memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid);
254phys_addr_t memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
9d1e2492 255
581adcbe 256phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align);
e63075a3 257
79442ed1
TC
258#ifdef CONFIG_MOVABLE_NODE
259/*
260 * Set the allocation direction to bottom-up or top-down.
261 */
2cfb3665 262static inline void __init memblock_set_bottom_up(bool enable)
79442ed1
TC
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 */
272static inline bool memblock_bottom_up(void)
273{
274 return memblock.bottom_up;
275}
276#else
2cfb3665 277static inline void __init memblock_set_bottom_up(bool enable) {}
79442ed1
TC
278static inline bool memblock_bottom_up(void) { return false; }
279#endif
280
e63075a3 281/* Flags for memblock_alloc_base() amd __memblock_alloc_base() */
2898cc4c 282#define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
e63075a3
BH
283#define MEMBLOCK_ALLOC_ACCESSIBLE 0
284
2bfc2862 285phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
fc6daaf9
TL
286 phys_addr_t start, phys_addr_t end,
287 ulong flags);
581adcbe
TH
288phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align,
289 phys_addr_t max_addr);
290phys_addr_t __memblock_alloc_base(phys_addr_t size, phys_addr_t align,
291 phys_addr_t max_addr);
292phys_addr_t memblock_phys_mem_size(void);
595ad9af 293phys_addr_t memblock_mem_size(unsigned long limit_pfn);
581adcbe
TH
294phys_addr_t memblock_start_of_DRAM(void);
295phys_addr_t memblock_end_of_DRAM(void);
296void memblock_enforce_memory_limit(phys_addr_t memory_limit);
297int memblock_is_memory(phys_addr_t addr);
298int memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
299int memblock_is_reserved(phys_addr_t addr);
300int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
301
4ff7b82f
TH
302extern void __memblock_dump_all(void);
303
304static inline void memblock_dump_all(void)
305{
306 if (memblock_debug)
307 __memblock_dump_all();
308}
95f72d1e 309
e63075a3
BH
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 */
581adcbe 316void memblock_set_current_limit(phys_addr_t limit);
e63075a3 317
35a1f0bd 318
fec51014
LA
319phys_addr_t memblock_get_current_limit(void);
320
5b385f25
BH
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/**
c7fc2de0 330 * memblock_region_memory_base_pfn - Return the lowest pfn intersecting with the memory region
5b385f25
BH
331 * @reg: memblock_region structure
332 */
c7fc2de0 333static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
5b385f25 334{
c7fc2de0 335 return PFN_UP(reg->base);
5b385f25
BH
336}
337
338/**
c7fc2de0 339 * memblock_region_memory_end_pfn - Return the end_pfn this region
5b385f25
BH
340 * @reg: memblock_region structure
341 */
c7fc2de0 342static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
5b385f25 343{
c7fc2de0 344 return PFN_DOWN(reg->base + reg->size);
5b385f25
BH
345}
346
347/**
c7fc2de0 348 * memblock_region_reserved_base_pfn - Return the lowest pfn intersecting with the reserved region
5b385f25
BH
349 * @reg: memblock_region structure
350 */
c7fc2de0 351static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
5b385f25 352{
c7fc2de0 353 return PFN_DOWN(reg->base);
5b385f25
BH
354}
355
356/**
c7fc2de0 357 * memblock_region_reserved_end_pfn - Return the end_pfn this region
5b385f25
BH
358 * @reg: memblock_region structure
359 */
c7fc2de0 360static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
5b385f25 361{
c7fc2de0 362 return PFN_UP(reg->base + reg->size);
5b385f25
BH
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
c378ddd5 371#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
67e24bcb
TH
372#define __init_memblock __meminit
373#define __initdata_memblock __meminitdata
10d06439
YL
374#else
375#define __init_memblock
376#define __initdata_memblock
377#endif
378
4a20799d 379#ifdef CONFIG_MEMTEST
7f70baee 380extern void early_memtest(phys_addr_t start, phys_addr_t end);
4a20799d 381#else
7f70baee 382static inline void early_memtest(phys_addr_t start, phys_addr_t end)
4a20799d
VM
383{
384}
385#endif
386
95dde501
YL
387#else
388static inline phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align)
389{
1f5026a7 390 return 0;
95dde501
YL
391}
392
f0b37fad
YL
393#endif /* CONFIG_HAVE_MEMBLOCK */
394
95f72d1e
YL
395#endif /* __KERNEL__ */
396
397#endif /* _LINUX_MEMBLOCK_H */