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