1
2#ifndef _LINUX_MEMBLOCK_H
3#define _LINUX_MEMBLOCK_H
4
5
6
7
8
9
10
11#include <linux/init.h>
12#include <linux/mm.h>
13#include <asm/dma.h>
14
15extern unsigned long max_low_pfn;
16extern unsigned long min_low_pfn;
17
18
19
20
21extern unsigned long max_pfn;
22
23
24
25extern unsigned long long max_possible_pfn;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44enum memblock_flags {
45 MEMBLOCK_NONE = 0x0,
46 MEMBLOCK_HOTPLUG = 0x1,
47 MEMBLOCK_MIRROR = 0x2,
48 MEMBLOCK_NOMAP = 0x4,
49 MEMBLOCK_DRIVER_MANAGED = 0x8,
50};
51
52
53
54
55
56
57
58
59struct memblock_region {
60 phys_addr_t base;
61 phys_addr_t size;
62 enum memblock_flags flags;
63#ifdef CONFIG_NUMA
64 int nid;
65#endif
66};
67
68
69
70
71
72
73
74
75
76struct memblock_type {
77 unsigned long cnt;
78 unsigned long max;
79 phys_addr_t total_size;
80 struct memblock_region *regions;
81 char *name;
82};
83
84
85
86
87
88
89
90
91struct memblock {
92 bool bottom_up;
93 phys_addr_t current_limit;
94 struct memblock_type memory;
95 struct memblock_type reserved;
96};
97
98extern struct memblock memblock;
99
100#ifndef CONFIG_ARCH_KEEP_MEMBLOCK
101#define __init_memblock __meminit
102#define __initdata_memblock __meminitdata
103void memblock_discard(void);
104#else
105#define __init_memblock
106#define __initdata_memblock
107static inline void memblock_discard(void) {}
108#endif
109
110void memblock_allow_resize(void);
111int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid,
112 enum memblock_flags flags);
113int memblock_add(phys_addr_t base, phys_addr_t size);
114int memblock_remove(phys_addr_t base, phys_addr_t size);
115int memblock_phys_free(phys_addr_t base, phys_addr_t size);
116int memblock_reserve(phys_addr_t base, phys_addr_t size);
117#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
118int memblock_physmem_add(phys_addr_t base, phys_addr_t size);
119#endif
120void memblock_trim_memory(phys_addr_t align);
121bool memblock_overlaps_region(struct memblock_type *type,
122 phys_addr_t base, phys_addr_t size);
123int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
124int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
125int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
126int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
127int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
128
129void memblock_free_all(void);
130void memblock_free(void *ptr, size_t size);
131void reset_node_managed_pages(pg_data_t *pgdat);
132void reset_all_zones_managed_pages(void);
133
134
135void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
136 struct memblock_type *type_a,
137 struct memblock_type *type_b, phys_addr_t *out_start,
138 phys_addr_t *out_end, int *out_nid);
139
140void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags,
141 struct memblock_type *type_a,
142 struct memblock_type *type_b, phys_addr_t *out_start,
143 phys_addr_t *out_end, int *out_nid);
144
145void memblock_free_late(phys_addr_t base, phys_addr_t size);
146
147#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
148static inline void __next_physmem_range(u64 *idx, struct memblock_type *type,
149 phys_addr_t *out_start,
150 phys_addr_t *out_end)
151{
152 extern struct memblock_type physmem;
153
154 __next_mem_range(idx, NUMA_NO_NODE, MEMBLOCK_NONE, &physmem, type,
155 out_start, out_end, NULL);
156}
157
158
159
160
161
162
163
164
165#define for_each_physmem_range(i, type, p_start, p_end) \
166 for (i = 0, __next_physmem_range(&i, type, p_start, p_end); \
167 i != (u64)ULLONG_MAX; \
168 __next_physmem_range(&i, type, p_start, p_end))
169#endif
170
171
172
173
174
175
176
177
178
179
180
181
182
183#define __for_each_mem_range(i, type_a, type_b, nid, flags, \
184 p_start, p_end, p_nid) \
185 for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \
186 p_start, p_end, p_nid); \
187 i != (u64)ULLONG_MAX; \
188 __next_mem_range(&i, nid, flags, type_a, type_b, \
189 p_start, p_end, p_nid))
190
191
192
193
194
195
196
197
198
199
200
201
202
203#define __for_each_mem_range_rev(i, type_a, type_b, nid, flags, \
204 p_start, p_end, p_nid) \
205 for (i = (u64)ULLONG_MAX, \
206 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
207 p_start, p_end, p_nid); \
208 i != (u64)ULLONG_MAX; \
209 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
210 p_start, p_end, p_nid))
211
212
213
214
215
216
217
218#define for_each_mem_range(i, p_start, p_end) \
219 __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE, \
220 MEMBLOCK_HOTPLUG | MEMBLOCK_DRIVER_MANAGED, \
221 p_start, p_end, NULL)
222
223
224
225
226
227
228
229
230#define for_each_mem_range_rev(i, p_start, p_end) \
231 __for_each_mem_range_rev(i, &memblock.memory, NULL, NUMA_NO_NODE, \
232 MEMBLOCK_HOTPLUG | MEMBLOCK_DRIVER_MANAGED,\
233 p_start, p_end, NULL)
234
235
236
237
238
239
240
241
242
243
244#define for_each_reserved_mem_range(i, p_start, p_end) \
245 __for_each_mem_range(i, &memblock.reserved, NULL, NUMA_NO_NODE, \
246 MEMBLOCK_NONE, p_start, p_end, NULL)
247
248static inline bool memblock_is_hotpluggable(struct memblock_region *m)
249{
250 return m->flags & MEMBLOCK_HOTPLUG;
251}
252
253static inline bool memblock_is_mirror(struct memblock_region *m)
254{
255 return m->flags & MEMBLOCK_MIRROR;
256}
257
258static inline bool memblock_is_nomap(struct memblock_region *m)
259{
260 return m->flags & MEMBLOCK_NOMAP;
261}
262
263static inline bool memblock_is_driver_managed(struct memblock_region *m)
264{
265 return m->flags & MEMBLOCK_DRIVER_MANAGED;
266}
267
268int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
269 unsigned long *end_pfn);
270void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
271 unsigned long *out_end_pfn, int *out_nid);
272
273
274
275
276
277
278
279
280
281
282
283#define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
284 for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
285 i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
286
287#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
288void __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
289 unsigned long *out_spfn,
290 unsigned long *out_epfn);
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305#define for_each_free_mem_pfn_range_in_zone(i, zone, p_start, p_end) \
306 for (i = 0, \
307 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end); \
308 i != U64_MAX; \
309 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
310
311
312
313
314
315
316
317
318
319
320
321
322
323#define for_each_free_mem_pfn_range_in_zone_from(i, zone, p_start, p_end) \
324 for (; i != U64_MAX; \
325 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
326
327int __init deferred_page_init_max_threads(const struct cpumask *node_cpumask);
328
329#endif
330
331
332
333
334
335
336
337
338
339
340
341
342
343#define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \
344 __for_each_mem_range(i, &memblock.memory, &memblock.reserved, \
345 nid, flags, p_start, p_end, p_nid)
346
347
348
349
350
351
352
353
354
355
356
357
358
359#define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
360 p_nid) \
361 __for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
362 nid, flags, p_start, p_end, p_nid)
363
364int memblock_set_node(phys_addr_t base, phys_addr_t size,
365 struct memblock_type *type, int nid);
366
367#ifdef CONFIG_NUMA
368static inline void memblock_set_region_node(struct memblock_region *r, int nid)
369{
370 r->nid = nid;
371}
372
373static inline int memblock_get_region_node(const struct memblock_region *r)
374{
375 return r->nid;
376}
377#else
378static inline void memblock_set_region_node(struct memblock_region *r, int nid)
379{
380}
381
382static inline int memblock_get_region_node(const struct memblock_region *r)
383{
384 return 0;
385}
386#endif
387
388
389#define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
390#define MEMBLOCK_ALLOC_ACCESSIBLE 0
391#define MEMBLOCK_ALLOC_NOLEAKTRACE 1
392
393
394#define MEMBLOCK_LOW_LIMIT 0
395
396#ifndef ARCH_LOW_ADDRESS_LIMIT
397#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
398#endif
399
400phys_addr_t memblock_phys_alloc_range(phys_addr_t size, phys_addr_t align,
401 phys_addr_t start, phys_addr_t end);
402phys_addr_t memblock_alloc_range_nid(phys_addr_t size,
403 phys_addr_t align, phys_addr_t start,
404 phys_addr_t end, int nid, bool exact_nid);
405phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
406
407static __always_inline phys_addr_t memblock_phys_alloc(phys_addr_t size,
408 phys_addr_t align)
409{
410 return memblock_phys_alloc_range(size, align, 0,
411 MEMBLOCK_ALLOC_ACCESSIBLE);
412}
413
414void *memblock_alloc_exact_nid_raw(phys_addr_t size, phys_addr_t align,
415 phys_addr_t min_addr, phys_addr_t max_addr,
416 int nid);
417void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align,
418 phys_addr_t min_addr, phys_addr_t max_addr,
419 int nid);
420void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align,
421 phys_addr_t min_addr, phys_addr_t max_addr,
422 int nid);
423
424static __always_inline void *memblock_alloc(phys_addr_t size, phys_addr_t align)
425{
426 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
427 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
428}
429
430static inline void *memblock_alloc_raw(phys_addr_t size,
431 phys_addr_t align)
432{
433 return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
434 MEMBLOCK_ALLOC_ACCESSIBLE,
435 NUMA_NO_NODE);
436}
437
438static inline void *memblock_alloc_from(phys_addr_t size,
439 phys_addr_t align,
440 phys_addr_t min_addr)
441{
442 return memblock_alloc_try_nid(size, align, min_addr,
443 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
444}
445
446static inline void *memblock_alloc_low(phys_addr_t size,
447 phys_addr_t align)
448{
449 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
450 ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE);
451}
452
453static inline void *memblock_alloc_node(phys_addr_t size,
454 phys_addr_t align, int nid)
455{
456 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
457 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
458}
459
460
461
462
463static inline __init_memblock void memblock_set_bottom_up(bool enable)
464{
465 memblock.bottom_up = enable;
466}
467
468
469
470
471
472
473static inline __init_memblock bool memblock_bottom_up(void)
474{
475 return memblock.bottom_up;
476}
477
478phys_addr_t memblock_phys_mem_size(void);
479phys_addr_t memblock_reserved_size(void);
480phys_addr_t memblock_start_of_DRAM(void);
481phys_addr_t memblock_end_of_DRAM(void);
482void memblock_enforce_memory_limit(phys_addr_t memory_limit);
483void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
484void memblock_mem_limit_remove_map(phys_addr_t limit);
485bool memblock_is_memory(phys_addr_t addr);
486bool memblock_is_map_memory(phys_addr_t addr);
487bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
488bool memblock_is_reserved(phys_addr_t addr);
489bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
490
491void memblock_dump_all(void);
492
493
494
495
496
497
498
499void memblock_set_current_limit(phys_addr_t limit);
500
501
502phys_addr_t memblock_get_current_limit(void);
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
519{
520 return PFN_UP(reg->base);
521}
522
523
524
525
526
527
528
529static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
530{
531 return PFN_DOWN(reg->base + reg->size);
532}
533
534
535
536
537
538
539
540static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
541{
542 return PFN_DOWN(reg->base);
543}
544
545
546
547
548
549
550
551static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
552{
553 return PFN_UP(reg->base + reg->size);
554}
555
556
557
558
559
560#define for_each_mem_region(region) \
561 for (region = memblock.memory.regions; \
562 region < (memblock.memory.regions + memblock.memory.cnt); \
563 region++)
564
565
566
567
568
569#define for_each_reserved_mem_region(region) \
570 for (region = memblock.reserved.regions; \
571 region < (memblock.reserved.regions + memblock.reserved.cnt); \
572 region++)
573
574extern void *alloc_large_system_hash(const char *tablename,
575 unsigned long bucketsize,
576 unsigned long numentries,
577 int scale,
578 int flags,
579 unsigned int *_hash_shift,
580 unsigned int *_hash_mask,
581 unsigned long low_limit,
582 unsigned long high_limit);
583
584#define HASH_EARLY 0x00000001
585#define HASH_SMALL 0x00000002
586
587#define HASH_ZERO 0x00000004
588
589
590
591
592#ifdef CONFIG_NUMA
593#define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
594extern int hashdist;
595#else
596#define hashdist (0)
597#endif
598
599#ifdef CONFIG_MEMTEST
600extern void early_memtest(phys_addr_t start, phys_addr_t end);
601#else
602static inline void early_memtest(phys_addr_t start, phys_addr_t end)
603{
604}
605#endif
606
607
608#endif
609