blob: aee299a6aa76d328f4d0bcf3494a5ad93b496ee9 [file] [log] [blame]
Yinghai Lu95f72d12010-07-12 14:36:09 +10001#ifndef _LINUX_MEMBLOCK_H
2#define _LINUX_MEMBLOCK_H
3#ifdef __KERNEL__
4
5/*
6 * Logical memory blocks.
7 *
8 * Copyright (C) 2001 Peter Bergner, IBM Corp.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16#include <linux/init.h>
17#include <linux/mm.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070018#include <asm/dma.h>
19
20extern unsigned long max_low_pfn;
21extern unsigned long min_low_pfn;
22
23/*
24 * highest page
25 */
26extern unsigned long max_pfn;
27/*
28 * highest possible page
29 */
30extern unsigned long long max_possible_pfn;
Yinghai Lu95f72d12010-07-12 14:36:09 +100031
Yinghai Lu37d8d4b2010-07-28 15:20:58 +100032#define INIT_MEMBLOCK_REGIONS 128
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010033#define INIT_PHYSMEM_REGIONS 4
Yinghai Lu95f72d12010-07-12 14:36:09 +100034
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030035/**
36 * enum memblock_flags - definition of memory region attributes
37 * @MEMBLOCK_NONE: no special request
38 * @MEMBLOCK_HOTPLUG: hotpluggable region
39 * @MEMBLOCK_MIRROR: mirrored region
40 * @MEMBLOCK_NOMAP: don't add to kernel direct mapping
41 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +030042enum memblock_flags {
Tony Luckfc6daaf2015-06-24 16:58:09 -070043 MEMBLOCK_NONE = 0x0, /* No special request */
44 MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */
Tony Lucka3f5baf2015-06-24 16:58:12 -070045 MEMBLOCK_MIRROR = 0x2, /* mirrored region */
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +010046 MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */
Tony Luckfc6daaf2015-06-24 16:58:09 -070047};
Tang Chen66b16ed2014-01-21 15:49:23 -080048
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030049/**
50 * struct memblock_region - represents a memory region
51 * @base: physical address of the region
52 * @size: size of the region
53 * @flags: memory region attributes
54 * @nid: NUMA node id
55 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100056struct memblock_region {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100057 phys_addr_t base;
58 phys_addr_t size;
Mike Rapoporte1720fe2018-06-30 17:55:01 +030059 enum memblock_flags flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +020060#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
61 int nid;
62#endif
Yinghai Lu95f72d12010-07-12 14:36:09 +100063};
64
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030065/**
66 * struct memblock_type - collection of memory regions of certain type
67 * @cnt: number of regions
68 * @max: size of the allocated array
69 * @total_size: size of all regions
70 * @regions: array of regions
71 * @name: the memory type symbolic name
72 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100073struct memblock_type {
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030074 unsigned long cnt;
75 unsigned long max;
76 phys_addr_t total_size;
Benjamin Herrenschmidtbf23c512010-07-06 15:39:06 -070077 struct memblock_region *regions;
Heiko Carstens0262d9c2017-02-24 14:55:59 -080078 char *name;
Yinghai Lu95f72d12010-07-12 14:36:09 +100079};
80
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030081/**
82 * struct memblock - memblock allocator metadata
83 * @bottom_up: is bottom up direction?
84 * @current_limit: physical address of the current allocation limit
85 * @memory: usabe memory regions
86 * @reserved: reserved memory regions
87 * @physmem: all physical memory
88 */
Yinghai Lu95f72d12010-07-12 14:36:09 +100089struct memblock {
Tang Chen79442ed2013-11-12 15:07:59 -080090 bool bottom_up; /* is bottom up direction? */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100091 phys_addr_t current_limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100092 struct memblock_type memory;
93 struct memblock_type reserved;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010094#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
95 struct memblock_type physmem;
96#endif
Yinghai Lu95f72d12010-07-12 14:36:09 +100097};
98
99extern struct memblock memblock;
Yinghai Lu5e63cf42010-07-28 15:07:21 +1000100extern int memblock_debug;
Yinghai Lu5e63cf42010-07-28 15:07:21 +1000101
Kirill A. Shutemov036fbb22016-01-15 16:57:11 -0800102#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
103#define __init_memblock __meminit
104#define __initdata_memblock __meminitdata
Pavel Tatashin3010f872017-08-18 15:16:05 -0700105void memblock_discard(void);
Kirill A. Shutemov036fbb22016-01-15 16:57:11 -0800106#else
107#define __init_memblock
108#define __initdata_memblock
109#endif
110
Yinghai Lu5e63cf42010-07-28 15:07:21 +1000111#define memblock_dbg(fmt, ...) \
112 if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000113
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800114phys_addr_t memblock_find_in_range_node(phys_addr_t size, phys_addr_t align,
Chen Gangba6c19f2016-07-26 15:24:47 -0700115 phys_addr_t start, phys_addr_t end,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300116 int nid, enum memblock_flags flags);
Tejun Heofc769a82011-07-12 09:58:10 +0200117phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
118 phys_addr_t size, phys_addr_t align);
Tejun Heo1aadc052011-12-08 10:22:08 -0800119void memblock_allow_resize(void);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800120int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
Tejun Heo581adcb2011-12-08 10:22:06 -0800121int memblock_add(phys_addr_t base, phys_addr_t size);
122int memblock_remove(phys_addr_t base, phys_addr_t size);
123int memblock_free(phys_addr_t base, phys_addr_t size);
124int memblock_reserve(phys_addr_t base, phys_addr_t size);
Yinghai Lu6ede1fd2012-10-22 16:35:18 -0700125void memblock_trim_memory(phys_addr_t align);
Tang Chen95cf82e2015-09-08 15:02:03 -0700126bool memblock_overlaps_region(struct memblock_type *type,
127 phys_addr_t base, phys_addr_t size);
Tang Chen66b16ed2014-01-21 15:49:23 -0800128int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
129int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
Tony Lucka3f5baf2015-06-24 16:58:12 -0700130int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100131int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900132int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300133enum memblock_flags choose_memblock_flags(void);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100134
Mike Rapoport57c8a662018-10-30 15:09:49 -0700135unsigned long memblock_free_all(void);
136void reset_node_managed_pages(pg_data_t *pgdat);
137void reset_all_zones_managed_pages(void);
138
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100139/* Low level functions */
140int memblock_add_range(struct memblock_type *type,
141 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300142 int nid, enum memblock_flags flags);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100143
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300144void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700145 struct memblock_type *type_a,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100146 struct memblock_type *type_b, phys_addr_t *out_start,
147 phys_addr_t *out_end, int *out_nid);
148
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300149void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700150 struct memblock_type *type_a,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100151 struct memblock_type *type_b, phys_addr_t *out_start,
152 phys_addr_t *out_end, int *out_nid);
153
Robin Holt8e7a7f82015-06-30 14:56:41 -0700154void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start,
Chen Gangba6c19f2016-07-26 15:24:47 -0700155 phys_addr_t *out_end);
Robin Holt8e7a7f82015-06-30 14:56:41 -0700156
Pavel Tatashin3010f872017-08-18 15:16:05 -0700157void __memblock_free_early(phys_addr_t base, phys_addr_t size);
158void __memblock_free_late(phys_addr_t base, phys_addr_t size);
159
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100160/**
161 * for_each_mem_range - iterate through memblock areas from type_a and not
162 * included in type_b. Or just type_a if type_b is NULL.
163 * @i: u64 used as loop variable
164 * @type_a: ptr to memblock_type to iterate
165 * @type_b: ptr to memblock_type which excludes from the iteration
166 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700167 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100168 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
169 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
170 * @p_nid: ptr to int for nid of the range, can be %NULL
171 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700172#define for_each_mem_range(i, type_a, type_b, nid, flags, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100173 p_start, p_end, p_nid) \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700174 for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100175 p_start, p_end, p_nid); \
176 i != (u64)ULLONG_MAX; \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700177 __next_mem_range(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100178 p_start, p_end, p_nid))
179
180/**
181 * for_each_mem_range_rev - reverse iterate through memblock areas from
182 * type_a and not included in type_b. Or just type_a if type_b is NULL.
183 * @i: u64 used as loop variable
184 * @type_a: ptr to memblock_type to iterate
185 * @type_b: ptr to memblock_type which excludes from the iteration
186 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700187 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100188 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
189 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
190 * @p_nid: ptr to int for nid of the range, can be %NULL
191 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700192#define for_each_mem_range_rev(i, type_a, type_b, nid, flags, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100193 p_start, p_end, p_nid) \
194 for (i = (u64)ULLONG_MAX, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700195 __next_mem_range_rev(&i, nid, flags, type_a, type_b,\
Chen Gangba6c19f2016-07-26 15:24:47 -0700196 p_start, p_end, p_nid); \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100197 i != (u64)ULLONG_MAX; \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700198 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100199 p_start, p_end, p_nid))
200
Robin Holt8e7a7f82015-06-30 14:56:41 -0700201/**
202 * for_each_reserved_mem_region - iterate over all reserved memblock areas
203 * @i: u64 used as loop variable
204 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
205 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
206 *
207 * Walks over reserved areas of memblock. Available as soon as memblock
208 * is initialized.
209 */
210#define for_each_reserved_mem_region(i, p_start, p_end) \
Chen Gangba6c19f2016-07-26 15:24:47 -0700211 for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end); \
Robin Holt8e7a7f82015-06-30 14:56:41 -0700212 i != (u64)ULLONG_MAX; \
213 __next_reserved_mem_region(&i, p_start, p_end))
214
Tang Chen55ac5902014-01-21 15:49:35 -0800215static inline bool memblock_is_hotpluggable(struct memblock_region *m)
216{
217 return m->flags & MEMBLOCK_HOTPLUG;
218}
219
Tony Lucka3f5baf2015-06-24 16:58:12 -0700220static inline bool memblock_is_mirror(struct memblock_region *m)
221{
222 return m->flags & MEMBLOCK_MIRROR;
223}
224
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100225static inline bool memblock_is_nomap(struct memblock_region *m)
226{
227 return m->flags & MEMBLOCK_NOMAP;
228}
229
Tejun Heo0ee332c2011-12-08 10:22:09 -0800230#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Yinghai Lue76b63f2013-09-11 14:22:17 -0700231int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
232 unsigned long *end_pfn);
Tejun Heo0ee332c2011-12-08 10:22:09 -0800233void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
234 unsigned long *out_end_pfn, int *out_nid);
235
236/**
237 * for_each_mem_pfn_range - early memory pfn range iterator
238 * @i: an integer used as loop variable
239 * @nid: node selector, %MAX_NUMNODES for all nodes
240 * @p_start: ptr to ulong for start pfn of the range, can be %NULL
241 * @p_end: ptr to ulong for end pfn of the range, can be %NULL
242 * @p_nid: ptr to int for nid of the range, can be %NULL
243 *
Wanpeng Lif2d52fe2012-10-08 16:32:24 -0700244 * Walks over configured memory ranges.
Tejun Heo0ee332c2011-12-08 10:22:09 -0800245 */
246#define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
247 for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
248 i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
249#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
250
Tejun Heo35fd0802011-07-12 11:15:59 +0200251/**
252 * for_each_free_mem_range - iterate through free memblock areas
253 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800254 * @nid: node selector, %NUMA_NO_NODE for all nodes
Florian Fainellid30b5542016-01-14 15:22:04 -0800255 * @flags: pick from blocks based on memory attributes
Tejun Heo35fd0802011-07-12 11:15:59 +0200256 * @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 * @p_nid: ptr to int for nid of the range, can be %NULL
259 *
260 * Walks over free (memory && !reserved) areas of memblock. Available as
261 * soon as memblock is initialized.
262 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700263#define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100264 for_each_mem_range(i, &memblock.memory, &memblock.reserved, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700265 nid, flags, p_start, p_end, p_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800266
267/**
268 * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
269 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800270 * @nid: node selector, %NUMA_NO_NODE for all nodes
Florian Fainellid30b5542016-01-14 15:22:04 -0800271 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800272 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
273 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
274 * @p_nid: ptr to int for nid of the range, can be %NULL
275 *
276 * Walks over free (memory && !reserved) areas of memblock in reverse
277 * order. Available as soon as memblock is initialized.
278 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700279#define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
280 p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100281 for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700282 nid, flags, p_start, p_end, p_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800283
Tang Chen66b16ed2014-01-21 15:49:23 -0800284static inline void memblock_set_region_flags(struct memblock_region *r,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300285 enum memblock_flags flags)
Tang Chen66b16ed2014-01-21 15:49:23 -0800286{
287 r->flags |= flags;
288}
289
290static inline void memblock_clear_region_flags(struct memblock_region *r,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300291 enum memblock_flags flags)
Tang Chen66b16ed2014-01-21 15:49:23 -0800292{
293 r->flags &= ~flags;
294}
295
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200296#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Tang Chene7e8de52014-01-21 15:49:26 -0800297int memblock_set_node(phys_addr_t base, phys_addr_t size,
298 struct memblock_type *type, int nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200299
300static inline void memblock_set_region_node(struct memblock_region *r, int nid)
301{
302 r->nid = nid;
303}
304
305static inline int memblock_get_region_node(const struct memblock_region *r)
306{
307 return r->nid;
308}
309#else
310static inline void memblock_set_region_node(struct memblock_region *r, int nid)
311{
312}
313
314static inline int memblock_get_region_node(const struct memblock_region *r)
315{
316 return 0;
317}
318#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
319
Mike Rapoport57c8a662018-10-30 15:09:49 -0700320/* Flags for memblock allocation APIs */
321#define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
322#define MEMBLOCK_ALLOC_ACCESSIBLE 0
323
324/* We are using top down, so it is safe to use 0 here */
325#define MEMBLOCK_LOW_LIMIT 0
326
327#ifndef ARCH_LOW_ADDRESS_LIMIT
328#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
329#endif
330
Mike Rapoport9a8dd702018-10-30 15:07:59 -0700331phys_addr_t memblock_phys_alloc_nid(phys_addr_t size, phys_addr_t align, int nid);
332phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700333
Mike Rapoport9a8dd702018-10-30 15:07:59 -0700334phys_addr_t memblock_phys_alloc(phys_addr_t size, phys_addr_t align);
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700335
Mike Rapoport57c8a662018-10-30 15:09:49 -0700336void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align,
337 phys_addr_t min_addr, phys_addr_t max_addr,
338 int nid);
339void *memblock_alloc_try_nid_nopanic(phys_addr_t size, phys_addr_t align,
340 phys_addr_t min_addr, phys_addr_t max_addr,
341 int nid);
342void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align,
343 phys_addr_t min_addr, phys_addr_t max_addr,
344 int nid);
345
346static inline void * __init memblock_alloc(phys_addr_t size, phys_addr_t align)
347{
348 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
349 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
350}
351
352static inline void * __init memblock_alloc_raw(phys_addr_t size,
353 phys_addr_t align)
354{
355 return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
356 MEMBLOCK_ALLOC_ACCESSIBLE,
357 NUMA_NO_NODE);
358}
359
360static inline void * __init memblock_alloc_from(phys_addr_t size,
361 phys_addr_t align,
362 phys_addr_t min_addr)
363{
364 return memblock_alloc_try_nid(size, align, min_addr,
365 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
366}
367
368static inline void * __init memblock_alloc_nopanic(phys_addr_t size,
369 phys_addr_t align)
370{
371 return memblock_alloc_try_nid_nopanic(size, align, MEMBLOCK_LOW_LIMIT,
372 MEMBLOCK_ALLOC_ACCESSIBLE,
373 NUMA_NO_NODE);
374}
375
376static inline void * __init memblock_alloc_low(phys_addr_t size,
377 phys_addr_t align)
378{
379 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
380 ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE);
381}
382static inline void * __init memblock_alloc_low_nopanic(phys_addr_t size,
383 phys_addr_t align)
384{
385 return memblock_alloc_try_nid_nopanic(size, align, MEMBLOCK_LOW_LIMIT,
386 ARCH_LOW_ADDRESS_LIMIT,
387 NUMA_NO_NODE);
388}
389
390static inline void * __init memblock_alloc_from_nopanic(phys_addr_t size,
391 phys_addr_t align,
392 phys_addr_t min_addr)
393{
394 return memblock_alloc_try_nid_nopanic(size, align, min_addr,
395 MEMBLOCK_ALLOC_ACCESSIBLE,
396 NUMA_NO_NODE);
397}
398
399static inline void * __init memblock_alloc_node(phys_addr_t size,
400 phys_addr_t align, int nid)
401{
402 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
403 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
404}
405
406static inline void * __init memblock_alloc_node_nopanic(phys_addr_t size,
407 int nid)
408{
Mike Rapoport7e1c4e22018-10-30 15:09:57 -0700409 return memblock_alloc_try_nid_nopanic(size, SMP_CACHE_BYTES,
410 MEMBLOCK_LOW_LIMIT,
Mike Rapoport57c8a662018-10-30 15:09:49 -0700411 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
412}
413
414static inline void __init memblock_free_early(phys_addr_t base,
415 phys_addr_t size)
416{
417 __memblock_free_early(base, size);
418}
419
420static inline void __init memblock_free_early_nid(phys_addr_t base,
421 phys_addr_t size, int nid)
422{
423 __memblock_free_early(base, size);
424}
425
426static inline void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
427{
428 __memblock_free_late(base, size);
429}
430
Tang Chen79442ed2013-11-12 15:07:59 -0800431/*
432 * Set the allocation direction to bottom-up or top-down.
433 */
Fabian Frederick2cfb3662014-08-06 16:05:03 -0700434static inline void __init memblock_set_bottom_up(bool enable)
Tang Chen79442ed2013-11-12 15:07:59 -0800435{
436 memblock.bottom_up = enable;
437}
438
439/*
440 * Check if the allocation direction is bottom-up or not.
441 * if this is true, that said, memblock will allocate memory
442 * in bottom-up direction.
443 */
444static inline bool memblock_bottom_up(void)
445{
446 return memblock.bottom_up;
447}
Tang Chen79442ed2013-11-12 15:07:59 -0800448
Akinobu Mita2bfc2862014-06-04 16:06:53 -0700449phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700450 phys_addr_t start, phys_addr_t end,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300451 enum memblock_flags flags);
Nicholas Pigginb575454f2018-02-14 01:08:15 +1000452phys_addr_t memblock_alloc_base_nid(phys_addr_t size,
453 phys_addr_t align, phys_addr_t max_addr,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300454 int nid, enum memblock_flags flags);
Tejun Heo581adcb2011-12-08 10:22:06 -0800455phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align,
456 phys_addr_t max_addr);
457phys_addr_t __memblock_alloc_base(phys_addr_t size, phys_addr_t align,
458 phys_addr_t max_addr);
459phys_addr_t memblock_phys_mem_size(void);
Srikar Dronamraju8907de52016-10-07 16:59:18 -0700460phys_addr_t memblock_reserved_size(void);
Yinghai Lu595ad9a2013-01-24 12:20:09 -0800461phys_addr_t memblock_mem_size(unsigned long limit_pfn);
Tejun Heo581adcb2011-12-08 10:22:06 -0800462phys_addr_t memblock_start_of_DRAM(void);
463phys_addr_t memblock_end_of_DRAM(void);
464void memblock_enforce_memory_limit(phys_addr_t memory_limit);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +0900465void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
Dennis Chena571d4e2016-07-28 15:48:26 -0700466void memblock_mem_limit_remove_map(phys_addr_t limit);
Yaowei Baib4ad0c72016-01-14 15:18:54 -0800467bool memblock_is_memory(phys_addr_t addr);
Yaowei Bai937f0c22018-02-06 15:41:18 -0800468bool memblock_is_map_memory(phys_addr_t addr);
469bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
Yaowei Baib4ad0c72016-01-14 15:18:54 -0800470bool memblock_is_reserved(phys_addr_t addr);
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700471bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000472
Tejun Heo4ff7b822011-12-08 10:22:06 -0800473extern void __memblock_dump_all(void);
474
475static inline void memblock_dump_all(void)
476{
477 if (memblock_debug)
478 __memblock_dump_all();
479}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000480
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700481/**
482 * memblock_set_current_limit - Set the current allocation limit to allow
483 * limiting allocations to what is currently
484 * accessible during boot
485 * @limit: New limit value (physical address)
486 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800487void memblock_set_current_limit(phys_addr_t limit);
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700488
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700489
Laura Abbottfec51012014-02-27 01:23:43 +0100490phys_addr_t memblock_get_current_limit(void);
491
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000492/*
493 * pfn conversion functions
494 *
495 * While the memory MEMBLOCKs should always be page aligned, the reserved
496 * MEMBLOCKs may not be. This accessor attempt to provide a very clear
497 * idea of what they return for such non aligned MEMBLOCKs.
498 */
499
500/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300501 * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000502 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300503 *
504 * Return: the lowest pfn intersecting with the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000505 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700506static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000507{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700508 return PFN_UP(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000509}
510
511/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300512 * memblock_region_memory_end_pfn - get the end pfn of the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000513 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300514 *
515 * Return: the end_pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000516 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700517static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000518{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700519 return PFN_DOWN(reg->base + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000520}
521
522/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300523 * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000524 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300525 *
526 * Return: the lowest pfn intersecting with the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000527 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700528static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000529{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700530 return PFN_DOWN(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000531}
532
533/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300534 * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000535 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300536 *
537 * Return: the end_pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000538 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700539static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000540{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700541 return PFN_UP(reg->base + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000542}
543
544#define for_each_memblock(memblock_type, region) \
Chen Gangba6c19f2016-07-26 15:24:47 -0700545 for (region = memblock.memblock_type.regions; \
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000546 region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \
547 region++)
548
Gioh Kim66e8b432017-11-15 17:33:42 -0800549#define for_each_memblock_type(i, memblock_type, rgn) \
550 for (i = 0, rgn = &memblock_type->regions[0]; \
551 i < memblock_type->cnt; \
552 i++, rgn = &memblock_type->regions[i])
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000553
Mike Rapoport57c8a662018-10-30 15:09:49 -0700554extern void *alloc_large_system_hash(const char *tablename,
555 unsigned long bucketsize,
556 unsigned long numentries,
557 int scale,
558 int flags,
559 unsigned int *_hash_shift,
560 unsigned int *_hash_mask,
561 unsigned long low_limit,
562 unsigned long high_limit);
563
564#define HASH_EARLY 0x00000001 /* Allocating during early boot? */
565#define HASH_SMALL 0x00000002 /* sub-page allocation allowed, min
566 * shift passed via *_hash_shift */
567#define HASH_ZERO 0x00000004 /* Zero allocated hash table */
568
569/* Only NUMA needs hash distribution. 64bit NUMA architectures have
570 * sufficient vmalloc space.
571 */
572#ifdef CONFIG_NUMA
573#define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
574extern int hashdist; /* Distribute hashes across NUMA nodes? */
575#else
576#define hashdist (0)
577#endif
578
Vladimir Murzin4a207992015-04-14 15:48:27 -0700579#ifdef CONFIG_MEMTEST
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700580extern void early_memtest(phys_addr_t start, phys_addr_t end);
Vladimir Murzin4a207992015-04-14 15:48:27 -0700581#else
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700582static inline void early_memtest(phys_addr_t start, phys_addr_t end)
Vladimir Murzin4a207992015-04-14 15:48:27 -0700583{
584}
585#endif
Yinghai Luf0b37fad2010-07-28 15:28:21 +1000586
Yinghai Lu95f72d12010-07-12 14:36:09 +1000587#endif /* __KERNEL__ */
588
589#endif /* _LINUX_MEMBLOCK_H */