blob: 7caecb42bfea46e9cf7897bb8f29e5ed094456ef [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
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800111phys_addr_t memblock_find_in_range_node(phys_addr_t size, phys_addr_t align,
Chen Gangba6c19f2016-07-26 15:24:47 -0700112 phys_addr_t start, phys_addr_t end,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300113 int nid, enum memblock_flags flags);
Tejun Heofc769a82011-07-12 09:58:10 +0200114phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
115 phys_addr_t size, phys_addr_t align);
Tejun Heo1aadc052011-12-08 10:22:08 -0800116void memblock_allow_resize(void);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800117int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
Tejun Heo581adcb2011-12-08 10:22:06 -0800118int memblock_add(phys_addr_t base, phys_addr_t size);
119int memblock_remove(phys_addr_t base, phys_addr_t size);
120int memblock_free(phys_addr_t base, phys_addr_t size);
121int memblock_reserve(phys_addr_t base, phys_addr_t size);
Yinghai Lu6ede1fd2012-10-22 16:35:18 -0700122void memblock_trim_memory(phys_addr_t align);
Tang Chen95cf82e2015-09-08 15:02:03 -0700123bool memblock_overlaps_region(struct memblock_type *type,
124 phys_addr_t base, phys_addr_t size);
Tang Chen66b16ed2014-01-21 15:49:23 -0800125int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
126int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
Tony Lucka3f5baf2015-06-24 16:58:12 -0700127int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100128int memblock_mark_nomap(phys_addr_t base, phys_addr_t size);
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900129int memblock_clear_nomap(phys_addr_t base, phys_addr_t size);
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300130enum memblock_flags choose_memblock_flags(void);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100131
Mike Rapoport57c8a662018-10-30 15:09:49 -0700132unsigned long memblock_free_all(void);
133void reset_node_managed_pages(pg_data_t *pgdat);
134void reset_all_zones_managed_pages(void);
135
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100136/* Low level functions */
137int memblock_add_range(struct memblock_type *type,
138 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300139 int nid, enum memblock_flags flags);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100140
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300141void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700142 struct memblock_type *type_a,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100143 struct memblock_type *type_b, phys_addr_t *out_start,
144 phys_addr_t *out_end, int *out_nid);
145
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300146void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700147 struct memblock_type *type_a,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100148 struct memblock_type *type_b, phys_addr_t *out_start,
149 phys_addr_t *out_end, int *out_nid);
150
Robin Holt8e7a7f82015-06-30 14:56:41 -0700151void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start,
Chen Gangba6c19f2016-07-26 15:24:47 -0700152 phys_addr_t *out_end);
Robin Holt8e7a7f82015-06-30 14:56:41 -0700153
Pavel Tatashin3010f872017-08-18 15:16:05 -0700154void __memblock_free_late(phys_addr_t base, phys_addr_t size);
155
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100156/**
157 * for_each_mem_range - iterate through memblock areas from type_a and not
158 * included in type_b. Or just type_a if type_b is NULL.
159 * @i: u64 used as loop variable
160 * @type_a: ptr to memblock_type to iterate
161 * @type_b: ptr to memblock_type which excludes from the iteration
162 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700163 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100164 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
165 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
166 * @p_nid: ptr to int for nid of the range, can be %NULL
167 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700168#define for_each_mem_range(i, type_a, type_b, nid, flags, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100169 p_start, p_end, p_nid) \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700170 for (i = 0, __next_mem_range(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100171 p_start, p_end, p_nid); \
172 i != (u64)ULLONG_MAX; \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700173 __next_mem_range(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100174 p_start, p_end, p_nid))
175
176/**
177 * for_each_mem_range_rev - reverse iterate through memblock areas from
178 * type_a and not included in type_b. Or just type_a if type_b is NULL.
179 * @i: u64 used as loop variable
180 * @type_a: ptr to memblock_type to iterate
181 * @type_b: ptr to memblock_type which excludes from the iteration
182 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700183 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100184 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
185 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
186 * @p_nid: ptr to int for nid of the range, can be %NULL
187 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700188#define for_each_mem_range_rev(i, type_a, type_b, nid, flags, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100189 p_start, p_end, p_nid) \
190 for (i = (u64)ULLONG_MAX, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700191 __next_mem_range_rev(&i, nid, flags, type_a, type_b,\
Chen Gangba6c19f2016-07-26 15:24:47 -0700192 p_start, p_end, p_nid); \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100193 i != (u64)ULLONG_MAX; \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700194 __next_mem_range_rev(&i, nid, flags, type_a, type_b, \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100195 p_start, p_end, p_nid))
196
Robin Holt8e7a7f82015-06-30 14:56:41 -0700197/**
198 * for_each_reserved_mem_region - iterate over all reserved memblock areas
199 * @i: u64 used as loop variable
200 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
201 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
202 *
203 * Walks over reserved areas of memblock. Available as soon as memblock
204 * is initialized.
205 */
206#define for_each_reserved_mem_region(i, p_start, p_end) \
Chen Gangba6c19f2016-07-26 15:24:47 -0700207 for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end); \
Robin Holt8e7a7f82015-06-30 14:56:41 -0700208 i != (u64)ULLONG_MAX; \
209 __next_reserved_mem_region(&i, p_start, p_end))
210
Tang Chen55ac5902014-01-21 15:49:35 -0800211static inline bool memblock_is_hotpluggable(struct memblock_region *m)
212{
213 return m->flags & MEMBLOCK_HOTPLUG;
214}
215
Tony Lucka3f5baf2015-06-24 16:58:12 -0700216static inline bool memblock_is_mirror(struct memblock_region *m)
217{
218 return m->flags & MEMBLOCK_MIRROR;
219}
220
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100221static inline bool memblock_is_nomap(struct memblock_region *m)
222{
223 return m->flags & MEMBLOCK_NOMAP;
224}
225
Tejun Heo0ee332c2011-12-08 10:22:09 -0800226#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Yinghai Lue76b63f2013-09-11 14:22:17 -0700227int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn,
228 unsigned long *end_pfn);
Tejun Heo0ee332c2011-12-08 10:22:09 -0800229void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
230 unsigned long *out_end_pfn, int *out_nid);
231
232/**
233 * for_each_mem_pfn_range - early memory pfn range iterator
234 * @i: an integer used as loop variable
235 * @nid: node selector, %MAX_NUMNODES for all nodes
236 * @p_start: ptr to ulong for start pfn of the range, can be %NULL
237 * @p_end: ptr to ulong for end pfn of the range, can be %NULL
238 * @p_nid: ptr to int for nid of the range, can be %NULL
239 *
Wanpeng Lif2d52fe2012-10-08 16:32:24 -0700240 * Walks over configured memory ranges.
Tejun Heo0ee332c2011-12-08 10:22:09 -0800241 */
242#define for_each_mem_pfn_range(i, nid, p_start, p_end, p_nid) \
243 for (i = -1, __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid); \
244 i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
245#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
246
Tejun Heo35fd0802011-07-12 11:15:59 +0200247/**
248 * for_each_free_mem_range - iterate through free memblock areas
249 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800250 * @nid: node selector, %NUMA_NO_NODE for all nodes
Florian Fainellid30b5542016-01-14 15:22:04 -0800251 * @flags: pick from blocks based on memory attributes
Tejun Heo35fd0802011-07-12 11:15:59 +0200252 * @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 * @p_nid: ptr to int for nid of the range, can be %NULL
255 *
256 * Walks over free (memory && !reserved) areas of memblock. Available as
257 * soon as memblock is initialized.
258 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700259#define for_each_free_mem_range(i, nid, flags, p_start, p_end, p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100260 for_each_mem_range(i, &memblock.memory, &memblock.reserved, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700261 nid, flags, p_start, p_end, p_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800262
263/**
264 * for_each_free_mem_range_reverse - rev-iterate through free memblock areas
265 * @i: u64 used as loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800266 * @nid: node selector, %NUMA_NO_NODE for all nodes
Florian Fainellid30b5542016-01-14 15:22:04 -0800267 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800268 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
269 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
270 * @p_nid: ptr to int for nid of the range, can be %NULL
271 *
272 * Walks over free (memory && !reserved) areas of memblock in reverse
273 * order. Available as soon as memblock is initialized.
274 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700275#define for_each_free_mem_range_reverse(i, nid, flags, p_start, p_end, \
276 p_nid) \
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100277 for_each_mem_range_rev(i, &memblock.memory, &memblock.reserved, \
Tony Luckfc6daaf2015-06-24 16:58:09 -0700278 nid, flags, p_start, p_end, p_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800279
Tang Chen66b16ed2014-01-21 15:49:23 -0800280static inline void memblock_set_region_flags(struct memblock_region *r,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300281 enum memblock_flags flags)
Tang Chen66b16ed2014-01-21 15:49:23 -0800282{
283 r->flags |= flags;
284}
285
286static inline void memblock_clear_region_flags(struct memblock_region *r,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300287 enum memblock_flags flags)
Tang Chen66b16ed2014-01-21 15:49:23 -0800288{
289 r->flags &= ~flags;
290}
291
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200292#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Tang Chene7e8de52014-01-21 15:49:26 -0800293int memblock_set_node(phys_addr_t base, phys_addr_t size,
294 struct memblock_type *type, int nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200295
296static inline void memblock_set_region_node(struct memblock_region *r, int nid)
297{
298 r->nid = nid;
299}
300
301static inline int memblock_get_region_node(const struct memblock_region *r)
302{
303 return r->nid;
304}
305#else
306static inline void memblock_set_region_node(struct memblock_region *r, int nid)
307{
308}
309
310static inline int memblock_get_region_node(const struct memblock_region *r)
311{
312 return 0;
313}
314#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
315
Mike Rapoport57c8a662018-10-30 15:09:49 -0700316/* Flags for memblock allocation APIs */
317#define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0)
318#define MEMBLOCK_ALLOC_ACCESSIBLE 0
Qian Caifed84c72018-12-28 00:36:29 -0800319#define MEMBLOCK_ALLOC_KASAN 1
Mike Rapoport57c8a662018-10-30 15:09:49 -0700320
321/* We are using top down, so it is safe to use 0 here */
322#define MEMBLOCK_LOW_LIMIT 0
323
324#ifndef ARCH_LOW_ADDRESS_LIMIT
325#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
326#endif
327
Mike Rapoport8a770c22019-03-11 23:29:16 -0700328phys_addr_t memblock_phys_alloc_range(phys_addr_t size, phys_addr_t align,
329 phys_addr_t start, phys_addr_t end);
Mike Rapoport9a8dd702018-10-30 15:07:59 -0700330phys_addr_t memblock_phys_alloc_nid(phys_addr_t size, phys_addr_t align, int nid);
331phys_addr_t memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid);
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700332
Mike Rapoportecc3e772019-03-11 23:29:26 -0700333static inline phys_addr_t memblock_phys_alloc(phys_addr_t size,
334 phys_addr_t align)
335{
336 return memblock_phys_alloc_range(size, align, 0,
337 MEMBLOCK_ALLOC_ACCESSIBLE);
338}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700339
Mike Rapoport57c8a662018-10-30 15:09:49 -0700340void *memblock_alloc_try_nid_raw(phys_addr_t size, phys_addr_t align,
341 phys_addr_t min_addr, phys_addr_t max_addr,
342 int nid);
343void *memblock_alloc_try_nid_nopanic(phys_addr_t size, phys_addr_t align,
344 phys_addr_t min_addr, phys_addr_t max_addr,
345 int nid);
346void *memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align,
347 phys_addr_t min_addr, phys_addr_t max_addr,
348 int nid);
349
350static inline void * __init memblock_alloc(phys_addr_t size, phys_addr_t align)
351{
352 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
353 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
354}
355
356static inline void * __init memblock_alloc_raw(phys_addr_t size,
357 phys_addr_t align)
358{
359 return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
360 MEMBLOCK_ALLOC_ACCESSIBLE,
361 NUMA_NO_NODE);
362}
363
364static inline void * __init memblock_alloc_from(phys_addr_t size,
365 phys_addr_t align,
366 phys_addr_t min_addr)
367{
368 return memblock_alloc_try_nid(size, align, min_addr,
369 MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
370}
371
372static inline void * __init memblock_alloc_nopanic(phys_addr_t size,
373 phys_addr_t align)
374{
375 return memblock_alloc_try_nid_nopanic(size, align, MEMBLOCK_LOW_LIMIT,
376 MEMBLOCK_ALLOC_ACCESSIBLE,
377 NUMA_NO_NODE);
378}
379
380static inline void * __init memblock_alloc_low(phys_addr_t size,
381 phys_addr_t align)
382{
383 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
384 ARCH_LOW_ADDRESS_LIMIT, NUMA_NO_NODE);
385}
386static inline void * __init memblock_alloc_low_nopanic(phys_addr_t size,
387 phys_addr_t align)
388{
389 return memblock_alloc_try_nid_nopanic(size, align, MEMBLOCK_LOW_LIMIT,
390 ARCH_LOW_ADDRESS_LIMIT,
391 NUMA_NO_NODE);
392}
393
394static inline void * __init memblock_alloc_from_nopanic(phys_addr_t size,
395 phys_addr_t align,
396 phys_addr_t min_addr)
397{
398 return memblock_alloc_try_nid_nopanic(size, align, min_addr,
399 MEMBLOCK_ALLOC_ACCESSIBLE,
400 NUMA_NO_NODE);
401}
402
403static inline void * __init memblock_alloc_node(phys_addr_t size,
404 phys_addr_t align, int nid)
405{
406 return memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
407 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
408}
409
410static inline void * __init memblock_alloc_node_nopanic(phys_addr_t size,
411 int nid)
412{
Mike Rapoport7e1c4e22018-10-30 15:09:57 -0700413 return memblock_alloc_try_nid_nopanic(size, SMP_CACHE_BYTES,
414 MEMBLOCK_LOW_LIMIT,
Mike Rapoport57c8a662018-10-30 15:09:49 -0700415 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
416}
417
418static inline void __init memblock_free_early(phys_addr_t base,
419 phys_addr_t size)
420{
Mike Rapoport4d728682018-12-28 00:35:29 -0800421 memblock_free(base, size);
Mike Rapoport57c8a662018-10-30 15:09:49 -0700422}
423
424static inline void __init memblock_free_early_nid(phys_addr_t base,
425 phys_addr_t size, int nid)
426{
Mike Rapoport4d728682018-12-28 00:35:29 -0800427 memblock_free(base, size);
Mike Rapoport57c8a662018-10-30 15:09:49 -0700428}
429
430static inline void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
431{
432 __memblock_free_late(base, size);
433}
434
Tang Chen79442ed2013-11-12 15:07:59 -0800435/*
436 * Set the allocation direction to bottom-up or top-down.
437 */
Fabian Frederick2cfb3662014-08-06 16:05:03 -0700438static inline void __init memblock_set_bottom_up(bool enable)
Tang Chen79442ed2013-11-12 15:07:59 -0800439{
440 memblock.bottom_up = enable;
441}
442
443/*
444 * Check if the allocation direction is bottom-up or not.
445 * if this is true, that said, memblock will allocate memory
446 * in bottom-up direction.
447 */
448static inline bool memblock_bottom_up(void)
449{
450 return memblock.bottom_up;
451}
Tang Chen79442ed2013-11-12 15:07:59 -0800452
Tejun Heo581adcb2011-12-08 10:22:06 -0800453phys_addr_t memblock_alloc_base(phys_addr_t size, phys_addr_t align,
454 phys_addr_t max_addr);
455phys_addr_t __memblock_alloc_base(phys_addr_t size, phys_addr_t align,
456 phys_addr_t max_addr);
457phys_addr_t memblock_phys_mem_size(void);
Srikar Dronamraju8907de52016-10-07 16:59:18 -0700458phys_addr_t memblock_reserved_size(void);
Yinghai Lu595ad9a2013-01-24 12:20:09 -0800459phys_addr_t memblock_mem_size(unsigned long limit_pfn);
Tejun Heo581adcb2011-12-08 10:22:06 -0800460phys_addr_t memblock_start_of_DRAM(void);
461phys_addr_t memblock_end_of_DRAM(void);
462void memblock_enforce_memory_limit(phys_addr_t memory_limit);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +0900463void memblock_cap_memory_range(phys_addr_t base, phys_addr_t size);
Dennis Chena571d4e2016-07-28 15:48:26 -0700464void memblock_mem_limit_remove_map(phys_addr_t limit);
Yaowei Baib4ad0c72016-01-14 15:18:54 -0800465bool memblock_is_memory(phys_addr_t addr);
Yaowei Bai937f0c22018-02-06 15:41:18 -0800466bool memblock_is_map_memory(phys_addr_t addr);
467bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
Yaowei Baib4ad0c72016-01-14 15:18:54 -0800468bool memblock_is_reserved(phys_addr_t addr);
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700469bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000470
Tejun Heo4ff7b822011-12-08 10:22:06 -0800471extern void __memblock_dump_all(void);
472
473static inline void memblock_dump_all(void)
474{
475 if (memblock_debug)
476 __memblock_dump_all();
477}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000478
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700479/**
480 * memblock_set_current_limit - Set the current allocation limit to allow
481 * limiting allocations to what is currently
482 * accessible during boot
483 * @limit: New limit value (physical address)
484 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800485void memblock_set_current_limit(phys_addr_t limit);
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700486
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700487
Laura Abbottfec51012014-02-27 01:23:43 +0100488phys_addr_t memblock_get_current_limit(void);
489
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000490/*
491 * pfn conversion functions
492 *
493 * While the memory MEMBLOCKs should always be page aligned, the reserved
494 * MEMBLOCKs may not be. This accessor attempt to provide a very clear
495 * idea of what they return for such non aligned MEMBLOCKs.
496 */
497
498/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300499 * memblock_region_memory_base_pfn - get the lowest pfn of the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000500 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300501 *
502 * Return: the lowest pfn intersecting with the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000503 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700504static inline unsigned long memblock_region_memory_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000505{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700506 return PFN_UP(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000507}
508
509/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300510 * memblock_region_memory_end_pfn - get the end pfn of the memory region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000511 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300512 *
513 * Return: the end_pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000514 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700515static inline unsigned long memblock_region_memory_end_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000516{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700517 return PFN_DOWN(reg->base + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000518}
519
520/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300521 * memblock_region_reserved_base_pfn - get the lowest pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000522 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300523 *
524 * Return: the lowest pfn intersecting with the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000525 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700526static inline unsigned long memblock_region_reserved_base_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000527{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700528 return PFN_DOWN(reg->base);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000529}
530
531/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300532 * memblock_region_reserved_end_pfn - get the end pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000533 * @reg: memblock_region structure
Mike Rapoport47cec442018-06-30 17:55:02 +0300534 *
535 * Return: the end_pfn of the reserved region
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000536 */
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700537static inline unsigned long memblock_region_reserved_end_pfn(const struct memblock_region *reg)
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000538{
Yinghai Luc7fc2de2010-10-12 14:07:09 -0700539 return PFN_UP(reg->base + reg->size);
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000540}
541
542#define for_each_memblock(memblock_type, region) \
Chen Gangba6c19f2016-07-26 15:24:47 -0700543 for (region = memblock.memblock_type.regions; \
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000544 region < (memblock.memblock_type.regions + memblock.memblock_type.cnt); \
545 region++)
546
Gioh Kim66e8b432017-11-15 17:33:42 -0800547#define for_each_memblock_type(i, memblock_type, rgn) \
548 for (i = 0, rgn = &memblock_type->regions[0]; \
549 i < memblock_type->cnt; \
550 i++, rgn = &memblock_type->regions[i])
Benjamin Herrenschmidt5b385f22010-08-04 13:40:38 +1000551
Mike Rapoport57c8a662018-10-30 15:09:49 -0700552extern void *alloc_large_system_hash(const char *tablename,
553 unsigned long bucketsize,
554 unsigned long numentries,
555 int scale,
556 int flags,
557 unsigned int *_hash_shift,
558 unsigned int *_hash_mask,
559 unsigned long low_limit,
560 unsigned long high_limit);
561
562#define HASH_EARLY 0x00000001 /* Allocating during early boot? */
563#define HASH_SMALL 0x00000002 /* sub-page allocation allowed, min
564 * shift passed via *_hash_shift */
565#define HASH_ZERO 0x00000004 /* Zero allocated hash table */
566
567/* Only NUMA needs hash distribution. 64bit NUMA architectures have
568 * sufficient vmalloc space.
569 */
570#ifdef CONFIG_NUMA
571#define HASHDIST_DEFAULT IS_ENABLED(CONFIG_64BIT)
572extern int hashdist; /* Distribute hashes across NUMA nodes? */
573#else
574#define hashdist (0)
575#endif
576
Vladimir Murzin4a207992015-04-14 15:48:27 -0700577#ifdef CONFIG_MEMTEST
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700578extern void early_memtest(phys_addr_t start, phys_addr_t end);
Vladimir Murzin4a207992015-04-14 15:48:27 -0700579#else
Vladimir Murzin7f70bae2015-04-14 15:48:30 -0700580static inline void early_memtest(phys_addr_t start, phys_addr_t end)
Vladimir Murzin4a207992015-04-14 15:48:27 -0700581{
582}
583#endif
Yinghai Luf0b37fad2010-07-28 15:28:21 +1000584
Yinghai Lu95f72d12010-07-12 14:36:09 +1000585#endif /* __KERNEL__ */
586
587#endif /* _LINUX_MEMBLOCK_H */