blob: 9d46a7204975083a0d0863a39c846407904f277a [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>
18
Yinghai Lu37d8d4b2010-07-28 15:20:58 +100019#define INIT_MEMBLOCK_REGIONS 128
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010020#define INIT_PHYSMEM_REGIONS 4
Yinghai Lu95f72d12010-07-12 14:36:09 +100021
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030022/**
23 * enum memblock_flags - definition of memory region attributes
24 * @MEMBLOCK_NONE: no special request
25 * @MEMBLOCK_HOTPLUG: hotpluggable region
26 * @MEMBLOCK_MIRROR: mirrored region
27 * @MEMBLOCK_NOMAP: don't add to kernel direct mapping
28 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +030029enum memblock_flags {
Tony Luckfc6daaf2015-06-24 16:58:09 -070030 MEMBLOCK_NONE = 0x0, /* No special request */
31 MEMBLOCK_HOTPLUG = 0x1, /* hotpluggable region */
Tony Lucka3f5baf2015-06-24 16:58:12 -070032 MEMBLOCK_MIRROR = 0x2, /* mirrored region */
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +010033 MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */
Tony Luckfc6daaf2015-06-24 16:58:09 -070034};
Tang Chen66b16ed2014-01-21 15:49:23 -080035
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030036/**
37 * struct memblock_region - represents a memory region
38 * @base: physical address of the region
39 * @size: size of the region
40 * @flags: memory region attributes
41 * @nid: NUMA node id
42 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100043struct memblock_region {
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100044 phys_addr_t base;
45 phys_addr_t size;
Mike Rapoporte1720fe2018-06-30 17:55:01 +030046 enum memblock_flags flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +020047#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
48 int nid;
49#endif
Yinghai Lu95f72d12010-07-12 14:36:09 +100050};
51
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030052/**
53 * struct memblock_type - collection of memory regions of certain type
54 * @cnt: number of regions
55 * @max: size of the allocated array
56 * @total_size: size of all regions
57 * @regions: array of regions
58 * @name: the memory type symbolic name
59 */
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100060struct memblock_type {
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030061 unsigned long cnt;
62 unsigned long max;
63 phys_addr_t total_size;
Benjamin Herrenschmidtbf23c512010-07-06 15:39:06 -070064 struct memblock_region *regions;
Heiko Carstens0262d9c2017-02-24 14:55:59 -080065 char *name;
Yinghai Lu95f72d12010-07-12 14:36:09 +100066};
67
Mike Rapoport9a0de1b2018-06-30 17:55:04 +030068/**
69 * struct memblock - memblock allocator metadata
70 * @bottom_up: is bottom up direction?
71 * @current_limit: physical address of the current allocation limit
72 * @memory: usabe memory regions
73 * @reserved: reserved memory regions
74 * @physmem: all physical memory
75 */
Yinghai Lu95f72d12010-07-12 14:36:09 +100076struct memblock {
Tang Chen79442ed2013-11-12 15:07:59 -080077 bool bottom_up; /* is bottom up direction? */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100078 phys_addr_t current_limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +100079 struct memblock_type memory;
80 struct memblock_type reserved;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010081#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
82 struct memblock_type physmem;
83#endif
Yinghai Lu95f72d12010-07-12 14:36:09 +100084};
85
86extern struct memblock memblock;
Yinghai Lu5e63cf42010-07-28 15:07:21 +100087extern int memblock_debug;
Yinghai Lu5e63cf42010-07-28 15:07:21 +100088
Kirill A. Shutemov036fbb22016-01-15 16:57:11 -080089#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
90#define __init_memblock __meminit
91#define __initdata_memblock __meminitdata
Pavel Tatashin3010f872017-08-18 15:16:05 -070092void memblock_discard(void);
Kirill A. Shutemov036fbb22016-01-15 16:57:11 -080093#else
94#define __init_memblock
95#define __initdata_memblock
96#endif
97
Yinghai Lu5e63cf42010-07-28 15:07:21 +100098#define memblock_dbg(fmt, ...) \
99 if (memblock_debug) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000100
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800101phys_addr_t memblock_find_in_range_node(phys_addr_t size, phys_addr_t align,
Chen Gangba6c19f2016-07-26 15:24:47 -0700102 phys_addr_t start, phys_addr_t end,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300103 int nid, enum memblock_flags flags);
Tejun Heofc769a82011-07-12 09:58:10 +0200104phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
105 phys_addr_t size, phys_addr_t align);
Tejun Heo1aadc052011-12-08 10:22:08 -0800106void memblock_allow_resize(void);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800107int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
Tejun Heo581adcb2011-12-08 10:22:06 -0800108int memblock_add(phys_addr_t base, phys_addr_t size);
109int memblock_remove(phys_addr_t base, phys_addr_t size);
110int memblock_free(phys_addr_t base, phys_addr_t size);
111int memblock_reserve(phys_addr_t base, phys_addr_t size);
Yinghai Lu6ede1fd2012-10-22 16:35:18 -0700112void memblock_trim_memory(phys_addr_t align);
Tang Chen95cf82e2015-09-08 15:02:03 -0700113bool memblock_overlaps_region(struct memblock_type *type,
114 phys_addr_t base, phys_addr_t size);
Tang Chen66b16ed2014-01-21 15:49:23 -0800115int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
116int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
Tony Lucka3f5baf2015-06-24 16:58:12 -0700117int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100118int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900119int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300120enum memblock_flags choose_memblock_flags(void);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100121
122/* Low level functions */
123int memblock_add_range(struct memblock_type *type,
124 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300125 int nid, enum memblock_flags flags);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100126
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300127void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700128 struct memblock_type *type_a,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100129 struct memblock_type *type_b, phys_addr_t *out_start,
130 phys_addr_t *out_end, int *out_nid);
131
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300132void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700133 struct memblock_type *type_a,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100134 struct memblock_type *type_b, phys_addr_t *out_start,
135 phys_addr_t *out_end, int *out_nid);
136
Robin Holt8e7a7f82015-06-30 14:56:41 -0700137void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start,
Chen Gangba6c19f2016-07-26 15:24:47 -0700138 phys_addr_t *out_end);
Robin Holt8e7a7f82015-06-30 14:56:41 -0700139
Pavel Tatashin3010f872017-08-18 15:16:05 -0700140void __memblock_free_early(phys_addr_t base, phys_addr_t size);
141void __memblock_free_late(phys_addr_t base, phys_addr_t size);
142
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100143/**
144 * for_each_mem_range - iterate through memblock areas from type_a and not
145 * included in type_b. Or just type_a if type_b is NULL.
146 * @i: u64 used as loop variable
147 * @type_a: ptr to memblock_type to iterate
148 * @type_b: ptr to memblock_type which excludes from the iteration
149 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700150 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100151 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
152 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
153 * @p_nid: ptr to int for nid of the range, can be %NULL
154 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700155#define for_each_mem_range(i, type_a, type_b, nid, flags, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100156 p_start, p_end, p_nid) \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700157 for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100158 p_start, p_end, p_nid); \
159 i != (u64)ULLONG_MAX; \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700160 __next_mem_range(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100161 p_start, p_end, p_nid))
162
163/**
164 * for_each_mem_range_rev - reverse iterate through memblock areas from
165 * type_a and not included in type_b. Or just type_a if type_b is NULL.
166 * @i: u64 used as loop variable
167 * @type_a: ptr to memblock_type to iterate
168 * @type_b: ptr to memblock_type which excludes from the iteration
169 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700170 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100171 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
172 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
173 * @p_nid: ptr to int for nid of the range, can be %NULL
174 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700175#define for_each_mem_range_rev(i, type_a, type_b, nid, flags, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100176 p_start, p_end, p_nid) \
177 for (i = (u64)ULLONG_MAX, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700178 __next_mem_range_rev(&i, nid, flags, type_a, type_b,\
Chen Gangba6c19f2016-07-26 15:24:47 -0700179 p_start, p_end, p_nid); \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100180 i != (u64)ULLONG_MAX; \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700181 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100182 p_start, p_end, p_nid))
183
Robin Holt8e7a7f82015-06-30 14:56:41 -0700184/**
185 * for_each_reserved_mem_region - iterate over all reserved memblock areas
186 * @i: u64 used as loop variable
187 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
188 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
189 *
190 * Walks over reserved areas of memblock. Available as soon as memblock
191 * is initialized.
192 */
193#define for_each_reserved_mem_region(i, p_start, p_end) \
Chen Gangba6c19f2016-07-26 15:24:47 -0700194 for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end); \
Robin Holt8e7a7f82015-06-30 14:56:41 -0700195 i != (u64)ULLONG_MAX; \
196 __next_reserved_mem_region(&i, p_start, p_end))
197
Tang Chen55ac5902014-01-21 15:49:35 -0800198static inline bool memblock_is_hotpluggable(struct memblock_region *m)
199{
200 return m->flags & MEMBLOCK_HOTPLUG;
201}
202
Tony Lucka3f5baf2015-06-24 16:58:12 -0700203static inline bool memblock_is_mirror(struct memblock_region *m)
204{
205 return m->flags & MEMBLOCK_MIRROR;
206}
207
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100208static inline bool memblock_is_nomap(struct memblock_region *m)
209{
210 return m->flags & MEMBLOCK_NOMAP;
211}
212
Tejun Heo0ee332c2011-12-08 10:22:09 -0800213#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Yinghai Lue76b63f2013-09-11 14:22:17 -0700214int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
215 unsigned long *end_pfn);
Tejun Heo0ee332c2011-12-08 10:22:09 -0800216void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
217 unsigned long *out_end_pfn, int *out_nid);
218
219/**
220 * for_each_mem_pfn_range - early memory pfn range iterator
221 * @i: an integer used as loop variable
222 * @nid: node selector, %MAX_NUMNODES for all nodes
223 * @p_start: ptr to ulong for start pfn of the range, can be %NULL
224 * @p_end: ptr to ulong for end pfn of the range, can be %NULL
225 * @p_nid: ptr to int for nid of the range, can be %NULL
226 *
Wanpeng Lif2d52fe2012-10-08 16:32:24 -0700227 * Walks over configured memory ranges.
Tejun Heo0ee332c2011-12-08 10:22:09 -0800228 */
229#define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
230 for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
231 i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
232#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
233
Tejun Heo35fd0802011-07-12 11:15:59 +0200234/**
235 * for_each_free_mem_range - iterate through free memblock areas
236 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800237 * @nid: node selector, %NUMA_NO_NODE for all nodes
Florian Fainellid30b5542016-01-14 15:22:04 -0800238 * @flags: pick from blocks based on memory attributes
Tejun Heo35fd0802011-07-12 11:15:59 +0200239 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
240 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
241 * @p_nid: ptr to int for nid of the range, can be %NULL
242 *
243 * Walks over free (memory && !reserved) areas of memblock. Available as
244 * soon as memblock is initialized.
245 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700246#define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100247 for_each_mem_range(i, &memblock.memory, &memblock.reserved, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700248 nid, flags, p_start, p_end, p_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800249
250/**
251 * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
252 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800253 * @nid: node selector, %NUMA_NO_NODE for all nodes
Florian Fainellid30b5542016-01-14 15:22:04 -0800254 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800255 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
256 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
257 * @p_nid: ptr to int for nid of the range, can be %NULL
258 *
259 * Walks over free (memory && !reserved) areas of memblock in reverse
260 * order. Available as soon as memblock is initialized.
261 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700262#define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
263 p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100264 for_each_mem_range_rev(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
Tang Chen66b16ed2014-01-21 15:49:23 -0800267static inline void memblock_set_region_flags(struct memblock_region *r,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300268 enum memblock_flags flags)
Tang Chen66b16ed2014-01-21 15:49:23 -0800269{
270 r->flags |= flags;
271}
272
273static inline void memblock_clear_region_flags(struct memblock_region *r,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300274 enum memblock_flags flags)
Tang Chen66b16ed2014-01-21 15:49:23 -0800275{
276 r->flags &= ~flags;
277}
278
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200279#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Tang Chene7e8de52014-01-21 15:49:26 -0800280int memblock_set_node(phys_addr_t base, phys_addr_t size,
281 struct memblock_type *type, int nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200282
283static inline void memblock_set_region_node(struct memblock_region *r, int nid)
284{
285 r->nid = nid;
286}
287
288static inline int memblock_get_region_node(const struct memblock_region *r)
289{
290 return r->nid;
291}
292#else
293static inline void memblock_set_region_node(struct memblock_region *r, int nid)
294{
295}
296
297static inline int memblock_get_region_node(const struct memblock_region *r)
298{
299 return 0;
300}
301#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
302
Mike Rapoport9a8dd702018-10-30 15:07:59 -0700303phys_addr_t memblock_phys_alloc_nid(phys_addr_t size, phys_addr_t align, int nid);
304phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700305
Mike Rapoport9a8dd702018-10-30 15:07:59 -0700306phys_addr_t memblock_phys_alloc(phys_addr_t size, phys_addr_t align);
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700307
Tang Chen79442ed2013-11-12 15:07:59 -0800308/*
309 * Set the allocation direction to bottom-up or top-down.
310 */
Fabian Frederick2cfb3662014-08-06 16:05:03 -0700311static inline void __init memblock_set_bottom_up(bool enable)
Tang Chen79442ed2013-11-12 15:07:59 -0800312{
313 memblock.bottom_up = enable;
314}
315
316/*
317 * Check if the allocation direction is bottom-up or not.
318 * if this is true, that said, memblock will allocate memory
319 * in bottom-up direction.
320 */
321static inline bool memblock_bottom_up(void)
322{
323 return memblock.bottom_up;
324}
Tang Chen79442ed2013-11-12 15:07:59 -0800325
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700326/* Flags for memblock_alloc_base() amd __memblock_alloc_base() */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000327#define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700328#define MEMBLOCK_ALLOC_ACCESSIBLE 0
329
Akinobu Mita2bfc2862014-06-04 16:06:53 -0700330phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700331 phys_addr_t start, phys_addr_t end,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300332 enum memblock_flags flags);
Nicholas Pigginb575454f2018-02-14 01:08:15 +1000333phys_addr_t memblock_alloc_base_nid(phys_addr_t size,
334 phys_addr_t align, phys_addr_t max_addr,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300335 int nid, enum memblock_flags flags);
Tejun Heo581adcb2011-12-08 10:22:06 -0800336phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align,
337 phys_addr_t max_addr);
338phys_addr_t __memblock_alloc_base(phys_addr_t size, phys_addr_t align,
339 phys_addr_t max_addr);
340phys_addr_t memblock_phys_mem_size(void);
Srikar Dronamraju8907de52016-10-07 16:59:18 -0700341phys_addr_t memblock_reserved_size(void);
Yinghai Lu595ad9a2013-01-24 12:20:09 -0800342phys_addr_t memblock_mem_size(unsigned long limit_pfn);
Tejun Heo581adcb2011-12-08 10:22:06 -0800343phys_addr_t memblock_start_of_DRAM(void);
344phys_addr_t memblock_end_of_DRAM(void);
345void memblock_enforce_memory_limit(phys_addr_t memory_limit);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +0900346void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
Dennis Chena571d4e2016-07-28 15:48:26 -0700347void memblock_mem_limit_remove_map(phys_addr_t limit);
Yaowei Baib4ad0c72016-01-14 15:18:54 -0800348bool memblock_is_memory(phys_addr_t addr);
Yaowei Bai937f0c22018-02-06 15:41:18 -0800349bool memblock_is_map_memory(phys_addr_t addr);
350bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
Yaowei Baib4ad0c72016-01-14 15:18:54 -0800351bool memblock_is_reserved(phys_addr_t addr);
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700352bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000353
Tejun Heo4ff7b822011-12-08 10:22:06 -0800354extern void __memblock_dump_all(void);
355
356static inline void memblock_dump_all(void)
357{
358 if (memblock_debug)
359 __memblock_dump_all();
360}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000361
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700362/**
363 * memblock_set_current_limit - Set the current allocation limit to allow
364 * limiting allocations to what is currently
365 * accessible during boot
366 * @limit: New limit value (physical address)
367 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800368void memblock_set_current_limit(phys_addr_t limit);
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700369
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700370
Laura Abbottfec51012014-02-27 01:23:43 +0100371phys_addr_t memblock_get_current_limit(void);
372
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000373/*
374 * pfn conversion functions
375 *
376 * While the memory MEMBLOCKs should always be page aligned, the reserved
377 * MEMBLOCKs may not be. This accessor attempt to provide a very clear
378 * idea of what they return for such non aligned MEMBLOCKs.
379 */
380
381/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300382 * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000383 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300384 *
385 * Return: the lowest pfn intersecting with the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000386 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700387static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000388{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700389 return PFN_UP(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000390}
391
392/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300393 * memblock_region_memory_end_pfn - get the end pfn of the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000394 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300395 *
396 * Return: the end_pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000397 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700398static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000399{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700400 return PFN_DOWN(reg->base + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000401}
402
403/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300404 * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000405 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300406 *
407 * Return: the lowest pfn intersecting with the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000408 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700409static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000410{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700411 return PFN_DOWN(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000412}
413
414/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300415 * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000416 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300417 *
418 * Return: the end_pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000419 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700420static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000421{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700422 return PFN_UP(reg->base + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000423}
424
425#define for_each_memblock(memblock_type, region) \
Chen Gangba6c19f2016-07-26 15:24:47 -0700426 for (region = memblock.memblock_type.regions; \
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000427 region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \
428 region++)
429
Gioh Kim66e8b432017-11-15 17:33:42 -0800430#define for_each_memblock_type(i, memblock_type, rgn) \
431 for (i = 0, rgn = &memblock_type->regions[0]; \
432 i < memblock_type->cnt; \
433 i++, rgn = &memblock_type->regions[i])
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000434
Vladimir Murzin4a207992015-04-14 15:48:27 -0700435#ifdef CONFIG_MEMTEST
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700436extern void early_memtest(phys_addr_t start, phys_addr_t end);
Vladimir Murzin4a207992015-04-14 15:48:27 -0700437#else
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700438static inline void early_memtest(phys_addr_t start, phys_addr_t end)
Vladimir Murzin4a207992015-04-14 15:48:27 -0700439{
440}
441#endif
Yinghai Luf0b37fad2010-07-28 15:28:21 +1000442
Yinghai Lu95f72d12010-07-12 14:36:09 +1000443#endif /* __KERNEL__ */
444
445#endif /* _LINUX_MEMBLOCK_H */