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