1 #ifndef _LINUX_MEMBLOCK_H
2 #define _LINUX_MEMBLOCK_H
6 * Logical memory blocks.
8 * Copyright (C) 2001 Peter Bergner, IBM Corp.
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.
16 #include <linux/init.h>
19 #define INIT_MEMBLOCK_REGIONS 128
20 #define INIT_PHYSMEM_REGIONS 4
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
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 */
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
43 struct memblock_region {
46 enum memblock_flags flags;
47 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
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
60 struct memblock_type {
63 phys_addr_t total_size;
64 struct memblock_region *regions;
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
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;
86 extern struct memblock memblock;
87 extern int memblock_debug;
89 #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
90 #define __init_memblock __meminit
91 #define __initdata_memblock __meminitdata
92 void memblock_discard(void);
94 #define __init_memblock
95 #define __initdata_memblock
98 #define memblock_dbg(fmt, ...) \
99 if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
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);
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);
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);
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);
137 void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start,
138 phys_addr_t *out_end);
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);
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
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))
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
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))
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
190 * Walks over reserved areas of memblock. Available as soon as memblock
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))
198 static inline bool memblock_is_hotpluggable(struct memblock_region *m)
200 return m->flags & MEMBLOCK_HOTPLUG;
203 static inline bool memblock_is_mirror(struct memblock_region *m)
205 return m->flags & MEMBLOCK_MIRROR;
208 static inline bool memblock_is_nomap(struct memblock_region *m)
210 return m->flags & MEMBLOCK_NOMAP;
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);
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
227 * Walks over configured memory ranges.
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 */
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
243 * Walks over free (memory && !reserved) areas of memblock. Available as
244 * soon as memblock is initialized.
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)
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
259 * Walks over free (memory && !reserved) areas of memblock in reverse
260 * order. Available as soon as memblock is initialized.
262 #define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
264 for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
265 nid, flags, p_start, p_end, p_nid)
267 static inline void memblock_set_region_flags(struct memblock_region *r,
268 enum memblock_flags flags)
273 static inline void memblock_clear_region_flags(struct memblock_region *r,
274 enum memblock_flags flags)
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);
283 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
288 static inline int memblock_get_region_node(const struct memblock_region *r)
293 static inline void memblock_set_region_node(struct memblock_region *r, int nid)
297 static inline int memblock_get_region_node(const struct memblock_region *r)
301 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
303 phys_addr_t memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid);
304 phys_addr_t memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
306 phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align);
309 * Set the allocation direction to bottom-up or top-down.
311 static inline void __init memblock_set_bottom_up(bool enable)
313 memblock.bottom_up = enable;
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.
321 static inline bool memblock_bottom_up(void)
323 return memblock.bottom_up;
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
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);
354 extern void __memblock_dump_all(void);
356 static inline void memblock_dump_all(void)
359 __memblock_dump_all();
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)
368 void memblock_set_current_limit(phys_addr_t limit);
371 phys_addr_t memblock_get_current_limit(void);
374 * pfn conversion functions
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.
382 * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
383 * @reg: memblock_region structure
385 * Return: the lowest pfn intersecting with the memory region
387 static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
389 return PFN_UP(reg->base);
393 * memblock_region_memory_end_pfn - get the end pfn of the memory region
394 * @reg: memblock_region structure
396 * Return: the end_pfn of the reserved region
398 static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
400 return PFN_DOWN(reg->base + reg->size);
404 * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
405 * @reg: memblock_region structure
407 * Return: the lowest pfn intersecting with the reserved region
409 static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
411 return PFN_DOWN(reg->base);
415 * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
416 * @reg: memblock_region structure
418 * Return: the end_pfn of the reserved region
420 static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
422 return PFN_UP(reg->base + reg->size);
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); \
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])
435 #ifdef CONFIG_MEMTEST
436 extern void early_memtest(phys_addr_t start, phys_addr_t end);
438 static inline void early_memtest(phys_addr_t start, phys_addr_t end)
443 #endif /* __KERNEL__ */
445 #endif /* _LINUX_MEMBLOCK_H */