blob: e2f397174734e5f9df57040dc3692c43f9d68d0f [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 */
256 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
257 end = memblock.current_limit;
258
259 /* avoid allocating the first page */
260 start = max_t(phys_addr_t, start, PAGE_SIZE);
261 end = max(start, end);
Tang Chen79442ed2013-11-12 15:07:59 -0800262 kernel_end = __pa_symbol(_end);
263
264 /*
265 * try bottom-up allocation only when bottom-up mode
266 * is set and @end is above the kernel image.
267 */
268 if (memblock_bottom_up() && end > kernel_end) {
269 phys_addr_t bottom_up_start;
270
271 /* make sure we will allocate above the kernel */
272 bottom_up_start = max(start, kernel_end);
273
274 /* ok, try bottom-up allocation first */
275 ret = __memblock_find_range_bottom_up(bottom_up_start, end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700276 size, align, nid, flags);
Tang Chen79442ed2013-11-12 15:07:59 -0800277 if (ret)
278 return ret;
279
280 /*
281 * we always limit bottom-up allocation above the kernel,
282 * but top-down allocation doesn't have the limit, so
283 * retrying top-down allocation may succeed when bottom-up
284 * allocation failed.
285 *
286 * bottom-up allocation is expected to be fail very rarely,
287 * so we use WARN_ONCE() here to see the stack trace if
288 * fail happens.
289 */
Michal Hockoe3d301c2018-07-13 16:59:16 -0700290 WARN_ONCE(IS_ENABLED(CONFIG_MEMORY_HOTREMOVE),
291 "memblock: bottom-up allocation failed, memory hotremove may be affected\n");
Tang Chen79442ed2013-11-12 15:07:59 -0800292 }
Tang Chenf7210e62013-02-22 16:33:51 -0800293
Tony Luckfc6daaf2015-06-24 16:58:09 -0700294 return __memblock_find_range_top_down(start, end, size, align, nid,
295 flags);
Tang Chenf7210e62013-02-22 16:33:51 -0800296}
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000297
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800298/**
299 * memblock_find_in_range - find free area in given range
300 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300301 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
302 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800303 * @size: size of free area to find
304 * @align: alignment of free area to find
305 *
306 * Find @size free area aligned to @align in the specified range.
307 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300308 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800309 * Found address on success, 0 on failure.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800310 */
311phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
312 phys_addr_t end, phys_addr_t size,
313 phys_addr_t align)
314{
Tony Lucka3f5baf2015-06-24 16:58:12 -0700315 phys_addr_t ret;
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300316 enum memblock_flags flags = choose_memblock_flags();
Tony Lucka3f5baf2015-06-24 16:58:12 -0700317
318again:
319 ret = memblock_find_in_range_node(size, align, start, end,
320 NUMA_NO_NODE, flags);
321
322 if (!ret && (flags & MEMBLOCK_MIRROR)) {
323 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
324 &size);
325 flags &= ~MEMBLOCK_MIRROR;
326 goto again;
327 }
328
329 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800330}
331
Yinghai Lu10d06432010-07-28 15:43:02 +1000332static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000333{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800334 type->total_size -= type->regions[r].size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200335 memmove(&type->regions[r], &type->regions[r + 1],
336 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000337 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000338
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700339 /* Special case for empty arrays */
340 if (type->cnt == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800341 WARN_ON(type->total_size != 0);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700342 type->cnt = 1;
343 type->regions[0].base = 0;
344 type->regions[0].size = 0;
Tang Chen66a20752014-01-21 15:49:20 -0800345 type->regions[0].flags = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200346 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700347 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000348}
349
Philipp Hachtmann354f17e2014-01-23 15:53:24 -0800350#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
Pavel Tatashin3010f872017-08-18 15:16:05 -0700351/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300352 * memblock_discard - discard memory and reserved arrays if they were allocated
Pavel Tatashin3010f872017-08-18 15:16:05 -0700353 */
354void __init memblock_discard(void)
Yinghai Lu29f67382012-07-11 14:02:56 -0700355{
Pavel Tatashin3010f872017-08-18 15:16:05 -0700356 phys_addr_t addr, size;
Yinghai Lu29f67382012-07-11 14:02:56 -0700357
Pavel Tatashin3010f872017-08-18 15:16:05 -0700358 if (memblock.reserved.regions != memblock_reserved_init_regions) {
359 addr = __pa(memblock.reserved.regions);
360 size = PAGE_ALIGN(sizeof(struct memblock_region) *
361 memblock.reserved.max);
362 __memblock_free_late(addr, size);
363 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700364
Pavel Tatashin91b540f92017-08-25 15:55:46 -0700365 if (memblock.memory.regions != memblock_memory_init_regions) {
Pavel Tatashin3010f872017-08-18 15:16:05 -0700366 addr = __pa(memblock.memory.regions);
367 size = PAGE_ALIGN(sizeof(struct memblock_region) *
368 memblock.memory.max);
369 __memblock_free_late(addr, size);
370 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700371}
Philipp Hachtmann5e270e22014-01-23 15:53:11 -0800372#endif
373
Greg Pearson48c3b582012-06-20 12:53:05 -0700374/**
375 * memblock_double_array - double the size of the memblock regions array
376 * @type: memblock type of the regions array being doubled
377 * @new_area_start: starting address of memory range to avoid overlap with
378 * @new_area_size: size of memory range to avoid overlap with
379 *
380 * Double the size of the @type regions array. If memblock is being used to
381 * allocate memory for a new reserved regions array and there is a previously
Mike Rapoport47cec442018-06-30 17:55:02 +0300382 * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
Greg Pearson48c3b582012-06-20 12:53:05 -0700383 * waiting to be reserved, ensure the memory used by the new array does
384 * not overlap.
385 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300386 * Return:
Greg Pearson48c3b582012-06-20 12:53:05 -0700387 * 0 on success, -1 on failure.
388 */
389static int __init_memblock memblock_double_array(struct memblock_type *type,
390 phys_addr_t new_area_start,
391 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700392{
393 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700394 phys_addr_t old_alloc_size, new_alloc_size;
Mike Rapoporta36aab82018-08-17 15:47:17 -0700395 phys_addr_t old_size, new_size, addr, new_end;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700396 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700397 int *in_slab;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700398
399 /* We don't allow resizing until we know about the reserved regions
400 * of memory that aren't suitable for allocation
401 */
402 if (!memblock_can_resize)
403 return -1;
404
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700405 /* Calculate new doubled size */
406 old_size = type->max * sizeof(struct memblock_region);
407 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700408 /*
409 * We need to allocated new one align to PAGE_SIZE,
410 * so we can free them completely later.
411 */
412 old_alloc_size = PAGE_ALIGN(old_size);
413 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700414
Gavin Shan181eb392012-05-29 15:06:50 -0700415 /* Retrieve the slab flag */
416 if (type == &memblock.memory)
417 in_slab = &memblock_memory_in_slab;
418 else
419 in_slab = &memblock_reserved_in_slab;
420
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700421 /* Try to find some space for it.
422 *
423 * WARNING: We assume that either slab_is_available() and we use it or
Andrew Mortonfd073832012-07-31 16:42:40 -0700424 * we use MEMBLOCK for allocations. That means that this is unsafe to
425 * use when bootmem is currently active (unless bootmem itself is
426 * implemented on top of MEMBLOCK which isn't the case yet)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700427 *
428 * This should however not be an issue for now, as we currently only
Andrew Mortonfd073832012-07-31 16:42:40 -0700429 * call into MEMBLOCK while it's still active, or much later when slab
430 * is active for memory hotplug operations
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700431 */
432 if (use_slab) {
433 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200434 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700435 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700436 /* only exclude range when trying to double reserved.regions */
437 if (type != &memblock.reserved)
438 new_area_start = new_area_size = 0;
439
440 addr = memblock_find_in_range(new_area_start + new_area_size,
441 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700442 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700443 if (!addr && new_area_size)
444 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700445 min(new_area_start, memblock.current_limit),
446 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700447
Sachin Kamat15674862012-09-04 13:55:05 +0530448 new_array = addr ? __va(addr) : NULL;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700449 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200450 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700451 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800452 type->name, type->max, type->max * 2);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700453 return -1;
454 }
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700455
Mike Rapoporta36aab82018-08-17 15:47:17 -0700456 new_end = addr + new_size - 1;
457 memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
458 type->name, type->max * 2, &addr, &new_end);
Yinghai Luea9e4372010-07-28 15:13:22 +1000459
Andrew Mortonfd073832012-07-31 16:42:40 -0700460 /*
461 * Found space, we now need to move the array over before we add the
462 * reserved region since it may be our reserved array itself that is
463 * full.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700464 */
465 memcpy(new_array, type->regions, old_size);
466 memset(new_array + type->max, 0, old_size);
467 old_array = type->regions;
468 type->regions = new_array;
469 type->max <<= 1;
470
Andrew Mortonfd073832012-07-31 16:42:40 -0700471 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700472 if (*in_slab)
473 kfree(old_array);
474 else if (old_array != memblock_memory_init_regions &&
475 old_array != memblock_reserved_init_regions)
Yinghai Lu29f67382012-07-11 14:02:56 -0700476 memblock_free(__pa(old_array), old_alloc_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700477
Andrew Mortonfd073832012-07-31 16:42:40 -0700478 /*
479 * Reserve the new array if that comes from the memblock. Otherwise, we
480 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700481 */
482 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700483 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700484
485 /* Update slab flag */
486 *in_slab = use_slab;
487
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700488 return 0;
489}
490
Tejun Heo784656f92011-07-12 11:15:55 +0200491/**
492 * memblock_merge_regions - merge neighboring compatible regions
493 * @type: memblock type to scan
494 *
495 * Scan @type and merge neighboring compatible regions.
496 */
497static void __init_memblock memblock_merge_regions(struct memblock_type *type)
498{
499 int i = 0;
500
501 /* cnt never goes below 1 */
502 while (i < type->cnt - 1) {
503 struct memblock_region *this = &type->regions[i];
504 struct memblock_region *next = &type->regions[i + 1];
505
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200506 if (this->base + this->size != next->base ||
507 memblock_get_region_node(this) !=
Tang Chen66a20752014-01-21 15:49:20 -0800508 memblock_get_region_node(next) ||
509 this->flags != next->flags) {
Tejun Heo784656f92011-07-12 11:15:55 +0200510 BUG_ON(this->base + this->size > next->base);
511 i++;
512 continue;
513 }
514
515 this->size += next->size;
Lin Fengc0232ae2013-01-11 14:31:44 -0800516 /* move forward from next + 1, index of which is i + 2 */
517 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
Tejun Heo784656f92011-07-12 11:15:55 +0200518 type->cnt--;
519 }
520}
521
522/**
523 * memblock_insert_region - insert new memblock region
Tang Chen209ff862013-04-29 15:08:41 -0700524 * @type: memblock type to insert into
525 * @idx: index for the insertion point
526 * @base: base address of the new region
527 * @size: size of the new region
528 * @nid: node id of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800529 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200530 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300531 * Insert new memblock region [@base, @base + @size) into @type at @idx.
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700532 * @type must already have extra room to accommodate the new region.
Tejun Heo784656f92011-07-12 11:15:55 +0200533 */
534static void __init_memblock memblock_insert_region(struct memblock_type *type,
535 int idx, phys_addr_t base,
Tang Chen66a20752014-01-21 15:49:20 -0800536 phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300537 int nid,
538 enum memblock_flags flags)
Tejun Heo784656f92011-07-12 11:15:55 +0200539{
540 struct memblock_region *rgn = &type->regions[idx];
541
542 BUG_ON(type->cnt >= type->max);
543 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
544 rgn->base = base;
545 rgn->size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800546 rgn->flags = flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200547 memblock_set_region_node(rgn, nid);
Tejun Heo784656f92011-07-12 11:15:55 +0200548 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800549 type->total_size += size;
Tejun Heo784656f92011-07-12 11:15:55 +0200550}
551
552/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100553 * memblock_add_range - add new memblock region
Tejun Heo784656f92011-07-12 11:15:55 +0200554 * @type: memblock type to add new region into
555 * @base: base address of the new region
556 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800557 * @nid: nid of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800558 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200559 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300560 * Add new memblock region [@base, @base + @size) into @type. The new region
Tejun Heo784656f92011-07-12 11:15:55 +0200561 * is allowed to overlap with existing ones - overlaps don't affect already
562 * existing regions. @type is guaranteed to be minimal (all neighbouring
563 * compatible regions are merged) after the addition.
564 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300565 * Return:
Tejun Heo784656f92011-07-12 11:15:55 +0200566 * 0 on success, -errno on failure.
567 */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100568int __init_memblock memblock_add_range(struct memblock_type *type,
Tang Chen66a20752014-01-21 15:49:20 -0800569 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300570 int nid, enum memblock_flags flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000571{
Tejun Heo784656f92011-07-12 11:15:55 +0200572 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800573 phys_addr_t obase = base;
574 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800575 int idx, nr_new;
576 struct memblock_region *rgn;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000577
Tejun Heob3dc6272012-04-20 08:31:34 -0700578 if (!size)
579 return 0;
580
Tejun Heo784656f92011-07-12 11:15:55 +0200581 /* special case for empty array */
582 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800583 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000584 type->regions[0].base = base;
585 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800586 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800587 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800588 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000589 return 0;
590 }
Tejun Heo784656f92011-07-12 11:15:55 +0200591repeat:
592 /*
593 * The following is executed twice. Once with %false @insert and
594 * then with %true. The first counts the number of regions needed
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700595 * to accommodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700596 */
Tejun Heo784656f92011-07-12 11:15:55 +0200597 base = obase;
598 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000599
Gioh Kim66e8b432017-11-15 17:33:42 -0800600 for_each_memblock_type(idx, type, rgn) {
Tejun Heo784656f92011-07-12 11:15:55 +0200601 phys_addr_t rbase = rgn->base;
602 phys_addr_t rend = rbase + rgn->size;
603
604 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000605 break;
Tejun Heo784656f92011-07-12 11:15:55 +0200606 if (rend <= base)
607 continue;
608 /*
609 * @rgn overlaps. If it separates the lower part of new
610 * area, insert that portion.
611 */
612 if (rbase > base) {
Wei Yangc0a29492015-09-04 15:47:38 -0700613#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
614 WARN_ON(nid != memblock_get_region_node(rgn));
615#endif
Wei Yang4fcab5f2015-09-08 14:59:53 -0700616 WARN_ON(flags != rgn->flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200617 nr_new++;
618 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800619 memblock_insert_region(type, idx++, base,
Tang Chen66a20752014-01-21 15:49:20 -0800620 rbase - base, nid,
621 flags);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000622 }
Tejun Heo784656f92011-07-12 11:15:55 +0200623 /* area below @rend is dealt with, forget about it */
624 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000625 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000626
Tejun Heo784656f92011-07-12 11:15:55 +0200627 /* insert the remaining portion */
628 if (base < end) {
629 nr_new++;
630 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800631 memblock_insert_region(type, idx, base, end - base,
Tang Chen66a20752014-01-21 15:49:20 -0800632 nid, flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200633 }
634
nimisoloef3cc4d2016-07-26 15:24:56 -0700635 if (!nr_new)
636 return 0;
637
Tejun Heo784656f92011-07-12 11:15:55 +0200638 /*
639 * If this was the first round, resize array and repeat for actual
640 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700641 */
Tejun Heo784656f92011-07-12 11:15:55 +0200642 if (!insert) {
643 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700644 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f92011-07-12 11:15:55 +0200645 return -ENOMEM;
646 insert = true;
647 goto repeat;
648 } else {
649 memblock_merge_regions(type);
650 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700651 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000652}
653
Mike Rapoport48a833c2018-06-30 17:55:03 +0300654/**
655 * memblock_add_node - add new memblock region within a NUMA node
656 * @base: base address of the new region
657 * @size: size of the new region
658 * @nid: nid of the new region
659 *
660 * Add new memblock region [@base, @base + @size) to the "memory"
661 * type. See memblock_add_range() description for mode details
662 *
663 * Return:
664 * 0 on success, -errno on failure.
665 */
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800666int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
667 int nid)
668{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100669 return memblock_add_range(&memblock.memory, base, size, nid, 0);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800670}
671
Mike Rapoport48a833c2018-06-30 17:55:03 +0300672/**
673 * memblock_add - add new memblock region
674 * @base: base address of the new region
675 * @size: size of the new region
676 *
677 * Add new memblock region [@base, @base + @size) to the "memory"
678 * type. See memblock_add_range() description for mode details
679 *
680 * Return:
681 * 0 on success, -errno on failure.
682 */
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700683int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700684{
Miles Chen5d63f812017-02-22 15:46:42 -0800685 phys_addr_t end = base + size - 1;
686
687 memblock_dbg("memblock_add: [%pa-%pa] %pF\n",
688 &base, &end, (void *)_RET_IP_);
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700689
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700690 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000691}
692
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800693/**
694 * memblock_isolate_range - isolate given range into disjoint memblocks
695 * @type: memblock type to isolate range for
696 * @base: base of range to isolate
697 * @size: size of range to isolate
698 * @start_rgn: out parameter for the start of isolated region
699 * @end_rgn: out parameter for the end of isolated region
700 *
701 * Walk @type and ensure that regions don't cross the boundaries defined by
Mike Rapoport47cec442018-06-30 17:55:02 +0300702 * [@base, @base + @size). Crossing regions are split at the boundaries,
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800703 * which may create at most two more regions. The index of the first
704 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
705 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300706 * Return:
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800707 * 0 on success, -errno on failure.
708 */
709static int __init_memblock memblock_isolate_range(struct memblock_type *type,
710 phys_addr_t base, phys_addr_t size,
711 int *start_rgn, int *end_rgn)
712{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800713 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800714 int idx;
715 struct memblock_region *rgn;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800716
717 *start_rgn = *end_rgn = 0;
718
Tejun Heob3dc6272012-04-20 08:31:34 -0700719 if (!size)
720 return 0;
721
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800722 /* we'll create at most two more regions */
723 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700724 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800725 return -ENOMEM;
726
Gioh Kim66e8b432017-11-15 17:33:42 -0800727 for_each_memblock_type(idx, type, rgn) {
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800728 phys_addr_t rbase = rgn->base;
729 phys_addr_t rend = rbase + rgn->size;
730
731 if (rbase >= end)
732 break;
733 if (rend <= base)
734 continue;
735
736 if (rbase < base) {
737 /*
738 * @rgn intersects from below. Split and continue
739 * to process the next region - the new top half.
740 */
741 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800742 rgn->size -= base - rbase;
743 type->total_size -= base - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800744 memblock_insert_region(type, idx, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800745 memblock_get_region_node(rgn),
746 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800747 } else if (rend > end) {
748 /*
749 * @rgn intersects from above. Split and redo the
750 * current region - the new bottom half.
751 */
752 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800753 rgn->size -= end - rbase;
754 type->total_size -= end - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800755 memblock_insert_region(type, idx--, rbase, end - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800756 memblock_get_region_node(rgn),
757 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800758 } else {
759 /* @rgn is fully contained, record it */
760 if (!*end_rgn)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800761 *start_rgn = idx;
762 *end_rgn = idx + 1;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800763 }
764 }
765
766 return 0;
767}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800768
Alexander Kuleshov35bd16a2015-11-05 18:47:00 -0800769static int __init_memblock memblock_remove_range(struct memblock_type *type,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100770 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000771{
Tejun Heo71936182011-12-08 10:22:07 -0800772 int start_rgn, end_rgn;
773 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000774
Tejun Heo71936182011-12-08 10:22:07 -0800775 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
776 if (ret)
777 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000778
Tejun Heo71936182011-12-08 10:22:07 -0800779 for (i = end_rgn - 1; i >= start_rgn; i--)
780 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700781 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000782}
783
Tejun Heo581adcb2011-12-08 10:22:06 -0800784int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000785{
Minchan Kim25cf23d2018-06-07 17:07:35 -0700786 phys_addr_t end = base + size - 1;
787
788 memblock_dbg("memblock_remove: [%pa-%pa] %pS\n",
789 &base, &end, (void *)_RET_IP_);
790
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100791 return memblock_remove_range(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000792}
793
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100794
Tejun Heo581adcb2011-12-08 10:22:06 -0800795int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000796{
Miles Chen5d63f812017-02-22 15:46:42 -0800797 phys_addr_t end = base + size - 1;
798
799 memblock_dbg(" memblock_free: [%pa-%pa] %pF\n",
800 &base, &end, (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200801
Catalin Marinas9099dae2016-10-11 13:55:11 -0700802 kmemleak_free_part_phys(base, size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100803 return memblock_remove_range(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000804}
805
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700806int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000807{
Miles Chen5d63f812017-02-22 15:46:42 -0800808 phys_addr_t end = base + size - 1;
809
810 memblock_dbg("memblock_reserve: [%pa-%pa] %pF\n",
811 &base, &end, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000812
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700813 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000814}
815
Tejun Heo35fd0802011-07-12 11:15:59 +0200816/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300817 * memblock_setclr_flag - set or clear flag for a memory region
818 * @base: base address of the region
819 * @size: size of the region
820 * @set: set or clear the flag
821 * @flag: the flag to udpate
Tang Chen66b16ed2014-01-21 15:49:23 -0800822 *
Tony Luck4308ce12014-12-12 16:54:59 -0800823 * This function isolates region [@base, @base + @size), and sets/clears flag
Tang Chen66b16ed2014-01-21 15:49:23 -0800824 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300825 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800826 */
Tony Luck4308ce12014-12-12 16:54:59 -0800827static int __init_memblock memblock_setclr_flag(phys_addr_t base,
828 phys_addr_t size, int set, int flag)
Tang Chen66b16ed2014-01-21 15:49:23 -0800829{
830 struct memblock_type *type = &memblock.memory;
831 int i, ret, start_rgn, end_rgn;
832
833 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
834 if (ret)
835 return ret;
836
837 for (i = start_rgn; i < end_rgn; i++)
Tony Luck4308ce12014-12-12 16:54:59 -0800838 if (set)
839 memblock_set_region_flags(&type->regions[i], flag);
840 else
841 memblock_clear_region_flags(&type->regions[i], flag);
Tang Chen66b16ed2014-01-21 15:49:23 -0800842
843 memblock_merge_regions(type);
844 return 0;
845}
846
847/**
Tony Luck4308ce12014-12-12 16:54:59 -0800848 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
849 * @base: the base phys addr of the region
850 * @size: the size of the region
851 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300852 * Return: 0 on success, -errno on failure.
Tony Luck4308ce12014-12-12 16:54:59 -0800853 */
854int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
855{
856 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
857}
858
859/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800860 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
861 * @base: the base phys addr of the region
862 * @size: the size of the region
863 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300864 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800865 */
866int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
867{
Tony Luck4308ce12014-12-12 16:54:59 -0800868 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
Tang Chen66b16ed2014-01-21 15:49:23 -0800869}
870
871/**
Tony Lucka3f5baf2015-06-24 16:58:12 -0700872 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
873 * @base: the base phys addr of the region
874 * @size: the size of the region
875 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300876 * Return: 0 on success, -errno on failure.
Tony Lucka3f5baf2015-06-24 16:58:12 -0700877 */
878int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
879{
880 system_has_some_mirror = true;
881
882 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
883}
884
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100885/**
886 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
887 * @base: the base phys addr of the region
888 * @size: the size of the region
889 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300890 * Return: 0 on success, -errno on failure.
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100891 */
892int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
893{
894 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
895}
Tony Lucka3f5baf2015-06-24 16:58:12 -0700896
897/**
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900898 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
899 * @base: the base phys addr of the region
900 * @size: the size of the region
901 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300902 * Return: 0 on success, -errno on failure.
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900903 */
904int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
905{
906 return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
907}
908
909/**
Robin Holt8e7a7f82015-06-30 14:56:41 -0700910 * __next_reserved_mem_region - next function for for_each_reserved_region()
911 * @idx: pointer to u64 loop variable
912 * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
913 * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
914 *
915 * Iterate over all reserved memory regions.
916 */
917void __init_memblock __next_reserved_mem_region(u64 *idx,
918 phys_addr_t *out_start,
919 phys_addr_t *out_end)
920{
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700921 struct memblock_type *type = &memblock.reserved;
Robin Holt8e7a7f82015-06-30 14:56:41 -0700922
Richard Leitnercd33a762016-05-20 16:58:33 -0700923 if (*idx < type->cnt) {
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700924 struct memblock_region *r = &type->regions[*idx];
Robin Holt8e7a7f82015-06-30 14:56:41 -0700925 phys_addr_t base = r->base;
926 phys_addr_t size = r->size;
927
928 if (out_start)
929 *out_start = base;
930 if (out_end)
931 *out_end = base + size - 1;
932
933 *idx += 1;
934 return;
935 }
936
937 /* signal end of iteration */
938 *idx = ULLONG_MAX;
939}
940
941/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100942 * __next__mem_range - next function for for_each_free_mem_range() etc.
Tejun Heo35fd0802011-07-12 11:15:59 +0200943 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800944 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700945 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100946 * @type_a: pointer to memblock_type from where the range is taken
947 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -0700948 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
949 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
950 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +0200951 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100952 * Find the first area from *@idx which matches @nid, fill the out
Tejun Heo35fd0802011-07-12 11:15:59 +0200953 * parameters, and update *@idx for the next iteration. The lower 32bit of
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100954 * *@idx contains index into type_a and the upper 32bit indexes the
955 * areas before each region in type_b. For example, if type_b regions
Tejun Heo35fd0802011-07-12 11:15:59 +0200956 * look like the following,
957 *
958 * 0:[0-16), 1:[32-48), 2:[128-130)
959 *
960 * The upper 32bit indexes the following regions.
961 *
962 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
963 *
964 * As both region arrays are sorted, the function advances the two indices
965 * in lockstep and returns each intersection.
966 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300967void __init_memblock __next_mem_range(u64 *idx, int nid,
968 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100969 struct memblock_type *type_a,
970 struct memblock_type *type_b,
971 phys_addr_t *out_start,
972 phys_addr_t *out_end, int *out_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200973{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100974 int idx_a = *idx & 0xffffffff;
975 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -0800976
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100977 if (WARN_ONCE(nid == MAX_NUMNODES,
978 "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
Grygorii Strashko560dca272014-01-21 15:50:55 -0800979 nid = NUMA_NO_NODE;
Tejun Heo35fd0802011-07-12 11:15:59 +0200980
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100981 for (; idx_a < type_a->cnt; idx_a++) {
982 struct memblock_region *m = &type_a->regions[idx_a];
983
Tejun Heo35fd0802011-07-12 11:15:59 +0200984 phys_addr_t m_start = m->base;
985 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100986 int m_nid = memblock_get_region_node(m);
Tejun Heo35fd0802011-07-12 11:15:59 +0200987
988 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100989 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200990 continue;
991
Xishi Qiu0a313a92014-09-09 14:50:46 -0700992 /* skip hotpluggable memory regions if needed */
993 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
994 continue;
995
Tony Lucka3f5baf2015-06-24 16:58:12 -0700996 /* if we want mirror memory skip non-mirror memory regions */
997 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
998 continue;
999
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001000 /* skip nomap memory unless we were asked for it explicitly */
1001 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1002 continue;
1003
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001004 if (!type_b) {
1005 if (out_start)
1006 *out_start = m_start;
1007 if (out_end)
1008 *out_end = m_end;
1009 if (out_nid)
1010 *out_nid = m_nid;
1011 idx_a++;
1012 *idx = (u32)idx_a | (u64)idx_b << 32;
1013 return;
1014 }
Tejun Heo35fd0802011-07-12 11:15:59 +02001015
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001016 /* scan areas before each reservation */
1017 for (; idx_b < type_b->cnt + 1; idx_b++) {
1018 struct memblock_region *r;
1019 phys_addr_t r_start;
1020 phys_addr_t r_end;
1021
1022 r = &type_b->regions[idx_b];
1023 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1024 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001025 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001026
1027 /*
1028 * if idx_b advanced past idx_a,
1029 * break out to advance idx_a
1030 */
Tejun Heo35fd0802011-07-12 11:15:59 +02001031 if (r_start >= m_end)
1032 break;
1033 /* if the two regions intersect, we're done */
1034 if (m_start < r_end) {
1035 if (out_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001036 *out_start =
1037 max(m_start, r_start);
Tejun Heo35fd0802011-07-12 11:15:59 +02001038 if (out_end)
1039 *out_end = min(m_end, r_end);
1040 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001041 *out_nid = m_nid;
Tejun Heo35fd0802011-07-12 11:15:59 +02001042 /*
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001043 * The region which ends first is
1044 * advanced for the next iteration.
Tejun Heo35fd0802011-07-12 11:15:59 +02001045 */
1046 if (m_end <= r_end)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001047 idx_a++;
Tejun Heo35fd0802011-07-12 11:15:59 +02001048 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001049 idx_b++;
1050 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo35fd0802011-07-12 11:15:59 +02001051 return;
1052 }
1053 }
1054 }
1055
1056 /* signal end of iteration */
1057 *idx = ULLONG_MAX;
1058}
1059
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001060/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001061 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1062 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001063 * @idx: pointer to u64 loop variable
Alexander Kuleshovad5ea8c2015-09-08 15:04:22 -07001064 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -07001065 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001066 * @type_a: pointer to memblock_type from where the range is taken
1067 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -07001068 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1069 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1070 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001071 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001072 * Finds the next range from type_a which is not marked as unsuitable
1073 * in type_b.
1074 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001075 * Reverse of __next_mem_range().
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001076 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001077void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1078 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001079 struct memblock_type *type_a,
1080 struct memblock_type *type_b,
1081 phys_addr_t *out_start,
1082 phys_addr_t *out_end, int *out_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001083{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001084 int idx_a = *idx & 0xffffffff;
1085 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -08001086
Grygorii Strashko560dca272014-01-21 15:50:55 -08001087 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1088 nid = NUMA_NO_NODE;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001089
1090 if (*idx == (u64)ULLONG_MAX) {
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001091 idx_a = type_a->cnt - 1;
zijun_hue47608a2016-08-04 15:32:00 -07001092 if (type_b != NULL)
1093 idx_b = type_b->cnt;
1094 else
1095 idx_b = 0;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001096 }
1097
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001098 for (; idx_a >= 0; idx_a--) {
1099 struct memblock_region *m = &type_a->regions[idx_a];
1100
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001101 phys_addr_t m_start = m->base;
1102 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001103 int m_nid = memblock_get_region_node(m);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001104
1105 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001106 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001107 continue;
1108
Tang Chen55ac5902014-01-21 15:49:35 -08001109 /* skip hotpluggable memory regions if needed */
1110 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
1111 continue;
1112
Tony Lucka3f5baf2015-06-24 16:58:12 -07001113 /* if we want mirror memory skip non-mirror memory regions */
1114 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1115 continue;
1116
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001117 /* skip nomap memory unless we were asked for it explicitly */
1118 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1119 continue;
1120
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001121 if (!type_b) {
1122 if (out_start)
1123 *out_start = m_start;
1124 if (out_end)
1125 *out_end = m_end;
1126 if (out_nid)
1127 *out_nid = m_nid;
zijun_hufb399b42016-07-28 15:48:56 -07001128 idx_a--;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001129 *idx = (u32)idx_a | (u64)idx_b << 32;
1130 return;
1131 }
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001132
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001133 /* scan areas before each reservation */
1134 for (; idx_b >= 0; idx_b--) {
1135 struct memblock_region *r;
1136 phys_addr_t r_start;
1137 phys_addr_t r_end;
1138
1139 r = &type_b->regions[idx_b];
1140 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1141 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001142 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001143 /*
1144 * if idx_b advanced past idx_a,
1145 * break out to advance idx_a
1146 */
1147
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001148 if (r_end <= m_start)
1149 break;
1150 /* if the two regions intersect, we're done */
1151 if (m_end > r_start) {
1152 if (out_start)
1153 *out_start = max(m_start, r_start);
1154 if (out_end)
1155 *out_end = min(m_end, r_end);
1156 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001157 *out_nid = m_nid;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001158 if (m_start >= r_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001159 idx_a--;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001160 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001161 idx_b--;
1162 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001163 return;
1164 }
1165 }
1166 }
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001167 /* signal end of iteration */
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001168 *idx = ULLONG_MAX;
1169}
1170
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001171#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1172/*
1173 * Common iterator interface used to define for_each_mem_range().
1174 */
1175void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1176 unsigned long *out_start_pfn,
1177 unsigned long *out_end_pfn, int *out_nid)
1178{
1179 struct memblock_type *type = &memblock.memory;
1180 struct memblock_region *r;
1181
1182 while (++*idx < type->cnt) {
1183 r = &type->regions[*idx];
1184
1185 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1186 continue;
1187 if (nid == MAX_NUMNODES || nid == r->nid)
1188 break;
1189 }
1190 if (*idx >= type->cnt) {
1191 *idx = -1;
1192 return;
1193 }
1194
1195 if (out_start_pfn)
1196 *out_start_pfn = PFN_UP(r->base);
1197 if (out_end_pfn)
1198 *out_end_pfn = PFN_DOWN(r->base + r->size);
1199 if (out_nid)
1200 *out_nid = r->nid;
1201}
1202
1203/**
1204 * memblock_set_node - set node ID on memblock regions
1205 * @base: base of area to set node ID for
1206 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -08001207 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001208 * @nid: node ID to set
1209 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001210 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001211 * Regions which cross the area boundaries are split as necessary.
1212 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001213 * Return:
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001214 * 0 on success, -errno on failure.
1215 */
1216int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -08001217 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001218{
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001219 int start_rgn, end_rgn;
1220 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001221
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001222 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1223 if (ret)
1224 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001225
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001226 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -07001227 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001228
1229 memblock_merge_regions(type);
1230 return 0;
1231}
1232#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1233
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001234static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1235 phys_addr_t align, phys_addr_t start,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001236 phys_addr_t end, int nid,
1237 enum memblock_flags flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001238{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001239 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001240
Grygorii Strashko79f40fa2014-01-21 15:50:12 -08001241 if (!align)
1242 align = SMP_CACHE_BYTES;
Vineet Gupta94f3d3a2013-04-29 15:06:15 -07001243
Tony Luckfc6daaf2015-06-24 16:58:09 -07001244 found = memblock_find_in_range_node(size, align, start, end, nid,
1245 flags);
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001246 if (found && !memblock_reserve(found, size)) {
1247 /*
1248 * The min_count is set to 0 so that memblock allocations are
1249 * never reported as leaks.
1250 */
Catalin Marinas9099dae2016-10-11 13:55:11 -07001251 kmemleak_alloc_phys(found, size, 0, 0);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001252 return found;
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001253 }
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001254 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001255}
1256
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001257phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001258 phys_addr_t start, phys_addr_t end,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001259 enum memblock_flags flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001260{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001261 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1262 flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001263}
1264
Nicholas Pigginb575454f2018-02-14 01:08:15 +10001265phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001266 phys_addr_t align, phys_addr_t max_addr,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001267 int nid, enum memblock_flags flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001268{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001269 return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001270}
1271
Mike Rapoport9a8dd702018-10-30 15:07:59 -07001272phys_addr_t __init memblock_phys_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001273{
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001274 enum memblock_flags flags = choose_memblock_flags();
Tony Lucka3f5baf2015-06-24 16:58:12 -07001275 phys_addr_t ret;
1276
1277again:
1278 ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE,
1279 nid, flags);
1280
1281 if (!ret && (flags & MEMBLOCK_MIRROR)) {
1282 flags &= ~MEMBLOCK_MIRROR;
1283 goto again;
1284 }
1285 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001286}
1287
1288phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1289{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001290 return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE,
1291 MEMBLOCK_NONE);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001292}
1293
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001294phys_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 +10001295{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001296 phys_addr_t alloc;
1297
1298 alloc = __memblock_alloc_base(size, align, max_addr);
1299
1300 if (alloc == 0)
Miles Chen5d63f812017-02-22 15:46:42 -08001301 panic("ERROR: Failed to allocate %pa bytes below %pa.\n",
1302 &size, &max_addr);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001303
1304 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001305}
1306
Mike Rapoport9a8dd702018-10-30 15:07:59 -07001307phys_addr_t __init memblock_phys_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001308{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001309 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001310}
1311
Mike Rapoport9a8dd702018-10-30 15:07:59 -07001312phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001313{
Mike Rapoport9a8dd702018-10-30 15:07:59 -07001314 phys_addr_t res = memblock_phys_alloc_nid(size, align, nid);
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001315
1316 if (res)
1317 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +02001318 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001319}
1320
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001321/**
Mike Rapoporteb31d552018-10-30 15:08:04 -07001322 * memblock_alloc_internal - allocate boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001323 * @size: size of memory block to be allocated in bytes
1324 * @align: alignment of the region and block's size
1325 * @min_addr: the lower bound of the memory region to allocate (phys address)
1326 * @max_addr: the upper bound of the memory region to allocate (phys address)
1327 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1328 *
1329 * The @min_addr limit is dropped if it can not be satisfied and the allocation
1330 * will fall back to memory below @min_addr. Also, allocation may fall back
1331 * to any node in the system if the specified node can not
1332 * hold the requested memory.
1333 *
1334 * The allocation is performed from memory region limited by
1335 * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE.
1336 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001337 * The memory block is aligned on %SMP_CACHE_BYTES if @align == 0.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001338 *
1339 * The phys address of allocated boot memory block is converted to virtual and
1340 * allocated memory is reset to 0.
1341 *
1342 * In addition, function sets the min_count to 0 using kmemleak_alloc for
1343 * allocated boot memory block, so that it is never reported as leaks.
1344 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001345 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001346 * Virtual address of allocated memory block on success, NULL on failure.
1347 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001348static void * __init memblock_alloc_internal(
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001349 phys_addr_t size, phys_addr_t align,
1350 phys_addr_t min_addr, phys_addr_t max_addr,
1351 int nid)
1352{
1353 phys_addr_t alloc;
1354 void *ptr;
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001355 enum memblock_flags flags = choose_memblock_flags();
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001356
Grygorii Strashko560dca272014-01-21 15:50:55 -08001357 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1358 nid = NUMA_NO_NODE;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001359
1360 /*
1361 * Detect any accidental use of these APIs after slab is ready, as at
1362 * this moment memblock may be deinitialized already and its
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -07001363 * internal data may be destroyed (after execution of memblock_free_all)
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001364 */
1365 if (WARN_ON_ONCE(slab_is_available()))
1366 return kzalloc_node(size, GFP_NOWAIT, nid);
1367
1368 if (!align)
1369 align = SMP_CACHE_BYTES;
1370
Yinghai Luf544e142014-01-29 14:05:52 -08001371 if (max_addr > memblock.current_limit)
1372 max_addr = memblock.current_limit;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001373again:
1374 alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001375 nid, flags);
Wei Yang7d41c032017-02-22 15:45:07 -08001376 if (alloc && !memblock_reserve(alloc, size))
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001377 goto done;
1378
1379 if (nid != NUMA_NO_NODE) {
1380 alloc = memblock_find_in_range_node(size, align, min_addr,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001381 max_addr, NUMA_NO_NODE,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001382 flags);
Wei Yang7d41c032017-02-22 15:45:07 -08001383 if (alloc && !memblock_reserve(alloc, size))
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001384 goto done;
1385 }
1386
1387 if (min_addr) {
1388 min_addr = 0;
1389 goto again;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001390 }
1391
Tony Lucka3f5baf2015-06-24 16:58:12 -07001392 if (flags & MEMBLOCK_MIRROR) {
1393 flags &= ~MEMBLOCK_MIRROR;
1394 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1395 &size);
1396 goto again;
1397 }
1398
1399 return NULL;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001400done:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001401 ptr = phys_to_virt(alloc);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001402
1403 /*
1404 * The min_count is set to 0 so that bootmem allocated blocks
1405 * are never reported as leaks. This is because many of these blocks
1406 * are only referred via the physical address which is not
1407 * looked up by kmemleak.
1408 */
1409 kmemleak_alloc(ptr, size, 0, 0);
1410
1411 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001412}
1413
1414/**
Mike Rapoporteb31d552018-10-30 15:08:04 -07001415 * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001416 * memory and without panicking
1417 * @size: size of memory block to be allocated in bytes
1418 * @align: alignment of the region and block's size
1419 * @min_addr: the lower bound of the memory region from where the allocation
1420 * is preferred (phys address)
1421 * @max_addr: the upper bound of the memory region from where the allocation
1422 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1423 * allocate only from memory limited by memblock.current_limit value
1424 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1425 *
1426 * Public function, provides additional debug information (including caller
1427 * info), if enabled. Does not zero allocated memory, does not panic if request
1428 * cannot be satisfied.
1429 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001430 * Return:
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001431 * Virtual address of allocated memory block on success, NULL on failure.
1432 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001433void * __init memblock_alloc_try_nid_raw(
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001434 phys_addr_t size, phys_addr_t align,
1435 phys_addr_t min_addr, phys_addr_t max_addr,
1436 int nid)
1437{
1438 void *ptr;
1439
Mike Rapoporta36aab82018-08-17 15:47:17 -07001440 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1441 __func__, (u64)size, (u64)align, nid, &min_addr,
1442 &max_addr, (void *)_RET_IP_);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001443
Mike Rapoporteb31d552018-10-30 15:08:04 -07001444 ptr = memblock_alloc_internal(size, align,
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001445 min_addr, max_addr, nid);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001446 if (ptr && size > 0)
Alexander Duyckf682a972018-10-26 15:07:45 -07001447 page_init_poison(ptr, size);
1448
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001449 return ptr;
1450}
1451
1452/**
Mike Rapoporteb31d552018-10-30 15:08:04 -07001453 * memblock_alloc_try_nid_nopanic - allocate boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001454 * @size: size of memory block to be allocated in bytes
1455 * @align: alignment of the region and block's size
1456 * @min_addr: the lower bound of the memory region from where the allocation
1457 * is preferred (phys address)
1458 * @max_addr: the upper bound of the memory region from where the allocation
1459 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1460 * allocate only from memory limited by memblock.current_limit value
1461 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1462 *
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001463 * Public function, provides additional debug information (including caller
1464 * info), if enabled. This function zeroes the allocated memory.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001465 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001466 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001467 * Virtual address of allocated memory block on success, NULL on failure.
1468 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001469void * __init memblock_alloc_try_nid_nopanic(
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001470 phys_addr_t size, phys_addr_t align,
1471 phys_addr_t min_addr, phys_addr_t max_addr,
1472 int nid)
1473{
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001474 void *ptr;
1475
Mike Rapoporta36aab82018-08-17 15:47:17 -07001476 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1477 __func__, (u64)size, (u64)align, nid, &min_addr,
1478 &max_addr, (void *)_RET_IP_);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001479
Mike Rapoporteb31d552018-10-30 15:08:04 -07001480 ptr = memblock_alloc_internal(size, align,
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001481 min_addr, max_addr, nid);
1482 if (ptr)
1483 memset(ptr, 0, size);
1484 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001485}
1486
1487/**
Mike Rapoporteb31d552018-10-30 15:08:04 -07001488 * memblock_alloc_try_nid - allocate boot memory block with panicking
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001489 * @size: size of memory block to be allocated in bytes
1490 * @align: alignment of the region and block's size
1491 * @min_addr: the lower bound of the memory region from where the allocation
1492 * is preferred (phys address)
1493 * @max_addr: the upper bound of the memory region from where the allocation
1494 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1495 * allocate only from memory limited by memblock.current_limit value
1496 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1497 *
Mike Rapoporteb31d552018-10-30 15:08:04 -07001498 * Public panicking version of memblock_alloc_try_nid_nopanic()
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001499 * which provides debug information (including caller info), if enabled,
1500 * and panics if the request can not be satisfied.
1501 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001502 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001503 * Virtual address of allocated memory block on success, NULL on failure.
1504 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001505void * __init memblock_alloc_try_nid(
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001506 phys_addr_t size, phys_addr_t align,
1507 phys_addr_t min_addr, phys_addr_t max_addr,
1508 int nid)
1509{
1510 void *ptr;
1511
Mike Rapoporta36aab82018-08-17 15:47:17 -07001512 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1513 __func__, (u64)size, (u64)align, nid, &min_addr,
1514 &max_addr, (void *)_RET_IP_);
Mike Rapoporteb31d552018-10-30 15:08:04 -07001515 ptr = memblock_alloc_internal(size, align,
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001516 min_addr, max_addr, nid);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001517 if (ptr) {
1518 memset(ptr, 0, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001519 return ptr;
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001520 }
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001521
Mike Rapoporta36aab82018-08-17 15:47:17 -07001522 panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa\n",
1523 __func__, (u64)size, (u64)align, nid, &min_addr, &max_addr);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001524 return NULL;
1525}
1526
1527/**
1528 * __memblock_free_early - free boot memory block
1529 * @base: phys starting address of the boot memory block
1530 * @size: size of the boot memory block in bytes
1531 *
Mike Rapoporteb31d552018-10-30 15:08:04 -07001532 * Free boot memory block previously allocated by memblock_alloc_xx() API.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001533 * The freeing memory will not be released to the buddy allocator.
1534 */
1535void __init __memblock_free_early(phys_addr_t base, phys_addr_t size)
1536{
Mike Rapoporta36aab82018-08-17 15:47:17 -07001537 phys_addr_t end = base + size - 1;
1538
1539 memblock_dbg("%s: [%pa-%pa] %pF\n",
1540 __func__, &base, &end, (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001541 kmemleak_free_part_phys(base, size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001542 memblock_remove_range(&memblock.reserved, base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001543}
1544
Mike Rapoport48a833c2018-06-30 17:55:03 +03001545/**
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001546 * __memblock_free_late - free bootmem block pages directly to buddy allocator
Mike Rapoport48a833c2018-06-30 17:55:03 +03001547 * @base: phys starting address of the boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001548 * @size: size of the boot memory block in bytes
1549 *
1550 * This is only useful when the bootmem allocator has already been torn
1551 * down, but we are still initializing the system. Pages are released directly
1552 * to the buddy allocator, no bootmem metadata is updated because it is gone.
1553 */
1554void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1555{
Mike Rapoporta36aab82018-08-17 15:47:17 -07001556 phys_addr_t cursor, end;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001557
Mike Rapoporta36aab82018-08-17 15:47:17 -07001558 end = base + size - 1;
1559 memblock_dbg("%s: [%pa-%pa] %pF\n",
1560 __func__, &base, &end, (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001561 kmemleak_free_part_phys(base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001562 cursor = PFN_UP(base);
1563 end = PFN_DOWN(base + size);
1564
1565 for (; cursor < end; cursor++) {
Mel Gormand70ddd72015-06-30 14:56:52 -07001566 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001567 totalram_pages++;
1568 }
1569}
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001570
1571/*
1572 * Remaining API functions
1573 */
1574
David Gibson1f1ffb8a2016-02-05 15:36:19 -08001575phys_addr_t __init_memblock memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001576{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001577 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001578}
1579
Srikar Dronamraju8907de52016-10-07 16:59:18 -07001580phys_addr_t __init_memblock memblock_reserved_size(void)
1581{
1582 return memblock.reserved.total_size;
1583}
1584
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001585phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1586{
1587 unsigned long pages = 0;
1588 struct memblock_region *r;
1589 unsigned long start_pfn, end_pfn;
1590
1591 for_each_memblock(memory, r) {
1592 start_pfn = memblock_region_memory_base_pfn(r);
1593 end_pfn = memblock_region_memory_end_pfn(r);
1594 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1595 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1596 pages += end_pfn - start_pfn;
1597 }
1598
Fabian Frederick16763232014-04-07 15:37:53 -07001599 return PFN_PHYS(pages);
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001600}
1601
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001602/* lowest address */
1603phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1604{
1605 return memblock.memory.regions[0].base;
1606}
1607
Yinghai Lu10d06432010-07-28 15:43:02 +10001608phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001609{
1610 int idx = memblock.memory.cnt - 1;
1611
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001612 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001613}
1614
Dennis Chena571d4e2016-07-28 15:48:26 -07001615static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001616{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001617 phys_addr_t max_addr = PHYS_ADDR_MAX;
Emil Medve136199f2014-04-07 15:37:52 -07001618 struct memblock_region *r;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001619
Dennis Chena571d4e2016-07-28 15:48:26 -07001620 /*
1621 * translate the memory @limit size into the max address within one of
1622 * the memory memblock regions, if the @limit exceeds the total size
Stefan Agner1c4bc432018-06-07 17:06:15 -07001623 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
Dennis Chena571d4e2016-07-28 15:48:26 -07001624 */
Emil Medve136199f2014-04-07 15:37:52 -07001625 for_each_memblock(memory, r) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001626 if (limit <= r->size) {
1627 max_addr = r->base + limit;
1628 break;
1629 }
1630 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001631 }
1632
Dennis Chena571d4e2016-07-28 15:48:26 -07001633 return max_addr;
1634}
1635
1636void __init memblock_enforce_memory_limit(phys_addr_t limit)
1637{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001638 phys_addr_t max_addr = PHYS_ADDR_MAX;
Dennis Chena571d4e2016-07-28 15:48:26 -07001639
1640 if (!limit)
1641 return;
1642
1643 max_addr = __find_max_addr(limit);
1644
1645 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001646 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001647 return;
1648
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001649 /* truncate both memory and reserved regions */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001650 memblock_remove_range(&memblock.memory, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001651 PHYS_ADDR_MAX);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001652 memblock_remove_range(&memblock.reserved, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001653 PHYS_ADDR_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001654}
1655
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001656void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1657{
1658 int start_rgn, end_rgn;
1659 int i, ret;
1660
1661 if (!size)
1662 return;
1663
1664 ret = memblock_isolate_range(&memblock.memory, base, size,
1665 &start_rgn, &end_rgn);
1666 if (ret)
1667 return;
1668
1669 /* remove all the MAP regions */
1670 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1671 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1672 memblock_remove_region(&memblock.memory, i);
1673
1674 for (i = start_rgn - 1; i >= 0; i--)
1675 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1676 memblock_remove_region(&memblock.memory, i);
1677
1678 /* truncate the reserved regions */
1679 memblock_remove_range(&memblock.reserved, 0, base);
1680 memblock_remove_range(&memblock.reserved,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001681 base + size, PHYS_ADDR_MAX);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001682}
1683
Dennis Chena571d4e2016-07-28 15:48:26 -07001684void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1685{
Dennis Chena571d4e2016-07-28 15:48:26 -07001686 phys_addr_t max_addr;
Dennis Chena571d4e2016-07-28 15:48:26 -07001687
1688 if (!limit)
1689 return;
1690
1691 max_addr = __find_max_addr(limit);
1692
1693 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001694 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001695 return;
1696
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001697 memblock_cap_memory_range(0, max_addr);
Dennis Chena571d4e2016-07-28 15:48:26 -07001698}
1699
Yinghai Lucd794812010-10-11 12:34:09 -07001700static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001701{
1702 unsigned int left = 0, right = type->cnt;
1703
1704 do {
1705 unsigned int mid = (right + left) / 2;
1706
1707 if (addr < type->regions[mid].base)
1708 right = mid;
1709 else if (addr >= (type->regions[mid].base +
1710 type->regions[mid].size))
1711 left = mid + 1;
1712 else
1713 return mid;
1714 } while (left < right);
1715 return -1;
1716}
1717
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001718bool __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001719{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001720 return memblock_search(&memblock.reserved, addr) != -1;
1721}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001722
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001723bool __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001724{
1725 return memblock_search(&memblock.memory, addr) != -1;
1726}
1727
Yaowei Bai937f0c22018-02-06 15:41:18 -08001728bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001729{
1730 int i = memblock_search(&memblock.memory, addr);
1731
1732 if (i == -1)
1733 return false;
1734 return !memblock_is_nomap(&memblock.memory.regions[i]);
1735}
1736
Yinghai Lue76b63f2013-09-11 14:22:17 -07001737#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1738int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1739 unsigned long *start_pfn, unsigned long *end_pfn)
1740{
1741 struct memblock_type *type = &memblock.memory;
Fabian Frederick16763232014-04-07 15:37:53 -07001742 int mid = memblock_search(type, PFN_PHYS(pfn));
Yinghai Lue76b63f2013-09-11 14:22:17 -07001743
1744 if (mid == -1)
1745 return -1;
1746
Fabian Frederickf7e2f7e2014-06-04 16:07:51 -07001747 *start_pfn = PFN_DOWN(type->regions[mid].base);
1748 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001749
1750 return type->regions[mid].nid;
1751}
1752#endif
1753
Stephen Boydeab30942012-05-24 00:45:21 -07001754/**
1755 * memblock_is_region_memory - check if a region is a subset of memory
1756 * @base: base of region to check
1757 * @size: size of region to check
1758 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001759 * Check if the region [@base, @base + @size) is a subset of a memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001760 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001761 * Return:
Stephen Boydeab30942012-05-24 00:45:21 -07001762 * 0 if false, non-zero if true
1763 */
Yaowei Bai937f0c22018-02-06 15:41:18 -08001764bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001765{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001766 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001767 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001768
1769 if (idx == -1)
Yaowei Bai937f0c22018-02-06 15:41:18 -08001770 return false;
Wei Yangef415ef2017-02-22 15:45:04 -08001771 return (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001772 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001773}
1774
Stephen Boydeab30942012-05-24 00:45:21 -07001775/**
1776 * memblock_is_region_reserved - check if a region intersects reserved memory
1777 * @base: base of region to check
1778 * @size: size of region to check
1779 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001780 * Check if the region [@base, @base + @size) intersects a reserved
1781 * memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001782 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001783 * Return:
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001784 * True if they intersect, false if not.
Stephen Boydeab30942012-05-24 00:45:21 -07001785 */
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001786bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001787{
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001788 memblock_cap_size(base, &size);
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001789 return memblock_overlaps_region(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001790}
1791
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001792void __init_memblock memblock_trim_memory(phys_addr_t align)
1793{
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001794 phys_addr_t start, end, orig_start, orig_end;
Emil Medve136199f2014-04-07 15:37:52 -07001795 struct memblock_region *r;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001796
Emil Medve136199f2014-04-07 15:37:52 -07001797 for_each_memblock(memory, r) {
1798 orig_start = r->base;
1799 orig_end = r->base + r->size;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001800 start = round_up(orig_start, align);
1801 end = round_down(orig_end, align);
1802
1803 if (start == orig_start && end == orig_end)
1804 continue;
1805
1806 if (start < end) {
Emil Medve136199f2014-04-07 15:37:52 -07001807 r->base = start;
1808 r->size = end - start;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001809 } else {
Emil Medve136199f2014-04-07 15:37:52 -07001810 memblock_remove_region(&memblock.memory,
1811 r - memblock.memory.regions);
1812 r--;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001813 }
1814 }
1815}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001816
Yinghai Lu3661ca62010-09-15 13:05:29 -07001817void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001818{
1819 memblock.current_limit = limit;
1820}
1821
Laura Abbottfec51012014-02-27 01:23:43 +01001822phys_addr_t __init_memblock memblock_get_current_limit(void)
1823{
1824 return memblock.current_limit;
1825}
1826
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001827static void __init_memblock memblock_dump(struct memblock_type *type)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001828{
Miles Chen5d63f812017-02-22 15:46:42 -08001829 phys_addr_t base, end, size;
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001830 enum memblock_flags flags;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001831 int idx;
1832 struct memblock_region *rgn;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001833
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001834 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001835
Gioh Kim66e8b432017-11-15 17:33:42 -08001836 for_each_memblock_type(idx, type, rgn) {
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001837 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001838
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001839 base = rgn->base;
1840 size = rgn->size;
Miles Chen5d63f812017-02-22 15:46:42 -08001841 end = base + size - 1;
Tang Chen66a20752014-01-21 15:49:20 -08001842 flags = rgn->flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001843#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1844 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1845 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1846 memblock_get_region_node(rgn));
1847#endif
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001848 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001849 type->name, idx, &base, &end, &size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001850 }
1851}
1852
Tejun Heo4ff7b822011-12-08 10:22:06 -08001853void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001854{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001855 pr_info("MEMBLOCK configuration:\n");
Miles Chen5d63f812017-02-22 15:46:42 -08001856 pr_info(" memory size = %pa reserved size = %pa\n",
1857 &memblock.memory.total_size,
1858 &memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001859
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001860 memblock_dump(&memblock.memory);
1861 memblock_dump(&memblock.reserved);
Heiko Carstens409efd42017-02-24 14:55:56 -08001862#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001863 memblock_dump(&memblock.physmem);
Heiko Carstens409efd42017-02-24 14:55:56 -08001864#endif
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001865}
1866
Tejun Heo1aadc052011-12-08 10:22:08 -08001867void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001868{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001869 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001870}
1871
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001872static int __init early_memblock(char *p)
1873{
1874 if (p && strstr(p, "debug"))
1875 memblock_debug = 1;
1876 return 0;
1877}
1878early_param("memblock", early_memblock);
1879
Tejun Heoc378ddd2011-07-14 11:46:03 +02001880#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001881
1882static int memblock_debug_show(struct seq_file *m, void *private)
1883{
1884 struct memblock_type *type = m->private;
1885 struct memblock_region *reg;
1886 int i;
Miles Chen5d63f812017-02-22 15:46:42 -08001887 phys_addr_t end;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001888
1889 for (i = 0; i < type->cnt; i++) {
1890 reg = &type->regions[i];
Miles Chen5d63f812017-02-22 15:46:42 -08001891 end = reg->base + reg->size - 1;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001892
Miles Chen5d63f812017-02-22 15:46:42 -08001893 seq_printf(m, "%4d: ", i);
1894 seq_printf(m, "%pa..%pa\n", &reg->base, &end);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001895 }
1896 return 0;
1897}
Andy Shevchenko5ad35092018-04-05 16:23:16 -07001898DEFINE_SHOW_ATTRIBUTE(memblock_debug);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001899
1900static int __init memblock_init_debugfs(void)
1901{
1902 struct dentry *root = debugfs_create_dir("memblock", NULL);
1903 if (!root)
1904 return -ENXIO;
Joe Perches0825a6f2018-06-14 15:27:58 -07001905 debugfs_create_file("memory", 0444, root,
1906 &memblock.memory, &memblock_debug_fops);
1907 debugfs_create_file("reserved", 0444, root,
1908 &memblock.reserved, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001909#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
Joe Perches0825a6f2018-06-14 15:27:58 -07001910 debugfs_create_file("physmem", 0444, root,
1911 &memblock.physmem, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001912#endif
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001913
1914 return 0;
1915}
1916__initcall(memblock_init_debugfs);
1917
1918#endif /* CONFIG_DEBUG_FS */