blob: f491690d54c6c258f35516bb59893318f4871625 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Yinghai Lu95f72d12010-07-12 14:36:09 +10002#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.
Yinghai Lu95f72d12010-07-12 14:36:09 +100010 */
11
12#include <linux/init.h>
13#include <linux/mm.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070014#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;
Yinghai Lu95f72d12010-07-12 14:36:09 +100027
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030028/**
29 * enum memblock_flags - definition of memory region attributes
30 * @MEMBLOCK_NONE: no special request
31 * @MEMBLOCK_HOTPLUG: hotpluggable region
32 * @MEMBLOCK_MIRROR: mirrored region
33 * @MEMBLOCK_NOMAP: don't add to kernel direct mapping
34 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +030035enum memblock_flags {
Tony Luckfc6daaf2015-06-24 16:58:09 -070036 MEMBLOCK_NONE = 0x0, /* No special request */
37 MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */
Tony Lucka3f5baf2015-06-24 16:58:12 -070038 MEMBLOCK_MIRROR = 0x2, /* mirrored region */
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +010039 MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */
Tony Luckfc6daaf2015-06-24 16:58:09 -070040};
Tang Chen66b16ed2014-01-21 15:49:23 -080041
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030042/**
43 * struct memblock_region - represents a memory region
44 * @base: physical address of the region
45 * @size: size of the region
46 * @flags: memory region attributes
47 * @nid: NUMA node id
48 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100049struct memblock_region {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100050 phys_addr_t base;
51 phys_addr_t size;
Mike Rapoporte1720fe2018-06-30 17:55:01 +030052 enum memblock_flags flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +020053#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
54 int nid;
55#endif
Yinghai Lu95f72d12010-07-12 14:36:09 +100056};
57
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030058/**
59 * struct memblock_type - collection of memory regions of certain type
60 * @cnt: number of regions
61 * @max: size of the allocated array
62 * @total_size: size of all regions
63 * @regions: array of regions
64 * @name: the memory type symbolic name
65 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100066struct memblock_type {
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030067 unsigned long cnt;
68 unsigned long max;
69 phys_addr_t total_size;
Benjamin Herrenschmidtbf23c512010-07-06 15:39:06 -070070 struct memblock_region *regions;
Heiko Carstens0262d9c2017-02-24 14:55:59 -080071 char *name;
Yinghai Lu95f72d12010-07-12 14:36:09 +100072};
73
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030074/**
75 * struct memblock - memblock allocator metadata
76 * @bottom_up: is bottom up direction?
77 * @current_limit: physical address of the current allocation limit
78 * @memory: usabe memory regions
79 * @reserved: reserved memory regions
80 * @physmem: all physical memory
81 */
Yinghai Lu95f72d12010-07-12 14:36:09 +100082struct memblock {
Tang Chen79442ed2013-11-12 15:07:59 -080083 bool bottom_up; /* is bottom up direction? */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100084 phys_addr_t current_limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100085 struct memblock_type memory;
86 struct memblock_type reserved;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010087#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
88 struct memblock_type physmem;
89#endif
Yinghai Lu95f72d12010-07-12 14:36:09 +100090};
91
92extern struct memblock memblock;
Yinghai Lu5e63cf42010-07-28 15:07:21 +100093extern int memblock_debug;
Yinghai Lu5e63cf42010-07-28 15:07:21 +100094
Mike Rapoport350e88b2019-05-13 17:22:59 -070095#ifndef CONFIG_ARCH_KEEP_MEMBLOCK
Kirill A. Shutemov036fbb22016-01-15 16:57:11 -080096#define __init_memblock __meminit
97#define __initdata_memblock __meminitdata
Pavel Tatashin3010f872017-08-18 15:16:05 -070098void memblock_discard(void);
Kirill A. Shutemov036fbb22016-01-15 16:57:11 -080099#else
100#define __init_memblock
101#define __initdata_memblock
Mike Rapoport350e88b2019-05-13 17:22:59 -0700102static inline void memblock_discard(void) {}
Kirill A. Shutemov036fbb22016-01-15 16:57:11 -0800103#endif
104
Yinghai Lu5e63cf42010-07-28 15:07:21 +1000105#define memblock_dbg(fmt, ...) \
106 if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000107
Tejun Heofc769a82011-07-12 09:58:10 +0200108phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
109 phys_addr_t size, phys_addr_t align);
Tejun Heo1aadc052011-12-08 10:22:08 -0800110void memblock_allow_resize(void);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800111int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
Tejun Heo581adcb2011-12-08 10:22:06 -0800112int memblock_add(phys_addr_t base, phys_addr_t size);
113int memblock_remove(phys_addr_t base, phys_addr_t size);
114int memblock_free(phys_addr_t base, phys_addr_t size);
115int memblock_reserve(phys_addr_t base, phys_addr_t size);
Yinghai Lu6ede1fd2012-10-22 16:35:18 -0700116void memblock_trim_memory(phys_addr_t align);
Tang Chen95cf82e2015-09-08 15:02:03 -0700117bool memblock_overlaps_region(struct memblock_type *type,
118 phys_addr_t base, phys_addr_t size);
Tang Chen66b16ed2014-01-21 15:49:23 -0800119int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
120int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
Tony Lucka3f5baf2015-06-24 16:58:12 -0700121int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100122int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900123int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100124
Mike Rapoport57c8a662018-10-30 15:09:49 -0700125unsigned long memblock_free_all(void);
126void reset_node_managed_pages(pg_data_t *pgdat);
127void reset_all_zones_managed_pages(void);
128
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100129/* Low level functions */
130int memblock_add_range(struct memblock_type *type,
131 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300132 int nid, enum memblock_flags flags);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100133
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300134void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700135 struct memblock_type *type_a,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100136 struct memblock_type *type_b, phys_addr_t *out_start,
137 phys_addr_t *out_end, int *out_nid);
138
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300139void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700140 struct memblock_type *type_a,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100141 struct memblock_type *type_b, phys_addr_t *out_start,
142 phys_addr_t *out_end, int *out_nid);
143
Robin Holt8e7a7f82015-06-30 14:56:41 -0700144void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start,
Chen Gangba6c19f2016-07-26 15:24:47 -0700145 phys_addr_t *out_end);
Robin Holt8e7a7f82015-06-30 14:56:41 -0700146
Pavel Tatashin3010f872017-08-18 15:16:05 -0700147void __memblock_free_late(phys_addr_t base, phys_addr_t size);
148
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100149/**
150 * for_each_mem_range - iterate through memblock areas from type_a and not
151 * included in type_b. Or just type_a if type_b is NULL.
152 * @i: u64 used as loop variable
153 * @type_a: ptr to memblock_type to iterate
154 * @type_b: ptr to memblock_type which excludes from the iteration
155 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700156 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100157 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
158 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
159 * @p_nid: ptr to int for nid of the range, can be %NULL
160 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700161#define for_each_mem_range(i, type_a, type_b, nid, flags, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100162 p_start, p_end, p_nid) \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700163 for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100164 p_start, p_end, p_nid); \
165 i != (u64)ULLONG_MAX; \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700166 __next_mem_range(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100167 p_start, p_end, p_nid))
168
169/**
170 * for_each_mem_range_rev - reverse iterate through memblock areas from
171 * type_a and not included in type_b. Or just type_a if type_b is NULL.
172 * @i: u64 used as loop variable
173 * @type_a: ptr to memblock_type to iterate
174 * @type_b: ptr to memblock_type which excludes from the iteration
175 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700176 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100177 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
178 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
179 * @p_nid: ptr to int for nid of the range, can be %NULL
180 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700181#define for_each_mem_range_rev(i, type_a, type_b, nid, flags, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100182 p_start, p_end, p_nid) \
183 for (i = (u64)ULLONG_MAX, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700184 __next_mem_range_rev(&i, nid, flags, type_a, type_b,\
Chen Gangba6c19f2016-07-26 15:24:47 -0700185 p_start, p_end, p_nid); \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100186 i != (u64)ULLONG_MAX; \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700187 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100188 p_start, p_end, p_nid))
189
Robin Holt8e7a7f82015-06-30 14:56:41 -0700190/**
191 * for_each_reserved_mem_region - iterate over all reserved memblock areas
192 * @i: u64 used as loop variable
193 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
194 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
195 *
196 * Walks over reserved areas of memblock. Available as soon as memblock
197 * is initialized.
198 */
199#define for_each_reserved_mem_region(i, p_start, p_end) \
Chen Gangba6c19f2016-07-26 15:24:47 -0700200 for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end); \
Robin Holt8e7a7f82015-06-30 14:56:41 -0700201 i != (u64)ULLONG_MAX; \
202 __next_reserved_mem_region(&i, p_start, p_end))
203
Tang Chen55ac5902014-01-21 15:49:35 -0800204static inline bool memblock_is_hotpluggable(struct memblock_region *m)
205{
206 return m->flags & MEMBLOCK_HOTPLUG;
207}
208
Tony Lucka3f5baf2015-06-24 16:58:12 -0700209static inline bool memblock_is_mirror(struct memblock_region *m)
210{
211 return m->flags & MEMBLOCK_MIRROR;
212}
213
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100214static inline bool memblock_is_nomap(struct memblock_region *m)
215{
216 return m->flags & MEMBLOCK_NOMAP;
217}
218
Tejun Heo0ee332c2011-12-08 10:22:09 -0800219#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Yinghai Lue76b63f2013-09-11 14:22:17 -0700220int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
221 unsigned long *end_pfn);
Tejun Heo0ee332c2011-12-08 10:22:09 -0800222void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
223 unsigned long *out_end_pfn, int *out_nid);
224
225/**
226 * for_each_mem_pfn_range - early memory pfn range iterator
227 * @i: an integer used as loop variable
228 * @nid: node selector, %MAX_NUMNODES for all nodes
229 * @p_start: ptr to ulong for start pfn of the range, can be %NULL
230 * @p_end: ptr to ulong for end pfn of the range, can be %NULL
231 * @p_nid: ptr to int for nid of the range, can be %NULL
232 *
Wanpeng Lif2d52fe2012-10-08 16:32:24 -0700233 * Walks over configured memory ranges.
Tejun Heo0ee332c2011-12-08 10:22:09 -0800234 */
235#define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
236 for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
237 i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
238#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
239
Alexander Duyck837566e2019-05-13 17:21:17 -0700240#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
241void __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
242 unsigned long *out_spfn,
243 unsigned long *out_epfn);
244/**
245 * for_each_free_mem_range_in_zone - iterate through zone specific free
246 * memblock areas
247 * @i: u64 used as loop variable
248 * @zone: zone in which all of the memory blocks reside
249 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
250 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
251 *
252 * Walks over free (memory && !reserved) areas of memblock in a specific
253 * zone. Available once memblock and an empty zone is initialized. The main
254 * assumption is that the zone start, end, and pgdat have been associated.
255 * This way we can use the zone to determine NUMA node, and if a given part
256 * of the memblock is valid for the zone.
257 */
258#define for_each_free_mem_pfn_range_in_zone(i, zone, p_start, p_end) \
259 for (i = 0, \
260 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end); \
261 i != U64_MAX; \
262 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
Alexander Duyck0e56aca2019-05-13 17:21:20 -0700263
264/**
265 * for_each_free_mem_range_in_zone_from - iterate through zone specific
266 * free memblock areas from a given point
267 * @i: u64 used as loop variable
268 * @zone: zone in which all of the memory blocks reside
269 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
270 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
271 *
272 * Walks over free (memory && !reserved) areas of memblock in a specific
273 * zone, continuing from current position. Available as soon as memblock is
274 * initialized.
275 */
276#define for_each_free_mem_pfn_range_in_zone_from(i, zone, p_start, p_end) \
277 for (; i != U64_MAX; \
278 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
Alexander Duyck837566e2019-05-13 17:21:17 -0700279#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
280
Tejun Heo35fd0802011-07-12 11:15:59 +0200281/**
282 * for_each_free_mem_range - iterate through free memblock areas
283 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800284 * @nid: node selector, %NUMA_NO_NODE for all nodes
Florian Fainellid30b5542016-01-14 15:22:04 -0800285 * @flags: pick from blocks based on memory attributes
Tejun Heo35fd0802011-07-12 11:15:59 +0200286 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
287 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
288 * @p_nid: ptr to int for nid of the range, can be %NULL
289 *
290 * Walks over free (memory && !reserved) areas of memblock. Available as
291 * soon as memblock is initialized.
292 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700293#define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100294 for_each_mem_range(i, &memblock.memory, &memblock.reserved, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700295 nid, flags, p_start, p_end, p_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800296
297/**
298 * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
299 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800300 * @nid: node selector, %NUMA_NO_NODE for all nodes
Florian Fainellid30b5542016-01-14 15:22:04 -0800301 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800302 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
303 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
304 * @p_nid: ptr to int for nid of the range, can be %NULL
305 *
306 * Walks over free (memory && !reserved) areas of memblock in reverse
307 * order. Available as soon as memblock is initialized.
308 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700309#define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
310 p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100311 for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700312 nid, flags, p_start, p_end, p_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800313
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200314#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Tang Chene7e8de52014-01-21 15:49:26 -0800315int memblock_set_node(phys_addr_t base, phys_addr_t size,
316 struct memblock_type *type, int nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200317
318static inline void memblock_set_region_node(struct memblock_region *r, int nid)
319{
320 r->nid = nid;
321}
322
323static inline int memblock_get_region_node(const struct memblock_region *r)
324{
325 return r->nid;
326}
327#else
328static inline void memblock_set_region_node(struct memblock_region *r, int nid)
329{
330}
331
332static inline int memblock_get_region_node(const struct memblock_region *r)
333{
334 return 0;
335}
336#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
337
Mike Rapoport57c8a662018-10-30 15:09:49 -0700338/* Flags for memblock allocation APIs */
339#define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
340#define MEMBLOCK_ALLOC_ACCESSIBLE 0
Qian Caifed84c72018-12-28 00:36:29 -0800341#define MEMBLOCK_ALLOC_KASAN 1
Mike Rapoport57c8a662018-10-30 15:09:49 -0700342
343/* We are using top down, so it is safe to use 0 here */
344#define MEMBLOCK_LOW_LIMIT 0
345
346#ifndef ARCH_LOW_ADDRESS_LIMIT
347#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
348#endif
349
Mike Rapoport8a770c22019-03-11 23:29:16 -0700350phys_addr_t memblock_phys_alloc_range(phys_addr_t size, phys_addr_t align,
351 phys_addr_t start, phys_addr_t end);
Mike Rapoport9a8dd702018-10-30 15:07:59 -0700352phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700353
Mike Rapoportecc3e772019-03-11 23:29:26 -0700354static inline phys_addr_t memblock_phys_alloc(phys_addr_t size,
355 phys_addr_t align)
356{
357 return memblock_phys_alloc_range(size, align, 0,
358 MEMBLOCK_ALLOC_ACCESSIBLE);
359}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700360
Mike Rapoport57c8a662018-10-30 15:09:49 -0700361void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align,
362 phys_addr_t min_addr, phys_addr_t max_addr,
363 int nid);
Mike Rapoport57c8a662018-10-30 15:09:49 -0700364void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align,
365 phys_addr_t min_addr, phys_addr_t max_addr,
366 int nid);
367
368static inline void * __init memblock_alloc(phys_addr_t size, phys_addr_t align)
369{
370 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
371 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
372}
373
374static inline void * __init memblock_alloc_raw(phys_addr_t size,
375 phys_addr_t align)
376{
377 return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
378 MEMBLOCK_ALLOC_ACCESSIBLE,
379 NUMA_NO_NODE);
380}
381
382static inline void * __init memblock_alloc_from(phys_addr_t size,
383 phys_addr_t align,
384 phys_addr_t min_addr)
385{
386 return memblock_alloc_try_nid(size, align, min_addr,
387 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
388}
389
Mike Rapoport57c8a662018-10-30 15:09:49 -0700390static inline void * __init memblock_alloc_low(phys_addr_t size,
391 phys_addr_t align)
392{
393 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
394 ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE);
395}
Mike Rapoport57c8a662018-10-30 15:09:49 -0700396
397static inline void * __init memblock_alloc_node(phys_addr_t size,
398 phys_addr_t align, int nid)
399{
400 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
401 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
402}
403
Mike Rapoport57c8a662018-10-30 15:09:49 -0700404static inline void __init memblock_free_early(phys_addr_t base,
405 phys_addr_t size)
406{
Mike Rapoport4d728682018-12-28 00:35:29 -0800407 memblock_free(base, size);
Mike Rapoport57c8a662018-10-30 15:09:49 -0700408}
409
410static inline void __init memblock_free_early_nid(phys_addr_t base,
411 phys_addr_t size, int nid)
412{
Mike Rapoport4d728682018-12-28 00:35:29 -0800413 memblock_free(base, size);
Mike Rapoport57c8a662018-10-30 15:09:49 -0700414}
415
416static inline void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
417{
418 __memblock_free_late(base, size);
419}
420
Tang Chen79442ed2013-11-12 15:07:59 -0800421/*
422 * Set the allocation direction to bottom-up or top-down.
423 */
Fabian Frederick2cfb3662014-08-06 16:05:03 -0700424static inline void __init memblock_set_bottom_up(bool enable)
Tang Chen79442ed2013-11-12 15:07:59 -0800425{
426 memblock.bottom_up = enable;
427}
428
429/*
430 * Check if the allocation direction is bottom-up or not.
431 * if this is true, that said, memblock will allocate memory
432 * in bottom-up direction.
433 */
434static inline bool memblock_bottom_up(void)
435{
436 return memblock.bottom_up;
437}
Tang Chen79442ed2013-11-12 15:07:59 -0800438
Tejun Heo581adcb2011-12-08 10:22:06 -0800439phys_addr_t memblock_phys_mem_size(void);
Srikar Dronamraju8907de52016-10-07 16:59:18 -0700440phys_addr_t memblock_reserved_size(void);
Yinghai Lu595ad9a2013-01-24 12:20:09 -0800441phys_addr_t memblock_mem_size(unsigned long limit_pfn);
Tejun Heo581adcb2011-12-08 10:22:06 -0800442phys_addr_t memblock_start_of_DRAM(void);
443phys_addr_t memblock_end_of_DRAM(void);
444void memblock_enforce_memory_limit(phys_addr_t memory_limit);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +0900445void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
Dennis Chena571d4e2016-07-28 15:48:26 -0700446void memblock_mem_limit_remove_map(phys_addr_t limit);
Yaowei Baib4ad0c72016-01-14 15:18:54 -0800447bool memblock_is_memory(phys_addr_t addr);
Yaowei Bai937f0c22018-02-06 15:41:18 -0800448bool memblock_is_map_memory(phys_addr_t addr);
449bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
Yaowei Baib4ad0c72016-01-14 15:18:54 -0800450bool memblock_is_reserved(phys_addr_t addr);
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700451bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000452
Tejun Heo4ff7b822011-12-08 10:22:06 -0800453extern void __memblock_dump_all(void);
454
455static inline void memblock_dump_all(void)
456{
457 if (memblock_debug)
458 __memblock_dump_all();
459}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000460
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700461/**
462 * memblock_set_current_limit - Set the current allocation limit to allow
463 * limiting allocations to what is currently
464 * accessible during boot
465 * @limit: New limit value (physical address)
466 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800467void memblock_set_current_limit(phys_addr_t limit);
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700468
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700469
Laura Abbottfec51012014-02-27 01:23:43 +0100470phys_addr_t memblock_get_current_limit(void);
471
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000472/*
473 * pfn conversion functions
474 *
475 * While the memory MEMBLOCKs should always be page aligned, the reserved
476 * MEMBLOCKs may not be. This accessor attempt to provide a very clear
477 * idea of what they return for such non aligned MEMBLOCKs.
478 */
479
480/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300481 * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000482 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300483 *
484 * Return: the lowest pfn intersecting with the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000485 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700486static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000487{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700488 return PFN_UP(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000489}
490
491/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300492 * memblock_region_memory_end_pfn - get the end pfn of the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000493 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300494 *
495 * Return: the end_pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000496 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700497static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000498{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700499 return PFN_DOWN(reg->base + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000500}
501
502/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300503 * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000504 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300505 *
506 * Return: the lowest pfn intersecting with the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000507 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700508static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000509{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700510 return PFN_DOWN(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000511}
512
513/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300514 * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000515 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300516 *
517 * Return: the end_pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000518 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700519static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000520{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700521 return PFN_UP(reg->base + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000522}
523
524#define for_each_memblock(memblock_type, region) \
Chen Gangba6c19f2016-07-26 15:24:47 -0700525 for (region = memblock.memblock_type.regions; \
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000526 region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \
527 region++)
528
Gioh Kim66e8b432017-11-15 17:33:42 -0800529#define for_each_memblock_type(i, memblock_type, rgn) \
530 for (i = 0, rgn = &memblock_type->regions[0]; \
531 i < memblock_type->cnt; \
532 i++, rgn = &memblock_type->regions[i])
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000533
Mike Rapoport57c8a662018-10-30 15:09:49 -0700534extern void *alloc_large_system_hash(const char *tablename,
535 unsigned long bucketsize,
536 unsigned long numentries,
537 int scale,
538 int flags,
539 unsigned int *_hash_shift,
540 unsigned int *_hash_mask,
541 unsigned long low_limit,
542 unsigned long high_limit);
543
544#define HASH_EARLY 0x00000001 /* Allocating during early boot? */
545#define HASH_SMALL 0x00000002 /* sub-page allocation allowed, min
546 * shift passed via *_hash_shift */
547#define HASH_ZERO 0x00000004 /* Zero allocated hash table */
548
549/* Only NUMA needs hash distribution. 64bit NUMA architectures have
550 * sufficient vmalloc space.
551 */
552#ifdef CONFIG_NUMA
553#define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
554extern int hashdist; /* Distribute hashes across NUMA nodes? */
555#else
556#define hashdist (0)
557#endif
558
Vladimir Murzin4a207992015-04-14 15:48:27 -0700559#ifdef CONFIG_MEMTEST
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700560extern void early_memtest(phys_addr_t start, phys_addr_t end);
Vladimir Murzin4a207992015-04-14 15:48:27 -0700561#else
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700562static inline void early_memtest(phys_addr_t start, phys_addr_t end)
Vladimir Murzin4a207992015-04-14 15:48:27 -0700563{
564}
565#endif
Yinghai Luf0b37fad2010-07-28 15:28:21 +1000566
Yinghai Lu95f72d12010-07-12 14:36:09 +1000567#endif /* __KERNEL__ */
568
569#endif /* _LINUX_MEMBLOCK_H */