Linux 5.16-rc5
[linux-2.6-block.git] / include / linux / memblock.h
CommitLineData
2874c5fd 1/* SPDX-License-Identifier: GPL-2.0-or-later */
95f72d1e
YL
2#ifndef _LINUX_MEMBLOCK_H
3#define _LINUX_MEMBLOCK_H
4#ifdef __KERNEL__
5
6/*
7 * Logical memory blocks.
8 *
9 * Copyright (C) 2001 Peter Bergner, IBM Corp.
95f72d1e
YL
10 */
11
12#include <linux/init.h>
13#include <linux/mm.h>
57c8a661
MR
14#include <asm/dma.h>
15
16extern unsigned long max_low_pfn;
17extern unsigned long min_low_pfn;
18
19/*
20 * highest page
21 */
22extern unsigned long max_pfn;
23/*
24 * highest possible page
25 */
26extern unsigned long long max_possible_pfn;
95f72d1e 27
9a0de1bf
MR
28/**
29 * enum memblock_flags - definition of memory region attributes
30 * @MEMBLOCK_NONE: no special request
e14b4155
DH
31 * @MEMBLOCK_HOTPLUG: memory region indicated in the firmware-provided memory
32 * map during early boot as hot(un)pluggable system RAM (e.g., memory range
33 * that might get hotunplugged later). With "movable_node" set on the kernel
34 * commandline, try keeping this memory region hotunpluggable. Does not apply
35 * to memblocks added ("hotplugged") after early boot.
9a0de1bf 36 * @MEMBLOCK_MIRROR: mirrored region
9092d4f7
MR
37 * @MEMBLOCK_NOMAP: don't add to kernel direct mapping and treat as
38 * reserved in the memory map; refer to memblock_mark_nomap() description
39 * for further details
f7892d8e
DH
40 * @MEMBLOCK_DRIVER_MANAGED: memory region that is always detected and added
41 * via a driver, and never indicated in the firmware-provided memory map as
42 * system RAM. This corresponds to IORESOURCE_SYSRAM_DRIVER_MANAGED in the
43 * kernel resource tree.
9a0de1bf 44 */
e1720fee 45enum memblock_flags {
fc6daaf9
TL
46 MEMBLOCK_NONE = 0x0, /* No special request */
47 MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */
a3f5bafc 48 MEMBLOCK_MIRROR = 0x2, /* mirrored region */
bf3d3cc5 49 MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */
f7892d8e 50 MEMBLOCK_DRIVER_MANAGED = 0x8, /* always detected via a driver */
fc6daaf9 51};
66b16edf 52
9a0de1bf
MR
53/**
54 * struct memblock_region - represents a memory region
8cbd54f5 55 * @base: base address of the region
9a0de1bf
MR
56 * @size: size of the region
57 * @flags: memory region attributes
58 * @nid: NUMA node id
59 */
e3239ff9 60struct memblock_region {
2898cc4c
BH
61 phys_addr_t base;
62 phys_addr_t size;
e1720fee 63 enum memblock_flags flags;
a9ee6cf5 64#ifdef CONFIG_NUMA
7c0caeb8
TH
65 int nid;
66#endif
95f72d1e
YL
67};
68
9a0de1bf
MR
69/**
70 * struct memblock_type - collection of memory regions of certain type
71 * @cnt: number of regions
72 * @max: size of the allocated array
73 * @total_size: size of all regions
74 * @regions: array of regions
75 * @name: the memory type symbolic name
76 */
e3239ff9 77struct memblock_type {
9a0de1bf
MR
78 unsigned long cnt;
79 unsigned long max;
80 phys_addr_t total_size;
bf23c51f 81 struct memblock_region *regions;
0262d9c8 82 char *name;
95f72d1e
YL
83};
84
9a0de1bf
MR
85/**
86 * struct memblock - memblock allocator metadata
87 * @bottom_up: is bottom up direction?
88 * @current_limit: physical address of the current allocation limit
8cbd54f5 89 * @memory: usable memory regions
9a0de1bf 90 * @reserved: reserved memory regions
9a0de1bf 91 */
95f72d1e 92struct memblock {
79442ed1 93 bool bottom_up; /* is bottom up direction? */
2898cc4c 94 phys_addr_t current_limit;
e3239ff9
BH
95 struct memblock_type memory;
96 struct memblock_type reserved;
95f72d1e
YL
97};
98
99extern struct memblock memblock;
5e63cf43 100
350e88ba 101#ifndef CONFIG_ARCH_KEEP_MEMBLOCK
036fbb21
KS
102#define __init_memblock __meminit
103#define __initdata_memblock __meminitdata
3010f876 104void memblock_discard(void);
036fbb21
KS
105#else
106#define __init_memblock
107#define __initdata_memblock
350e88ba 108static inline void memblock_discard(void) {}
036fbb21
KS
109#endif
110
1aadc056 111void memblock_allow_resize(void);
952eea9b
DH
112int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid,
113 enum memblock_flags flags);
581adcbe
TH
114int memblock_add(phys_addr_t base, phys_addr_t size);
115int memblock_remove(phys_addr_t base, phys_addr_t size);
3ecc6834 116int memblock_phys_free(phys_addr_t base, phys_addr_t size);
581adcbe 117int memblock_reserve(phys_addr_t base, phys_addr_t size);
02634a44
AK
118#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
119int memblock_physmem_add(phys_addr_t base, phys_addr_t size);
120#endif
6ede1fd3 121void memblock_trim_memory(phys_addr_t align);
95cf82ec
TC
122bool memblock_overlaps_region(struct memblock_type *type,
123 phys_addr_t base, phys_addr_t size);
66b16edf
TC
124int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
125int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
a3f5bafc 126int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
bf3d3cc5 127int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
4c546b8a 128int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
f1af9d3a 129
097d43d8 130void memblock_free_all(void);
4421cca0 131void memblock_free(void *ptr, size_t size);
57c8a661
MR
132void reset_node_managed_pages(pg_data_t *pgdat);
133void reset_all_zones_managed_pages(void);
134
f1af9d3a 135/* Low level functions */
e1720fee 136void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
fc6daaf9 137 struct memblock_type *type_a,
f1af9d3a
PH
138 struct memblock_type *type_b, phys_addr_t *out_start,
139 phys_addr_t *out_end, int *out_nid);
140
e1720fee 141void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags,
fc6daaf9 142 struct memblock_type *type_a,
f1af9d3a
PH
143 struct memblock_type *type_b, phys_addr_t *out_start,
144 phys_addr_t *out_end, int *out_nid);
145
621d9739 146void memblock_free_late(phys_addr_t base, phys_addr_t size);
3010f876 147
77649905
DH
148#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
149static inline void __next_physmem_range(u64 *idx, struct memblock_type *type,
150 phys_addr_t *out_start,
151 phys_addr_t *out_end)
152{
153 extern struct memblock_type physmem;
154
155 __next_mem_range(idx, NUMA_NO_NODE, MEMBLOCK_NONE, &physmem, type,
156 out_start, out_end, NULL);
157}
158
159/**
160 * for_each_physmem_range - iterate through physmem areas not included in type.
161 * @i: u64 used as loop variable
162 * @type: ptr to memblock_type which excludes from the iteration, can be %NULL
163 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
164 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
165 */
166#define for_each_physmem_range(i, type, p_start, p_end) \
167 for (i = 0, __next_physmem_range(&i, type, p_start, p_end); \
168 i != (u64)ULLONG_MAX; \
169 __next_physmem_range(&i, type, p_start, p_end))
170#endif /* CONFIG_HAVE_MEMBLOCK_PHYS_MAP */
171
f1af9d3a 172/**
6e245ad4 173 * __for_each_mem_range - iterate through memblock areas from type_a and not
f1af9d3a
PH
174 * included in type_b. Or just type_a if type_b is NULL.
175 * @i: u64 used as loop variable
176 * @type_a: ptr to memblock_type to iterate
177 * @type_b: ptr to memblock_type which excludes from the iteration
178 * @nid: node selector, %NUMA_NO_NODE for all nodes
fc6daaf9 179 * @flags: pick from blocks based on memory attributes
f1af9d3a
PH
180 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
181 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
182 * @p_nid: ptr to int for nid of the range, can be %NULL
183 */
6e245ad4 184#define __for_each_mem_range(i, type_a, type_b, nid, flags, \
f1af9d3a 185 p_start, p_end, p_nid) \
fc6daaf9 186 for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \
f1af9d3a
PH
187 p_start, p_end, p_nid); \
188 i != (u64)ULLONG_MAX; \
fc6daaf9 189 __next_mem_range(&i, nid, flags, type_a, type_b, \
f1af9d3a
PH
190 p_start, p_end, p_nid))
191
192/**
6e245ad4 193 * __for_each_mem_range_rev - reverse iterate through memblock areas from
f1af9d3a
PH
194 * type_a and not included in type_b. Or just type_a if type_b is NULL.
195 * @i: u64 used as loop variable
196 * @type_a: ptr to memblock_type to iterate
197 * @type_b: ptr to memblock_type which excludes from the iteration
198 * @nid: node selector, %NUMA_NO_NODE for all nodes
fc6daaf9 199 * @flags: pick from blocks based on memory attributes
f1af9d3a
PH
200 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
201 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
202 * @p_nid: ptr to int for nid of the range, can be %NULL
203 */
6e245ad4
MR
204#define __for_each_mem_range_rev(i, type_a, type_b, nid, flags, \
205 p_start, p_end, p_nid) \
f1af9d3a 206 for (i = (u64)ULLONG_MAX, \
6e245ad4 207 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
ba6c19fd 208 p_start, p_end, p_nid); \
f1af9d3a 209 i != (u64)ULLONG_MAX; \
fc6daaf9 210 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
f1af9d3a
PH
211 p_start, p_end, p_nid))
212
6e245ad4
MR
213/**
214 * for_each_mem_range - iterate through memory areas.
215 * @i: u64 used as loop variable
216 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
217 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
218 */
219#define for_each_mem_range(i, p_start, p_end) \
220 __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE, \
f7892d8e
DH
221 MEMBLOCK_HOTPLUG | MEMBLOCK_DRIVER_MANAGED, \
222 p_start, p_end, NULL)
6e245ad4
MR
223
224/**
225 * for_each_mem_range_rev - reverse iterate through memblock areas from
226 * type_a and not included in type_b. Or just type_a if type_b is NULL.
227 * @i: u64 used as loop variable
228 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
229 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
230 */
231#define for_each_mem_range_rev(i, p_start, p_end) \
232 __for_each_mem_range_rev(i, &memblock.memory, NULL, NUMA_NO_NODE, \
f7892d8e
DH
233 MEMBLOCK_HOTPLUG | MEMBLOCK_DRIVER_MANAGED,\
234 p_start, p_end, NULL)
6e245ad4 235
8e7a7f86 236/**
9f3d5eaa 237 * for_each_reserved_mem_range - iterate over all reserved memblock areas
8e7a7f86
RH
238 * @i: u64 used as loop variable
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 *
242 * Walks over reserved areas of memblock. Available as soon as memblock
243 * is initialized.
244 */
9f3d5eaa
MR
245#define for_each_reserved_mem_range(i, p_start, p_end) \
246 __for_each_mem_range(i, &memblock.reserved, NULL, NUMA_NO_NODE, \
247 MEMBLOCK_NONE, p_start, p_end, NULL)
8e7a7f86 248
55ac590c
TC
249static inline bool memblock_is_hotpluggable(struct memblock_region *m)
250{
251 return m->flags & MEMBLOCK_HOTPLUG;
252}
253
a3f5bafc
TL
254static inline bool memblock_is_mirror(struct memblock_region *m)
255{
256 return m->flags & MEMBLOCK_MIRROR;
257}
258
bf3d3cc5
AB
259static inline bool memblock_is_nomap(struct memblock_region *m)
260{
261 return m->flags & MEMBLOCK_NOMAP;
262}
263
f7892d8e
DH
264static inline bool memblock_is_driver_managed(struct memblock_region *m)
265{
266 return m->flags & MEMBLOCK_DRIVER_MANAGED;
267}
268
e76b63f8
YL
269int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
270 unsigned long *end_pfn);
0ee332c1
TH
271void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
272 unsigned long *out_end_pfn, int *out_nid);
273
274/**
275 * for_each_mem_pfn_range - early memory pfn range iterator
276 * @i: an integer used as loop variable
277 * @nid: node selector, %MAX_NUMNODES for all nodes
278 * @p_start: ptr to ulong for start pfn of the range, can be %NULL
279 * @p_end: ptr to ulong for end pfn of the range, can be %NULL
280 * @p_nid: ptr to int for nid of the range, can be %NULL
281 *
f2d52fe5 282 * Walks over configured memory ranges.
0ee332c1
TH
283 */
284#define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
285 for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
286 i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
0ee332c1 287
837566e7
AD
288#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
289void __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
290 unsigned long *out_spfn,
291 unsigned long *out_epfn);
292/**
909782ad 293 * for_each_free_mem_pfn_range_in_zone - iterate through zone specific free
837566e7
AD
294 * memblock areas
295 * @i: u64 used as loop variable
296 * @zone: zone in which all of the memory blocks reside
297 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
298 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
299 *
300 * Walks over free (memory && !reserved) areas of memblock in a specific
301 * zone. Available once memblock and an empty zone is initialized. The main
302 * assumption is that the zone start, end, and pgdat have been associated.
303 * This way we can use the zone to determine NUMA node, and if a given part
304 * of the memblock is valid for the zone.
305 */
306#define for_each_free_mem_pfn_range_in_zone(i, zone, p_start, p_end) \
307 for (i = 0, \
308 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end); \
309 i != U64_MAX; \
310 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
0e56acae
AD
311
312/**
909782ad 313 * for_each_free_mem_pfn_range_in_zone_from - iterate through zone specific
0e56acae
AD
314 * free memblock areas from a given point
315 * @i: u64 used as loop variable
316 * @zone: zone in which all of the memory blocks reside
317 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
318 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
319 *
320 * Walks over free (memory && !reserved) areas of memblock in a specific
321 * zone, continuing from current position. Available as soon as memblock is
322 * initialized.
323 */
324#define for_each_free_mem_pfn_range_in_zone_from(i, zone, p_start, p_end) \
325 for (; i != U64_MAX; \
326 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
ecd09650
DJ
327
328int __init deferred_page_init_max_threads(const struct cpumask *node_cpumask);
329
837566e7
AD
330#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
331
35fd0808
TH
332/**
333 * for_each_free_mem_range - iterate through free memblock areas
334 * @i: u64 used as loop variable
b1154233 335 * @nid: node selector, %NUMA_NO_NODE for all nodes
d30b5545 336 * @flags: pick from blocks based on memory attributes
35fd0808
TH
337 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
338 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
339 * @p_nid: ptr to int for nid of the range, can be %NULL
340 *
341 * Walks over free (memory && !reserved) areas of memblock. Available as
342 * soon as memblock is initialized.
343 */
fc6daaf9 344#define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \
6e245ad4
MR
345 __for_each_mem_range(i, &memblock.memory, &memblock.reserved, \
346 nid, flags, p_start, p_end, p_nid)
7bd0b0f0
TH
347
348/**
349 * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
350 * @i: u64 used as loop variable
b1154233 351 * @nid: node selector, %NUMA_NO_NODE for all nodes
d30b5545 352 * @flags: pick from blocks based on memory attributes
7bd0b0f0
TH
353 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
354 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
355 * @p_nid: ptr to int for nid of the range, can be %NULL
356 *
357 * Walks over free (memory && !reserved) areas of memblock in reverse
358 * order. Available as soon as memblock is initialized.
359 */
fc6daaf9
TL
360#define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
361 p_nid) \
6e245ad4
MR
362 __for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
363 nid, flags, p_start, p_end, p_nid)
7bd0b0f0 364
e7e8de59
TC
365int memblock_set_node(phys_addr_t base, phys_addr_t size,
366 struct memblock_type *type, int nid);
7c0caeb8 367
a9ee6cf5 368#ifdef CONFIG_NUMA
7c0caeb8
TH
369static inline void memblock_set_region_node(struct memblock_region *r, int nid)
370{
371 r->nid = nid;
372}
373
374static inline int memblock_get_region_node(const struct memblock_region *r)
375{
376 return r->nid;
377}
378#else
379static inline void memblock_set_region_node(struct memblock_region *r, int nid)
380{
381}
382
383static inline int memblock_get_region_node(const struct memblock_region *r)
384{
385 return 0;
386}
a9ee6cf5 387#endif /* CONFIG_NUMA */
7c0caeb8 388
57c8a661
MR
389/* Flags for memblock allocation APIs */
390#define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
391#define MEMBLOCK_ALLOC_ACCESSIBLE 0
c6975d7c 392#define MEMBLOCK_ALLOC_NOLEAKTRACE 1
57c8a661
MR
393
394/* We are using top down, so it is safe to use 0 here */
395#define MEMBLOCK_LOW_LIMIT 0
396
397#ifndef ARCH_LOW_ADDRESS_LIMIT
398#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
399#endif
400
8a770c2a
MR
401phys_addr_t memblock_phys_alloc_range(phys_addr_t size, phys_addr_t align,
402 phys_addr_t start, phys_addr_t end);
8676af1f
AB
403phys_addr_t memblock_alloc_range_nid(phys_addr_t size,
404 phys_addr_t align, phys_addr_t start,
405 phys_addr_t end, int nid, bool exact_nid);
9a8dd708 406phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
9d1e2492 407
ecc3e771
MR
408static inline phys_addr_t memblock_phys_alloc(phys_addr_t size,
409 phys_addr_t align)
410{
411 return memblock_phys_alloc_range(size, align, 0,
412 MEMBLOCK_ALLOC_ACCESSIBLE);
413}
e63075a3 414
0ac398b1
YY
415void *memblock_alloc_exact_nid_raw(phys_addr_t size, phys_addr_t align,
416 phys_addr_t min_addr, phys_addr_t max_addr,
417 int nid);
57c8a661
MR
418void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align,
419 phys_addr_t min_addr, phys_addr_t max_addr,
420 int nid);
57c8a661
MR
421void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align,
422 phys_addr_t min_addr, phys_addr_t max_addr,
423 int nid);
424
5bdba520 425static __always_inline void *memblock_alloc(phys_addr_t size, phys_addr_t align)
57c8a661
MR
426{
427 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
428 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
429}
430
5bdba520 431static inline void *memblock_alloc_raw(phys_addr_t size,
57c8a661
MR
432 phys_addr_t align)
433{
434 return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
435 MEMBLOCK_ALLOC_ACCESSIBLE,
436 NUMA_NO_NODE);
437}
438
5bdba520 439static inline void *memblock_alloc_from(phys_addr_t size,
57c8a661
MR
440 phys_addr_t align,
441 phys_addr_t min_addr)
442{
443 return memblock_alloc_try_nid(size, align, min_addr,
444 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
445}
446
5bdba520 447static inline void *memblock_alloc_low(phys_addr_t size,
57c8a661
MR
448 phys_addr_t align)
449{
450 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
451 ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE);
452}
57c8a661 453
5bdba520 454static inline void *memblock_alloc_node(phys_addr_t size,
57c8a661
MR
455 phys_addr_t align, int nid)
456{
457 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
458 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
459}
460
79442ed1
TC
461/*
462 * Set the allocation direction to bottom-up or top-down.
463 */
a024b7c2 464static inline __init_memblock void memblock_set_bottom_up(bool enable)
79442ed1
TC
465{
466 memblock.bottom_up = enable;
467}
468
469/*
470 * Check if the allocation direction is bottom-up or not.
471 * if this is true, that said, memblock will allocate memory
472 * in bottom-up direction.
473 */
a024b7c2 474static inline __init_memblock bool memblock_bottom_up(void)
79442ed1
TC
475{
476 return memblock.bottom_up;
477}
79442ed1 478
581adcbe 479phys_addr_t memblock_phys_mem_size(void);
8907de5d 480phys_addr_t memblock_reserved_size(void);
581adcbe
TH
481phys_addr_t memblock_start_of_DRAM(void);
482phys_addr_t memblock_end_of_DRAM(void);
483void memblock_enforce_memory_limit(phys_addr_t memory_limit);
c9ca9b4e 484void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
a571d4eb 485void memblock_mem_limit_remove_map(phys_addr_t limit);
b4ad0c7e 486bool memblock_is_memory(phys_addr_t addr);
937f0c26
YB
487bool memblock_is_map_memory(phys_addr_t addr);
488bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
b4ad0c7e 489bool memblock_is_reserved(phys_addr_t addr);
c5c5c9d1 490bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
581adcbe 491
87c55870 492void memblock_dump_all(void);
95f72d1e 493
e63075a3
BH
494/**
495 * memblock_set_current_limit - Set the current allocation limit to allow
496 * limiting allocations to what is currently
497 * accessible during boot
498 * @limit: New limit value (physical address)
499 */
581adcbe 500void memblock_set_current_limit(phys_addr_t limit);
e63075a3 501
35a1f0bd 502
fec51014
LA
503phys_addr_t memblock_get_current_limit(void);
504
5b385f25
BH
505/*
506 * pfn conversion functions
507 *
508 * While the memory MEMBLOCKs should always be page aligned, the reserved
509 * MEMBLOCKs may not be. This accessor attempt to provide a very clear
510 * idea of what they return for such non aligned MEMBLOCKs.
511 */
512
513/**
47cec443 514 * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
5b385f25 515 * @reg: memblock_region structure
47cec443
MR
516 *
517 * Return: the lowest pfn intersecting with the memory region
5b385f25 518 */
c7fc2de0 519static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
5b385f25 520{
c7fc2de0 521 return PFN_UP(reg->base);
5b385f25
BH
522}
523
524/**
47cec443 525 * memblock_region_memory_end_pfn - get the end pfn of the memory region
5b385f25 526 * @reg: memblock_region structure
47cec443
MR
527 *
528 * Return: the end_pfn of the reserved region
5b385f25 529 */
c7fc2de0 530static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
5b385f25 531{
c7fc2de0 532 return PFN_DOWN(reg->base + reg->size);
5b385f25
BH
533}
534
535/**
47cec443 536 * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
5b385f25 537 * @reg: memblock_region structure
47cec443
MR
538 *
539 * Return: the lowest pfn intersecting with the reserved region
5b385f25 540 */
c7fc2de0 541static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
5b385f25 542{
c7fc2de0 543 return PFN_DOWN(reg->base);
5b385f25
BH
544}
545
546/**
47cec443 547 * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
5b385f25 548 * @reg: memblock_region structure
47cec443
MR
549 *
550 * Return: the end_pfn of the reserved region
5b385f25 551 */
c7fc2de0 552static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
5b385f25 553{
c7fc2de0 554 return PFN_UP(reg->base + reg->size);
5b385f25
BH
555}
556
cc6de168
MR
557/**
558 * for_each_mem_region - itereate over memory regions
559 * @region: loop variable
560 */
561#define for_each_mem_region(region) \
562 for (region = memblock.memory.regions; \
563 region < (memblock.memory.regions + memblock.memory.cnt); \
564 region++)
565
566/**
567 * for_each_reserved_mem_region - itereate over reserved memory regions
568 * @region: loop variable
569 */
570#define for_each_reserved_mem_region(region) \
571 for (region = memblock.reserved.regions; \
572 region < (memblock.reserved.regions + memblock.reserved.cnt); \
5b385f25
BH
573 region++)
574
57c8a661
MR
575extern void *alloc_large_system_hash(const char *tablename,
576 unsigned long bucketsize,
577 unsigned long numentries,
578 int scale,
579 int flags,
580 unsigned int *_hash_shift,
581 unsigned int *_hash_mask,
582 unsigned long low_limit,
583 unsigned long high_limit);
584
585#define HASH_EARLY 0x00000001 /* Allocating during early boot? */
586#define HASH_SMALL 0x00000002 /* sub-page allocation allowed, min
587 * shift passed via *_hash_shift */
588#define HASH_ZERO 0x00000004 /* Zero allocated hash table */
589
590/* Only NUMA needs hash distribution. 64bit NUMA architectures have
591 * sufficient vmalloc space.
592 */
593#ifdef CONFIG_NUMA
594#define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
595extern int hashdist; /* Distribute hashes across NUMA nodes? */
596#else
597#define hashdist (0)
598#endif
599
4a20799d 600#ifdef CONFIG_MEMTEST
7f70baee 601extern void early_memtest(phys_addr_t start, phys_addr_t end);
4a20799d 602#else
7f70baee 603static inline void early_memtest(phys_addr_t start, phys_addr_t end)
4a20799d
VM
604{
605}
606#endif
f0b37fad 607
95f72d1e
YL
608#endif /* __KERNEL__ */
609
610#endif /* _LINUX_MEMBLOCK_H */