blob: 78855d4c8d05c21b2ff120848c35ebc23eedc0eb [file] [log] [blame]
Yinghai Lu95f72d12010-07-12 14:36:09 +10001/*
2 * Procedures for maintaining information about logical memory blocks.
3 *
4 * Peter Bergner, IBM Corp. June 2001.
5 * Copyright (C) 2001 Peter Bergner.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/kernel.h>
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070014#include <linux/slab.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100015#include <linux/init.h>
16#include <linux/bitops.h>
Benjamin Herrenschmidt449e8df2010-07-06 15:39:07 -070017#include <linux/poison.h>
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -070018#include <linux/pfn.h>
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -070019#include <linux/debugfs.h>
Randy Dunlap514c6032018-04-05 16:25:34 -070020#include <linux/kmemleak.h>
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -070021#include <linux/seq_file.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100022#include <linux/memblock.h>
Mathieu Malaterre19373672018-07-20 17:53:31 -070023#include <linux/bootmem.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100024
Christoph Hellwigc4c5ad62016-07-28 15:48:06 -070025#include <asm/sections.h>
Santosh Shilimkar26f09e92014-01-21 15:50:19 -080026#include <linux/io.h>
27
28#include "internal.h"
Tang Chen79442ed2013-11-12 15:07:59 -080029
Mike Rapoport3e039c52018-06-30 17:55:05 +030030/**
31 * DOC: memblock overview
32 *
33 * Memblock is a method of managing memory regions during the early
34 * boot period when the usual kernel memory allocators are not up and
35 * running.
36 *
37 * Memblock views the system memory as collections of contiguous
38 * regions. There are several types of these collections:
39 *
40 * * ``memory`` - describes the physical memory available to the
41 * kernel; this may differ from the actual physical memory installed
42 * in the system, for instance when the memory is restricted with
43 * ``mem=`` command line parameter
44 * * ``reserved`` - describes the regions that were allocated
45 * * ``physmap`` - describes the actual physical memory regardless of
46 * the possible restrictions; the ``physmap`` type is only available
47 * on some architectures.
48 *
49 * Each region is represented by :c:type:`struct memblock_region` that
50 * defines the region extents, its attributes and NUMA node id on NUMA
51 * systems. Every memory type is described by the :c:type:`struct
52 * memblock_type` which contains an array of memory regions along with
53 * the allocator metadata. The memory types are nicely wrapped with
54 * :c:type:`struct memblock`. This structure is statically initialzed
55 * at build time. The region arrays for the "memory" and "reserved"
56 * types are initially sized to %INIT_MEMBLOCK_REGIONS and for the
57 * "physmap" type to %INIT_PHYSMEM_REGIONS.
58 * The :c:func:`memblock_allow_resize` enables automatic resizing of
59 * the region arrays during addition of new regions. This feature
60 * should be used with care so that memory allocated for the region
61 * array will not overlap with areas that should be reserved, for
62 * example initrd.
63 *
64 * The early architecture setup should tell memblock what the physical
65 * memory layout is by using :c:func:`memblock_add` or
66 * :c:func:`memblock_add_node` functions. The first function does not
67 * assign the region to a NUMA node and it is appropriate for UMA
68 * systems. Yet, it is possible to use it on NUMA systems as well and
69 * assign the region to a NUMA node later in the setup process using
70 * :c:func:`memblock_set_node`. The :c:func:`memblock_add_node`
71 * performs such an assignment directly.
72 *
73 * Once memblock is setup the memory can be allocated using either
74 * memblock or bootmem APIs.
75 *
76 * As the system boot progresses, the architecture specific
77 * :c:func:`mem_init` function frees all the memory to the buddy page
78 * allocator.
79 *
80 * If an architecure enables %CONFIG_ARCH_DISCARD_MEMBLOCK, the
81 * memblock data structures will be discarded after the system
82 * initialization compltes.
83 */
84
Tejun Heofe091c22011-12-08 10:22:07 -080085static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
86static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010087#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
88static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
89#endif
Tejun Heofe091c22011-12-08 10:22:07 -080090
91struct memblock memblock __initdata_memblock = {
92 .memory.regions = memblock_memory_init_regions,
93 .memory.cnt = 1, /* empty dummy entry */
94 .memory.max = INIT_MEMBLOCK_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -080095 .memory.name = "memory",
Tejun Heofe091c22011-12-08 10:22:07 -080096
97 .reserved.regions = memblock_reserved_init_regions,
98 .reserved.cnt = 1, /* empty dummy entry */
99 .reserved.max = INIT_MEMBLOCK_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800100 .reserved.name = "reserved",
Tejun Heofe091c22011-12-08 10:22:07 -0800101
Philipp Hachtmann70210ed2014-01-29 18:16:01 +0100102#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
103 .physmem.regions = memblock_physmem_init_regions,
104 .physmem.cnt = 1, /* empty dummy entry */
105 .physmem.max = INIT_PHYSMEM_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800106 .physmem.name = "physmem",
Philipp Hachtmann70210ed2014-01-29 18:16:01 +0100107#endif
108
Tang Chen79442ed2013-11-12 15:07:59 -0800109 .bottom_up = false,
Tejun Heofe091c22011-12-08 10:22:07 -0800110 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
111};
Yinghai Lu95f72d12010-07-12 14:36:09 +1000112
Yinghai Lu10d06432010-07-28 15:43:02 +1000113int memblock_debug __initdata_memblock;
Tony Lucka3f5baf2015-06-24 16:58:12 -0700114static bool system_has_some_mirror __initdata_memblock = false;
Tejun Heo1aadc052011-12-08 10:22:08 -0800115static int memblock_can_resize __initdata_memblock;
Gavin Shan181eb392012-05-29 15:06:50 -0700116static int memblock_memory_in_slab __initdata_memblock = 0;
117static int memblock_reserved_in_slab __initdata_memblock = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000118
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300119enum memblock_flags __init_memblock choose_memblock_flags(void)
Tony Lucka3f5baf2015-06-24 16:58:12 -0700120{
121 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
122}
123
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800124/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
125static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
126{
Stefan Agner1c4bc432018-06-07 17:06:15 -0700127 return *size = min(*size, PHYS_ADDR_MAX - base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800128}
129
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000130/*
131 * Address comparison utilities
132 */
Yinghai Lu10d06432010-07-28 15:43:02 +1000133static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000134 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000135{
136 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
137}
138
Tang Chen95cf82e2015-09-08 15:02:03 -0700139bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -0700140 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000141{
142 unsigned long i;
143
Alexander Kuleshovf14516f2016-01-14 15:20:39 -0800144 for (i = 0; i < type->cnt; i++)
145 if (memblock_addrs_overlap(base, size, type->regions[i].base,
146 type->regions[i].size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000147 break;
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700148 return i < type->cnt;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000149}
150
Mike Rapoport47cec442018-06-30 17:55:02 +0300151/**
Tang Chen79442ed2013-11-12 15:07:59 -0800152 * __memblock_find_range_bottom_up - find free area utility in bottom-up
153 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300154 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
155 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen79442ed2013-11-12 15:07:59 -0800156 * @size: size of free area to find
157 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800158 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700159 * @flags: pick from blocks based on memory attributes
Tang Chen79442ed2013-11-12 15:07:59 -0800160 *
161 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
162 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300163 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800164 * Found address on success, 0 on failure.
165 */
166static phys_addr_t __init_memblock
167__memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700168 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300169 enum memblock_flags flags)
Tang Chen79442ed2013-11-12 15:07:59 -0800170{
171 phys_addr_t this_start, this_end, cand;
172 u64 i;
173
Tony Luckfc6daaf2015-06-24 16:58:09 -0700174 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
Tang Chen79442ed2013-11-12 15:07:59 -0800175 this_start = clamp(this_start, start, end);
176 this_end = clamp(this_end, start, end);
177
178 cand = round_up(this_start, align);
179 if (cand < this_end && this_end - cand >= size)
180 return cand;
181 }
182
183 return 0;
184}
185
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800186/**
Tang Chen14028992013-11-12 15:07:57 -0800187 * __memblock_find_range_top_down - find free area utility, in top-down
188 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300189 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
190 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen14028992013-11-12 15:07:57 -0800191 * @size: size of free area to find
192 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800193 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700194 * @flags: pick from blocks based on memory attributes
Tang Chen14028992013-11-12 15:07:57 -0800195 *
196 * Utility called from memblock_find_in_range_node(), find free area top-down.
197 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300198 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800199 * Found address on success, 0 on failure.
Tang Chen14028992013-11-12 15:07:57 -0800200 */
201static phys_addr_t __init_memblock
202__memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700203 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300204 enum memblock_flags flags)
Tang Chen14028992013-11-12 15:07:57 -0800205{
206 phys_addr_t this_start, this_end, cand;
207 u64 i;
208
Tony Luckfc6daaf2015-06-24 16:58:09 -0700209 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
210 NULL) {
Tang Chen14028992013-11-12 15:07:57 -0800211 this_start = clamp(this_start, start, end);
212 this_end = clamp(this_end, start, end);
213
214 if (this_end < size)
215 continue;
216
217 cand = round_down(this_end - size, align);
218 if (cand >= this_start)
219 return cand;
220 }
221
222 return 0;
223}
224
225/**
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800226 * memblock_find_in_range_node - find free area in given range and node
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800227 * @size: size of free area to find
228 * @align: alignment of free area to find
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800229 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300230 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
231 * %MEMBLOCK_ALLOC_ACCESSIBLE
Grygorii Strashkob1154232014-01-21 15:50:16 -0800232 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700233 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800234 *
235 * Find @size free area aligned to @align in the specified range and node.
236 *
Tang Chen79442ed2013-11-12 15:07:59 -0800237 * When allocation direction is bottom-up, the @start should be greater
238 * than the end of the kernel image. Otherwise, it will be trimmed. The
239 * reason is that we want the bottom-up allocation just near the kernel
240 * image so it is highly likely that the allocated memory and the kernel
241 * will reside in the same node.
242 *
243 * If bottom-up allocation failed, will try to allocate memory top-down.
244 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300245 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800246 * Found address on success, 0 on failure.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000247 */
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800248phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
249 phys_addr_t align, phys_addr_t start,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300250 phys_addr_t end, int nid,
251 enum memblock_flags flags)
Tang Chenf7210e62013-02-22 16:33:51 -0800252{
Tang Chen0cfb8f02014-08-29 15:18:31 -0700253 phys_addr_t kernel_end, ret;
Tang Chen79442ed2013-11-12 15:07:59 -0800254
Tang Chenf7210e62013-02-22 16:33:51 -0800255 /* pump up @end */
Qian Cai88299f62018-12-28 00:36:29 -0800256 if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
257 end == MEMBLOCK_ALLOC_KASAN)
Tang Chenf7210e62013-02-22 16:33:51 -0800258 end = memblock.current_limit;
259
260 /* avoid allocating the first page */
261 start = max_t(phys_addr_t, start, PAGE_SIZE);
262 end = max(start, end);
Tang Chen79442ed2013-11-12 15:07:59 -0800263 kernel_end = __pa_symbol(_end);
264
265 /*
266 * try bottom-up allocation only when bottom-up mode
267 * is set and @end is above the kernel image.
268 */
269 if (memblock_bottom_up() && end > kernel_end) {
270 phys_addr_t bottom_up_start;
271
272 /* make sure we will allocate above the kernel */
273 bottom_up_start = max(start, kernel_end);
274
275 /* ok, try bottom-up allocation first */
276 ret = __memblock_find_range_bottom_up(bottom_up_start, end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700277 size, align, nid, flags);
Tang Chen79442ed2013-11-12 15:07:59 -0800278 if (ret)
279 return ret;
280
281 /*
282 * we always limit bottom-up allocation above the kernel,
283 * but top-down allocation doesn't have the limit, so
284 * retrying top-down allocation may succeed when bottom-up
285 * allocation failed.
286 *
287 * bottom-up allocation is expected to be fail very rarely,
288 * so we use WARN_ONCE() here to see the stack trace if
289 * fail happens.
290 */
Michal Hockoe3d301c2018-07-13 16:59:16 -0700291 WARN_ONCE(IS_ENABLED(CONFIG_MEMORY_HOTREMOVE),
292 "memblock: bottom-up allocation failed, memory hotremove may be affected\n");
Tang Chen79442ed2013-11-12 15:07:59 -0800293 }
Tang Chenf7210e62013-02-22 16:33:51 -0800294
Tony Luckfc6daaf2015-06-24 16:58:09 -0700295 return __memblock_find_range_top_down(start, end, size, align, nid,
296 flags);
Tang Chenf7210e62013-02-22 16:33:51 -0800297}
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000298
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800299/**
300 * memblock_find_in_range - find free area in given range
301 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300302 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
303 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800304 * @size: size of free area to find
305 * @align: alignment of free area to find
306 *
307 * Find @size free area aligned to @align in the specified range.
308 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300309 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800310 * Found address on success, 0 on failure.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800311 */
312phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
313 phys_addr_t end, phys_addr_t size,
314 phys_addr_t align)
315{
Tony Lucka3f5baf2015-06-24 16:58:12 -0700316 phys_addr_t ret;
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300317 enum memblock_flags flags = choose_memblock_flags();
Tony Lucka3f5baf2015-06-24 16:58:12 -0700318
319again:
320 ret = memblock_find_in_range_node(size, align, start, end,
321 NUMA_NO_NODE, flags);
322
323 if (!ret && (flags & MEMBLOCK_MIRROR)) {
324 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
325 &size);
326 flags &= ~MEMBLOCK_MIRROR;
327 goto again;
328 }
329
330 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800331}
332
Yinghai Lu10d06432010-07-28 15:43:02 +1000333static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000334{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800335 type->total_size -= type->regions[r].size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200336 memmove(&type->regions[r], &type->regions[r + 1],
337 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000338 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000339
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700340 /* Special case for empty arrays */
341 if (type->cnt == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800342 WARN_ON(type->total_size != 0);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700343 type->cnt = 1;
344 type->regions[0].base = 0;
345 type->regions[0].size = 0;
Tang Chen66a20752014-01-21 15:49:20 -0800346 type->regions[0].flags = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200347 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700348 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000349}
350
Philipp Hachtmann354f17e2014-01-23 15:53:24 -0800351#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
Pavel Tatashin3010f872017-08-18 15:16:05 -0700352/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300353 * memblock_discard - discard memory and reserved arrays if they were allocated
Pavel Tatashin3010f872017-08-18 15:16:05 -0700354 */
355void __init memblock_discard(void)
Yinghai Lu29f67382012-07-11 14:02:56 -0700356{
Pavel Tatashin3010f872017-08-18 15:16:05 -0700357 phys_addr_t addr, size;
Yinghai Lu29f67382012-07-11 14:02:56 -0700358
Pavel Tatashin3010f872017-08-18 15:16:05 -0700359 if (memblock.reserved.regions != memblock_reserved_init_regions) {
360 addr = __pa(memblock.reserved.regions);
361 size = PAGE_ALIGN(sizeof(struct memblock_region) *
362 memblock.reserved.max);
363 __memblock_free_late(addr, size);
364 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700365
Pavel Tatashin91b540f2017-08-25 15:55:46 -0700366 if (memblock.memory.regions != memblock_memory_init_regions) {
Pavel Tatashin3010f872017-08-18 15:16:05 -0700367 addr = __pa(memblock.memory.regions);
368 size = PAGE_ALIGN(sizeof(struct memblock_region) *
369 memblock.memory.max);
370 __memblock_free_late(addr, size);
371 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700372}
Philipp Hachtmann5e270e22014-01-23 15:53:11 -0800373#endif
374
Greg Pearson48c3b582012-06-20 12:53:05 -0700375/**
376 * memblock_double_array - double the size of the memblock regions array
377 * @type: memblock type of the regions array being doubled
378 * @new_area_start: starting address of memory range to avoid overlap with
379 * @new_area_size: size of memory range to avoid overlap with
380 *
381 * Double the size of the @type regions array. If memblock is being used to
382 * allocate memory for a new reserved regions array and there is a previously
Mike Rapoport47cec442018-06-30 17:55:02 +0300383 * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
Greg Pearson48c3b582012-06-20 12:53:05 -0700384 * waiting to be reserved, ensure the memory used by the new array does
385 * not overlap.
386 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300387 * Return:
Greg Pearson48c3b582012-06-20 12:53:05 -0700388 * 0 on success, -1 on failure.
389 */
390static int __init_memblock memblock_double_array(struct memblock_type *type,
391 phys_addr_t new_area_start,
392 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700393{
394 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700395 phys_addr_t old_alloc_size, new_alloc_size;
Mike Rapoporta36aab82018-08-17 15:47:17 -0700396 phys_addr_t old_size, new_size, addr, new_end;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700397 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700398 int *in_slab;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700399
400 /* We don't allow resizing until we know about the reserved regions
401 * of memory that aren't suitable for allocation
402 */
403 if (!memblock_can_resize)
404 return -1;
405
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700406 /* Calculate new doubled size */
407 old_size = type->max * sizeof(struct memblock_region);
408 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700409 /*
410 * We need to allocated new one align to PAGE_SIZE,
411 * so we can free them completely later.
412 */
413 old_alloc_size = PAGE_ALIGN(old_size);
414 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700415
Gavin Shan181eb392012-05-29 15:06:50 -0700416 /* Retrieve the slab flag */
417 if (type == &memblock.memory)
418 in_slab = &memblock_memory_in_slab;
419 else
420 in_slab = &memblock_reserved_in_slab;
421
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700422 /* Try to find some space for it.
423 *
424 * WARNING: We assume that either slab_is_available() and we use it or
Andrew Mortonfd073832012-07-31 16:42:40 -0700425 * we use MEMBLOCK for allocations. That means that this is unsafe to
426 * use when bootmem is currently active (unless bootmem itself is
427 * implemented on top of MEMBLOCK which isn't the case yet)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700428 *
429 * This should however not be an issue for now, as we currently only
Andrew Mortonfd073832012-07-31 16:42:40 -0700430 * call into MEMBLOCK while it's still active, or much later when slab
431 * is active for memory hotplug operations
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700432 */
433 if (use_slab) {
434 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200435 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700436 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700437 /* only exclude range when trying to double reserved.regions */
438 if (type != &memblock.reserved)
439 new_area_start = new_area_size = 0;
440
441 addr = memblock_find_in_range(new_area_start + new_area_size,
442 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700443 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700444 if (!addr && new_area_size)
445 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700446 min(new_area_start, memblock.current_limit),
447 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700448
Sachin Kamat15674862012-09-04 13:55:05 +0530449 new_array = addr ? __va(addr) : NULL;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700450 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200451 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700452 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800453 type->name, type->max, type->max * 2);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700454 return -1;
455 }
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700456
Mike Rapoporta36aab82018-08-17 15:47:17 -0700457 new_end = addr + new_size - 1;
458 memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
459 type->name, type->max * 2, &addr, &new_end);
Yinghai Luea9e4372010-07-28 15:13:22 +1000460
Andrew Mortonfd073832012-07-31 16:42:40 -0700461 /*
462 * Found space, we now need to move the array over before we add the
463 * reserved region since it may be our reserved array itself that is
464 * full.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700465 */
466 memcpy(new_array, type->regions, old_size);
467 memset(new_array + type->max, 0, old_size);
468 old_array = type->regions;
469 type->regions = new_array;
470 type->max <<= 1;
471
Andrew Mortonfd073832012-07-31 16:42:40 -0700472 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700473 if (*in_slab)
474 kfree(old_array);
475 else if (old_array != memblock_memory_init_regions &&
476 old_array != memblock_reserved_init_regions)
Yinghai Lu29f67382012-07-11 14:02:56 -0700477 memblock_free(__pa(old_array), old_alloc_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700478
Andrew Mortonfd073832012-07-31 16:42:40 -0700479 /*
480 * Reserve the new array if that comes from the memblock. Otherwise, we
481 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700482 */
483 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700484 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700485
486 /* Update slab flag */
487 *in_slab = use_slab;
488
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700489 return 0;
490}
491
Tejun Heo784656f92011-07-12 11:15:55 +0200492/**
493 * memblock_merge_regions - merge neighboring compatible regions
494 * @type: memblock type to scan
495 *
496 * Scan @type and merge neighboring compatible regions.
497 */
498static void __init_memblock memblock_merge_regions(struct memblock_type *type)
499{
500 int i = 0;
501
502 /* cnt never goes below 1 */
503 while (i < type->cnt - 1) {
504 struct memblock_region *this = &type->regions[i];
505 struct memblock_region *next = &type->regions[i + 1];
506
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200507 if (this->base + this->size != next->base ||
508 memblock_get_region_node(this) !=
Tang Chen66a20752014-01-21 15:49:20 -0800509 memblock_get_region_node(next) ||
510 this->flags != next->flags) {
Tejun Heo784656f92011-07-12 11:15:55 +0200511 BUG_ON(this->base + this->size > next->base);
512 i++;
513 continue;
514 }
515
516 this->size += next->size;
Lin Fengc0232ae2013-01-11 14:31:44 -0800517 /* move forward from next + 1, index of which is i + 2 */
518 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
Tejun Heo784656f92011-07-12 11:15:55 +0200519 type->cnt--;
520 }
521}
522
523/**
524 * memblock_insert_region - insert new memblock region
Tang Chen209ff862013-04-29 15:08:41 -0700525 * @type: memblock type to insert into
526 * @idx: index for the insertion point
527 * @base: base address of the new region
528 * @size: size of the new region
529 * @nid: node id of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800530 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200531 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300532 * Insert new memblock region [@base, @base + @size) into @type at @idx.
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700533 * @type must already have extra room to accommodate the new region.
Tejun Heo784656f92011-07-12 11:15:55 +0200534 */
535static void __init_memblock memblock_insert_region(struct memblock_type *type,
536 int idx, phys_addr_t base,
Tang Chen66a20752014-01-21 15:49:20 -0800537 phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300538 int nid,
539 enum memblock_flags flags)
Tejun Heo784656f92011-07-12 11:15:55 +0200540{
541 struct memblock_region *rgn = &type->regions[idx];
542
543 BUG_ON(type->cnt >= type->max);
544 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
545 rgn->base = base;
546 rgn->size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800547 rgn->flags = flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200548 memblock_set_region_node(rgn, nid);
Tejun Heo784656f92011-07-12 11:15:55 +0200549 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800550 type->total_size += size;
Tejun Heo784656f92011-07-12 11:15:55 +0200551}
552
553/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100554 * memblock_add_range - add new memblock region
Tejun Heo784656f92011-07-12 11:15:55 +0200555 * @type: memblock type to add new region into
556 * @base: base address of the new region
557 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800558 * @nid: nid of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800559 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200560 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300561 * Add new memblock region [@base, @base + @size) into @type. The new region
Tejun Heo784656f92011-07-12 11:15:55 +0200562 * is allowed to overlap with existing ones - overlaps don't affect already
563 * existing regions. @type is guaranteed to be minimal (all neighbouring
564 * compatible regions are merged) after the addition.
565 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300566 * Return:
Tejun Heo784656f92011-07-12 11:15:55 +0200567 * 0 on success, -errno on failure.
568 */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100569int __init_memblock memblock_add_range(struct memblock_type *type,
Tang Chen66a20752014-01-21 15:49:20 -0800570 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300571 int nid, enum memblock_flags flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000572{
Tejun Heo784656f92011-07-12 11:15:55 +0200573 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800574 phys_addr_t obase = base;
575 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800576 int idx, nr_new;
577 struct memblock_region *rgn;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000578
Tejun Heob3dc6272012-04-20 08:31:34 -0700579 if (!size)
580 return 0;
581
Tejun Heo784656f92011-07-12 11:15:55 +0200582 /* special case for empty array */
583 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800584 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000585 type->regions[0].base = base;
586 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800587 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800588 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800589 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000590 return 0;
591 }
Tejun Heo784656f92011-07-12 11:15:55 +0200592repeat:
593 /*
594 * The following is executed twice. Once with %false @insert and
595 * then with %true. The first counts the number of regions needed
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700596 * to accommodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700597 */
Tejun Heo784656f92011-07-12 11:15:55 +0200598 base = obase;
599 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000600
Gioh Kim66e8b432017-11-15 17:33:42 -0800601 for_each_memblock_type(idx, type, rgn) {
Tejun Heo784656f92011-07-12 11:15:55 +0200602 phys_addr_t rbase = rgn->base;
603 phys_addr_t rend = rbase + rgn->size;
604
605 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000606 break;
Tejun Heo784656f92011-07-12 11:15:55 +0200607 if (rend <= base)
608 continue;
609 /*
610 * @rgn overlaps. If it separates the lower part of new
611 * area, insert that portion.
612 */
613 if (rbase > base) {
Wei Yangc0a29492015-09-04 15:47:38 -0700614#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
615 WARN_ON(nid != memblock_get_region_node(rgn));
616#endif
Wei Yang4fcab5f2015-09-08 14:59:53 -0700617 WARN_ON(flags != rgn->flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200618 nr_new++;
619 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800620 memblock_insert_region(type, idx++, base,
Tang Chen66a20752014-01-21 15:49:20 -0800621 rbase - base, nid,
622 flags);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000623 }
Tejun Heo784656f92011-07-12 11:15:55 +0200624 /* area below @rend is dealt with, forget about it */
625 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000626 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000627
Tejun Heo784656f92011-07-12 11:15:55 +0200628 /* insert the remaining portion */
629 if (base < end) {
630 nr_new++;
631 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800632 memblock_insert_region(type, idx, base, end - base,
Tang Chen66a20752014-01-21 15:49:20 -0800633 nid, flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200634 }
635
nimisoloef3cc4d2016-07-26 15:24:56 -0700636 if (!nr_new)
637 return 0;
638
Tejun Heo784656f92011-07-12 11:15:55 +0200639 /*
640 * If this was the first round, resize array and repeat for actual
641 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700642 */
Tejun Heo784656f92011-07-12 11:15:55 +0200643 if (!insert) {
644 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700645 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f92011-07-12 11:15:55 +0200646 return -ENOMEM;
647 insert = true;
648 goto repeat;
649 } else {
650 memblock_merge_regions(type);
651 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700652 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000653}
654
Mike Rapoport48a833c2018-06-30 17:55:03 +0300655/**
656 * memblock_add_node - add new memblock region within a NUMA node
657 * @base: base address of the new region
658 * @size: size of the new region
659 * @nid: nid of the new region
660 *
661 * Add new memblock region [@base, @base + @size) to the "memory"
662 * type. See memblock_add_range() description for mode details
663 *
664 * Return:
665 * 0 on success, -errno on failure.
666 */
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800667int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
668 int nid)
669{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100670 return memblock_add_range(&memblock.memory, base, size, nid, 0);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800671}
672
Mike Rapoport48a833c2018-06-30 17:55:03 +0300673/**
674 * memblock_add - add new memblock region
675 * @base: base address of the new region
676 * @size: size of the new region
677 *
678 * Add new memblock region [@base, @base + @size) to the "memory"
679 * type. See memblock_add_range() description for mode details
680 *
681 * Return:
682 * 0 on success, -errno on failure.
683 */
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700684int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700685{
Miles Chen5d63f812017-02-22 15:46:42 -0800686 phys_addr_t end = base + size - 1;
687
688 memblock_dbg("memblock_add: [%pa-%pa] %pF\n",
689 &base, &end, (void *)_RET_IP_);
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700690
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700691 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000692}
693
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800694/**
695 * memblock_isolate_range - isolate given range into disjoint memblocks
696 * @type: memblock type to isolate range for
697 * @base: base of range to isolate
698 * @size: size of range to isolate
699 * @start_rgn: out parameter for the start of isolated region
700 * @end_rgn: out parameter for the end of isolated region
701 *
702 * Walk @type and ensure that regions don't cross the boundaries defined by
Mike Rapoport47cec442018-06-30 17:55:02 +0300703 * [@base, @base + @size). Crossing regions are split at the boundaries,
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800704 * which may create at most two more regions. The index of the first
705 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
706 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300707 * Return:
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800708 * 0 on success, -errno on failure.
709 */
710static int __init_memblock memblock_isolate_range(struct memblock_type *type,
711 phys_addr_t base, phys_addr_t size,
712 int *start_rgn, int *end_rgn)
713{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800714 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800715 int idx;
716 struct memblock_region *rgn;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800717
718 *start_rgn = *end_rgn = 0;
719
Tejun Heob3dc6272012-04-20 08:31:34 -0700720 if (!size)
721 return 0;
722
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800723 /* we'll create at most two more regions */
724 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700725 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800726 return -ENOMEM;
727
Gioh Kim66e8b432017-11-15 17:33:42 -0800728 for_each_memblock_type(idx, type, rgn) {
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800729 phys_addr_t rbase = rgn->base;
730 phys_addr_t rend = rbase + rgn->size;
731
732 if (rbase >= end)
733 break;
734 if (rend <= base)
735 continue;
736
737 if (rbase < base) {
738 /*
739 * @rgn intersects from below. Split and continue
740 * to process the next region - the new top half.
741 */
742 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800743 rgn->size -= base - rbase;
744 type->total_size -= base - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800745 memblock_insert_region(type, idx, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800746 memblock_get_region_node(rgn),
747 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800748 } else if (rend > end) {
749 /*
750 * @rgn intersects from above. Split and redo the
751 * current region - the new bottom half.
752 */
753 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800754 rgn->size -= end - rbase;
755 type->total_size -= end - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800756 memblock_insert_region(type, idx--, rbase, end - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800757 memblock_get_region_node(rgn),
758 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800759 } else {
760 /* @rgn is fully contained, record it */
761 if (!*end_rgn)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800762 *start_rgn = idx;
763 *end_rgn = idx + 1;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800764 }
765 }
766
767 return 0;
768}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800769
Alexander Kuleshov35bd16a2015-11-05 18:47:00 -0800770static int __init_memblock memblock_remove_range(struct memblock_type *type,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100771 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000772{
Tejun Heo71936182011-12-08 10:22:07 -0800773 int start_rgn, end_rgn;
774 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000775
Tejun Heo71936182011-12-08 10:22:07 -0800776 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
777 if (ret)
778 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000779
Tejun Heo71936182011-12-08 10:22:07 -0800780 for (i = end_rgn - 1; i >= start_rgn; i--)
781 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700782 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000783}
784
Tejun Heo581adcb2011-12-08 10:22:06 -0800785int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000786{
Minchan Kim25cf23d2018-06-07 17:07:35 -0700787 phys_addr_t end = base + size - 1;
788
789 memblock_dbg("memblock_remove: [%pa-%pa] %pS\n",
790 &base, &end, (void *)_RET_IP_);
791
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100792 return memblock_remove_range(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000793}
794
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100795
Tejun Heo581adcb2011-12-08 10:22:06 -0800796int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000797{
Miles Chen5d63f812017-02-22 15:46:42 -0800798 phys_addr_t end = base + size - 1;
799
800 memblock_dbg(" memblock_free: [%pa-%pa] %pF\n",
801 &base, &end, (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200802
Shiraz Hashim37b60802016-01-19 15:11:10 +0530803 if (base < memblock.current_limit)
804 kmemleak_free_part(__va(base), size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100805 return memblock_remove_range(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000806}
807
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700808int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000809{
Miles Chen5d63f812017-02-22 15:46:42 -0800810 phys_addr_t end = base + size - 1;
811
812 memblock_dbg("memblock_reserve: [%pa-%pa] %pF\n",
813 &base, &end, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000814
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700815 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000816}
817
Tejun Heo35fd0802011-07-12 11:15:59 +0200818/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300819 * memblock_setclr_flag - set or clear flag for a memory region
820 * @base: base address of the region
821 * @size: size of the region
822 * @set: set or clear the flag
823 * @flag: the flag to udpate
Tang Chen66b16ed2014-01-21 15:49:23 -0800824 *
Tony Luck4308ce12014-12-12 16:54:59 -0800825 * This function isolates region [@base, @base + @size), and sets/clears flag
Tang Chen66b16ed2014-01-21 15:49:23 -0800826 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300827 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800828 */
Tony Luck4308ce12014-12-12 16:54:59 -0800829static int __init_memblock memblock_setclr_flag(phys_addr_t base,
830 phys_addr_t size, int set, int flag)
Tang Chen66b16ed2014-01-21 15:49:23 -0800831{
832 struct memblock_type *type = &memblock.memory;
833 int i, ret, start_rgn, end_rgn;
834
835 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
836 if (ret)
837 return ret;
838
839 for (i = start_rgn; i < end_rgn; i++)
Tony Luck4308ce12014-12-12 16:54:59 -0800840 if (set)
841 memblock_set_region_flags(&type->regions[i], flag);
842 else
843 memblock_clear_region_flags(&type->regions[i], flag);
Tang Chen66b16ed2014-01-21 15:49:23 -0800844
845 memblock_merge_regions(type);
846 return 0;
847}
848
849/**
Tony Luck4308ce12014-12-12 16:54:59 -0800850 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
851 * @base: the base phys addr of the region
852 * @size: the size of the region
853 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300854 * Return: 0 on success, -errno on failure.
Tony Luck4308ce12014-12-12 16:54:59 -0800855 */
856int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
857{
858 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
859}
860
861/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800862 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
863 * @base: the base phys addr of the region
864 * @size: the size of the region
865 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300866 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800867 */
868int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
869{
Tony Luck4308ce12014-12-12 16:54:59 -0800870 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
Tang Chen66b16ed2014-01-21 15:49:23 -0800871}
872
873/**
Tony Lucka3f5baf2015-06-24 16:58:12 -0700874 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
875 * @base: the base phys addr of the region
876 * @size: the size of the region
877 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300878 * Return: 0 on success, -errno on failure.
Tony Lucka3f5baf2015-06-24 16:58:12 -0700879 */
880int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
881{
882 system_has_some_mirror = true;
883
884 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
885}
886
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100887/**
888 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
889 * @base: the base phys addr of the region
890 * @size: the size of the region
891 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300892 * Return: 0 on success, -errno on failure.
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100893 */
894int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
895{
896 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
897}
Tony Lucka3f5baf2015-06-24 16:58:12 -0700898
899/**
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900900 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
901 * @base: the base phys addr of the region
902 * @size: the size of the region
903 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300904 * Return: 0 on success, -errno on failure.
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900905 */
906int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
907{
908 return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
909}
910
911/**
Robin Holt8e7a7f82015-06-30 14:56:41 -0700912 * __next_reserved_mem_region - next function for for_each_reserved_region()
913 * @idx: pointer to u64 loop variable
914 * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
915 * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
916 *
917 * Iterate over all reserved memory regions.
918 */
919void __init_memblock __next_reserved_mem_region(u64 *idx,
920 phys_addr_t *out_start,
921 phys_addr_t *out_end)
922{
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700923 struct memblock_type *type = &memblock.reserved;
Robin Holt8e7a7f82015-06-30 14:56:41 -0700924
Richard Leitnercd33a762016-05-20 16:58:33 -0700925 if (*idx < type->cnt) {
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700926 struct memblock_region *r = &type->regions[*idx];
Robin Holt8e7a7f82015-06-30 14:56:41 -0700927 phys_addr_t base = r->base;
928 phys_addr_t size = r->size;
929
930 if (out_start)
931 *out_start = base;
932 if (out_end)
933 *out_end = base + size - 1;
934
935 *idx += 1;
936 return;
937 }
938
939 /* signal end of iteration */
940 *idx = ULLONG_MAX;
941}
942
943/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100944 * __next__mem_range - next function for for_each_free_mem_range() etc.
Tejun Heo35fd0802011-07-12 11:15:59 +0200945 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800946 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700947 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100948 * @type_a: pointer to memblock_type from where the range is taken
949 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -0700950 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
951 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
952 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +0200953 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100954 * Find the first area from *@idx which matches @nid, fill the out
Tejun Heo35fd0802011-07-12 11:15:59 +0200955 * parameters, and update *@idx for the next iteration. The lower 32bit of
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100956 * *@idx contains index into type_a and the upper 32bit indexes the
957 * areas before each region in type_b. For example, if type_b regions
Tejun Heo35fd0802011-07-12 11:15:59 +0200958 * look like the following,
959 *
960 * 0:[0-16), 1:[32-48), 2:[128-130)
961 *
962 * The upper 32bit indexes the following regions.
963 *
964 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
965 *
966 * As both region arrays are sorted, the function advances the two indices
967 * in lockstep and returns each intersection.
968 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300969void __init_memblock __next_mem_range(u64 *idx, int nid,
970 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100971 struct memblock_type *type_a,
972 struct memblock_type *type_b,
973 phys_addr_t *out_start,
974 phys_addr_t *out_end, int *out_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200975{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100976 int idx_a = *idx & 0xffffffff;
977 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -0800978
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100979 if (WARN_ONCE(nid == MAX_NUMNODES,
980 "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
Grygorii Strashko560dca272014-01-21 15:50:55 -0800981 nid = NUMA_NO_NODE;
Tejun Heo35fd0802011-07-12 11:15:59 +0200982
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100983 for (; idx_a < type_a->cnt; idx_a++) {
984 struct memblock_region *m = &type_a->regions[idx_a];
985
Tejun Heo35fd0802011-07-12 11:15:59 +0200986 phys_addr_t m_start = m->base;
987 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100988 int m_nid = memblock_get_region_node(m);
Tejun Heo35fd0802011-07-12 11:15:59 +0200989
990 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100991 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200992 continue;
993
Xishi Qiu0a313a92014-09-09 14:50:46 -0700994 /* skip hotpluggable memory regions if needed */
995 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
996 continue;
997
Tony Lucka3f5baf2015-06-24 16:58:12 -0700998 /* if we want mirror memory skip non-mirror memory regions */
999 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1000 continue;
1001
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001002 /* skip nomap memory unless we were asked for it explicitly */
1003 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1004 continue;
1005
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001006 if (!type_b) {
1007 if (out_start)
1008 *out_start = m_start;
1009 if (out_end)
1010 *out_end = m_end;
1011 if (out_nid)
1012 *out_nid = m_nid;
1013 idx_a++;
1014 *idx = (u32)idx_a | (u64)idx_b << 32;
1015 return;
1016 }
Tejun Heo35fd0802011-07-12 11:15:59 +02001017
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001018 /* scan areas before each reservation */
1019 for (; idx_b < type_b->cnt + 1; idx_b++) {
1020 struct memblock_region *r;
1021 phys_addr_t r_start;
1022 phys_addr_t r_end;
1023
1024 r = &type_b->regions[idx_b];
1025 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1026 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001027 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001028
1029 /*
1030 * if idx_b advanced past idx_a,
1031 * break out to advance idx_a
1032 */
Tejun Heo35fd0802011-07-12 11:15:59 +02001033 if (r_start >= m_end)
1034 break;
1035 /* if the two regions intersect, we're done */
1036 if (m_start < r_end) {
1037 if (out_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001038 *out_start =
1039 max(m_start, r_start);
Tejun Heo35fd0802011-07-12 11:15:59 +02001040 if (out_end)
1041 *out_end = min(m_end, r_end);
1042 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001043 *out_nid = m_nid;
Tejun Heo35fd0802011-07-12 11:15:59 +02001044 /*
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001045 * The region which ends first is
1046 * advanced for the next iteration.
Tejun Heo35fd0802011-07-12 11:15:59 +02001047 */
1048 if (m_end <= r_end)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001049 idx_a++;
Tejun Heo35fd0802011-07-12 11:15:59 +02001050 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001051 idx_b++;
1052 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo35fd0802011-07-12 11:15:59 +02001053 return;
1054 }
1055 }
1056 }
1057
1058 /* signal end of iteration */
1059 *idx = ULLONG_MAX;
1060}
1061
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001062/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001063 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1064 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001065 * @idx: pointer to u64 loop variable
Alexander Kuleshovad5ea8c2015-09-08 15:04:22 -07001066 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -07001067 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001068 * @type_a: pointer to memblock_type from where the range is taken
1069 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -07001070 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1071 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1072 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001073 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001074 * Finds the next range from type_a which is not marked as unsuitable
1075 * in type_b.
1076 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001077 * Reverse of __next_mem_range().
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001078 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001079void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1080 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001081 struct memblock_type *type_a,
1082 struct memblock_type *type_b,
1083 phys_addr_t *out_start,
1084 phys_addr_t *out_end, int *out_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001085{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001086 int idx_a = *idx & 0xffffffff;
1087 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -08001088
Grygorii Strashko560dca272014-01-21 15:50:55 -08001089 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1090 nid = NUMA_NO_NODE;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001091
1092 if (*idx == (u64)ULLONG_MAX) {
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001093 idx_a = type_a->cnt - 1;
zijun_hue47608a2016-08-04 15:32:00 -07001094 if (type_b != NULL)
1095 idx_b = type_b->cnt;
1096 else
1097 idx_b = 0;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001098 }
1099
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001100 for (; idx_a >= 0; idx_a--) {
1101 struct memblock_region *m = &type_a->regions[idx_a];
1102
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001103 phys_addr_t m_start = m->base;
1104 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001105 int m_nid = memblock_get_region_node(m);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001106
1107 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001108 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001109 continue;
1110
Tang Chen55ac5902014-01-21 15:49:35 -08001111 /* skip hotpluggable memory regions if needed */
1112 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
1113 continue;
1114
Tony Lucka3f5baf2015-06-24 16:58:12 -07001115 /* if we want mirror memory skip non-mirror memory regions */
1116 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1117 continue;
1118
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001119 /* skip nomap memory unless we were asked for it explicitly */
1120 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1121 continue;
1122
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001123 if (!type_b) {
1124 if (out_start)
1125 *out_start = m_start;
1126 if (out_end)
1127 *out_end = m_end;
1128 if (out_nid)
1129 *out_nid = m_nid;
zijun_hufb399b42016-07-28 15:48:56 -07001130 idx_a--;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001131 *idx = (u32)idx_a | (u64)idx_b << 32;
1132 return;
1133 }
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001134
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001135 /* scan areas before each reservation */
1136 for (; idx_b >= 0; idx_b--) {
1137 struct memblock_region *r;
1138 phys_addr_t r_start;
1139 phys_addr_t r_end;
1140
1141 r = &type_b->regions[idx_b];
1142 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1143 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001144 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001145 /*
1146 * if idx_b advanced past idx_a,
1147 * break out to advance idx_a
1148 */
1149
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001150 if (r_end <= m_start)
1151 break;
1152 /* if the two regions intersect, we're done */
1153 if (m_end > r_start) {
1154 if (out_start)
1155 *out_start = max(m_start, r_start);
1156 if (out_end)
1157 *out_end = min(m_end, r_end);
1158 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001159 *out_nid = m_nid;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001160 if (m_start >= r_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001161 idx_a--;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001162 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001163 idx_b--;
1164 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001165 return;
1166 }
1167 }
1168 }
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001169 /* signal end of iteration */
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001170 *idx = ULLONG_MAX;
1171}
1172
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001173#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1174/*
1175 * Common iterator interface used to define for_each_mem_range().
1176 */
1177void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1178 unsigned long *out_start_pfn,
1179 unsigned long *out_end_pfn, int *out_nid)
1180{
1181 struct memblock_type *type = &memblock.memory;
1182 struct memblock_region *r;
1183
1184 while (++*idx < type->cnt) {
1185 r = &type->regions[*idx];
1186
1187 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1188 continue;
1189 if (nid == MAX_NUMNODES || nid == r->nid)
1190 break;
1191 }
1192 if (*idx >= type->cnt) {
1193 *idx = -1;
1194 return;
1195 }
1196
1197 if (out_start_pfn)
1198 *out_start_pfn = PFN_UP(r->base);
1199 if (out_end_pfn)
1200 *out_end_pfn = PFN_DOWN(r->base + r->size);
1201 if (out_nid)
1202 *out_nid = r->nid;
1203}
1204
1205/**
1206 * memblock_set_node - set node ID on memblock regions
1207 * @base: base of area to set node ID for
1208 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -08001209 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001210 * @nid: node ID to set
1211 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001212 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001213 * Regions which cross the area boundaries are split as necessary.
1214 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001215 * Return:
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001216 * 0 on success, -errno on failure.
1217 */
1218int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -08001219 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001220{
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001221 int start_rgn, end_rgn;
1222 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001223
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001224 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1225 if (ret)
1226 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001227
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001228 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -07001229 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001230
1231 memblock_merge_regions(type);
1232 return 0;
1233}
1234#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1235
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001236static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1237 phys_addr_t align, phys_addr_t start,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001238 phys_addr_t end, int nid,
1239 enum memblock_flags flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001240{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001241 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001242
Grygorii Strashko79f40fa2014-01-21 15:50:12 -08001243 if (!align)
1244 align = SMP_CACHE_BYTES;
Vineet Gupta94f3d3a2013-04-29 15:06:15 -07001245
Tony Luckfc6daaf2015-06-24 16:58:09 -07001246 found = memblock_find_in_range_node(size, align, start, end, nid,
1247 flags);
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001248 if (found && !memblock_reserve(found, size)) {
1249 /*
1250 * The min_count is set to 0 so that memblock allocations are
1251 * never reported as leaks.
1252 */
Shiraz Hashim37b60802016-01-19 15:11:10 +05301253 if (found < memblock.current_limit)
1254 kmemleak_alloc(__va(found), size, 0, 0);
1255
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001256 return found;
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001257 }
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001258 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001259}
1260
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001261phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001262 phys_addr_t start, phys_addr_t end,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001263 enum memblock_flags flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001264{
Faiyaz Mohammedbf5dfef2019-10-09 11:38:35 +05301265 memblock_dbg("%s: size: %llu align: %llu %pS\n",
1266 __func__, (u64)size, (u64)align, (void *)_RET_IP_);
Tony Luckfc6daaf2015-06-24 16:58:09 -07001267 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1268 flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001269}
1270
Nicholas Pigginb575454f2018-02-14 01:08:15 +10001271phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001272 phys_addr_t align, phys_addr_t max_addr,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001273 int nid, enum memblock_flags flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001274{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001275 return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001276}
1277
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001278phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
1279{
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001280 enum memblock_flags flags = choose_memblock_flags();
Tony Lucka3f5baf2015-06-24 16:58:12 -07001281 phys_addr_t ret;
1282
1283again:
1284 ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE,
1285 nid, flags);
1286
1287 if (!ret && (flags & MEMBLOCK_MIRROR)) {
1288 flags &= ~MEMBLOCK_MIRROR;
1289 goto again;
1290 }
1291 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001292}
1293
1294phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1295{
Faiyaz Mohammedbf5dfef2019-10-09 11:38:35 +05301296 memblock_dbg("%s: size: %llu align: %llu %pS\n",
1297 __func__, (u64)size, (u64)align, (void *)_RET_IP_);
Tony Luckfc6daaf2015-06-24 16:58:09 -07001298 return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE,
1299 MEMBLOCK_NONE);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001300}
1301
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001302phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001303{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001304 phys_addr_t alloc;
1305
1306 alloc = __memblock_alloc_base(size, align, max_addr);
1307
1308 if (alloc == 0)
Miles Chen5d63f812017-02-22 15:46:42 -08001309 panic("ERROR: Failed to allocate %pa bytes below %pa.\n",
1310 &size, &max_addr);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001311
1312 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001313}
1314
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001315phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001316{
Faiyaz Mohammedbf5dfef2019-10-09 11:38:35 +05301317 memblock_dbg("%s: size: %llu align: %llu %pS\n",
1318 __func__, (u64)size, (u64)align, (void *)_RET_IP_);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001319 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001320}
1321
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001322phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1323{
1324 phys_addr_t res = memblock_alloc_nid(size, align, nid);
1325
1326 if (res)
1327 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +02001328 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001329}
1330
Mathieu Malaterre19373672018-07-20 17:53:31 -07001331#if defined(CONFIG_NO_BOOTMEM)
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001332/**
1333 * memblock_virt_alloc_internal - allocate boot memory block
1334 * @size: size of memory block to be allocated in bytes
1335 * @align: alignment of the region and block's size
1336 * @min_addr: the lower bound of the memory region to allocate (phys address)
1337 * @max_addr: the upper bound of the memory region to allocate (phys address)
1338 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1339 *
1340 * The @min_addr limit is dropped if it can not be satisfied and the allocation
1341 * will fall back to memory below @min_addr. Also, allocation may fall back
1342 * to any node in the system if the specified node can not
1343 * hold the requested memory.
1344 *
1345 * The allocation is performed from memory region limited by
1346 * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE.
1347 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001348 * The memory block is aligned on %SMP_CACHE_BYTES if @align == 0.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001349 *
1350 * The phys address of allocated boot memory block is converted to virtual and
1351 * allocated memory is reset to 0.
1352 *
1353 * In addition, function sets the min_count to 0 using kmemleak_alloc for
1354 * allocated boot memory block, so that it is never reported as leaks.
1355 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001356 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001357 * Virtual address of allocated memory block on success, NULL on failure.
1358 */
1359static void * __init memblock_virt_alloc_internal(
1360 phys_addr_t size, phys_addr_t align,
1361 phys_addr_t min_addr, phys_addr_t max_addr,
1362 int nid)
1363{
1364 phys_addr_t alloc;
1365 void *ptr;
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001366 enum memblock_flags flags = choose_memblock_flags();
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001367
Grygorii Strashko560dca272014-01-21 15:50:55 -08001368 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1369 nid = NUMA_NO_NODE;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001370
1371 /*
1372 * Detect any accidental use of these APIs after slab is ready, as at
1373 * this moment memblock may be deinitialized already and its
1374 * internal data may be destroyed (after execution of free_all_bootmem)
1375 */
1376 if (WARN_ON_ONCE(slab_is_available()))
1377 return kzalloc_node(size, GFP_NOWAIT, nid);
1378
1379 if (!align)
1380 align = SMP_CACHE_BYTES;
1381
Yinghai Luf544e142014-01-29 14:05:52 -08001382 if (max_addr > memblock.current_limit)
1383 max_addr = memblock.current_limit;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001384again:
1385 alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001386 nid, flags);
Wei Yang7d41c032017-02-22 15:45:07 -08001387 if (alloc && !memblock_reserve(alloc, size))
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001388 goto done;
1389
1390 if (nid != NUMA_NO_NODE) {
1391 alloc = memblock_find_in_range_node(size, align, min_addr,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001392 max_addr, NUMA_NO_NODE,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001393 flags);
Wei Yang7d41c032017-02-22 15:45:07 -08001394 if (alloc && !memblock_reserve(alloc, size))
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001395 goto done;
1396 }
1397
1398 if (min_addr) {
1399 min_addr = 0;
1400 goto again;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001401 }
1402
Tony Lucka3f5baf2015-06-24 16:58:12 -07001403 if (flags & MEMBLOCK_MIRROR) {
1404 flags &= ~MEMBLOCK_MIRROR;
1405 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1406 &size);
1407 goto again;
1408 }
1409
1410 return NULL;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001411done:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001412 ptr = phys_to_virt(alloc);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001413
Qian Cai88299f62018-12-28 00:36:29 -08001414 /* Skip kmemleak for kasan_init() due to high volume. */
1415 if (max_addr != MEMBLOCK_ALLOC_KASAN)
1416 /*
1417 * The min_count is set to 0 so that bootmem allocated
1418 * blocks are never reported as leaks. This is because many
1419 * of these blocks are only referred via the physical
1420 * address which is not looked up by kmemleak.
1421 */
1422 kmemleak_alloc(ptr, size, 0, 0);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001423
1424 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001425}
1426
1427/**
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001428 * memblock_virt_alloc_try_nid_raw - allocate boot memory block without zeroing
1429 * memory and without panicking
1430 * @size: size of memory block to be allocated in bytes
1431 * @align: alignment of the region and block's size
1432 * @min_addr: the lower bound of the memory region from where the allocation
1433 * is preferred (phys address)
1434 * @max_addr: the upper bound of the memory region from where the allocation
1435 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1436 * allocate only from memory limited by memblock.current_limit value
1437 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1438 *
1439 * Public function, provides additional debug information (including caller
1440 * info), if enabled. Does not zero allocated memory, does not panic if request
1441 * cannot be satisfied.
1442 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001443 * Return:
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001444 * Virtual address of allocated memory block on success, NULL on failure.
1445 */
1446void * __init memblock_virt_alloc_try_nid_raw(
1447 phys_addr_t size, phys_addr_t align,
1448 phys_addr_t min_addr, phys_addr_t max_addr,
1449 int nid)
1450{
1451 void *ptr;
1452
Mike Rapoporta36aab82018-08-17 15:47:17 -07001453 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1454 __func__, (u64)size, (u64)align, nid, &min_addr,
1455 &max_addr, (void *)_RET_IP_);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001456
1457 ptr = memblock_virt_alloc_internal(size, align,
1458 min_addr, max_addr, nid);
1459#ifdef CONFIG_DEBUG_VM
1460 if (ptr && size > 0)
Pavel Tatashinf165b372018-04-05 16:22:47 -07001461 memset(ptr, PAGE_POISON_PATTERN, size);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001462#endif
1463 return ptr;
1464}
1465
1466/**
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001467 * memblock_virt_alloc_try_nid_nopanic - allocate boot memory block
1468 * @size: size of memory block to be allocated in bytes
1469 * @align: alignment of the region and block's size
1470 * @min_addr: the lower bound of the memory region from where the allocation
1471 * is preferred (phys address)
1472 * @max_addr: the upper bound of the memory region from where the allocation
1473 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1474 * allocate only from memory limited by memblock.current_limit value
1475 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1476 *
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001477 * Public function, provides additional debug information (including caller
1478 * info), if enabled. This function zeroes the allocated memory.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001479 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001480 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001481 * Virtual address of allocated memory block on success, NULL on failure.
1482 */
1483void * __init memblock_virt_alloc_try_nid_nopanic(
1484 phys_addr_t size, phys_addr_t align,
1485 phys_addr_t min_addr, phys_addr_t max_addr,
1486 int nid)
1487{
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001488 void *ptr;
1489
Mike Rapoporta36aab82018-08-17 15:47:17 -07001490 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1491 __func__, (u64)size, (u64)align, nid, &min_addr,
1492 &max_addr, (void *)_RET_IP_);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001493
1494 ptr = memblock_virt_alloc_internal(size, align,
1495 min_addr, max_addr, nid);
1496 if (ptr)
1497 memset(ptr, 0, size);
1498 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001499}
1500
1501/**
1502 * memblock_virt_alloc_try_nid - allocate boot memory block with panicking
1503 * @size: size of memory block to be allocated in bytes
1504 * @align: alignment of the region and block's size
1505 * @min_addr: the lower bound of the memory region from where the allocation
1506 * is preferred (phys address)
1507 * @max_addr: the upper bound of the memory region from where the allocation
1508 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1509 * allocate only from memory limited by memblock.current_limit value
1510 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1511 *
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001512 * Public panicking version of memblock_virt_alloc_try_nid_nopanic()
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001513 * which provides debug information (including caller info), if enabled,
1514 * and panics if the request can not be satisfied.
1515 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001516 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001517 * Virtual address of allocated memory block on success, NULL on failure.
1518 */
1519void * __init memblock_virt_alloc_try_nid(
1520 phys_addr_t size, phys_addr_t align,
1521 phys_addr_t min_addr, phys_addr_t max_addr,
1522 int nid)
1523{
1524 void *ptr;
1525
Mike Rapoporta36aab82018-08-17 15:47:17 -07001526 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1527 __func__, (u64)size, (u64)align, nid, &min_addr,
1528 &max_addr, (void *)_RET_IP_);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001529 ptr = memblock_virt_alloc_internal(size, align,
1530 min_addr, max_addr, nid);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001531 if (ptr) {
1532 memset(ptr, 0, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001533 return ptr;
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001534 }
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001535
Mike Rapoporta36aab82018-08-17 15:47:17 -07001536 panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa\n",
1537 __func__, (u64)size, (u64)align, nid, &min_addr, &max_addr);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001538 return NULL;
1539}
Mathieu Malaterre19373672018-07-20 17:53:31 -07001540#endif
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001541
1542/**
1543 * __memblock_free_early - free boot memory block
1544 * @base: phys starting address of the boot memory block
1545 * @size: size of the boot memory block in bytes
1546 *
1547 * Free boot memory block previously allocated by memblock_virt_alloc_xx() API.
1548 * The freeing memory will not be released to the buddy allocator.
1549 */
1550void __init __memblock_free_early(phys_addr_t base, phys_addr_t size)
1551{
Wentao Wang56435692018-12-28 00:35:26 -08001552 memblock_free(base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001553}
1554
Mike Rapoport48a833c2018-06-30 17:55:03 +03001555/**
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001556 * __memblock_free_late - free bootmem block pages directly to buddy allocator
Mike Rapoport48a833c2018-06-30 17:55:03 +03001557 * @base: phys starting address of the boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001558 * @size: size of the boot memory block in bytes
1559 *
1560 * This is only useful when the bootmem allocator has already been torn
1561 * down, but we are still initializing the system. Pages are released directly
1562 * to the buddy allocator, no bootmem metadata is updated because it is gone.
1563 */
1564void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1565{
Mike Rapoporta36aab82018-08-17 15:47:17 -07001566 phys_addr_t cursor, end;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001567
Mike Rapoporta36aab82018-08-17 15:47:17 -07001568 end = base + size - 1;
1569 memblock_dbg("%s: [%pa-%pa] %pF\n",
1570 __func__, &base, &end, (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001571 kmemleak_free_part_phys(base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001572 cursor = PFN_UP(base);
1573 end = PFN_DOWN(base + size);
1574
1575 for (; cursor < end; cursor++) {
Mel Gormand70ddd72015-06-30 14:56:52 -07001576 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001577 totalram_pages++;
1578 }
1579}
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001580
1581/*
1582 * Remaining API functions
1583 */
1584
David Gibson1f1ffb8a2016-02-05 15:36:19 -08001585phys_addr_t __init_memblock memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001586{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001587 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001588}
1589
Srikar Dronamraju8907de52016-10-07 16:59:18 -07001590phys_addr_t __init_memblock memblock_reserved_size(void)
1591{
1592 return memblock.reserved.total_size;
1593}
1594
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001595phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1596{
1597 unsigned long pages = 0;
1598 struct memblock_region *r;
1599 unsigned long start_pfn, end_pfn;
1600
1601 for_each_memblock(memory, r) {
1602 start_pfn = memblock_region_memory_base_pfn(r);
1603 end_pfn = memblock_region_memory_end_pfn(r);
1604 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1605 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1606 pages += end_pfn - start_pfn;
1607 }
1608
Fabian Frederick16763232014-04-07 15:37:53 -07001609 return PFN_PHYS(pages);
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001610}
1611
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001612/* lowest address */
1613phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1614{
1615 return memblock.memory.regions[0].base;
1616}
1617
Yinghai Lu10d06432010-07-28 15:43:02 +10001618phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001619{
1620 int idx = memblock.memory.cnt - 1;
1621
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001622 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001623}
1624
Dennis Chena571d4e2016-07-28 15:48:26 -07001625static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001626{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001627 phys_addr_t max_addr = PHYS_ADDR_MAX;
Emil Medve136199f2014-04-07 15:37:52 -07001628 struct memblock_region *r;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001629
Dennis Chena571d4e2016-07-28 15:48:26 -07001630 /*
1631 * translate the memory @limit size into the max address within one of
1632 * the memory memblock regions, if the @limit exceeds the total size
Stefan Agner1c4bc432018-06-07 17:06:15 -07001633 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
Dennis Chena571d4e2016-07-28 15:48:26 -07001634 */
Emil Medve136199f2014-04-07 15:37:52 -07001635 for_each_memblock(memory, r) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001636 if (limit <= r->size) {
1637 max_addr = r->base + limit;
1638 break;
1639 }
1640 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001641 }
1642
Dennis Chena571d4e2016-07-28 15:48:26 -07001643 return max_addr;
1644}
1645
Sudarshan Rajagopalan1316b0b2018-07-05 18:41:13 -07001646phys_addr_t __init_memblock memblock_max_addr(phys_addr_t limit)
1647{
1648 return __find_max_addr(limit);
1649}
1650
Dennis Chena571d4e2016-07-28 15:48:26 -07001651void __init memblock_enforce_memory_limit(phys_addr_t limit)
1652{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001653 phys_addr_t max_addr = PHYS_ADDR_MAX;
Dennis Chena571d4e2016-07-28 15:48:26 -07001654
1655 if (!limit)
1656 return;
1657
1658 max_addr = __find_max_addr(limit);
1659
1660 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001661 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001662 return;
1663
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001664 /* truncate both memory and reserved regions */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001665 memblock_remove_range(&memblock.memory, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001666 PHYS_ADDR_MAX);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001667 memblock_remove_range(&memblock.reserved, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001668 PHYS_ADDR_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001669}
1670
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001671void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1672{
1673 int start_rgn, end_rgn;
1674 int i, ret;
1675
1676 if (!size)
1677 return;
1678
1679 ret = memblock_isolate_range(&memblock.memory, base, size,
1680 &start_rgn, &end_rgn);
1681 if (ret)
1682 return;
1683
1684 /* remove all the MAP regions */
1685 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1686 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1687 memblock_remove_region(&memblock.memory, i);
1688
1689 for (i = start_rgn - 1; i >= 0; i--)
1690 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1691 memblock_remove_region(&memblock.memory, i);
1692
1693 /* truncate the reserved regions */
1694 memblock_remove_range(&memblock.reserved, 0, base);
1695 memblock_remove_range(&memblock.reserved,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001696 base + size, PHYS_ADDR_MAX);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001697}
1698
Dennis Chena571d4e2016-07-28 15:48:26 -07001699void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1700{
Dennis Chena571d4e2016-07-28 15:48:26 -07001701 phys_addr_t max_addr;
Dennis Chena571d4e2016-07-28 15:48:26 -07001702
1703 if (!limit)
1704 return;
1705
1706 max_addr = __find_max_addr(limit);
1707
1708 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001709 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001710 return;
1711
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001712 memblock_cap_memory_range(0, max_addr);
Dennis Chena571d4e2016-07-28 15:48:26 -07001713}
1714
Yinghai Lucd794812010-10-11 12:34:09 -07001715static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001716{
1717 unsigned int left = 0, right = type->cnt;
1718
1719 do {
1720 unsigned int mid = (right + left) / 2;
1721
1722 if (addr < type->regions[mid].base)
1723 right = mid;
1724 else if (addr >= (type->regions[mid].base +
1725 type->regions[mid].size))
1726 left = mid + 1;
1727 else
1728 return mid;
1729 } while (left < right);
1730 return -1;
1731}
1732
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001733bool __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001734{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001735 return memblock_search(&memblock.reserved, addr) != -1;
1736}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001737
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001738bool __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001739{
1740 return memblock_search(&memblock.memory, addr) != -1;
1741}
1742
Yaowei Bai937f0c22018-02-06 15:41:18 -08001743bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001744{
1745 int i = memblock_search(&memblock.memory, addr);
1746
1747 if (i == -1)
1748 return false;
1749 return !memblock_is_nomap(&memblock.memory.regions[i]);
1750}
1751
Yinghai Lue76b63f2013-09-11 14:22:17 -07001752#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1753int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1754 unsigned long *start_pfn, unsigned long *end_pfn)
1755{
1756 struct memblock_type *type = &memblock.memory;
Fabian Frederick16763232014-04-07 15:37:53 -07001757 int mid = memblock_search(type, PFN_PHYS(pfn));
Yinghai Lue76b63f2013-09-11 14:22:17 -07001758
1759 if (mid == -1)
1760 return -1;
1761
Fabian Frederickf7e2f7e2014-06-04 16:07:51 -07001762 *start_pfn = PFN_DOWN(type->regions[mid].base);
1763 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001764
1765 return type->regions[mid].nid;
1766}
1767#endif
1768
Stephen Boydeab30942012-05-24 00:45:21 -07001769/**
1770 * memblock_is_region_memory - check if a region is a subset of memory
1771 * @base: base of region to check
1772 * @size: size of region to check
1773 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001774 * Check if the region [@base, @base + @size) is a subset of a memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001775 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001776 * Return:
Stephen Boydeab30942012-05-24 00:45:21 -07001777 * 0 if false, non-zero if true
1778 */
Yaowei Bai937f0c22018-02-06 15:41:18 -08001779bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001780{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001781 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001782 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001783
1784 if (idx == -1)
Yaowei Bai937f0c22018-02-06 15:41:18 -08001785 return false;
Wei Yangef415ef2017-02-22 15:45:04 -08001786 return (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001787 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001788}
1789
Swathi Sridharbbbc80b2018-07-13 10:02:08 -07001790bool __init_memblock memblock_overlaps_memory(phys_addr_t base,
1791 phys_addr_t size)
1792{
1793 memblock_cap_size(base, &size);
1794
1795 return memblock_overlaps_region(&memblock.memory, base, size);
1796}
1797
Stephen Boydeab30942012-05-24 00:45:21 -07001798/**
1799 * memblock_is_region_reserved - check if a region intersects reserved memory
1800 * @base: base of region to check
1801 * @size: size of region to check
1802 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001803 * Check if the region [@base, @base + @size) intersects a reserved
1804 * memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001805 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001806 * Return:
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001807 * True if they intersect, false if not.
Stephen Boydeab30942012-05-24 00:45:21 -07001808 */
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001809bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001810{
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001811 memblock_cap_size(base, &size);
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001812 return memblock_overlaps_region(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001813}
1814
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001815void __init_memblock memblock_trim_memory(phys_addr_t align)
1816{
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001817 phys_addr_t start, end, orig_start, orig_end;
Emil Medve136199f2014-04-07 15:37:52 -07001818 struct memblock_region *r;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001819
Emil Medve136199f2014-04-07 15:37:52 -07001820 for_each_memblock(memory, r) {
1821 orig_start = r->base;
1822 orig_end = r->base + r->size;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001823 start = round_up(orig_start, align);
1824 end = round_down(orig_end, align);
1825
1826 if (start == orig_start && end == orig_end)
1827 continue;
1828
1829 if (start < end) {
Emil Medve136199f2014-04-07 15:37:52 -07001830 r->base = start;
1831 r->size = end - start;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001832 } else {
Emil Medve136199f2014-04-07 15:37:52 -07001833 memblock_remove_region(&memblock.memory,
1834 r - memblock.memory.regions);
1835 r--;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001836 }
1837 }
1838}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001839
Yinghai Lu3661ca62010-09-15 13:05:29 -07001840void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001841{
1842 memblock.current_limit = limit;
1843}
1844
Laura Abbottfec51012014-02-27 01:23:43 +01001845phys_addr_t __init_memblock memblock_get_current_limit(void)
1846{
1847 return memblock.current_limit;
1848}
1849
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001850static void __init_memblock memblock_dump(struct memblock_type *type)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001851{
Miles Chen5d63f812017-02-22 15:46:42 -08001852 phys_addr_t base, end, size;
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001853 enum memblock_flags flags;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001854 int idx;
1855 struct memblock_region *rgn;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001856
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001857 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001858
Gioh Kim66e8b432017-11-15 17:33:42 -08001859 for_each_memblock_type(idx, type, rgn) {
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001860 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001861
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001862 base = rgn->base;
1863 size = rgn->size;
Miles Chen5d63f812017-02-22 15:46:42 -08001864 end = base + size - 1;
Tang Chen66a20752014-01-21 15:49:20 -08001865 flags = rgn->flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001866#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1867 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1868 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1869 memblock_get_region_node(rgn));
1870#endif
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001871 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001872 type->name, idx, &base, &end, &size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001873 }
1874}
1875
Tejun Heo4ff7b822011-12-08 10:22:06 -08001876void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001877{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001878 pr_info("MEMBLOCK configuration:\n");
Miles Chen5d63f812017-02-22 15:46:42 -08001879 pr_info(" memory size = %pa reserved size = %pa\n",
1880 &memblock.memory.total_size,
1881 &memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001882
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001883 memblock_dump(&memblock.memory);
1884 memblock_dump(&memblock.reserved);
Heiko Carstens409efd42017-02-24 14:55:56 -08001885#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001886 memblock_dump(&memblock.physmem);
Heiko Carstens409efd42017-02-24 14:55:56 -08001887#endif
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001888}
1889
Tejun Heo1aadc052011-12-08 10:22:08 -08001890void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001891{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001892 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001893}
1894
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001895static int __init early_memblock(char *p)
1896{
1897 if (p && strstr(p, "debug"))
1898 memblock_debug = 1;
1899 return 0;
1900}
1901early_param("memblock", early_memblock);
1902
Tejun Heoc378ddd2011-07-14 11:46:03 +02001903#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001904
1905static int memblock_debug_show(struct seq_file *m, void *private)
1906{
1907 struct memblock_type *type = m->private;
1908 struct memblock_region *reg;
1909 int i;
Miles Chen5d63f812017-02-22 15:46:42 -08001910 phys_addr_t end;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001911
1912 for (i = 0; i < type->cnt; i++) {
1913 reg = &type->regions[i];
Miles Chen5d63f812017-02-22 15:46:42 -08001914 end = reg->base + reg->size - 1;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001915
Miles Chen5d63f812017-02-22 15:46:42 -08001916 seq_printf(m, "%4d: ", i);
1917 seq_printf(m, "%pa..%pa\n", &reg->base, &end);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001918 }
1919 return 0;
1920}
Andy Shevchenko5ad35092018-04-05 16:23:16 -07001921DEFINE_SHOW_ATTRIBUTE(memblock_debug);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001922
1923static int __init memblock_init_debugfs(void)
1924{
1925 struct dentry *root = debugfs_create_dir("memblock", NULL);
1926 if (!root)
1927 return -ENXIO;
Joe Perches0825a6f2018-06-14 15:27:58 -07001928 debugfs_create_file("memory", 0444, root,
1929 &memblock.memory, &memblock_debug_fops);
1930 debugfs_create_file("reserved", 0444, root,
1931 &memblock.reserved, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001932#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
Joe Perches0825a6f2018-06-14 15:27:58 -07001933 debugfs_create_file("physmem", 0444, root,
1934 &memblock.physmem, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001935#endif
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001936
1937 return 0;
1938}
1939__initcall(memblock_init_debugfs);
1940
1941#endif /* CONFIG_DEBUG_FS */