1 #ifndef _LINUX_MEMBLOCK_H
2 #define _LINUX_MEMBLOCK_H
5 #ifdef CONFIG_HAVE_MEMBLOCK
7 * Logical memory blocks.
9 * Copyright (C) 2001 Peter Bergner, IBM Corp.
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.
17 #include <linux/init.h>
20 #define INIT_MEMBLOCK_REGIONS 128
21 #define INIT_PHYSMEM_REGIONS 4
23 /* Definition of memblock flags. */
25 MEMBLOCK_NONE = 0x0, /* No special request */
26 MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */
29 struct memblock_region {
33 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
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;
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;
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 */
62 #define memblock_dbg(fmt, ...) \
63 if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
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);
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);
87 int memblock_remove_range(struct memblock_type *type,
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);
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);
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
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))
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
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))
142 #ifdef CONFIG_MOVABLE_NODE
143 static inline bool memblock_is_hotpluggable(struct memblock_region *m)
145 return m->flags & MEMBLOCK_HOTPLUG;
148 static inline bool movable_node_is_enabled(void)
150 return movable_node_enabled;
153 static inline bool memblock_is_hotpluggable(struct memblock_region *m)
157 static inline bool movable_node_is_enabled(void)
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);
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
177 * Walks over configured memory ranges.
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 */
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
193 * Walks over free (memory && !reserved) areas of memblock. Available as
194 * soon as memblock is initialized.
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)
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
209 * Walks over free (memory && !reserved) areas of memblock in reverse
210 * order. Available as soon as memblock is initialized.
212 #define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
214 for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
215 nid, flags, p_start, p_end, p_nid)
217 static inline void memblock_set_region_flags(struct memblock_region *r,
223 static inline void memblock_clear_region_flags(struct memblock_region *r,
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);
233 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
238 static inline int memblock_get_region_node(const struct memblock_region *r)
243 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
247 static inline int memblock_get_region_node(const struct memblock_region *r)
251 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
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);
256 phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align);
258 #ifdef CONFIG_MOVABLE_NODE
260 * Set the allocation direction to bottom-up or top-down.
262 static inline void __init memblock_set_bottom_up(bool enable)
264 memblock.bottom_up = enable;
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.
272 static inline bool memblock_bottom_up(void)
274 return memblock.bottom_up;
277 static inline void __init memblock_set_bottom_up(bool enable) {}
278 static inline bool memblock_bottom_up(void) { return false; }
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
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,
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);
302 extern void __memblock_dump_all(void);
304 static inline void memblock_dump_all(void)
307 __memblock_dump_all();
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)
316 void memblock_set_current_limit(phys_addr_t limit);
319 phys_addr_t memblock_get_current_limit(void);
322 * pfn conversion functions
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.
330 * memblock_region_memory_base_pfn - Return the lowest pfn intersecting with the memory region
331 * @reg: memblock_region structure
333 static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
335 return PFN_UP(reg->base);
339 * memblock_region_memory_end_pfn - Return the end_pfn this region
340 * @reg: memblock_region structure
342 static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
344 return PFN_DOWN(reg->base + reg->size);
348 * memblock_region_reserved_base_pfn - Return the lowest pfn intersecting with the reserved region
349 * @reg: memblock_region structure
351 static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
353 return PFN_DOWN(reg->base);
357 * memblock_region_reserved_end_pfn - Return the end_pfn this region
358 * @reg: memblock_region structure
360 static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
362 return PFN_UP(reg->base + reg->size);
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); \
371 #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
372 #define __init_memblock __meminit
373 #define __initdata_memblock __meminitdata
375 #define __init_memblock
376 #define __initdata_memblock
379 #ifdef CONFIG_MEMTEST
380 extern void early_memtest(phys_addr_t start, phys_addr_t end);
382 static inline void early_memtest(phys_addr_t start, phys_addr_t end)
388 static inline phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align)
393 #endif /* CONFIG_HAVE_MEMBLOCK */
395 #endif /* __KERNEL__ */
397 #endif /* _LINUX_MEMBLOCK_H */