drivers/base/node: rename link_mem_sections() to register_memory_block_under_node()
[linux-2.6-block.git] / include / linux / memory_hotplug.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
208d54e5
DH
2#ifndef __LINUX_MEMORY_HOTPLUG_H
3#define __LINUX_MEMORY_HOTPLUG_H
4
5#include <linux/mmzone.h>
6#include <linux/spinlock.h>
3947be19 7#include <linux/notifier.h>
187f1882 8#include <linux/bug.h>
208d54e5 9
78679302
KH
10struct page;
11struct zone;
12struct pglist_data;
ea01ea93 13struct mem_section;
e90bdb7f 14struct memory_block;
836809ec 15struct memory_group;
62cedb9f 16struct resource;
24e6d5a5 17struct vmem_altmap;
78679302 18
e930d999
MH
19#ifdef CONFIG_HAVE_ARCH_NODEDATA_EXTENSION
20/*
21 * For supporting node-hotadd, we have to allocate a new pgdat.
22 *
23 * If an arch has generic style NODE_DATA(),
24 * node_data[nid] = kzalloc() works well. But it depends on the architecture.
25 *
26 * In general, generic_alloc_nodedata() is used.
e930d999
MH
27 *
28 */
29extern pg_data_t *arch_alloc_nodedata(int nid);
e930d999
MH
30extern void arch_refresh_nodedata(int nid, pg_data_t *pgdat);
31
32#else /* CONFIG_HAVE_ARCH_NODEDATA_EXTENSION */
33
34#define arch_alloc_nodedata(nid) generic_alloc_nodedata(nid)
e930d999
MH
35
36#ifdef CONFIG_NUMA
37/*
38 * XXX: node aware allocation can't work well to get new node's memory at this time.
39 * Because, pgdat for the new node is not allocated/initialized yet itself.
40 * To use new node's memory, more consideration will be necessary.
41 */
42#define generic_alloc_nodedata(nid) \
43({ \
09f49dca 44 memblock_alloc(sizeof(*pgdat), SMP_CACHE_BYTES); \
e930d999
MH
45})
46/*
47 * This definition is just for error path in node hotadd.
48 * For node hotremove, we have to replace this.
49 */
50#define generic_free_nodedata(pgdat) kfree(pgdat)
51
52extern pg_data_t *node_data[];
53static inline void arch_refresh_nodedata(int nid, pg_data_t *pgdat)
54{
55 node_data[nid] = pgdat;
56}
57
58#else /* !CONFIG_NUMA */
59
60/* never called */
61static inline pg_data_t *generic_alloc_nodedata(int nid)
62{
63 BUG();
64 return NULL;
65}
66static inline void generic_free_nodedata(pg_data_t *pgdat)
67{
68}
69static inline void arch_refresh_nodedata(int nid, pg_data_t *pgdat)
70{
71}
72#endif /* CONFIG_NUMA */
73#endif /* CONFIG_HAVE_ARCH_NODEDATA_EXTENSION */
74
208d54e5 75#ifdef CONFIG_MEMORY_HOTPLUG
9f605f26 76struct page *pfn_to_online_page(unsigned long pfn);
04753278 77
4f7c6b49 78/* Types for control the zone type of onlined and offlined memory */
511c2aba 79enum {
956f8b44 80 /* Offline the memory. */
efc978ad 81 MMOP_OFFLINE = 0,
956f8b44
DH
82 /* Online the memory. Zone depends, see default_zone_for_pfn(). */
83 MMOP_ONLINE,
84 /* Online the memory to ZONE_NORMAL. */
4f7c6b49 85 MMOP_ONLINE_KERNEL,
956f8b44 86 /* Online the memory to ZONE_MOVABLE. */
4f7c6b49 87 MMOP_ONLINE_MOVABLE,
511c2aba
LJ
88};
89
b6117199
DH
90/* Flags for add_memory() and friends to specify memory hotplug details. */
91typedef int __bitwise mhp_t;
92
93/* No special request */
94#define MHP_NONE ((__force mhp_t)0)
9ca6551e
DH
95/*
96 * Allow merging of the added System RAM resource with adjacent,
97 * mergeable resources. After a successful call to add_memory_resource()
98 * with this flag set, the resource pointer must no longer be used as it
99 * might be stale, or the resource might have changed.
100 */
26011267 101#define MHP_MERGE_RESOURCE ((__force mhp_t)BIT(0))
b6117199 102
a08a2ae3
OS
103/*
104 * We want memmap (struct page array) to be self contained.
105 * To do so, we will use the beginning of the hot-added range to build
106 * the page tables for the memmap array that describes the entire range.
107 * Only selected architectures support it with SPARSE_VMEMMAP.
108 */
109#define MHP_MEMMAP_ON_MEMORY ((__force mhp_t)BIT(1))
028fc57a
DH
110/*
111 * The nid field specifies a memory group id (mgid) instead. The memory group
112 * implies the node id (nid).
113 */
114#define MHP_NID_IS_MGID ((__force mhp_t)BIT(2))
a08a2ae3 115
940519f0 116/*
f5637d3b
LG
117 * Extended parameters for memory hotplug:
118 * altmap: alternative allocator for memmap array (optional)
bfeb022f
LG
119 * pgprot: page protection flags to apply to newly created page tables
120 * (required)
940519f0 121 */
f5637d3b 122struct mhp_params {
940519f0 123 struct vmem_altmap *altmap;
bfeb022f 124 pgprot_t pgprot;
940519f0
MH
125};
126
bca3feaa
AK
127bool mhp_range_allowed(u64 start, u64 size, bool need_mapping);
128struct range mhp_get_pluggable_range(bool need_mapping);
129
bdc8cb98
DH
130/*
131 * Zone resizing functions
511c2aba
LJ
132 *
133 * Note: any attempt to resize a zone should has pgdat_resize_lock()
134 * zone_span_writelock() both held. This ensure the size of a zone
135 * can't be changed while pgdat_resize_lock() held.
bdc8cb98
DH
136 */
137static inline unsigned zone_span_seqbegin(struct zone *zone)
138{
139 return read_seqbegin(&zone->span_seqlock);
140}
141static inline int zone_span_seqretry(struct zone *zone, unsigned iv)
142{
143 return read_seqretry(&zone->span_seqlock, iv);
144}
145static inline void zone_span_writelock(struct zone *zone)
146{
147 write_seqlock(&zone->span_seqlock);
148}
149static inline void zone_span_writeunlock(struct zone *zone)
150{
151 write_sequnlock(&zone->span_seqlock);
152}
153static inline void zone_seqlock_init(struct zone *zone)
154{
155 seqlock_init(&zone->span_seqlock);
156}
836809ec
DH
157extern void adjust_present_page_count(struct page *page,
158 struct memory_group *group,
159 long nr_pages);
3947be19 160/* VM interface that may be used by firmware interface */
a08a2ae3
OS
161extern int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages,
162 struct zone *zone);
163extern void mhp_deinit_memmap_on_memory(unsigned long pfn, unsigned long nr_pages);
bd5c2344 164extern int online_pages(unsigned long pfn, unsigned long nr_pages,
836809ec 165 struct zone *zone, struct memory_group *group);
92917998
DH
166extern struct zone *test_pages_in_a_zone(unsigned long start_pfn,
167 unsigned long end_pfn);
257bea71
DH
168extern void __offline_isolated_pages(unsigned long start_pfn,
169 unsigned long end_pfn);
48e94196 170
a9cd410a 171typedef void (*online_page_callback_t)(struct page *page, unsigned int order);
9d0ad8ca 172
18db1491 173extern void generic_online_page(struct page *page, unsigned int order);
9d0ad8ca
DK
174extern int set_online_page_callback(online_page_callback_t callback);
175extern int restore_online_page_callback(online_page_callback_t callback);
176
01b0f197
TK
177extern int try_online_node(int nid);
178
940519f0 179extern int arch_add_memory(int nid, u64 start, u64 size,
f5637d3b 180 struct mhp_params *params);
357b4da5
JG
181extern u64 max_mem_size;
182
1adf8b46 183extern int mhp_online_type_from_str(const char *str);
5f47adf7 184
862919e5 185/* Default online_type (MMOP_*) when new memory blocks are added. */
1adf8b46 186extern int mhp_default_online_type;
4932381e
MH
187/* If movable_node boot option specified */
188extern bool movable_node_enabled;
189static inline bool movable_node_is_enabled(void)
190{
191 return movable_node_enabled;
192}
31bc3858 193
65a2aa5f 194extern void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap);
feee6b29
DH
195extern void __remove_pages(unsigned long start_pfn, unsigned long nr_pages,
196 struct vmem_altmap *altmap);
49ac8255 197
f1dd2cd1 198/* reasonably generic interface to expand the physical pages */
24e6d5a5 199extern int __add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
f5637d3b 200 struct mhp_params *params);
bc02af93 201
3072e413
MH
202#ifndef CONFIG_ARCH_HAS_ADD_PAGES
203static inline int add_pages(int nid, unsigned long start_pfn,
f5637d3b 204 unsigned long nr_pages, struct mhp_params *params)
3072e413 205{
f5637d3b 206 return __add_pages(nid, start_pfn, nr_pages, params);
3072e413
MH
207}
208#else /* ARCH_HAS_ADD_PAGES */
24e6d5a5 209int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
f5637d3b 210 struct mhp_params *params);
3072e413
MH
211#endif /* ARCH_HAS_ADD_PAGES */
212
bfc8c901
VD
213void get_online_mems(void);
214void put_online_mems(void);
20d6c96b 215
30467e0b
DR
216void mem_hotplug_begin(void);
217void mem_hotplug_done(void);
218
208d54e5 219#else /* ! CONFIG_MEMORY_HOTPLUG */
2d070eab
MH
220#define pfn_to_online_page(pfn) \
221({ \
222 struct page *___page = NULL; \
223 if (pfn_valid(pfn)) \
224 ___page = pfn_to_page(pfn); \
225 ___page; \
226 })
227
bdc8cb98
DH
228static inline unsigned zone_span_seqbegin(struct zone *zone)
229{
230 return 0;
231}
232static inline int zone_span_seqretry(struct zone *zone, unsigned iv)
233{
234 return 0;
235}
236static inline void zone_span_writelock(struct zone *zone) {}
237static inline void zone_span_writeunlock(struct zone *zone) {}
238static inline void zone_seqlock_init(struct zone *zone) {}
3947be19 239
01b0f197
TK
240static inline int try_online_node(int nid)
241{
242 return 0;
243}
244
bfc8c901
VD
245static inline void get_online_mems(void) {}
246static inline void put_online_mems(void) {}
20d6c96b 247
30467e0b
DR
248static inline void mem_hotplug_begin(void) {}
249static inline void mem_hotplug_done(void) {}
250
4932381e
MH
251static inline bool movable_node_is_enabled(void)
252{
253 return false;
254}
bdc8cb98 255#endif /* ! CONFIG_MEMORY_HOTPLUG */
9d99aaa3 256
bca3feaa
AK
257/*
258 * Keep this declaration outside CONFIG_MEMORY_HOTPLUG as some
259 * platforms might override and use arch_get_mappable_range()
260 * for internal non memory hotplug purposes.
261 */
262struct range arch_get_mappable_range(void);
263
3a2d7fa8
PT
264#if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
265/*
266 * pgdat resizing functions
267 */
268static inline
269void pgdat_resize_lock(struct pglist_data *pgdat, unsigned long *flags)
270{
271 spin_lock_irqsave(&pgdat->node_size_lock, *flags);
272}
273static inline
274void pgdat_resize_unlock(struct pglist_data *pgdat, unsigned long *flags)
275{
276 spin_unlock_irqrestore(&pgdat->node_size_lock, *flags);
277}
278static inline
279void pgdat_resize_init(struct pglist_data *pgdat)
280{
281 spin_lock_init(&pgdat->node_size_lock);
282}
283#else /* !(CONFIG_MEMORY_HOTPLUG || CONFIG_DEFERRED_STRUCT_PAGE_INIT) */
284/*
285 * Stub functions for when hotplug is off
286 */
287static inline void pgdat_resize_lock(struct pglist_data *p, unsigned long *f) {}
288static inline void pgdat_resize_unlock(struct pglist_data *p, unsigned long *f) {}
289static inline void pgdat_resize_init(struct pglist_data *pgdat) {}
290#endif /* !(CONFIG_MEMORY_HOTPLUG || CONFIG_DEFERRED_STRUCT_PAGE_INIT) */
291
5c755e9f
BP
292#ifdef CONFIG_MEMORY_HOTREMOVE
293
90b30cdc 294extern void try_offline_node(int nid);
836809ec
DH
295extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages,
296 struct memory_group *group);
e1c158e4
DH
297extern int remove_memory(u64 start, u64 size);
298extern void __remove_memory(u64 start, u64 size);
299extern int offline_and_remove_memory(u64 start, u64 size);
5c755e9f
BP
300
301#else
90b30cdc 302static inline void try_offline_node(int nid) {}
aba6efc4 303
836809ec
DH
304static inline int offline_pages(unsigned long start_pfn, unsigned long nr_pages,
305 struct memory_group *group)
aba6efc4
RW
306{
307 return -EINVAL;
308}
309
e1c158e4 310static inline int remove_memory(u64 start, u64 size)
eca499ab
PT
311{
312 return -EBUSY;
313}
314
e1c158e4 315static inline void __remove_memory(u64 start, u64 size) {}
5c755e9f
BP
316#endif /* CONFIG_MEMORY_HOTREMOVE */
317
aba9817d
BDC
318extern void set_zone_contiguous(struct zone *zone);
319extern void clear_zone_contiguous(struct zone *zone);
320
3a0aaefe 321#ifdef CONFIG_MEMORY_HOTPLUG
70b5b46a 322extern void __ref free_area_init_core_hotplug(struct pglist_data *pgdat);
b6117199
DH
323extern int __add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags);
324extern int add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags);
325extern int add_memory_resource(int nid, struct resource *resource,
326 mhp_t mhp_flags);
7b7b2721 327extern int add_memory_driver_managed(int nid, u64 start, u64 size,
b6117199
DH
328 const char *resource_name,
329 mhp_t mhp_flags);
f1dd2cd1 330extern void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
d882c006
DH
331 unsigned long nr_pages,
332 struct vmem_altmap *altmap, int migratetype);
feee6b29
DH
333extern void remove_pfn_range_from_zone(struct zone *zone,
334 unsigned long start_pfn,
335 unsigned long nr_pages);
6677e3ea 336extern bool is_memblock_offlined(struct memory_block *mem);
7ea62160
DW
337extern int sparse_add_section(int nid, unsigned long pfn,
338 unsigned long nr_pages, struct vmem_altmap *altmap);
ba72b4c8 339extern void sparse_remove_section(struct mem_section *ms,
7ea62160 340 unsigned long pfn, unsigned long nr_pages,
24b6d416 341 unsigned long map_offset, struct vmem_altmap *altmap);
04753278
YG
342extern struct page *sparse_decode_mem_map(unsigned long coded_mem_map,
343 unsigned long pnum);
7cf209ba 344extern struct zone *zone_for_pfn_range(int online_type, int nid,
445fcf7c
DH
345 struct memory_group *group, unsigned long start_pfn,
346 unsigned long nr_pages);
4abb1e5b
DH
347extern int arch_create_linear_mapping(int nid, u64 start, u64 size,
348 struct mhp_params *params);
349void arch_remove_linear_mapping(u64 start, u64 size);
a08a2ae3 350extern bool mhp_supports_memmap_on_memory(unsigned long size);
3a0aaefe
DH
351#endif /* CONFIG_MEMORY_HOTPLUG */
352
208d54e5 353#endif /* __LINUX_MEMORY_HOTPLUG_H */