blob: 1e1ea24e66edce14fd530115e265d363dab8cd3d [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>
23
Christoph Hellwigc4c5ad62016-07-28 15:48:06 -070024#include <asm/sections.h>
Santosh Shilimkar26f09e92014-01-21 15:50:19 -080025#include <linux/io.h>
26
27#include "internal.h"
Tang Chen79442ed2013-11-12 15:07:59 -080028
Mike Rapoport3e039c52018-06-30 17:55:05 +030029/**
30 * DOC: memblock overview
31 *
32 * Memblock is a method of managing memory regions during the early
33 * boot period when the usual kernel memory allocators are not up and
34 * running.
35 *
36 * Memblock views the system memory as collections of contiguous
37 * regions. There are several types of these collections:
38 *
39 * * ``memory`` - describes the physical memory available to the
40 * kernel; this may differ from the actual physical memory installed
41 * in the system, for instance when the memory is restricted with
42 * ``mem=`` command line parameter
43 * * ``reserved`` - describes the regions that were allocated
44 * * ``physmap`` - describes the actual physical memory regardless of
45 * the possible restrictions; the ``physmap`` type is only available
46 * on some architectures.
47 *
48 * Each region is represented by :c:type:`struct memblock_region` that
49 * defines the region extents, its attributes and NUMA node id on NUMA
50 * systems. Every memory type is described by the :c:type:`struct
51 * memblock_type` which contains an array of memory regions along with
52 * the allocator metadata. The memory types are nicely wrapped with
53 * :c:type:`struct memblock`. This structure is statically initialzed
54 * at build time. The region arrays for the "memory" and "reserved"
55 * types are initially sized to %INIT_MEMBLOCK_REGIONS and for the
56 * "physmap" type to %INIT_PHYSMEM_REGIONS.
57 * The :c:func:`memblock_allow_resize` enables automatic resizing of
58 * the region arrays during addition of new regions. This feature
59 * should be used with care so that memory allocated for the region
60 * array will not overlap with areas that should be reserved, for
61 * example initrd.
62 *
63 * The early architecture setup should tell memblock what the physical
64 * memory layout is by using :c:func:`memblock_add` or
65 * :c:func:`memblock_add_node` functions. The first function does not
66 * assign the region to a NUMA node and it is appropriate for UMA
67 * systems. Yet, it is possible to use it on NUMA systems as well and
68 * assign the region to a NUMA node later in the setup process using
69 * :c:func:`memblock_set_node`. The :c:func:`memblock_add_node`
70 * performs such an assignment directly.
71 *
72 * Once memblock is setup the memory can be allocated using either
73 * memblock or bootmem APIs.
74 *
75 * As the system boot progresses, the architecture specific
76 * :c:func:`mem_init` function frees all the memory to the buddy page
77 * allocator.
78 *
79 * If an architecure enables %CONFIG_ARCH_DISCARD_MEMBLOCK, the
80 * memblock data structures will be discarded after the system
81 * initialization compltes.
82 */
83
Tejun Heofe091c22011-12-08 10:22:07 -080084static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
85static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010086#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
87static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
88#endif
Tejun Heofe091c22011-12-08 10:22:07 -080089
90struct memblock memblock __initdata_memblock = {
91 .memory.regions = memblock_memory_init_regions,
92 .memory.cnt = 1, /* empty dummy entry */
93 .memory.max = INIT_MEMBLOCK_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -080094 .memory.name = "memory",
Tejun Heofe091c22011-12-08 10:22:07 -080095
96 .reserved.regions = memblock_reserved_init_regions,
97 .reserved.cnt = 1, /* empty dummy entry */
98 .reserved.max = INIT_MEMBLOCK_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -080099 .reserved.name = "reserved",
Tejun Heofe091c22011-12-08 10:22:07 -0800100
Philipp Hachtmann70210ed2014-01-29 18:16:01 +0100101#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
102 .physmem.regions = memblock_physmem_init_regions,
103 .physmem.cnt = 1, /* empty dummy entry */
104 .physmem.max = INIT_PHYSMEM_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800105 .physmem.name = "physmem",
Philipp Hachtmann70210ed2014-01-29 18:16:01 +0100106#endif
107
Tang Chen79442ed2013-11-12 15:07:59 -0800108 .bottom_up = false,
Tejun Heofe091c22011-12-08 10:22:07 -0800109 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
110};
Yinghai Lu95f72d12010-07-12 14:36:09 +1000111
Yinghai Lu10d06432010-07-28 15:43:02 +1000112int memblock_debug __initdata_memblock;
Tony Lucka3f5baf2015-06-24 16:58:12 -0700113static bool system_has_some_mirror __initdata_memblock = false;
Tejun Heo1aadc052011-12-08 10:22:08 -0800114static int memblock_can_resize __initdata_memblock;
Gavin Shan181eb392012-05-29 15:06:50 -0700115static int memblock_memory_in_slab __initdata_memblock = 0;
116static int memblock_reserved_in_slab __initdata_memblock = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000117
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300118enum memblock_flags __init_memblock choose_memblock_flags(void)
Tony Lucka3f5baf2015-06-24 16:58:12 -0700119{
120 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
121}
122
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800123/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
124static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
125{
Stefan Agner1c4bc432018-06-07 17:06:15 -0700126 return *size = min(*size, PHYS_ADDR_MAX - base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800127}
128
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000129/*
130 * Address comparison utilities
131 */
Yinghai Lu10d06432010-07-28 15:43:02 +1000132static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000133 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000134{
135 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
136}
137
Tang Chen95cf82e2015-09-08 15:02:03 -0700138bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -0700139 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000140{
141 unsigned long i;
142
Alexander Kuleshovf14516f2016-01-14 15:20:39 -0800143 for (i = 0; i < type->cnt; i++)
144 if (memblock_addrs_overlap(base, size, type->regions[i].base,
145 type->regions[i].size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000146 break;
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700147 return i < type->cnt;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000148}
149
Mike Rapoport47cec442018-06-30 17:55:02 +0300150/**
Tang Chen79442ed2013-11-12 15:07:59 -0800151 * __memblock_find_range_bottom_up - find free area utility in bottom-up
152 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300153 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
154 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen79442ed2013-11-12 15:07:59 -0800155 * @size: size of free area to find
156 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800157 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700158 * @flags: pick from blocks based on memory attributes
Tang Chen79442ed2013-11-12 15:07:59 -0800159 *
160 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
161 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300162 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800163 * Found address on success, 0 on failure.
164 */
165static phys_addr_t __init_memblock
166__memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700167 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300168 enum memblock_flags flags)
Tang Chen79442ed2013-11-12 15:07:59 -0800169{
170 phys_addr_t this_start, this_end, cand;
171 u64 i;
172
Tony Luckfc6daaf2015-06-24 16:58:09 -0700173 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
Tang Chen79442ed2013-11-12 15:07:59 -0800174 this_start = clamp(this_start, start, end);
175 this_end = clamp(this_end, start, end);
176
177 cand = round_up(this_start, align);
178 if (cand < this_end && this_end - cand >= size)
179 return cand;
180 }
181
182 return 0;
183}
184
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800185/**
Tang Chen14028992013-11-12 15:07:57 -0800186 * __memblock_find_range_top_down - find free area utility, in top-down
187 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300188 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
189 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen14028992013-11-12 15:07:57 -0800190 * @size: size of free area to find
191 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800192 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700193 * @flags: pick from blocks based on memory attributes
Tang Chen14028992013-11-12 15:07:57 -0800194 *
195 * Utility called from memblock_find_in_range_node(), find free area top-down.
196 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300197 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800198 * Found address on success, 0 on failure.
Tang Chen14028992013-11-12 15:07:57 -0800199 */
200static phys_addr_t __init_memblock
201__memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700202 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300203 enum memblock_flags flags)
Tang Chen14028992013-11-12 15:07:57 -0800204{
205 phys_addr_t this_start, this_end, cand;
206 u64 i;
207
Tony Luckfc6daaf2015-06-24 16:58:09 -0700208 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
209 NULL) {
Tang Chen14028992013-11-12 15:07:57 -0800210 this_start = clamp(this_start, start, end);
211 this_end = clamp(this_end, start, end);
212
213 if (this_end < size)
214 continue;
215
216 cand = round_down(this_end - size, align);
217 if (cand >= this_start)
218 return cand;
219 }
220
221 return 0;
222}
223
224/**
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800225 * memblock_find_in_range_node - find free area in given range and node
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800226 * @size: size of free area to find
227 * @align: alignment of free area to find
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800228 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300229 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
230 * %MEMBLOCK_ALLOC_ACCESSIBLE
Grygorii Strashkob1154232014-01-21 15:50:16 -0800231 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700232 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800233 *
234 * Find @size free area aligned to @align in the specified range and node.
235 *
Tang Chen79442ed2013-11-12 15:07:59 -0800236 * When allocation direction is bottom-up, the @start should be greater
237 * than the end of the kernel image. Otherwise, it will be trimmed. The
238 * reason is that we want the bottom-up allocation just near the kernel
239 * image so it is highly likely that the allocated memory and the kernel
240 * will reside in the same node.
241 *
242 * If bottom-up allocation failed, will try to allocate memory top-down.
243 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300244 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800245 * Found address on success, 0 on failure.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000246 */
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800247phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
248 phys_addr_t align, phys_addr_t start,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300249 phys_addr_t end, int nid,
250 enum memblock_flags flags)
Tang Chenf7210e62013-02-22 16:33:51 -0800251{
Tang Chen0cfb8f02014-08-29 15:18:31 -0700252 phys_addr_t kernel_end, ret;
Tang Chen79442ed2013-11-12 15:07:59 -0800253
Tang Chenf7210e62013-02-22 16:33:51 -0800254 /* pump up @end */
255 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
256 end = memblock.current_limit;
257
258 /* avoid allocating the first page */
259 start = max_t(phys_addr_t, start, PAGE_SIZE);
260 end = max(start, end);
Tang Chen79442ed2013-11-12 15:07:59 -0800261 kernel_end = __pa_symbol(_end);
262
263 /*
264 * try bottom-up allocation only when bottom-up mode
265 * is set and @end is above the kernel image.
266 */
267 if (memblock_bottom_up() && end > kernel_end) {
268 phys_addr_t bottom_up_start;
269
270 /* make sure we will allocate above the kernel */
271 bottom_up_start = max(start, kernel_end);
272
273 /* ok, try bottom-up allocation first */
274 ret = __memblock_find_range_bottom_up(bottom_up_start, end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700275 size, align, nid, flags);
Tang Chen79442ed2013-11-12 15:07:59 -0800276 if (ret)
277 return ret;
278
279 /*
280 * we always limit bottom-up allocation above the kernel,
281 * but top-down allocation doesn't have the limit, so
282 * retrying top-down allocation may succeed when bottom-up
283 * allocation failed.
284 *
285 * bottom-up allocation is expected to be fail very rarely,
286 * so we use WARN_ONCE() here to see the stack trace if
287 * fail happens.
288 */
Joe Perches756a0252016-03-17 14:19:47 -0700289 WARN_ONCE(1, "memblock: bottom-up allocation failed, memory hotunplug may be affected\n");
Tang Chen79442ed2013-11-12 15:07:59 -0800290 }
Tang Chenf7210e62013-02-22 16:33:51 -0800291
Tony Luckfc6daaf2015-06-24 16:58:09 -0700292 return __memblock_find_range_top_down(start, end, size, align, nid,
293 flags);
Tang Chenf7210e62013-02-22 16:33:51 -0800294}
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000295
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800296/**
297 * memblock_find_in_range - find free area in given range
298 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300299 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
300 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800301 * @size: size of free area to find
302 * @align: alignment of free area to find
303 *
304 * Find @size free area aligned to @align in the specified range.
305 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300306 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800307 * Found address on success, 0 on failure.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800308 */
309phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
310 phys_addr_t end, phys_addr_t size,
311 phys_addr_t align)
312{
Tony Lucka3f5baf2015-06-24 16:58:12 -0700313 phys_addr_t ret;
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300314 enum memblock_flags flags = choose_memblock_flags();
Tony Lucka3f5baf2015-06-24 16:58:12 -0700315
316again:
317 ret = memblock_find_in_range_node(size, align, start, end,
318 NUMA_NO_NODE, flags);
319
320 if (!ret && (flags & MEMBLOCK_MIRROR)) {
321 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
322 &size);
323 flags &= ~MEMBLOCK_MIRROR;
324 goto again;
325 }
326
327 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800328}
329
Yinghai Lu10d06432010-07-28 15:43:02 +1000330static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000331{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800332 type->total_size -= type->regions[r].size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200333 memmove(&type->regions[r], &type->regions[r + 1],
334 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000335 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000336
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700337 /* Special case for empty arrays */
338 if (type->cnt == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800339 WARN_ON(type->total_size != 0);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700340 type->cnt = 1;
341 type->regions[0].base = 0;
342 type->regions[0].size = 0;
Tang Chen66a20752014-01-21 15:49:20 -0800343 type->regions[0].flags = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200344 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700345 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000346}
347
Philipp Hachtmann354f17e2014-01-23 15:53:24 -0800348#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
Pavel Tatashin3010f872017-08-18 15:16:05 -0700349/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300350 * memblock_discard - discard memory and reserved arrays if they were allocated
Pavel Tatashin3010f872017-08-18 15:16:05 -0700351 */
352void __init memblock_discard(void)
Yinghai Lu29f67382012-07-11 14:02:56 -0700353{
Pavel Tatashin3010f872017-08-18 15:16:05 -0700354 phys_addr_t addr, size;
Yinghai Lu29f67382012-07-11 14:02:56 -0700355
Pavel Tatashin3010f872017-08-18 15:16:05 -0700356 if (memblock.reserved.regions != memblock_reserved_init_regions) {
357 addr = __pa(memblock.reserved.regions);
358 size = PAGE_ALIGN(sizeof(struct memblock_region) *
359 memblock.reserved.max);
360 __memblock_free_late(addr, size);
361 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700362
Pavel Tatashin91b540f2017-08-25 15:55:46 -0700363 if (memblock.memory.regions != memblock_memory_init_regions) {
Pavel Tatashin3010f872017-08-18 15:16:05 -0700364 addr = __pa(memblock.memory.regions);
365 size = PAGE_ALIGN(sizeof(struct memblock_region) *
366 memblock.memory.max);
367 __memblock_free_late(addr, size);
368 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700369}
Philipp Hachtmann5e270e22014-01-23 15:53:11 -0800370#endif
371
Greg Pearson48c3b582012-06-20 12:53:05 -0700372/**
373 * memblock_double_array - double the size of the memblock regions array
374 * @type: memblock type of the regions array being doubled
375 * @new_area_start: starting address of memory range to avoid overlap with
376 * @new_area_size: size of memory range to avoid overlap with
377 *
378 * Double the size of the @type regions array. If memblock is being used to
379 * allocate memory for a new reserved regions array and there is a previously
Mike Rapoport47cec442018-06-30 17:55:02 +0300380 * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
Greg Pearson48c3b582012-06-20 12:53:05 -0700381 * waiting to be reserved, ensure the memory used by the new array does
382 * not overlap.
383 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300384 * Return:
Greg Pearson48c3b582012-06-20 12:53:05 -0700385 * 0 on success, -1 on failure.
386 */
387static int __init_memblock memblock_double_array(struct memblock_type *type,
388 phys_addr_t new_area_start,
389 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700390{
391 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700392 phys_addr_t old_alloc_size, new_alloc_size;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700393 phys_addr_t old_size, new_size, addr;
394 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700395 int *in_slab;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700396
397 /* We don't allow resizing until we know about the reserved regions
398 * of memory that aren't suitable for allocation
399 */
400 if (!memblock_can_resize)
401 return -1;
402
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700403 /* Calculate new doubled size */
404 old_size = type->max * sizeof(struct memblock_region);
405 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700406 /*
407 * We need to allocated new one align to PAGE_SIZE,
408 * so we can free them completely later.
409 */
410 old_alloc_size = PAGE_ALIGN(old_size);
411 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700412
Gavin Shan181eb392012-05-29 15:06:50 -0700413 /* Retrieve the slab flag */
414 if (type == &memblock.memory)
415 in_slab = &memblock_memory_in_slab;
416 else
417 in_slab = &memblock_reserved_in_slab;
418
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700419 /* Try to find some space for it.
420 *
421 * WARNING: We assume that either slab_is_available() and we use it or
Andrew Mortonfd073832012-07-31 16:42:40 -0700422 * we use MEMBLOCK for allocations. That means that this is unsafe to
423 * use when bootmem is currently active (unless bootmem itself is
424 * implemented on top of MEMBLOCK which isn't the case yet)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700425 *
426 * This should however not be an issue for now, as we currently only
Andrew Mortonfd073832012-07-31 16:42:40 -0700427 * call into MEMBLOCK while it's still active, or much later when slab
428 * is active for memory hotplug operations
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700429 */
430 if (use_slab) {
431 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200432 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700433 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700434 /* only exclude range when trying to double reserved.regions */
435 if (type != &memblock.reserved)
436 new_area_start = new_area_size = 0;
437
438 addr = memblock_find_in_range(new_area_start + new_area_size,
439 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700440 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700441 if (!addr && new_area_size)
442 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700443 min(new_area_start, memblock.current_limit),
444 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700445
Sachin Kamat15674862012-09-04 13:55:05 +0530446 new_array = addr ? __va(addr) : NULL;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700447 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200448 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700449 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800450 type->name, type->max, type->max * 2);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700451 return -1;
452 }
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700453
Andrew Mortonfd073832012-07-31 16:42:40 -0700454 memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800455 type->name, type->max * 2, (u64)addr,
Andrew Mortonfd073832012-07-31 16:42:40 -0700456 (u64)addr + new_size - 1);
Yinghai Luea9e4372010-07-28 15:13:22 +1000457
Andrew Mortonfd073832012-07-31 16:42:40 -0700458 /*
459 * Found space, we now need to move the array over before we add the
460 * reserved region since it may be our reserved array itself that is
461 * full.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700462 */
463 memcpy(new_array, type->regions, old_size);
464 memset(new_array + type->max, 0, old_size);
465 old_array = type->regions;
466 type->regions = new_array;
467 type->max <<= 1;
468
Andrew Mortonfd073832012-07-31 16:42:40 -0700469 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700470 if (*in_slab)
471 kfree(old_array);
472 else if (old_array != memblock_memory_init_regions &&
473 old_array != memblock_reserved_init_regions)
Yinghai Lu29f67382012-07-11 14:02:56 -0700474 memblock_free(__pa(old_array), old_alloc_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700475
Andrew Mortonfd073832012-07-31 16:42:40 -0700476 /*
477 * Reserve the new array if that comes from the memblock. Otherwise, we
478 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700479 */
480 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700481 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700482
483 /* Update slab flag */
484 *in_slab = use_slab;
485
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700486 return 0;
487}
488
Tejun Heo784656f92011-07-12 11:15:55 +0200489/**
490 * memblock_merge_regions - merge neighboring compatible regions
491 * @type: memblock type to scan
492 *
493 * Scan @type and merge neighboring compatible regions.
494 */
495static void __init_memblock memblock_merge_regions(struct memblock_type *type)
496{
497 int i = 0;
498
499 /* cnt never goes below 1 */
500 while (i < type->cnt - 1) {
501 struct memblock_region *this = &type->regions[i];
502 struct memblock_region *next = &type->regions[i + 1];
503
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200504 if (this->base + this->size != next->base ||
505 memblock_get_region_node(this) !=
Tang Chen66a20752014-01-21 15:49:20 -0800506 memblock_get_region_node(next) ||
507 this->flags != next->flags) {
Tejun Heo784656f92011-07-12 11:15:55 +0200508 BUG_ON(this->base + this->size > next->base);
509 i++;
510 continue;
511 }
512
513 this->size += next->size;
Lin Fengc0232ae2013-01-11 14:31:44 -0800514 /* move forward from next + 1, index of which is i + 2 */
515 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
Tejun Heo784656f92011-07-12 11:15:55 +0200516 type->cnt--;
517 }
518}
519
520/**
521 * memblock_insert_region - insert new memblock region
Tang Chen209ff862013-04-29 15:08:41 -0700522 * @type: memblock type to insert into
523 * @idx: index for the insertion point
524 * @base: base address of the new region
525 * @size: size of the new region
526 * @nid: node id of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800527 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200528 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300529 * Insert new memblock region [@base, @base + @size) into @type at @idx.
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700530 * @type must already have extra room to accommodate the new region.
Tejun Heo784656f92011-07-12 11:15:55 +0200531 */
532static void __init_memblock memblock_insert_region(struct memblock_type *type,
533 int idx, phys_addr_t base,
Tang Chen66a20752014-01-21 15:49:20 -0800534 phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300535 int nid,
536 enum memblock_flags flags)
Tejun Heo784656f92011-07-12 11:15:55 +0200537{
538 struct memblock_region *rgn = &type->regions[idx];
539
540 BUG_ON(type->cnt >= type->max);
541 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
542 rgn->base = base;
543 rgn->size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800544 rgn->flags = flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200545 memblock_set_region_node(rgn, nid);
Tejun Heo784656f92011-07-12 11:15:55 +0200546 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800547 type->total_size += size;
Tejun Heo784656f92011-07-12 11:15:55 +0200548}
549
550/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100551 * memblock_add_range - add new memblock region
Tejun Heo784656f92011-07-12 11:15:55 +0200552 * @type: memblock type to add new region into
553 * @base: base address of the new region
554 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800555 * @nid: nid of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800556 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200557 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300558 * Add new memblock region [@base, @base + @size) into @type. The new region
Tejun Heo784656f92011-07-12 11:15:55 +0200559 * is allowed to overlap with existing ones - overlaps don't affect already
560 * existing regions. @type is guaranteed to be minimal (all neighbouring
561 * compatible regions are merged) after the addition.
562 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300563 * Return:
Tejun Heo784656f92011-07-12 11:15:55 +0200564 * 0 on success, -errno on failure.
565 */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100566int __init_memblock memblock_add_range(struct memblock_type *type,
Tang Chen66a20752014-01-21 15:49:20 -0800567 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300568 int nid, enum memblock_flags flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000569{
Tejun Heo784656f92011-07-12 11:15:55 +0200570 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800571 phys_addr_t obase = base;
572 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800573 int idx, nr_new;
574 struct memblock_region *rgn;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000575
Tejun Heob3dc6272012-04-20 08:31:34 -0700576 if (!size)
577 return 0;
578
Tejun Heo784656f92011-07-12 11:15:55 +0200579 /* special case for empty array */
580 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800581 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000582 type->regions[0].base = base;
583 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800584 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800585 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800586 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000587 return 0;
588 }
Tejun Heo784656f92011-07-12 11:15:55 +0200589repeat:
590 /*
591 * The following is executed twice. Once with %false @insert and
592 * then with %true. The first counts the number of regions needed
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700593 * to accommodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700594 */
Tejun Heo784656f92011-07-12 11:15:55 +0200595 base = obase;
596 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000597
Gioh Kim66e8b432017-11-15 17:33:42 -0800598 for_each_memblock_type(idx, type, rgn) {
Tejun Heo784656f92011-07-12 11:15:55 +0200599 phys_addr_t rbase = rgn->base;
600 phys_addr_t rend = rbase + rgn->size;
601
602 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000603 break;
Tejun Heo784656f92011-07-12 11:15:55 +0200604 if (rend <= base)
605 continue;
606 /*
607 * @rgn overlaps. If it separates the lower part of new
608 * area, insert that portion.
609 */
610 if (rbase > base) {
Wei Yangc0a29492015-09-04 15:47:38 -0700611#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
612 WARN_ON(nid != memblock_get_region_node(rgn));
613#endif
Wei Yang4fcab5f2015-09-08 14:59:53 -0700614 WARN_ON(flags != rgn->flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200615 nr_new++;
616 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800617 memblock_insert_region(type, idx++, base,
Tang Chen66a20752014-01-21 15:49:20 -0800618 rbase - base, nid,
619 flags);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000620 }
Tejun Heo784656f92011-07-12 11:15:55 +0200621 /* area below @rend is dealt with, forget about it */
622 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000623 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000624
Tejun Heo784656f92011-07-12 11:15:55 +0200625 /* insert the remaining portion */
626 if (base < end) {
627 nr_new++;
628 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800629 memblock_insert_region(type, idx, base, end - base,
Tang Chen66a20752014-01-21 15:49:20 -0800630 nid, flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200631 }
632
nimisoloef3cc4d2016-07-26 15:24:56 -0700633 if (!nr_new)
634 return 0;
635
Tejun Heo784656f92011-07-12 11:15:55 +0200636 /*
637 * If this was the first round, resize array and repeat for actual
638 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700639 */
Tejun Heo784656f92011-07-12 11:15:55 +0200640 if (!insert) {
641 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700642 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f92011-07-12 11:15:55 +0200643 return -ENOMEM;
644 insert = true;
645 goto repeat;
646 } else {
647 memblock_merge_regions(type);
648 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700649 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000650}
651
Mike Rapoport48a833c2018-06-30 17:55:03 +0300652/**
653 * memblock_add_node - add new memblock region within a NUMA node
654 * @base: base address of the new region
655 * @size: size of the new region
656 * @nid: nid of the new region
657 *
658 * Add new memblock region [@base, @base + @size) to the "memory"
659 * type. See memblock_add_range() description for mode details
660 *
661 * Return:
662 * 0 on success, -errno on failure.
663 */
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800664int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
665 int nid)
666{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100667 return memblock_add_range(&memblock.memory, base, size, nid, 0);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800668}
669
Mike Rapoport48a833c2018-06-30 17:55:03 +0300670/**
671 * memblock_add - add new memblock region
672 * @base: base address of the new region
673 * @size: size of the new region
674 *
675 * Add new memblock region [@base, @base + @size) to the "memory"
676 * type. See memblock_add_range() description for mode details
677 *
678 * Return:
679 * 0 on success, -errno on failure.
680 */
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700681int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700682{
Miles Chen5d63f812017-02-22 15:46:42 -0800683 phys_addr_t end = base + size - 1;
684
685 memblock_dbg("memblock_add: [%pa-%pa] %pF\n",
686 &base, &end, (void *)_RET_IP_);
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700687
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700688 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000689}
690
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800691/**
692 * memblock_isolate_range - isolate given range into disjoint memblocks
693 * @type: memblock type to isolate range for
694 * @base: base of range to isolate
695 * @size: size of range to isolate
696 * @start_rgn: out parameter for the start of isolated region
697 * @end_rgn: out parameter for the end of isolated region
698 *
699 * Walk @type and ensure that regions don't cross the boundaries defined by
Mike Rapoport47cec442018-06-30 17:55:02 +0300700 * [@base, @base + @size). Crossing regions are split at the boundaries,
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800701 * which may create at most two more regions. The index of the first
702 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
703 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300704 * Return:
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800705 * 0 on success, -errno on failure.
706 */
707static int __init_memblock memblock_isolate_range(struct memblock_type *type,
708 phys_addr_t base, phys_addr_t size,
709 int *start_rgn, int *end_rgn)
710{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800711 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800712 int idx;
713 struct memblock_region *rgn;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800714
715 *start_rgn = *end_rgn = 0;
716
Tejun Heob3dc6272012-04-20 08:31:34 -0700717 if (!size)
718 return 0;
719
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800720 /* we'll create at most two more regions */
721 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700722 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800723 return -ENOMEM;
724
Gioh Kim66e8b432017-11-15 17:33:42 -0800725 for_each_memblock_type(idx, type, rgn) {
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800726 phys_addr_t rbase = rgn->base;
727 phys_addr_t rend = rbase + rgn->size;
728
729 if (rbase >= end)
730 break;
731 if (rend <= base)
732 continue;
733
734 if (rbase < base) {
735 /*
736 * @rgn intersects from below. Split and continue
737 * to process the next region - the new top half.
738 */
739 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800740 rgn->size -= base - rbase;
741 type->total_size -= base - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800742 memblock_insert_region(type, idx, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800743 memblock_get_region_node(rgn),
744 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800745 } else if (rend > end) {
746 /*
747 * @rgn intersects from above. Split and redo the
748 * current region - the new bottom half.
749 */
750 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800751 rgn->size -= end - rbase;
752 type->total_size -= end - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800753 memblock_insert_region(type, idx--, rbase, end - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800754 memblock_get_region_node(rgn),
755 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800756 } else {
757 /* @rgn is fully contained, record it */
758 if (!*end_rgn)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800759 *start_rgn = idx;
760 *end_rgn = idx + 1;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800761 }
762 }
763
764 return 0;
765}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800766
Alexander Kuleshov35bd16a2015-11-05 18:47:00 -0800767static int __init_memblock memblock_remove_range(struct memblock_type *type,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100768 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000769{
Tejun Heo71936182011-12-08 10:22:07 -0800770 int start_rgn, end_rgn;
771 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000772
Tejun Heo71936182011-12-08 10:22:07 -0800773 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
774 if (ret)
775 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000776
Tejun Heo71936182011-12-08 10:22:07 -0800777 for (i = end_rgn - 1; i >= start_rgn; i--)
778 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700779 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000780}
781
Tejun Heo581adcb2011-12-08 10:22:06 -0800782int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000783{
Minchan Kim25cf23d2018-06-07 17:07:35 -0700784 phys_addr_t end = base + size - 1;
785
786 memblock_dbg("memblock_remove: [%pa-%pa] %pS\n",
787 &base, &end, (void *)_RET_IP_);
788
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100789 return memblock_remove_range(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000790}
791
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100792
Tejun Heo581adcb2011-12-08 10:22:06 -0800793int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000794{
Miles Chen5d63f812017-02-22 15:46:42 -0800795 phys_addr_t end = base + size - 1;
796
797 memblock_dbg(" memblock_free: [%pa-%pa] %pF\n",
798 &base, &end, (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200799
Catalin Marinas9099dae2016-10-11 13:55:11 -0700800 kmemleak_free_part_phys(base, size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100801 return memblock_remove_range(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000802}
803
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700804int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000805{
Miles Chen5d63f812017-02-22 15:46:42 -0800806 phys_addr_t end = base + size - 1;
807
808 memblock_dbg("memblock_reserve: [%pa-%pa] %pF\n",
809 &base, &end, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000810
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700811 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000812}
813
Tejun Heo35fd0802011-07-12 11:15:59 +0200814/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300815 * memblock_setclr_flag - set or clear flag for a memory region
816 * @base: base address of the region
817 * @size: size of the region
818 * @set: set or clear the flag
819 * @flag: the flag to udpate
Tang Chen66b16ed2014-01-21 15:49:23 -0800820 *
Tony Luck4308ce12014-12-12 16:54:59 -0800821 * This function isolates region [@base, @base + @size), and sets/clears flag
Tang Chen66b16ed2014-01-21 15:49:23 -0800822 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300823 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800824 */
Tony Luck4308ce12014-12-12 16:54:59 -0800825static int __init_memblock memblock_setclr_flag(phys_addr_t base,
826 phys_addr_t size, int set, int flag)
Tang Chen66b16ed2014-01-21 15:49:23 -0800827{
828 struct memblock_type *type = &memblock.memory;
829 int i, ret, start_rgn, end_rgn;
830
831 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
832 if (ret)
833 return ret;
834
835 for (i = start_rgn; i < end_rgn; i++)
Tony Luck4308ce12014-12-12 16:54:59 -0800836 if (set)
837 memblock_set_region_flags(&type->regions[i], flag);
838 else
839 memblock_clear_region_flags(&type->regions[i], flag);
Tang Chen66b16ed2014-01-21 15:49:23 -0800840
841 memblock_merge_regions(type);
842 return 0;
843}
844
845/**
Tony Luck4308ce12014-12-12 16:54:59 -0800846 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
847 * @base: the base phys addr of the region
848 * @size: the size of the region
849 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300850 * Return: 0 on success, -errno on failure.
Tony Luck4308ce12014-12-12 16:54:59 -0800851 */
852int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
853{
854 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
855}
856
857/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800858 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
859 * @base: the base phys addr of the region
860 * @size: the size of the region
861 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300862 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800863 */
864int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
865{
Tony Luck4308ce12014-12-12 16:54:59 -0800866 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
Tang Chen66b16ed2014-01-21 15:49:23 -0800867}
868
869/**
Tony Lucka3f5baf2015-06-24 16:58:12 -0700870 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
871 * @base: the base phys addr of the region
872 * @size: the size of the region
873 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300874 * Return: 0 on success, -errno on failure.
Tony Lucka3f5baf2015-06-24 16:58:12 -0700875 */
876int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
877{
878 system_has_some_mirror = true;
879
880 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
881}
882
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100883/**
884 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
885 * @base: the base phys addr of the region
886 * @size: the size of the region
887 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300888 * Return: 0 on success, -errno on failure.
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100889 */
890int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
891{
892 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
893}
Tony Lucka3f5baf2015-06-24 16:58:12 -0700894
895/**
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900896 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
897 * @base: the base phys addr of the region
898 * @size: the size of the region
899 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300900 * Return: 0 on success, -errno on failure.
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900901 */
902int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
903{
904 return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
905}
906
907/**
Robin Holt8e7a7f82015-06-30 14:56:41 -0700908 * __next_reserved_mem_region - next function for for_each_reserved_region()
909 * @idx: pointer to u64 loop variable
910 * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
911 * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
912 *
913 * Iterate over all reserved memory regions.
914 */
915void __init_memblock __next_reserved_mem_region(u64 *idx,
916 phys_addr_t *out_start,
917 phys_addr_t *out_end)
918{
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700919 struct memblock_type *type = &memblock.reserved;
Robin Holt8e7a7f82015-06-30 14:56:41 -0700920
Richard Leitnercd33a762016-05-20 16:58:33 -0700921 if (*idx < type->cnt) {
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700922 struct memblock_region *r = &type->regions[*idx];
Robin Holt8e7a7f82015-06-30 14:56:41 -0700923 phys_addr_t base = r->base;
924 phys_addr_t size = r->size;
925
926 if (out_start)
927 *out_start = base;
928 if (out_end)
929 *out_end = base + size - 1;
930
931 *idx += 1;
932 return;
933 }
934
935 /* signal end of iteration */
936 *idx = ULLONG_MAX;
937}
938
939/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100940 * __next__mem_range - next function for for_each_free_mem_range() etc.
Tejun Heo35fd0802011-07-12 11:15:59 +0200941 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800942 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700943 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100944 * @type_a: pointer to memblock_type from where the range is taken
945 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -0700946 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
947 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
948 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +0200949 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100950 * Find the first area from *@idx which matches @nid, fill the out
Tejun Heo35fd0802011-07-12 11:15:59 +0200951 * parameters, and update *@idx for the next iteration. The lower 32bit of
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100952 * *@idx contains index into type_a and the upper 32bit indexes the
953 * areas before each region in type_b. For example, if type_b regions
Tejun Heo35fd0802011-07-12 11:15:59 +0200954 * look like the following,
955 *
956 * 0:[0-16), 1:[32-48), 2:[128-130)
957 *
958 * The upper 32bit indexes the following regions.
959 *
960 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
961 *
962 * As both region arrays are sorted, the function advances the two indices
963 * in lockstep and returns each intersection.
964 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300965void __init_memblock __next_mem_range(u64 *idx, int nid,
966 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100967 struct memblock_type *type_a,
968 struct memblock_type *type_b,
969 phys_addr_t *out_start,
970 phys_addr_t *out_end, int *out_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200971{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100972 int idx_a = *idx & 0xffffffff;
973 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -0800974
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100975 if (WARN_ONCE(nid == MAX_NUMNODES,
976 "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
Grygorii Strashko560dca272014-01-21 15:50:55 -0800977 nid = NUMA_NO_NODE;
Tejun Heo35fd0802011-07-12 11:15:59 +0200978
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100979 for (; idx_a < type_a->cnt; idx_a++) {
980 struct memblock_region *m = &type_a->regions[idx_a];
981
Tejun Heo35fd0802011-07-12 11:15:59 +0200982 phys_addr_t m_start = m->base;
983 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100984 int m_nid = memblock_get_region_node(m);
Tejun Heo35fd0802011-07-12 11:15:59 +0200985
986 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100987 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200988 continue;
989
Xishi Qiu0a313a92014-09-09 14:50:46 -0700990 /* skip hotpluggable memory regions if needed */
991 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
992 continue;
993
Tony Lucka3f5baf2015-06-24 16:58:12 -0700994 /* if we want mirror memory skip non-mirror memory regions */
995 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
996 continue;
997
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100998 /* skip nomap memory unless we were asked for it explicitly */
999 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1000 continue;
1001
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001002 if (!type_b) {
1003 if (out_start)
1004 *out_start = m_start;
1005 if (out_end)
1006 *out_end = m_end;
1007 if (out_nid)
1008 *out_nid = m_nid;
1009 idx_a++;
1010 *idx = (u32)idx_a | (u64)idx_b << 32;
1011 return;
1012 }
Tejun Heo35fd0802011-07-12 11:15:59 +02001013
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001014 /* scan areas before each reservation */
1015 for (; idx_b < type_b->cnt + 1; idx_b++) {
1016 struct memblock_region *r;
1017 phys_addr_t r_start;
1018 phys_addr_t r_end;
1019
1020 r = &type_b->regions[idx_b];
1021 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1022 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001023 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001024
1025 /*
1026 * if idx_b advanced past idx_a,
1027 * break out to advance idx_a
1028 */
Tejun Heo35fd0802011-07-12 11:15:59 +02001029 if (r_start >= m_end)
1030 break;
1031 /* if the two regions intersect, we're done */
1032 if (m_start < r_end) {
1033 if (out_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001034 *out_start =
1035 max(m_start, r_start);
Tejun Heo35fd0802011-07-12 11:15:59 +02001036 if (out_end)
1037 *out_end = min(m_end, r_end);
1038 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001039 *out_nid = m_nid;
Tejun Heo35fd0802011-07-12 11:15:59 +02001040 /*
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001041 * The region which ends first is
1042 * advanced for the next iteration.
Tejun Heo35fd0802011-07-12 11:15:59 +02001043 */
1044 if (m_end <= r_end)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001045 idx_a++;
Tejun Heo35fd0802011-07-12 11:15:59 +02001046 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001047 idx_b++;
1048 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo35fd0802011-07-12 11:15:59 +02001049 return;
1050 }
1051 }
1052 }
1053
1054 /* signal end of iteration */
1055 *idx = ULLONG_MAX;
1056}
1057
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001058/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001059 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1060 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001061 * @idx: pointer to u64 loop variable
Alexander Kuleshovad5ea8c2015-09-08 15:04:22 -07001062 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -07001063 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001064 * @type_a: pointer to memblock_type from where the range is taken
1065 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -07001066 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1067 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1068 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001069 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001070 * Finds the next range from type_a which is not marked as unsuitable
1071 * in type_b.
1072 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001073 * Reverse of __next_mem_range().
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001074 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001075void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1076 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001077 struct memblock_type *type_a,
1078 struct memblock_type *type_b,
1079 phys_addr_t *out_start,
1080 phys_addr_t *out_end, int *out_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001081{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001082 int idx_a = *idx & 0xffffffff;
1083 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -08001084
Grygorii Strashko560dca272014-01-21 15:50:55 -08001085 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1086 nid = NUMA_NO_NODE;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001087
1088 if (*idx == (u64)ULLONG_MAX) {
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001089 idx_a = type_a->cnt - 1;
zijun_hue47608a2016-08-04 15:32:00 -07001090 if (type_b != NULL)
1091 idx_b = type_b->cnt;
1092 else
1093 idx_b = 0;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001094 }
1095
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001096 for (; idx_a >= 0; idx_a--) {
1097 struct memblock_region *m = &type_a->regions[idx_a];
1098
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001099 phys_addr_t m_start = m->base;
1100 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001101 int m_nid = memblock_get_region_node(m);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001102
1103 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001104 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001105 continue;
1106
Tang Chen55ac5902014-01-21 15:49:35 -08001107 /* skip hotpluggable memory regions if needed */
1108 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
1109 continue;
1110
Tony Lucka3f5baf2015-06-24 16:58:12 -07001111 /* if we want mirror memory skip non-mirror memory regions */
1112 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1113 continue;
1114
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001115 /* skip nomap memory unless we were asked for it explicitly */
1116 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1117 continue;
1118
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001119 if (!type_b) {
1120 if (out_start)
1121 *out_start = m_start;
1122 if (out_end)
1123 *out_end = m_end;
1124 if (out_nid)
1125 *out_nid = m_nid;
zijun_hufb399b42016-07-28 15:48:56 -07001126 idx_a--;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001127 *idx = (u32)idx_a | (u64)idx_b << 32;
1128 return;
1129 }
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001130
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001131 /* scan areas before each reservation */
1132 for (; idx_b >= 0; idx_b--) {
1133 struct memblock_region *r;
1134 phys_addr_t r_start;
1135 phys_addr_t r_end;
1136
1137 r = &type_b->regions[idx_b];
1138 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1139 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001140 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001141 /*
1142 * if idx_b advanced past idx_a,
1143 * break out to advance idx_a
1144 */
1145
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001146 if (r_end <= m_start)
1147 break;
1148 /* if the two regions intersect, we're done */
1149 if (m_end > r_start) {
1150 if (out_start)
1151 *out_start = max(m_start, r_start);
1152 if (out_end)
1153 *out_end = min(m_end, r_end);
1154 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001155 *out_nid = m_nid;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001156 if (m_start >= r_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001157 idx_a--;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001158 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001159 idx_b--;
1160 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001161 return;
1162 }
1163 }
1164 }
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001165 /* signal end of iteration */
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001166 *idx = ULLONG_MAX;
1167}
1168
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001169#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1170/*
1171 * Common iterator interface used to define for_each_mem_range().
1172 */
1173void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1174 unsigned long *out_start_pfn,
1175 unsigned long *out_end_pfn, int *out_nid)
1176{
1177 struct memblock_type *type = &memblock.memory;
1178 struct memblock_region *r;
1179
1180 while (++*idx < type->cnt) {
1181 r = &type->regions[*idx];
1182
1183 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1184 continue;
1185 if (nid == MAX_NUMNODES || nid == r->nid)
1186 break;
1187 }
1188 if (*idx >= type->cnt) {
1189 *idx = -1;
1190 return;
1191 }
1192
1193 if (out_start_pfn)
1194 *out_start_pfn = PFN_UP(r->base);
1195 if (out_end_pfn)
1196 *out_end_pfn = PFN_DOWN(r->base + r->size);
1197 if (out_nid)
1198 *out_nid = r->nid;
1199}
1200
1201/**
1202 * memblock_set_node - set node ID on memblock regions
1203 * @base: base of area to set node ID for
1204 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -08001205 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001206 * @nid: node ID to set
1207 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001208 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001209 * Regions which cross the area boundaries are split as necessary.
1210 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001211 * Return:
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001212 * 0 on success, -errno on failure.
1213 */
1214int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -08001215 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001216{
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001217 int start_rgn, end_rgn;
1218 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001219
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001220 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1221 if (ret)
1222 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001223
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001224 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -07001225 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001226
1227 memblock_merge_regions(type);
1228 return 0;
1229}
1230#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1231
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001232static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1233 phys_addr_t align, phys_addr_t start,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001234 phys_addr_t end, int nid,
1235 enum memblock_flags flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001236{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001237 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001238
Grygorii Strashko79f40fa2014-01-21 15:50:12 -08001239 if (!align)
1240 align = SMP_CACHE_BYTES;
Vineet Gupta94f3d3a2013-04-29 15:06:15 -07001241
Tony Luckfc6daaf2015-06-24 16:58:09 -07001242 found = memblock_find_in_range_node(size, align, start, end, nid,
1243 flags);
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001244 if (found && !memblock_reserve(found, size)) {
1245 /*
1246 * The min_count is set to 0 so that memblock allocations are
1247 * never reported as leaks.
1248 */
Catalin Marinas9099dae2016-10-11 13:55:11 -07001249 kmemleak_alloc_phys(found, size, 0, 0);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001250 return found;
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001251 }
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001252 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001253}
1254
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001255phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001256 phys_addr_t start, phys_addr_t end,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001257 enum memblock_flags flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001258{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001259 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1260 flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001261}
1262
Nicholas Pigginb575454f2018-02-14 01:08:15 +10001263phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001264 phys_addr_t align, phys_addr_t max_addr,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001265 int nid, enum memblock_flags flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001266{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001267 return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001268}
1269
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001270phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
1271{
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001272 enum memblock_flags flags = choose_memblock_flags();
Tony Lucka3f5baf2015-06-24 16:58:12 -07001273 phys_addr_t ret;
1274
1275again:
1276 ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE,
1277 nid, flags);
1278
1279 if (!ret && (flags & MEMBLOCK_MIRROR)) {
1280 flags &= ~MEMBLOCK_MIRROR;
1281 goto again;
1282 }
1283 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001284}
1285
1286phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1287{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001288 return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE,
1289 MEMBLOCK_NONE);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001290}
1291
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001292phys_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 +10001293{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001294 phys_addr_t alloc;
1295
1296 alloc = __memblock_alloc_base(size, align, max_addr);
1297
1298 if (alloc == 0)
Miles Chen5d63f812017-02-22 15:46:42 -08001299 panic("ERROR: Failed to allocate %pa bytes below %pa.\n",
1300 &size, &max_addr);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001301
1302 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001303}
1304
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001305phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001306{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001307 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001308}
1309
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001310phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1311{
1312 phys_addr_t res = memblock_alloc_nid(size, align, nid);
1313
1314 if (res)
1315 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +02001316 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001317}
1318
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001319/**
1320 * memblock_virt_alloc_internal - allocate boot memory block
1321 * @size: size of memory block to be allocated in bytes
1322 * @align: alignment of the region and block's size
1323 * @min_addr: the lower bound of the memory region to allocate (phys address)
1324 * @max_addr: the upper bound of the memory region to allocate (phys address)
1325 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1326 *
1327 * The @min_addr limit is dropped if it can not be satisfied and the allocation
1328 * will fall back to memory below @min_addr. Also, allocation may fall back
1329 * to any node in the system if the specified node can not
1330 * hold the requested memory.
1331 *
1332 * The allocation is performed from memory region limited by
1333 * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE.
1334 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001335 * The memory block is aligned on %SMP_CACHE_BYTES if @align == 0.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001336 *
1337 * The phys address of allocated boot memory block is converted to virtual and
1338 * allocated memory is reset to 0.
1339 *
1340 * In addition, function sets the min_count to 0 using kmemleak_alloc for
1341 * allocated boot memory block, so that it is never reported as leaks.
1342 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001343 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001344 * Virtual address of allocated memory block on success, NULL on failure.
1345 */
1346static void * __init memblock_virt_alloc_internal(
1347 phys_addr_t size, phys_addr_t align,
1348 phys_addr_t min_addr, phys_addr_t max_addr,
1349 int nid)
1350{
1351 phys_addr_t alloc;
1352 void *ptr;
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001353 enum memblock_flags flags = choose_memblock_flags();
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001354
Grygorii Strashko560dca272014-01-21 15:50:55 -08001355 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1356 nid = NUMA_NO_NODE;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001357
1358 /*
1359 * Detect any accidental use of these APIs after slab is ready, as at
1360 * this moment memblock may be deinitialized already and its
1361 * internal data may be destroyed (after execution of free_all_bootmem)
1362 */
1363 if (WARN_ON_ONCE(slab_is_available()))
1364 return kzalloc_node(size, GFP_NOWAIT, nid);
1365
1366 if (!align)
1367 align = SMP_CACHE_BYTES;
1368
Yinghai Luf544e142014-01-29 14:05:52 -08001369 if (max_addr > memblock.current_limit)
1370 max_addr = memblock.current_limit;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001371again:
1372 alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001373 nid, flags);
Wei Yang7d41c032017-02-22 15:45:07 -08001374 if (alloc && !memblock_reserve(alloc, size))
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001375 goto done;
1376
1377 if (nid != NUMA_NO_NODE) {
1378 alloc = memblock_find_in_range_node(size, align, min_addr,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001379 max_addr, NUMA_NO_NODE,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001380 flags);
Wei Yang7d41c032017-02-22 15:45:07 -08001381 if (alloc && !memblock_reserve(alloc, size))
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001382 goto done;
1383 }
1384
1385 if (min_addr) {
1386 min_addr = 0;
1387 goto again;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001388 }
1389
Tony Lucka3f5baf2015-06-24 16:58:12 -07001390 if (flags & MEMBLOCK_MIRROR) {
1391 flags &= ~MEMBLOCK_MIRROR;
1392 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1393 &size);
1394 goto again;
1395 }
1396
1397 return NULL;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001398done:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001399 ptr = phys_to_virt(alloc);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001400
1401 /*
1402 * The min_count is set to 0 so that bootmem allocated blocks
1403 * are never reported as leaks. This is because many of these blocks
1404 * are only referred via the physical address which is not
1405 * looked up by kmemleak.
1406 */
1407 kmemleak_alloc(ptr, size, 0, 0);
1408
1409 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001410}
1411
1412/**
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001413 * memblock_virt_alloc_try_nid_raw - allocate boot memory block without zeroing
1414 * memory and without panicking
1415 * @size: size of memory block to be allocated in bytes
1416 * @align: alignment of the region and block's size
1417 * @min_addr: the lower bound of the memory region from where the allocation
1418 * is preferred (phys address)
1419 * @max_addr: the upper bound of the memory region from where the allocation
1420 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1421 * allocate only from memory limited by memblock.current_limit value
1422 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1423 *
1424 * Public function, provides additional debug information (including caller
1425 * info), if enabled. Does not zero allocated memory, does not panic if request
1426 * cannot be satisfied.
1427 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001428 * Return:
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001429 * Virtual address of allocated memory block on success, NULL on failure.
1430 */
1431void * __init memblock_virt_alloc_try_nid_raw(
1432 phys_addr_t size, phys_addr_t align,
1433 phys_addr_t min_addr, phys_addr_t max_addr,
1434 int nid)
1435{
1436 void *ptr;
1437
1438 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1439 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1440 (u64)max_addr, (void *)_RET_IP_);
1441
1442 ptr = memblock_virt_alloc_internal(size, align,
1443 min_addr, max_addr, nid);
1444#ifdef CONFIG_DEBUG_VM
1445 if (ptr && size > 0)
Pavel Tatashinf165b372018-04-05 16:22:47 -07001446 memset(ptr, PAGE_POISON_PATTERN, size);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001447#endif
1448 return ptr;
1449}
1450
1451/**
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001452 * memblock_virt_alloc_try_nid_nopanic - allocate boot memory block
1453 * @size: size of memory block to be allocated in bytes
1454 * @align: alignment of the region and block's size
1455 * @min_addr: the lower bound of the memory region from where the allocation
1456 * is preferred (phys address)
1457 * @max_addr: the upper bound of the memory region from where the allocation
1458 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1459 * allocate only from memory limited by memblock.current_limit value
1460 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1461 *
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001462 * Public function, provides additional debug information (including caller
1463 * info), if enabled. This function zeroes the allocated memory.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001464 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001465 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001466 * Virtual address of allocated memory block on success, NULL on failure.
1467 */
1468void * __init memblock_virt_alloc_try_nid_nopanic(
1469 phys_addr_t size, phys_addr_t align,
1470 phys_addr_t min_addr, phys_addr_t max_addr,
1471 int nid)
1472{
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001473 void *ptr;
1474
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001475 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1476 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1477 (u64)max_addr, (void *)_RET_IP_);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001478
1479 ptr = memblock_virt_alloc_internal(size, align,
1480 min_addr, max_addr, nid);
1481 if (ptr)
1482 memset(ptr, 0, size);
1483 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001484}
1485
1486/**
1487 * memblock_virt_alloc_try_nid - allocate boot memory block with panicking
1488 * @size: size of memory block to be allocated in bytes
1489 * @align: alignment of the region and block's size
1490 * @min_addr: the lower bound of the memory region from where the allocation
1491 * is preferred (phys address)
1492 * @max_addr: the upper bound of the memory region from where the allocation
1493 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1494 * allocate only from memory limited by memblock.current_limit value
1495 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1496 *
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001497 * Public panicking version of memblock_virt_alloc_try_nid_nopanic()
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001498 * which provides debug information (including caller info), if enabled,
1499 * and panics if the request can not be satisfied.
1500 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001501 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001502 * Virtual address of allocated memory block on success, NULL on failure.
1503 */
1504void * __init memblock_virt_alloc_try_nid(
1505 phys_addr_t size, phys_addr_t align,
1506 phys_addr_t min_addr, phys_addr_t max_addr,
1507 int nid)
1508{
1509 void *ptr;
1510
1511 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1512 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1513 (u64)max_addr, (void *)_RET_IP_);
1514 ptr = memblock_virt_alloc_internal(size, align,
1515 min_addr, max_addr, nid);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001516 if (ptr) {
1517 memset(ptr, 0, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001518 return ptr;
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001519 }
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001520
1521 panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx\n",
1522 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1523 (u64)max_addr);
1524 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 *
1532 * Free boot memory block previously allocated by memblock_virt_alloc_xx() API.
1533 * 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{
1537 memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1538 __func__, (u64)base, (u64)base + size - 1,
1539 (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001540 kmemleak_free_part_phys(base, size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001541 memblock_remove_range(&memblock.reserved, base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001542}
1543
Mike Rapoport48a833c2018-06-30 17:55:03 +03001544/**
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001545 * __memblock_free_late - free bootmem block pages directly to buddy allocator
Mike Rapoport48a833c2018-06-30 17:55:03 +03001546 * @base: phys starting address of the boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001547 * @size: size of the boot memory block in bytes
1548 *
1549 * This is only useful when the bootmem allocator has already been torn
1550 * down, but we are still initializing the system. Pages are released directly
1551 * to the buddy allocator, no bootmem metadata is updated because it is gone.
1552 */
1553void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1554{
1555 u64 cursor, end;
1556
1557 memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1558 __func__, (u64)base, (u64)base + size - 1,
1559 (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001560 kmemleak_free_part_phys(base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001561 cursor = PFN_UP(base);
1562 end = PFN_DOWN(base + size);
1563
1564 for (; cursor < end; cursor++) {
Mel Gormand70ddd72015-06-30 14:56:52 -07001565 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001566 totalram_pages++;
1567 }
1568}
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001569
1570/*
1571 * Remaining API functions
1572 */
1573
David Gibson1f1ffb8a2016-02-05 15:36:19 -08001574phys_addr_t __init_memblock memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001575{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001576 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001577}
1578
Srikar Dronamraju8907de52016-10-07 16:59:18 -07001579phys_addr_t __init_memblock memblock_reserved_size(void)
1580{
1581 return memblock.reserved.total_size;
1582}
1583
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001584phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1585{
1586 unsigned long pages = 0;
1587 struct memblock_region *r;
1588 unsigned long start_pfn, end_pfn;
1589
1590 for_each_memblock(memory, r) {
1591 start_pfn = memblock_region_memory_base_pfn(r);
1592 end_pfn = memblock_region_memory_end_pfn(r);
1593 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1594 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1595 pages += end_pfn - start_pfn;
1596 }
1597
Fabian Frederick16763232014-04-07 15:37:53 -07001598 return PFN_PHYS(pages);
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001599}
1600
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001601/* lowest address */
1602phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1603{
1604 return memblock.memory.regions[0].base;
1605}
1606
Yinghai Lu10d06432010-07-28 15:43:02 +10001607phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001608{
1609 int idx = memblock.memory.cnt - 1;
1610
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001611 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001612}
1613
Dennis Chena571d4e2016-07-28 15:48:26 -07001614static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001615{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001616 phys_addr_t max_addr = PHYS_ADDR_MAX;
Emil Medve136199f2014-04-07 15:37:52 -07001617 struct memblock_region *r;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001618
Dennis Chena571d4e2016-07-28 15:48:26 -07001619 /*
1620 * translate the memory @limit size into the max address within one of
1621 * the memory memblock regions, if the @limit exceeds the total size
Stefan Agner1c4bc432018-06-07 17:06:15 -07001622 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
Dennis Chena571d4e2016-07-28 15:48:26 -07001623 */
Emil Medve136199f2014-04-07 15:37:52 -07001624 for_each_memblock(memory, r) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001625 if (limit <= r->size) {
1626 max_addr = r->base + limit;
1627 break;
1628 }
1629 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001630 }
1631
Dennis Chena571d4e2016-07-28 15:48:26 -07001632 return max_addr;
1633}
1634
1635void __init memblock_enforce_memory_limit(phys_addr_t limit)
1636{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001637 phys_addr_t max_addr = PHYS_ADDR_MAX;
Dennis Chena571d4e2016-07-28 15:48:26 -07001638
1639 if (!limit)
1640 return;
1641
1642 max_addr = __find_max_addr(limit);
1643
1644 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001645 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001646 return;
1647
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001648 /* truncate both memory and reserved regions */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001649 memblock_remove_range(&memblock.memory, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001650 PHYS_ADDR_MAX);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001651 memblock_remove_range(&memblock.reserved, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001652 PHYS_ADDR_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001653}
1654
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001655void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1656{
1657 int start_rgn, end_rgn;
1658 int i, ret;
1659
1660 if (!size)
1661 return;
1662
1663 ret = memblock_isolate_range(&memblock.memory, base, size,
1664 &start_rgn, &end_rgn);
1665 if (ret)
1666 return;
1667
1668 /* remove all the MAP regions */
1669 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1670 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1671 memblock_remove_region(&memblock.memory, i);
1672
1673 for (i = start_rgn - 1; i >= 0; i--)
1674 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1675 memblock_remove_region(&memblock.memory, i);
1676
1677 /* truncate the reserved regions */
1678 memblock_remove_range(&memblock.reserved, 0, base);
1679 memblock_remove_range(&memblock.reserved,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001680 base + size, PHYS_ADDR_MAX);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001681}
1682
Dennis Chena571d4e2016-07-28 15:48:26 -07001683void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1684{
Dennis Chena571d4e2016-07-28 15:48:26 -07001685 phys_addr_t max_addr;
Dennis Chena571d4e2016-07-28 15:48:26 -07001686
1687 if (!limit)
1688 return;
1689
1690 max_addr = __find_max_addr(limit);
1691
1692 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001693 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001694 return;
1695
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001696 memblock_cap_memory_range(0, max_addr);
Dennis Chena571d4e2016-07-28 15:48:26 -07001697}
1698
Yinghai Lucd794812010-10-11 12:34:09 -07001699static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001700{
1701 unsigned int left = 0, right = type->cnt;
1702
1703 do {
1704 unsigned int mid = (right + left) / 2;
1705
1706 if (addr < type->regions[mid].base)
1707 right = mid;
1708 else if (addr >= (type->regions[mid].base +
1709 type->regions[mid].size))
1710 left = mid + 1;
1711 else
1712 return mid;
1713 } while (left < right);
1714 return -1;
1715}
1716
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001717bool __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001718{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001719 return memblock_search(&memblock.reserved, addr) != -1;
1720}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001721
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001722bool __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001723{
1724 return memblock_search(&memblock.memory, addr) != -1;
1725}
1726
Yaowei Bai937f0c22018-02-06 15:41:18 -08001727bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001728{
1729 int i = memblock_search(&memblock.memory, addr);
1730
1731 if (i == -1)
1732 return false;
1733 return !memblock_is_nomap(&memblock.memory.regions[i]);
1734}
1735
Yinghai Lue76b63f2013-09-11 14:22:17 -07001736#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1737int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1738 unsigned long *start_pfn, unsigned long *end_pfn)
1739{
1740 struct memblock_type *type = &memblock.memory;
Fabian Frederick16763232014-04-07 15:37:53 -07001741 int mid = memblock_search(type, PFN_PHYS(pfn));
Yinghai Lue76b63f2013-09-11 14:22:17 -07001742
1743 if (mid == -1)
1744 return -1;
1745
Fabian Frederickf7e2f7e2014-06-04 16:07:51 -07001746 *start_pfn = PFN_DOWN(type->regions[mid].base);
1747 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001748
1749 return type->regions[mid].nid;
1750}
1751#endif
1752
Stephen Boydeab30942012-05-24 00:45:21 -07001753/**
1754 * memblock_is_region_memory - check if a region is a subset of memory
1755 * @base: base of region to check
1756 * @size: size of region to check
1757 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001758 * Check if the region [@base, @base + @size) is a subset of a memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001759 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001760 * Return:
Stephen Boydeab30942012-05-24 00:45:21 -07001761 * 0 if false, non-zero if true
1762 */
Yaowei Bai937f0c22018-02-06 15:41:18 -08001763bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001764{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001765 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001766 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001767
1768 if (idx == -1)
Yaowei Bai937f0c22018-02-06 15:41:18 -08001769 return false;
Wei Yangef415ef2017-02-22 15:45:04 -08001770 return (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001771 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001772}
1773
Stephen Boydeab30942012-05-24 00:45:21 -07001774/**
1775 * memblock_is_region_reserved - check if a region intersects reserved memory
1776 * @base: base of region to check
1777 * @size: size of region to check
1778 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001779 * Check if the region [@base, @base + @size) intersects a reserved
1780 * memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001781 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001782 * Return:
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001783 * True if they intersect, false if not.
Stephen Boydeab30942012-05-24 00:45:21 -07001784 */
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001785bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001786{
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001787 memblock_cap_size(base, &size);
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001788 return memblock_overlaps_region(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001789}
1790
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001791void __init_memblock memblock_trim_memory(phys_addr_t align)
1792{
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001793 phys_addr_t start, end, orig_start, orig_end;
Emil Medve136199f2014-04-07 15:37:52 -07001794 struct memblock_region *r;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001795
Emil Medve136199f2014-04-07 15:37:52 -07001796 for_each_memblock(memory, r) {
1797 orig_start = r->base;
1798 orig_end = r->base + r->size;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001799 start = round_up(orig_start, align);
1800 end = round_down(orig_end, align);
1801
1802 if (start == orig_start && end == orig_end)
1803 continue;
1804
1805 if (start < end) {
Emil Medve136199f2014-04-07 15:37:52 -07001806 r->base = start;
1807 r->size = end - start;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001808 } else {
Emil Medve136199f2014-04-07 15:37:52 -07001809 memblock_remove_region(&memblock.memory,
1810 r - memblock.memory.regions);
1811 r--;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001812 }
1813 }
1814}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001815
Yinghai Lu3661ca62010-09-15 13:05:29 -07001816void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001817{
1818 memblock.current_limit = limit;
1819}
1820
Laura Abbottfec51012014-02-27 01:23:43 +01001821phys_addr_t __init_memblock memblock_get_current_limit(void)
1822{
1823 return memblock.current_limit;
1824}
1825
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001826static void __init_memblock memblock_dump(struct memblock_type *type)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001827{
Miles Chen5d63f812017-02-22 15:46:42 -08001828 phys_addr_t base, end, size;
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001829 enum memblock_flags flags;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001830 int idx;
1831 struct memblock_region *rgn;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001832
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001833 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001834
Gioh Kim66e8b432017-11-15 17:33:42 -08001835 for_each_memblock_type(idx, type, rgn) {
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001836 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001837
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001838 base = rgn->base;
1839 size = rgn->size;
Miles Chen5d63f812017-02-22 15:46:42 -08001840 end = base + size - 1;
Tang Chen66a20752014-01-21 15:49:20 -08001841 flags = rgn->flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001842#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1843 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1844 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1845 memblock_get_region_node(rgn));
1846#endif
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001847 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001848 type->name, idx, &base, &end, &size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001849 }
1850}
1851
Tejun Heo4ff7b822011-12-08 10:22:06 -08001852void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001853{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001854 pr_info("MEMBLOCK configuration:\n");
Miles Chen5d63f812017-02-22 15:46:42 -08001855 pr_info(" memory size = %pa reserved size = %pa\n",
1856 &memblock.memory.total_size,
1857 &memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001858
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001859 memblock_dump(&memblock.memory);
1860 memblock_dump(&memblock.reserved);
Heiko Carstens409efd42017-02-24 14:55:56 -08001861#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001862 memblock_dump(&memblock.physmem);
Heiko Carstens409efd42017-02-24 14:55:56 -08001863#endif
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001864}
1865
Tejun Heo1aadc052011-12-08 10:22:08 -08001866void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001867{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001868 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001869}
1870
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001871static int __init early_memblock(char *p)
1872{
1873 if (p && strstr(p, "debug"))
1874 memblock_debug = 1;
1875 return 0;
1876}
1877early_param("memblock", early_memblock);
1878
Tejun Heoc378ddd2011-07-14 11:46:03 +02001879#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001880
1881static int memblock_debug_show(struct seq_file *m, void *private)
1882{
1883 struct memblock_type *type = m->private;
1884 struct memblock_region *reg;
1885 int i;
Miles Chen5d63f812017-02-22 15:46:42 -08001886 phys_addr_t end;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001887
1888 for (i = 0; i < type->cnt; i++) {
1889 reg = &type->regions[i];
Miles Chen5d63f812017-02-22 15:46:42 -08001890 end = reg->base + reg->size - 1;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001891
Miles Chen5d63f812017-02-22 15:46:42 -08001892 seq_printf(m, "%4d: ", i);
1893 seq_printf(m, "%pa..%pa\n", &reg->base, &end);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001894 }
1895 return 0;
1896}
Andy Shevchenko5ad35092018-04-05 16:23:16 -07001897DEFINE_SHOW_ATTRIBUTE(memblock_debug);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001898
1899static int __init memblock_init_debugfs(void)
1900{
1901 struct dentry *root = debugfs_create_dir("memblock", NULL);
1902 if (!root)
1903 return -ENXIO;
Joe Perches0825a6f2018-06-14 15:27:58 -07001904 debugfs_create_file("memory", 0444, root,
1905 &memblock.memory, &memblock_debug_fops);
1906 debugfs_create_file("reserved", 0444, root,
1907 &memblock.reserved, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001908#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
Joe Perches0825a6f2018-06-14 15:27:58 -07001909 debugfs_create_file("physmem", 0444, root,
1910 &memblock.physmem, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001911#endif
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001912
1913 return 0;
1914}
1915__initcall(memblock_init_debugfs);
1916
1917#endif /* CONFIG_DEBUG_FS */