blob: f669016874b3986d17b5ce924958b48403ac1bd7 [file] [log] [blame]
Yinghai Lu95f72d12010-07-12 14:36:09 +10001#ifndef _LINUX_MEMBLOCK_H
2#define _LINUX_MEMBLOCK_H
3#ifdef __KERNEL__
4
Yinghai Luf0b37fad2010-07-28 15:28:21 +10005#ifdef CONFIG_HAVE_MEMBLOCK
Yinghai Lu95f72d12010-07-12 14:36:09 +10006/*
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
Yinghai Lu37d8d4b2010-07-28 15:20:58 +100020#define INIT_MEMBLOCK_REGIONS 128
Yinghai Lu95f72d12010-07-12 14:36:09 +100021
Tang Chen66b16ed2014-01-21 15:49:23 -080022/* Definition of memblock flags. */
23#define MEMBLOCK_HOTPLUG 0x1 /* hotpluggable region */
24
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100025struct memblock_region {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100026 phys_addr_t base;
27 phys_addr_t size;
Tang Chen66a20752014-01-21 15:49:20 -080028 unsigned long flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +020029#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
30 int nid;
31#endif
Yinghai Lu95f72d12010-07-12 14:36:09 +100032};
33
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100034struct memblock_type {
Benjamin Herrenschmidtbf23c512010-07-06 15:39:06 -070035 unsigned long cnt; /* number of regions */
36 unsigned long max; /* size of the allocated array */
Tejun Heo1440c4e2011-12-08 10:22:08 -080037 phys_addr_t total_size; /* size of all regions */
Benjamin Herrenschmidtbf23c512010-07-06 15:39:06 -070038 struct memblock_region *regions;
Yinghai Lu95f72d12010-07-12 14:36:09 +100039};
40
41struct memblock {
Tang Chen79442ed2013-11-12 15:07:59 -080042 bool bottom_up; /* is bottom up direction? */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100043 phys_addr_t current_limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100044 struct memblock_type memory;
45 struct memblock_type reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +100046};
47
48extern struct memblock memblock;
Yinghai Lu5e63cf42010-07-28 15:07:21 +100049extern int memblock_debug;
Tang Chen55ac5902014-01-21 15:49:35 -080050#ifdef CONFIG_MOVABLE_NODE
51/* If movable_node boot option specified */
52extern bool movable_node_enabled;
53#endif /* CONFIG_MOVABLE_NODE */
Yinghai Lu5e63cf42010-07-28 15:07:21 +100054
55#define memblock_dbg(fmt, ...) \
56 if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
Yinghai Lu95f72d12010-07-12 14:36:09 +100057
Grygorii Strashko87029ee2014-01-21 15:50:14 -080058phys_addr_t memblock_find_in_range_node(phys_addr_t size, phys_addr_t align,
59 phys_addr_t start, phys_addr_t end,
60 int nid);
Tejun Heofc769a82011-07-12 09:58:10 +020061phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
62 phys_addr_t size, phys_addr_t align);
Yinghai Lu29f67382012-07-11 14:02:56 -070063phys_addr_t get_allocated_memblock_reserved_regions_info(phys_addr_t *addr);
Philipp Hachtmann5e270e22014-01-23 15:53:11 -080064phys_addr_t get_allocated_memblock_memory_regions_info(phys_addr_t *addr);
Tejun Heo1aadc052011-12-08 10:22:08 -080065void memblock_allow_resize(void);
Tejun Heo7fb0bc32011-12-08 10:22:08 -080066int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
Tejun Heo581adcb2011-12-08 10:22:06 -080067int memblock_add(phys_addr_t base, phys_addr_t size);
68int memblock_remove(phys_addr_t base, phys_addr_t size);
69int memblock_free(phys_addr_t base, phys_addr_t size);
70int memblock_reserve(phys_addr_t base, phys_addr_t size);
Yinghai Lu6ede1fd2012-10-22 16:35:18 -070071void memblock_trim_memory(phys_addr_t align);
Tang Chen66b16ed2014-01-21 15:49:23 -080072int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
73int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +010074
75/* Low level functions */
76int memblock_add_range(struct memblock_type *type,
77 phys_addr_t base, phys_addr_t size,
78 int nid, unsigned long flags);
79
80int memblock_remove_range(struct memblock_type *type,
81 phys_addr_t base,
82 phys_addr_t size);
83
84void __next_mem_range(u64 *idx, int nid, struct memblock_type *type_a,
85 struct memblock_type *type_b, phys_addr_t *out_start,
86 phys_addr_t *out_end, int *out_nid);
87
88void __next_mem_range_rev(u64 *idx, int nid, struct memblock_type *type_a,
89 struct memblock_type *type_b, phys_addr_t *out_start,
90 phys_addr_t *out_end, int *out_nid);
91
92/**
93 * for_each_mem_range - iterate through memblock areas from type_a and not
94 * included in type_b. Or just type_a if type_b is NULL.
95 * @i: u64 used as loop variable
96 * @type_a: ptr to memblock_type to iterate
97 * @type_b: ptr to memblock_type which excludes from the iteration
98 * @nid: node selector, %NUMA_NO_NODE for all nodes
99 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
100 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
101 * @p_nid: ptr to int for nid of the range, can be %NULL
102 */
103#define for_each_mem_range(i, type_a, type_b, nid, \
104 p_start, p_end, p_nid) \
105 for (i = 0, __next_mem_range(&i, nid, type_a, type_b, \
106 p_start, p_end, p_nid); \
107 i != (u64)ULLONG_MAX; \
108 __next_mem_range(&i, nid, type_a, type_b, \
109 p_start, p_end, p_nid))
110
111/**
112 * for_each_mem_range_rev - reverse iterate through memblock areas from
113 * type_a and not included in type_b. Or just type_a if type_b is NULL.
114 * @i: u64 used as loop variable
115 * @type_a: ptr to memblock_type to iterate
116 * @type_b: ptr to memblock_type which excludes from the iteration
117 * @nid: node selector, %NUMA_NO_NODE for all nodes
118 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
119 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
120 * @p_nid: ptr to int for nid of the range, can be %NULL
121 */
122#define for_each_mem_range_rev(i, type_a, type_b, nid, \
123 p_start, p_end, p_nid) \
124 for (i = (u64)ULLONG_MAX, \
125 __next_mem_range_rev(&i, nid, type_a, type_b, \
126 p_start, p_end, p_nid); \
127 i != (u64)ULLONG_MAX; \
128 __next_mem_range_rev(&i, nid, type_a, type_b, \
129 p_start, p_end, p_nid))
130
Tang Chen55ac5902014-01-21 15:49:35 -0800131#ifdef CONFIG_MOVABLE_NODE
132static inline bool memblock_is_hotpluggable(struct memblock_region *m)
133{
134 return m->flags & MEMBLOCK_HOTPLUG;
135}
136
137static inline bool movable_node_is_enabled(void)
138{
139 return movable_node_enabled;
140}
141#else
142static inline bool memblock_is_hotpluggable(struct memblock_region *m)
143{
144 return false;
145}
146static inline bool movable_node_is_enabled(void)
147{
148 return false;
149}
150#endif
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700151
Tejun Heo0ee332c2011-12-08 10:22:09 -0800152#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Yinghai Lue76b63f2013-09-11 14:22:17 -0700153int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
154 unsigned long *end_pfn);
Tejun Heo0ee332c2011-12-08 10:22:09 -0800155void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
156 unsigned long *out_end_pfn, int *out_nid);
157
158/**
159 * for_each_mem_pfn_range - early memory pfn range iterator
160 * @i: an integer used as loop variable
161 * @nid: node selector, %MAX_NUMNODES for all nodes
162 * @p_start: ptr to ulong for start pfn of the range, can be %NULL
163 * @p_end: ptr to ulong for end pfn of the range, can be %NULL
164 * @p_nid: ptr to int for nid of the range, can be %NULL
165 *
Wanpeng Lif2d52fe2012-10-08 16:32:24 -0700166 * Walks over configured memory ranges.
Tejun Heo0ee332c2011-12-08 10:22:09 -0800167 */
168#define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
169 for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
170 i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
171#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
172
Tejun Heo35fd0802011-07-12 11:15:59 +0200173/**
174 * for_each_free_mem_range - iterate through free memblock areas
175 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800176 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tejun Heo35fd0802011-07-12 11:15:59 +0200177 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
178 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
179 * @p_nid: ptr to int for nid of the range, can be %NULL
180 *
181 * Walks over free (memory && !reserved) areas of memblock. Available as
182 * soon as memblock is initialized.
183 */
184#define for_each_free_mem_range(i, nid, p_start, p_end, p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100185 for_each_mem_range(i, &memblock.memory, &memblock.reserved, \
186 nid, p_start, p_end, p_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800187
188/**
189 * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
190 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800191 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800192 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
193 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
194 * @p_nid: ptr to int for nid of the range, can be %NULL
195 *
196 * Walks over free (memory && !reserved) areas of memblock in reverse
197 * order. Available as soon as memblock is initialized.
198 */
199#define for_each_free_mem_range_reverse(i, nid, p_start, p_end, p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100200 for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
201 nid, p_start, p_end, p_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800202
Tang Chen66b16ed2014-01-21 15:49:23 -0800203static inline void memblock_set_region_flags(struct memblock_region *r,
204 unsigned long flags)
205{
206 r->flags |= flags;
207}
208
209static inline void memblock_clear_region_flags(struct memblock_region *r,
210 unsigned long flags)
211{
212 r->flags &= ~flags;
213}
214
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200215#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Tang Chene7e8de52014-01-21 15:49:26 -0800216int memblock_set_node(phys_addr_t base, phys_addr_t size,
217 struct memblock_type *type, int nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200218
219static inline void memblock_set_region_node(struct memblock_region *r, int nid)
220{
221 r->nid = nid;
222}
223
224static inline int memblock_get_region_node(const struct memblock_region *r)
225{
226 return r->nid;
227}
228#else
229static inline void memblock_set_region_node(struct memblock_region *r, int nid)
230{
231}
232
233static inline int memblock_get_region_node(const struct memblock_region *r)
234{
235 return 0;
236}
237#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
238
Tejun Heo581adcb2011-12-08 10:22:06 -0800239phys_addr_t memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid);
240phys_addr_t memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700241
Tejun Heo581adcb2011-12-08 10:22:06 -0800242phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align);
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700243
Tang Chen79442ed2013-11-12 15:07:59 -0800244#ifdef CONFIG_MOVABLE_NODE
245/*
246 * Set the allocation direction to bottom-up or top-down.
247 */
248static inline void memblock_set_bottom_up(bool enable)
249{
250 memblock.bottom_up = enable;
251}
252
253/*
254 * Check if the allocation direction is bottom-up or not.
255 * if this is true, that said, memblock will allocate memory
256 * in bottom-up direction.
257 */
258static inline bool memblock_bottom_up(void)
259{
260 return memblock.bottom_up;
261}
262#else
263static inline void memblock_set_bottom_up(bool enable) {}
264static inline bool memblock_bottom_up(void) { return false; }
265#endif
266
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700267/* Flags for memblock_alloc_base() amd __memblock_alloc_base() */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000268#define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700269#define MEMBLOCK_ALLOC_ACCESSIBLE 0
270
Tejun Heo581adcb2011-12-08 10:22:06 -0800271phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align,
272 phys_addr_t max_addr);
273phys_addr_t __memblock_alloc_base(phys_addr_t size, phys_addr_t align,
274 phys_addr_t max_addr);
275phys_addr_t memblock_phys_mem_size(void);
Yinghai Lu595ad9a2013-01-24 12:20:09 -0800276phys_addr_t memblock_mem_size(unsigned long limit_pfn);
Tejun Heo581adcb2011-12-08 10:22:06 -0800277phys_addr_t memblock_start_of_DRAM(void);
278phys_addr_t memblock_end_of_DRAM(void);
279void memblock_enforce_memory_limit(phys_addr_t memory_limit);
280int memblock_is_memory(phys_addr_t addr);
281int memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
282int memblock_is_reserved(phys_addr_t addr);
283int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000284
Tejun Heo4ff7b822011-12-08 10:22:06 -0800285extern void __memblock_dump_all(void);
286
287static inline void memblock_dump_all(void)
288{
289 if (memblock_debug)
290 __memblock_dump_all();
291}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000292
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700293/**
294 * memblock_set_current_limit - Set the current allocation limit to allow
295 * limiting allocations to what is currently
296 * accessible during boot
297 * @limit: New limit value (physical address)
298 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800299void memblock_set_current_limit(phys_addr_t limit);
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700300
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700301
Laura Abbottfec51012014-02-27 01:23:43 +0100302phys_addr_t memblock_get_current_limit(void);
303
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000304/*
305 * pfn conversion functions
306 *
307 * While the memory MEMBLOCKs should always be page aligned, the reserved
308 * MEMBLOCKs may not be. This accessor attempt to provide a very clear
309 * idea of what they return for such non aligned MEMBLOCKs.
310 */
311
312/**
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700313 * memblock_region_memory_base_pfn - Return the lowest pfn intersecting with the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000314 * @reg: memblock_region structure
315 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700316static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000317{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700318 return PFN_UP(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000319}
320
321/**
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700322 * memblock_region_memory_end_pfn - Return the end_pfn this region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000323 * @reg: memblock_region structure
324 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700325static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000326{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700327 return PFN_DOWN(reg->base + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000328}
329
330/**
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700331 * memblock_region_reserved_base_pfn - Return the lowest pfn intersecting with the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000332 * @reg: memblock_region structure
333 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700334static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000335{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700336 return PFN_DOWN(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000337}
338
339/**
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700340 * memblock_region_reserved_end_pfn - Return the end_pfn this region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000341 * @reg: memblock_region structure
342 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700343static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000344{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700345 return PFN_UP(reg->base + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000346}
347
348#define for_each_memblock(memblock_type, region) \
349 for (region = memblock.memblock_type.regions; \
350 region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \
351 region++)
352
353
Tejun Heoc378ddd2011-07-14 11:46:03 +0200354#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
Tejun Heo67e24bc2011-07-14 11:42:03 +0200355#define __init_memblock __meminit
356#define __initdata_memblock __meminitdata
Yinghai Lu10d06432010-07-28 15:43:02 +1000357#else
358#define __init_memblock
359#define __initdata_memblock
360#endif
361
Yinghai Lu95dde502011-05-24 17:13:19 -0700362#else
363static inline phys_addr_t memblock_alloc(phys_addr_t size, phys_addr_t align)
364{
Tejun Heo1f5026a2011-07-12 09:58:09 +0200365 return 0;
Yinghai Lu95dde502011-05-24 17:13:19 -0700366}
367
Yinghai Luf0b37fad2010-07-28 15:28:21 +1000368#endif /* CONFIG_HAVE_MEMBLOCK */
369
Yinghai Lu95f72d12010-07-12 14:36:09 +1000370#endif /* __KERNEL__ */
371
372#endif /* _LINUX_MEMBLOCK_H */