blob: f8b78892b977f582010046cd758cef17ca1ab54e [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
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030032/**
33 * enum memblock_flags - definition of memory region attributes
34 * @MEMBLOCK_NONE: no special request
35 * @MEMBLOCK_HOTPLUG: hotpluggable region
36 * @MEMBLOCK_MIRROR: mirrored region
37 * @MEMBLOCK_NOMAP: don't add to kernel direct mapping
38 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +030039enum memblock_flags {
Tony Luckfc6daaf2015-06-24 16:58:09 -070040 MEMBLOCK_NONE = 0x0, /* No special request */
41 MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */
Tony Lucka3f5baf2015-06-24 16:58:12 -070042 MEMBLOCK_MIRROR = 0x2, /* mirrored region */
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +010043 MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */
Tony Luckfc6daaf2015-06-24 16:58:09 -070044};
Tang Chen66b16ed2014-01-21 15:49:23 -080045
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030046/**
47 * struct memblock_region - represents a memory region
48 * @base: physical address of the region
49 * @size: size of the region
50 * @flags: memory region attributes
51 * @nid: NUMA node id
52 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100053struct memblock_region {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100054 phys_addr_t base;
55 phys_addr_t size;
Mike Rapoporte1720fe2018-06-30 17:55:01 +030056 enum memblock_flags flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +020057#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
58 int nid;
59#endif
Yinghai Lu95f72d12010-07-12 14:36:09 +100060};
61
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030062/**
63 * struct memblock_type - collection of memory regions of certain type
64 * @cnt: number of regions
65 * @max: size of the allocated array
66 * @total_size: size of all regions
67 * @regions: array of regions
68 * @name: the memory type symbolic name
69 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100070struct memblock_type {
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030071 unsigned long cnt;
72 unsigned long max;
73 phys_addr_t total_size;
Benjamin Herrenschmidtbf23c512010-07-06 15:39:06 -070074 struct memblock_region *regions;
Heiko Carstens0262d9c2017-02-24 14:55:59 -080075 char *name;
Yinghai Lu95f72d12010-07-12 14:36:09 +100076};
77
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030078/**
79 * struct memblock - memblock allocator metadata
80 * @bottom_up: is bottom up direction?
81 * @current_limit: physical address of the current allocation limit
82 * @memory: usabe memory regions
83 * @reserved: reserved memory regions
84 * @physmem: all physical memory
85 */
Yinghai Lu95f72d12010-07-12 14:36:09 +100086struct memblock {
Tang Chen79442ed2013-11-12 15:07:59 -080087 bool bottom_up; /* is bottom up direction? */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100088 phys_addr_t current_limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100089 struct memblock_type memory;
90 struct memblock_type reserved;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010091#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
92 struct memblock_type physmem;
93#endif
Yinghai Lu95f72d12010-07-12 14:36:09 +100094};
95
96extern struct memblock memblock;
Yinghai Lu5e63cf42010-07-28 15:07:21 +100097extern int memblock_debug;
Yinghai Lu5e63cf42010-07-28 15:07:21 +100098
Kirill A. Shutemov036fbb22016-01-15 16:57:11 -080099#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
100#define __init_memblock __meminit
101#define __initdata_memblock __meminitdata
Pavel Tatashin3010f872017-08-18 15:16:05 -0700102void memblock_discard(void);
Kirill A. Shutemov036fbb22016-01-15 16:57:11 -0800103#else
104#define __init_memblock
105#define __initdata_memblock
106#endif
107
Yinghai Lu5e63cf42010-07-28 15:07:21 +1000108#define memblock_dbg(fmt, ...) \
109 if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000110
Tejun Heofc769a82011-07-12 09:58:10 +0200111phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
112 phys_addr_t size, phys_addr_t align);
Tejun Heo1aadc052011-12-08 10:22:08 -0800113void memblock_allow_resize(void);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800114int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
Tejun Heo581adcb2011-12-08 10:22:06 -0800115int memblock_add(phys_addr_t base, phys_addr_t size);
116int memblock_remove(phys_addr_t base, phys_addr_t size);
117int memblock_free(phys_addr_t base, phys_addr_t size);
118int memblock_reserve(phys_addr_t base, phys_addr_t size);
Yinghai Lu6ede1fd2012-10-22 16:35:18 -0700119void memblock_trim_memory(phys_addr_t align);
Tang Chen95cf82e2015-09-08 15:02:03 -0700120bool memblock_overlaps_region(struct memblock_type *type,
121 phys_addr_t base, phys_addr_t size);
Tang Chen66b16ed2014-01-21 15:49:23 -0800122int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
123int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
Tony Lucka3f5baf2015-06-24 16:58:12 -0700124int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100125int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900126int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100127
Mike Rapoport57c8a662018-10-30 15:09:49 -0700128unsigned long memblock_free_all(void);
129void reset_node_managed_pages(pg_data_t *pgdat);
130void reset_all_zones_managed_pages(void);
131
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100132/* Low level functions */
133int memblock_add_range(struct memblock_type *type,
134 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300135 int nid, enum memblock_flags flags);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100136
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300137void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700138 struct memblock_type *type_a,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100139 struct memblock_type *type_b, phys_addr_t *out_start,
140 phys_addr_t *out_end, int *out_nid);
141
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300142void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700143 struct memblock_type *type_a,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100144 struct memblock_type *type_b, phys_addr_t *out_start,
145 phys_addr_t *out_end, int *out_nid);
146
Robin Holt8e7a7f82015-06-30 14:56:41 -0700147void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start,
Chen Gangba6c19f2016-07-26 15:24:47 -0700148 phys_addr_t *out_end);
Robin Holt8e7a7f82015-06-30 14:56:41 -0700149
Pavel Tatashin3010f872017-08-18 15:16:05 -0700150void __memblock_free_late(phys_addr_t base, phys_addr_t size);
151
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100152/**
153 * for_each_mem_range - iterate through memblock areas from type_a and not
154 * included in type_b. Or just type_a if type_b is NULL.
155 * @i: u64 used as loop variable
156 * @type_a: ptr to memblock_type to iterate
157 * @type_b: ptr to memblock_type which excludes from the iteration
158 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700159 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100160 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
161 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
162 * @p_nid: ptr to int for nid of the range, can be %NULL
163 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700164#define for_each_mem_range(i, type_a, type_b, nid, flags, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100165 p_start, p_end, p_nid) \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700166 for (i = 0, __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 i != (u64)ULLONG_MAX; \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700169 __next_mem_range(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100170 p_start, p_end, p_nid))
171
172/**
173 * for_each_mem_range_rev - reverse iterate through memblock areas from
174 * type_a and not included in type_b. Or just type_a if type_b is NULL.
175 * @i: u64 used as loop variable
176 * @type_a: ptr to memblock_type to iterate
177 * @type_b: ptr to memblock_type which excludes from the iteration
178 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700179 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100180 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
181 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
182 * @p_nid: ptr to int for nid of the range, can be %NULL
183 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700184#define for_each_mem_range_rev(i, type_a, type_b, nid, flags, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100185 p_start, p_end, p_nid) \
186 for (i = (u64)ULLONG_MAX, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700187 __next_mem_range_rev(&i, nid, flags, type_a, type_b,\
Chen Gangba6c19f2016-07-26 15:24:47 -0700188 p_start, p_end, p_nid); \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100189 i != (u64)ULLONG_MAX; \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700190 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100191 p_start, p_end, p_nid))
192
Robin Holt8e7a7f82015-06-30 14:56:41 -0700193/**
194 * for_each_reserved_mem_region - iterate over all reserved memblock areas
195 * @i: u64 used as loop variable
196 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
197 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
198 *
199 * Walks over reserved areas of memblock. Available as soon as memblock
200 * is initialized.
201 */
202#define for_each_reserved_mem_region(i, p_start, p_end) \
Chen Gangba6c19f2016-07-26 15:24:47 -0700203 for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end); \
Robin Holt8e7a7f82015-06-30 14:56:41 -0700204 i != (u64)ULLONG_MAX; \
205 __next_reserved_mem_region(&i, p_start, p_end))
206
Tang Chen55ac5902014-01-21 15:49:35 -0800207static inline bool memblock_is_hotpluggable(struct memblock_region *m)
208{
209 return m->flags & MEMBLOCK_HOTPLUG;
210}
211
Tony Lucka3f5baf2015-06-24 16:58:12 -0700212static inline bool memblock_is_mirror(struct memblock_region *m)
213{
214 return m->flags & MEMBLOCK_MIRROR;
215}
216
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100217static inline bool memblock_is_nomap(struct memblock_region *m)
218{
219 return m->flags & MEMBLOCK_NOMAP;
220}
221
Tejun Heo0ee332c2011-12-08 10:22:09 -0800222#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Yinghai Lue76b63f2013-09-11 14:22:17 -0700223int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
224 unsigned long *end_pfn);
Tejun Heo0ee332c2011-12-08 10:22:09 -0800225void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
226 unsigned long *out_end_pfn, int *out_nid);
227
228/**
229 * for_each_mem_pfn_range - early memory pfn range iterator
230 * @i: an integer used as loop variable
231 * @nid: node selector, %MAX_NUMNODES for all nodes
232 * @p_start: ptr to ulong for start pfn of the range, can be %NULL
233 * @p_end: ptr to ulong for end pfn of the range, can be %NULL
234 * @p_nid: ptr to int for nid of the range, can be %NULL
235 *
Wanpeng Lif2d52fe2012-10-08 16:32:24 -0700236 * Walks over configured memory ranges.
Tejun Heo0ee332c2011-12-08 10:22:09 -0800237 */
238#define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
239 for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
240 i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
241#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
242
Alexander Duyck837566e2019-05-13 17:21:17 -0700243#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
244void __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
245 unsigned long *out_spfn,
246 unsigned long *out_epfn);
247/**
248 * for_each_free_mem_range_in_zone - iterate through zone specific free
249 * memblock areas
250 * @i: u64 used as loop variable
251 * @zone: zone in which all of the memory blocks reside
252 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
253 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
254 *
255 * Walks over free (memory && !reserved) areas of memblock in a specific
256 * zone. Available once memblock and an empty zone is initialized. The main
257 * assumption is that the zone start, end, and pgdat have been associated.
258 * This way we can use the zone to determine NUMA node, and if a given part
259 * of the memblock is valid for the zone.
260 */
261#define for_each_free_mem_pfn_range_in_zone(i, zone, p_start, p_end) \
262 for (i = 0, \
263 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end); \
264 i != U64_MAX; \
265 __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
266#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
267
Tejun Heo35fd0802011-07-12 11:15:59 +0200268/**
269 * for_each_free_mem_range - iterate through free memblock areas
270 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800271 * @nid: node selector, %NUMA_NO_NODE for all nodes
Florian Fainellid30b5542016-01-14 15:22:04 -0800272 * @flags: pick from blocks based on memory attributes
Tejun Heo35fd0802011-07-12 11:15:59 +0200273 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
274 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
275 * @p_nid: ptr to int for nid of the range, can be %NULL
276 *
277 * Walks over free (memory && !reserved) areas of memblock. Available as
278 * soon as memblock is initialized.
279 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700280#define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100281 for_each_mem_range(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
284/**
285 * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
286 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800287 * @nid: node selector, %NUMA_NO_NODE for all nodes
Florian Fainellid30b5542016-01-14 15:22:04 -0800288 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800289 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
290 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
291 * @p_nid: ptr to int for nid of the range, can be %NULL
292 *
293 * Walks over free (memory && !reserved) areas of memblock in reverse
294 * order. Available as soon as memblock is initialized.
295 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700296#define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
297 p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100298 for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700299 nid, flags, p_start, p_end, p_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800300
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200301#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Tang Chene7e8de52014-01-21 15:49:26 -0800302int memblock_set_node(phys_addr_t base, phys_addr_t size,
303 struct memblock_type *type, int nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200304
305static inline void memblock_set_region_node(struct memblock_region *r, int nid)
306{
307 r->nid = nid;
308}
309
310static inline int memblock_get_region_node(const struct memblock_region *r)
311{
312 return r->nid;
313}
314#else
315static inline void memblock_set_region_node(struct memblock_region *r, int nid)
316{
317}
318
319static inline int memblock_get_region_node(const struct memblock_region *r)
320{
321 return 0;
322}
323#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
324
Mike Rapoport57c8a662018-10-30 15:09:49 -0700325/* Flags for memblock allocation APIs */
326#define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
327#define MEMBLOCK_ALLOC_ACCESSIBLE 0
Qian Caifed84c72018-12-28 00:36:29 -0800328#define MEMBLOCK_ALLOC_KASAN 1
Mike Rapoport57c8a662018-10-30 15:09:49 -0700329
330/* We are using top down, so it is safe to use 0 here */
331#define MEMBLOCK_LOW_LIMIT 0
332
333#ifndef ARCH_LOW_ADDRESS_LIMIT
334#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
335#endif
336
Mike Rapoport8a770c22019-03-11 23:29:16 -0700337phys_addr_t memblock_phys_alloc_range(phys_addr_t size, phys_addr_t align,
338 phys_addr_t start, phys_addr_t end);
Mike Rapoport9a8dd702018-10-30 15:07:59 -0700339phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700340
Mike Rapoportecc3e772019-03-11 23:29:26 -0700341static inline phys_addr_t memblock_phys_alloc(phys_addr_t size,
342 phys_addr_t align)
343{
344 return memblock_phys_alloc_range(size, align, 0,
345 MEMBLOCK_ALLOC_ACCESSIBLE);
346}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700347
Mike Rapoport57c8a662018-10-30 15:09:49 -0700348void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align,
349 phys_addr_t min_addr, phys_addr_t max_addr,
350 int nid);
Mike Rapoport57c8a662018-10-30 15:09:49 -0700351void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align,
352 phys_addr_t min_addr, phys_addr_t max_addr,
353 int nid);
354
355static inline void * __init memblock_alloc(phys_addr_t size, phys_addr_t align)
356{
357 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
358 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
359}
360
361static inline void * __init memblock_alloc_raw(phys_addr_t size,
362 phys_addr_t align)
363{
364 return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
365 MEMBLOCK_ALLOC_ACCESSIBLE,
366 NUMA_NO_NODE);
367}
368
369static inline void * __init memblock_alloc_from(phys_addr_t size,
370 phys_addr_t align,
371 phys_addr_t min_addr)
372{
373 return memblock_alloc_try_nid(size, align, min_addr,
374 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
375}
376
Mike Rapoport57c8a662018-10-30 15:09:49 -0700377static inline void * __init memblock_alloc_low(phys_addr_t size,
378 phys_addr_t align)
379{
380 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
381 ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE);
382}
Mike Rapoport57c8a662018-10-30 15:09:49 -0700383
384static inline void * __init memblock_alloc_node(phys_addr_t size,
385 phys_addr_t align, int nid)
386{
387 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
388 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
389}
390
Mike Rapoport57c8a662018-10-30 15:09:49 -0700391static inline void __init memblock_free_early(phys_addr_t base,
392 phys_addr_t size)
393{
Mike Rapoport4d728682018-12-28 00:35:29 -0800394 memblock_free(base, size);
Mike Rapoport57c8a662018-10-30 15:09:49 -0700395}
396
397static inline void __init memblock_free_early_nid(phys_addr_t base,
398 phys_addr_t size, int nid)
399{
Mike Rapoport4d728682018-12-28 00:35:29 -0800400 memblock_free(base, size);
Mike Rapoport57c8a662018-10-30 15:09:49 -0700401}
402
403static inline void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
404{
405 __memblock_free_late(base, size);
406}
407
Tang Chen79442ed2013-11-12 15:07:59 -0800408/*
409 * Set the allocation direction to bottom-up or top-down.
410 */
Fabian Frederick2cfb3662014-08-06 16:05:03 -0700411static inline void __init memblock_set_bottom_up(bool enable)
Tang Chen79442ed2013-11-12 15:07:59 -0800412{
413 memblock.bottom_up = enable;
414}
415
416/*
417 * Check if the allocation direction is bottom-up or not.
418 * if this is true, that said, memblock will allocate memory
419 * in bottom-up direction.
420 */
421static inline bool memblock_bottom_up(void)
422{
423 return memblock.bottom_up;
424}
Tang Chen79442ed2013-11-12 15:07:59 -0800425
Tejun Heo581adcb2011-12-08 10:22:06 -0800426phys_addr_t memblock_phys_mem_size(void);
Srikar Dronamraju8907de52016-10-07 16:59:18 -0700427phys_addr_t memblock_reserved_size(void);
Yinghai Lu595ad9a2013-01-24 12:20:09 -0800428phys_addr_t memblock_mem_size(unsigned long limit_pfn);
Tejun Heo581adcb2011-12-08 10:22:06 -0800429phys_addr_t memblock_start_of_DRAM(void);
430phys_addr_t memblock_end_of_DRAM(void);
431void memblock_enforce_memory_limit(phys_addr_t memory_limit);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +0900432void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
Dennis Chena571d4e2016-07-28 15:48:26 -0700433void memblock_mem_limit_remove_map(phys_addr_t limit);
Yaowei Baib4ad0c72016-01-14 15:18:54 -0800434bool memblock_is_memory(phys_addr_t addr);
Yaowei Bai937f0c22018-02-06 15:41:18 -0800435bool memblock_is_map_memory(phys_addr_t addr);
436bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
Yaowei Baib4ad0c72016-01-14 15:18:54 -0800437bool memblock_is_reserved(phys_addr_t addr);
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700438bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000439
Tejun Heo4ff7b822011-12-08 10:22:06 -0800440extern void __memblock_dump_all(void);
441
442static inline void memblock_dump_all(void)
443{
444 if (memblock_debug)
445 __memblock_dump_all();
446}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000447
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700448/**
449 * memblock_set_current_limit - Set the current allocation limit to allow
450 * limiting allocations to what is currently
451 * accessible during boot
452 * @limit: New limit value (physical address)
453 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800454void memblock_set_current_limit(phys_addr_t limit);
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700455
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700456
Laura Abbottfec51012014-02-27 01:23:43 +0100457phys_addr_t memblock_get_current_limit(void);
458
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000459/*
460 * pfn conversion functions
461 *
462 * While the memory MEMBLOCKs should always be page aligned, the reserved
463 * MEMBLOCKs may not be. This accessor attempt to provide a very clear
464 * idea of what they return for such non aligned MEMBLOCKs.
465 */
466
467/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300468 * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000469 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300470 *
471 * Return: the lowest pfn intersecting with the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000472 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700473static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000474{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700475 return PFN_UP(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000476}
477
478/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300479 * memblock_region_memory_end_pfn - get the end pfn of the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000480 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300481 *
482 * Return: the end_pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000483 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700484static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000485{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700486 return PFN_DOWN(reg->base + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000487}
488
489/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300490 * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000491 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300492 *
493 * Return: the lowest pfn intersecting with the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000494 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700495static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000496{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700497 return PFN_DOWN(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000498}
499
500/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300501 * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000502 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300503 *
504 * Return: the end_pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000505 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700506static inline unsigned long memblock_region_reserved_end_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 + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000509}
510
511#define for_each_memblock(memblock_type, region) \
Chen Gangba6c19f2016-07-26 15:24:47 -0700512 for (region = memblock.memblock_type.regions; \
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000513 region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \
514 region++)
515
Gioh Kim66e8b432017-11-15 17:33:42 -0800516#define for_each_memblock_type(i, memblock_type, rgn) \
517 for (i = 0, rgn = &memblock_type->regions[0]; \
518 i < memblock_type->cnt; \
519 i++, rgn = &memblock_type->regions[i])
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000520
Mike Rapoport57c8a662018-10-30 15:09:49 -0700521extern void *alloc_large_system_hash(const char *tablename,
522 unsigned long bucketsize,
523 unsigned long numentries,
524 int scale,
525 int flags,
526 unsigned int *_hash_shift,
527 unsigned int *_hash_mask,
528 unsigned long low_limit,
529 unsigned long high_limit);
530
531#define HASH_EARLY 0x00000001 /* Allocating during early boot? */
532#define HASH_SMALL 0x00000002 /* sub-page allocation allowed, min
533 * shift passed via *_hash_shift */
534#define HASH_ZERO 0x00000004 /* Zero allocated hash table */
535
536/* Only NUMA needs hash distribution. 64bit NUMA architectures have
537 * sufficient vmalloc space.
538 */
539#ifdef CONFIG_NUMA
540#define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
541extern int hashdist; /* Distribute hashes across NUMA nodes? */
542#else
543#define hashdist (0)
544#endif
545
Vladimir Murzin4a207992015-04-14 15:48:27 -0700546#ifdef CONFIG_MEMTEST
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700547extern void early_memtest(phys_addr_t start, phys_addr_t end);
Vladimir Murzin4a207992015-04-14 15:48:27 -0700548#else
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700549static inline void early_memtest(phys_addr_t start, phys_addr_t end)
Vladimir Murzin4a207992015-04-14 15:48:27 -0700550{
551}
552#endif
Yinghai Luf0b37fad2010-07-28 15:28:21 +1000553
Yinghai Lu95f72d12010-07-12 14:36:09 +1000554#endif /* __KERNEL__ */
555
556#endif /* _LINUX_MEMBLOCK_H */