blob: 068e147695eeedb7a50cd63a3198d2d2cfba97a7 [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
Ard Biesheuvel8a5b403d2019-02-15 13:33:32 +010029#define INIT_MEMBLOCK_REGIONS 128
30#define INIT_PHYSMEM_REGIONS 4
31
32#ifndef INIT_MEMBLOCK_RESERVED_REGIONS
33# define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
34#endif
35
Mike Rapoport3e039c52018-06-30 17:55:05 +030036/**
37 * DOC: memblock overview
38 *
39 * Memblock is a method of managing memory regions during the early
40 * boot period when the usual kernel memory allocators are not up and
41 * running.
42 *
43 * Memblock views the system memory as collections of contiguous
44 * regions. There are several types of these collections:
45 *
46 * * ``memory`` - describes the physical memory available to the
47 * kernel; this may differ from the actual physical memory installed
48 * in the system, for instance when the memory is restricted with
49 * ``mem=`` command line parameter
50 * * ``reserved`` - describes the regions that were allocated
51 * * ``physmap`` - describes the actual physical memory regardless of
52 * the possible restrictions; the ``physmap`` type is only available
53 * on some architectures.
54 *
55 * Each region is represented by :c:type:`struct memblock_region` that
56 * defines the region extents, its attributes and NUMA node id on NUMA
57 * systems. Every memory type is described by the :c:type:`struct
58 * memblock_type` which contains an array of memory regions along with
59 * the allocator metadata. The memory types are nicely wrapped with
60 * :c:type:`struct memblock`. This structure is statically initialzed
61 * at build time. The region arrays for the "memory" and "reserved"
62 * types are initially sized to %INIT_MEMBLOCK_REGIONS and for the
63 * "physmap" type to %INIT_PHYSMEM_REGIONS.
64 * The :c:func:`memblock_allow_resize` enables automatic resizing of
65 * the region arrays during addition of new regions. This feature
66 * should be used with care so that memory allocated for the region
67 * array will not overlap with areas that should be reserved, for
68 * example initrd.
69 *
70 * The early architecture setup should tell memblock what the physical
71 * memory layout is by using :c:func:`memblock_add` or
72 * :c:func:`memblock_add_node` functions. The first function does not
73 * assign the region to a NUMA node and it is appropriate for UMA
74 * systems. Yet, it is possible to use it on NUMA systems as well and
75 * assign the region to a NUMA node later in the setup process using
76 * :c:func:`memblock_set_node`. The :c:func:`memblock_add_node`
77 * performs such an assignment directly.
78 *
79 * Once memblock is setup the memory can be allocated using either
80 * memblock or bootmem APIs.
81 *
82 * As the system boot progresses, the architecture specific
83 * :c:func:`mem_init` function frees all the memory to the buddy page
84 * allocator.
85 *
86 * If an architecure enables %CONFIG_ARCH_DISCARD_MEMBLOCK, the
87 * memblock data structures will be discarded after the system
88 * initialization compltes.
89 */
90
Mike Rapoportbda49a82018-10-30 15:09:40 -070091#ifndef CONFIG_NEED_MULTIPLE_NODES
92struct pglist_data __refdata contig_page_data;
93EXPORT_SYMBOL(contig_page_data);
94#endif
95
96unsigned long max_low_pfn;
97unsigned long min_low_pfn;
98unsigned long max_pfn;
99unsigned long long max_possible_pfn;
100
Tejun Heofe091c22011-12-08 10:22:07 -0800101static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
Ard Biesheuvel8a5b403d2019-02-15 13:33:32 +0100102static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +0100103#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
104static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
105#endif
Tejun Heofe091c22011-12-08 10:22:07 -0800106
107struct memblock memblock __initdata_memblock = {
108 .memory.regions = memblock_memory_init_regions,
109 .memory.cnt = 1, /* empty dummy entry */
110 .memory.max = INIT_MEMBLOCK_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800111 .memory.name = "memory",
Tejun Heofe091c22011-12-08 10:22:07 -0800112
113 .reserved.regions = memblock_reserved_init_regions,
114 .reserved.cnt = 1, /* empty dummy entry */
Ard Biesheuvel8a5b403d2019-02-15 13:33:32 +0100115 .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800116 .reserved.name = "reserved",
Tejun Heofe091c22011-12-08 10:22:07 -0800117
Philipp Hachtmann70210ed2014-01-29 18:16:01 +0100118#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
119 .physmem.regions = memblock_physmem_init_regions,
120 .physmem.cnt = 1, /* empty dummy entry */
121 .physmem.max = INIT_PHYSMEM_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800122 .physmem.name = "physmem",
Philipp Hachtmann70210ed2014-01-29 18:16:01 +0100123#endif
124
Tang Chen79442ed2013-11-12 15:07:59 -0800125 .bottom_up = false,
Tejun Heofe091c22011-12-08 10:22:07 -0800126 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
127};
Yinghai Lu95f72d12010-07-12 14:36:09 +1000128
Yinghai Lu10d06432010-07-28 15:43:02 +1000129int memblock_debug __initdata_memblock;
Tony Lucka3f5baf2015-06-24 16:58:12 -0700130static bool system_has_some_mirror __initdata_memblock = false;
Tejun Heo1aadc052011-12-08 10:22:08 -0800131static int memblock_can_resize __initdata_memblock;
Gavin Shan181eb392012-05-29 15:06:50 -0700132static int memblock_memory_in_slab __initdata_memblock = 0;
133static int memblock_reserved_in_slab __initdata_memblock = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000134
Mike Rapoportc366ea82019-03-11 23:29:46 -0700135static enum memblock_flags __init_memblock choose_memblock_flags(void)
Tony Lucka3f5baf2015-06-24 16:58:12 -0700136{
137 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
138}
139
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800140/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
141static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
142{
Stefan Agner1c4bc432018-06-07 17:06:15 -0700143 return *size = min(*size, PHYS_ADDR_MAX - base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800144}
145
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000146/*
147 * Address comparison utilities
148 */
Yinghai Lu10d06432010-07-28 15:43:02 +1000149static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000150 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000151{
152 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
153}
154
Tang Chen95cf82e2015-09-08 15:02:03 -0700155bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -0700156 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000157{
158 unsigned long i;
159
Alexander Kuleshovf14516f2016-01-14 15:20:39 -0800160 for (i = 0; i < type->cnt; i++)
161 if (memblock_addrs_overlap(base, size, type->regions[i].base,
162 type->regions[i].size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000163 break;
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700164 return i < type->cnt;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000165}
166
Mike Rapoport47cec442018-06-30 17:55:02 +0300167/**
Tang Chen79442ed2013-11-12 15:07:59 -0800168 * __memblock_find_range_bottom_up - find free area utility in bottom-up
169 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300170 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
171 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen79442ed2013-11-12 15:07:59 -0800172 * @size: size of free area to find
173 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800174 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700175 * @flags: pick from blocks based on memory attributes
Tang Chen79442ed2013-11-12 15:07:59 -0800176 *
177 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
178 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300179 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800180 * Found address on success, 0 on failure.
181 */
182static phys_addr_t __init_memblock
183__memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700184 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300185 enum memblock_flags flags)
Tang Chen79442ed2013-11-12 15:07:59 -0800186{
187 phys_addr_t this_start, this_end, cand;
188 u64 i;
189
Tony Luckfc6daaf2015-06-24 16:58:09 -0700190 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
Tang Chen79442ed2013-11-12 15:07:59 -0800191 this_start = clamp(this_start, start, end);
192 this_end = clamp(this_end, start, end);
193
194 cand = round_up(this_start, align);
195 if (cand < this_end && this_end - cand >= size)
196 return cand;
197 }
198
199 return 0;
200}
201
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800202/**
Tang Chen14028992013-11-12 15:07:57 -0800203 * __memblock_find_range_top_down - find free area utility, in top-down
204 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300205 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
206 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen14028992013-11-12 15:07:57 -0800207 * @size: size of free area to find
208 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800209 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700210 * @flags: pick from blocks based on memory attributes
Tang Chen14028992013-11-12 15:07:57 -0800211 *
212 * Utility called from memblock_find_in_range_node(), find free area top-down.
213 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300214 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800215 * Found address on success, 0 on failure.
Tang Chen14028992013-11-12 15:07:57 -0800216 */
217static phys_addr_t __init_memblock
218__memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700219 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300220 enum memblock_flags flags)
Tang Chen14028992013-11-12 15:07:57 -0800221{
222 phys_addr_t this_start, this_end, cand;
223 u64 i;
224
Tony Luckfc6daaf2015-06-24 16:58:09 -0700225 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
226 NULL) {
Tang Chen14028992013-11-12 15:07:57 -0800227 this_start = clamp(this_start, start, end);
228 this_end = clamp(this_end, start, end);
229
230 if (this_end < size)
231 continue;
232
233 cand = round_down(this_end - size, align);
234 if (cand >= this_start)
235 return cand;
236 }
237
238 return 0;
239}
240
241/**
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800242 * memblock_find_in_range_node - find free area in given range and node
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800243 * @size: size of free area to find
244 * @align: alignment of free area to find
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800245 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300246 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
247 * %MEMBLOCK_ALLOC_ACCESSIBLE
Grygorii Strashkob1154232014-01-21 15:50:16 -0800248 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700249 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800250 *
251 * Find @size free area aligned to @align in the specified range and node.
252 *
Tang Chen79442ed2013-11-12 15:07:59 -0800253 * When allocation direction is bottom-up, the @start should be greater
254 * than the end of the kernel image. Otherwise, it will be trimmed. The
255 * reason is that we want the bottom-up allocation just near the kernel
256 * image so it is highly likely that the allocated memory and the kernel
257 * will reside in the same node.
258 *
259 * If bottom-up allocation failed, will try to allocate memory top-down.
260 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300261 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800262 * Found address on success, 0 on failure.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000263 */
Mike Rapoportc366ea82019-03-11 23:29:46 -0700264static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800265 phys_addr_t align, phys_addr_t start,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300266 phys_addr_t end, int nid,
267 enum memblock_flags flags)
Tang Chenf7210e62013-02-22 16:33:51 -0800268{
Tang Chen0cfb8f02014-08-29 15:18:31 -0700269 phys_addr_t kernel_end, ret;
Tang Chen79442ed2013-11-12 15:07:59 -0800270
Tang Chenf7210e62013-02-22 16:33:51 -0800271 /* pump up @end */
Qian Caifed84c72018-12-28 00:36:29 -0800272 if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
273 end == MEMBLOCK_ALLOC_KASAN)
Tang Chenf7210e62013-02-22 16:33:51 -0800274 end = memblock.current_limit;
275
276 /* avoid allocating the first page */
277 start = max_t(phys_addr_t, start, PAGE_SIZE);
278 end = max(start, end);
Tang Chen79442ed2013-11-12 15:07:59 -0800279 kernel_end = __pa_symbol(_end);
280
281 /*
282 * try bottom-up allocation only when bottom-up mode
283 * is set and @end is above the kernel image.
284 */
285 if (memblock_bottom_up() && end > kernel_end) {
286 phys_addr_t bottom_up_start;
287
288 /* make sure we will allocate above the kernel */
289 bottom_up_start = max(start, kernel_end);
290
291 /* ok, try bottom-up allocation first */
292 ret = __memblock_find_range_bottom_up(bottom_up_start, end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700293 size, align, nid, flags);
Tang Chen79442ed2013-11-12 15:07:59 -0800294 if (ret)
295 return ret;
296
297 /*
298 * we always limit bottom-up allocation above the kernel,
299 * but top-down allocation doesn't have the limit, so
300 * retrying top-down allocation may succeed when bottom-up
301 * allocation failed.
302 *
303 * bottom-up allocation is expected to be fail very rarely,
304 * so we use WARN_ONCE() here to see the stack trace if
305 * fail happens.
306 */
Michal Hockoe3d301c2018-07-13 16:59:16 -0700307 WARN_ONCE(IS_ENABLED(CONFIG_MEMORY_HOTREMOVE),
308 "memblock: bottom-up allocation failed, memory hotremove may be affected\n");
Tang Chen79442ed2013-11-12 15:07:59 -0800309 }
Tang Chenf7210e62013-02-22 16:33:51 -0800310
Tony Luckfc6daaf2015-06-24 16:58:09 -0700311 return __memblock_find_range_top_down(start, end, size, align, nid,
312 flags);
Tang Chenf7210e62013-02-22 16:33:51 -0800313}
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000314
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800315/**
316 * memblock_find_in_range - find free area in given range
317 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300318 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
319 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800320 * @size: size of free area to find
321 * @align: alignment of free area to find
322 *
323 * Find @size free area aligned to @align in the specified range.
324 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300325 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800326 * Found address on success, 0 on failure.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800327 */
328phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
329 phys_addr_t end, phys_addr_t size,
330 phys_addr_t align)
331{
Tony Lucka3f5baf2015-06-24 16:58:12 -0700332 phys_addr_t ret;
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300333 enum memblock_flags flags = choose_memblock_flags();
Tony Lucka3f5baf2015-06-24 16:58:12 -0700334
335again:
336 ret = memblock_find_in_range_node(size, align, start, end,
337 NUMA_NO_NODE, flags);
338
339 if (!ret && (flags & MEMBLOCK_MIRROR)) {
340 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
341 &size);
342 flags &= ~MEMBLOCK_MIRROR;
343 goto again;
344 }
345
346 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800347}
348
Yinghai Lu10d06432010-07-28 15:43:02 +1000349static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000350{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800351 type->total_size -= type->regions[r].size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200352 memmove(&type->regions[r], &type->regions[r + 1],
353 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000354 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000355
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700356 /* Special case for empty arrays */
357 if (type->cnt == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800358 WARN_ON(type->total_size != 0);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700359 type->cnt = 1;
360 type->regions[0].base = 0;
361 type->regions[0].size = 0;
Tang Chen66a20752014-01-21 15:49:20 -0800362 type->regions[0].flags = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200363 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700364 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000365}
366
Philipp Hachtmann354f17e2014-01-23 15:53:24 -0800367#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
Pavel Tatashin3010f872017-08-18 15:16:05 -0700368/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300369 * memblock_discard - discard memory and reserved arrays if they were allocated
Pavel Tatashin3010f872017-08-18 15:16:05 -0700370 */
371void __init memblock_discard(void)
Yinghai Lu29f67382012-07-11 14:02:56 -0700372{
Pavel Tatashin3010f872017-08-18 15:16:05 -0700373 phys_addr_t addr, size;
Yinghai Lu29f67382012-07-11 14:02:56 -0700374
Pavel Tatashin3010f872017-08-18 15:16:05 -0700375 if (memblock.reserved.regions != memblock_reserved_init_regions) {
376 addr = __pa(memblock.reserved.regions);
377 size = PAGE_ALIGN(sizeof(struct memblock_region) *
378 memblock.reserved.max);
379 __memblock_free_late(addr, size);
380 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700381
Pavel Tatashin91b540f92017-08-25 15:55:46 -0700382 if (memblock.memory.regions != memblock_memory_init_regions) {
Pavel Tatashin3010f872017-08-18 15:16:05 -0700383 addr = __pa(memblock.memory.regions);
384 size = PAGE_ALIGN(sizeof(struct memblock_region) *
385 memblock.memory.max);
386 __memblock_free_late(addr, size);
387 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700388}
Philipp Hachtmann5e270e22014-01-23 15:53:11 -0800389#endif
390
Greg Pearson48c3b582012-06-20 12:53:05 -0700391/**
392 * memblock_double_array - double the size of the memblock regions array
393 * @type: memblock type of the regions array being doubled
394 * @new_area_start: starting address of memory range to avoid overlap with
395 * @new_area_size: size of memory range to avoid overlap with
396 *
397 * Double the size of the @type regions array. If memblock is being used to
398 * allocate memory for a new reserved regions array and there is a previously
Mike Rapoport47cec442018-06-30 17:55:02 +0300399 * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
Greg Pearson48c3b582012-06-20 12:53:05 -0700400 * waiting to be reserved, ensure the memory used by the new array does
401 * not overlap.
402 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300403 * Return:
Greg Pearson48c3b582012-06-20 12:53:05 -0700404 * 0 on success, -1 on failure.
405 */
406static int __init_memblock memblock_double_array(struct memblock_type *type,
407 phys_addr_t new_area_start,
408 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700409{
410 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700411 phys_addr_t old_alloc_size, new_alloc_size;
Mike Rapoporta36aab82018-08-17 15:47:17 -0700412 phys_addr_t old_size, new_size, addr, new_end;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700413 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700414 int *in_slab;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700415
416 /* We don't allow resizing until we know about the reserved regions
417 * of memory that aren't suitable for allocation
418 */
419 if (!memblock_can_resize)
420 return -1;
421
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700422 /* Calculate new doubled size */
423 old_size = type->max * sizeof(struct memblock_region);
424 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700425 /*
426 * We need to allocated new one align to PAGE_SIZE,
427 * so we can free them completely later.
428 */
429 old_alloc_size = PAGE_ALIGN(old_size);
430 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700431
Gavin Shan181eb392012-05-29 15:06:50 -0700432 /* Retrieve the slab flag */
433 if (type == &memblock.memory)
434 in_slab = &memblock_memory_in_slab;
435 else
436 in_slab = &memblock_reserved_in_slab;
437
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700438 /* Try to find some space for it.
439 *
440 * WARNING: We assume that either slab_is_available() and we use it or
Andrew Mortonfd073832012-07-31 16:42:40 -0700441 * we use MEMBLOCK for allocations. That means that this is unsafe to
442 * use when bootmem is currently active (unless bootmem itself is
443 * implemented on top of MEMBLOCK which isn't the case yet)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700444 *
445 * This should however not be an issue for now, as we currently only
Andrew Mortonfd073832012-07-31 16:42:40 -0700446 * call into MEMBLOCK while it's still active, or much later when slab
447 * is active for memory hotplug operations
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700448 */
449 if (use_slab) {
450 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200451 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700452 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700453 /* only exclude range when trying to double reserved.regions */
454 if (type != &memblock.reserved)
455 new_area_start = new_area_size = 0;
456
457 addr = memblock_find_in_range(new_area_start + new_area_size,
458 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700459 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700460 if (!addr && new_area_size)
461 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700462 min(new_area_start, memblock.current_limit),
463 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700464
Sachin Kamat15674862012-09-04 13:55:05 +0530465 new_array = addr ? __va(addr) : NULL;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700466 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200467 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700468 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800469 type->name, type->max, type->max * 2);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700470 return -1;
471 }
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700472
Mike Rapoporta36aab82018-08-17 15:47:17 -0700473 new_end = addr + new_size - 1;
474 memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
475 type->name, type->max * 2, &addr, &new_end);
Yinghai Luea9e4372010-07-28 15:13:22 +1000476
Andrew Mortonfd073832012-07-31 16:42:40 -0700477 /*
478 * Found space, we now need to move the array over before we add the
479 * reserved region since it may be our reserved array itself that is
480 * full.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700481 */
482 memcpy(new_array, type->regions, old_size);
483 memset(new_array + type->max, 0, old_size);
484 old_array = type->regions;
485 type->regions = new_array;
486 type->max <<= 1;
487
Andrew Mortonfd073832012-07-31 16:42:40 -0700488 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700489 if (*in_slab)
490 kfree(old_array);
491 else if (old_array != memblock_memory_init_regions &&
492 old_array != memblock_reserved_init_regions)
Yinghai Lu29f67382012-07-11 14:02:56 -0700493 memblock_free(__pa(old_array), old_alloc_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700494
Andrew Mortonfd073832012-07-31 16:42:40 -0700495 /*
496 * Reserve the new array if that comes from the memblock. Otherwise, we
497 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700498 */
499 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700500 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700501
502 /* Update slab flag */
503 *in_slab = use_slab;
504
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700505 return 0;
506}
507
Tejun Heo784656f92011-07-12 11:15:55 +0200508/**
509 * memblock_merge_regions - merge neighboring compatible regions
510 * @type: memblock type to scan
511 *
512 * Scan @type and merge neighboring compatible regions.
513 */
514static void __init_memblock memblock_merge_regions(struct memblock_type *type)
515{
516 int i = 0;
517
518 /* cnt never goes below 1 */
519 while (i < type->cnt - 1) {
520 struct memblock_region *this = &type->regions[i];
521 struct memblock_region *next = &type->regions[i + 1];
522
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200523 if (this->base + this->size != next->base ||
524 memblock_get_region_node(this) !=
Tang Chen66a20752014-01-21 15:49:20 -0800525 memblock_get_region_node(next) ||
526 this->flags != next->flags) {
Tejun Heo784656f92011-07-12 11:15:55 +0200527 BUG_ON(this->base + this->size > next->base);
528 i++;
529 continue;
530 }
531
532 this->size += next->size;
Lin Fengc0232ae2013-01-11 14:31:44 -0800533 /* move forward from next + 1, index of which is i + 2 */
534 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
Tejun Heo784656f92011-07-12 11:15:55 +0200535 type->cnt--;
536 }
537}
538
539/**
540 * memblock_insert_region - insert new memblock region
Tang Chen209ff862013-04-29 15:08:41 -0700541 * @type: memblock type to insert into
542 * @idx: index for the insertion point
543 * @base: base address of the new region
544 * @size: size of the new region
545 * @nid: node id of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800546 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200547 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300548 * Insert new memblock region [@base, @base + @size) into @type at @idx.
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700549 * @type must already have extra room to accommodate the new region.
Tejun Heo784656f92011-07-12 11:15:55 +0200550 */
551static void __init_memblock memblock_insert_region(struct memblock_type *type,
552 int idx, phys_addr_t base,
Tang Chen66a20752014-01-21 15:49:20 -0800553 phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300554 int nid,
555 enum memblock_flags flags)
Tejun Heo784656f92011-07-12 11:15:55 +0200556{
557 struct memblock_region *rgn = &type->regions[idx];
558
559 BUG_ON(type->cnt >= type->max);
560 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
561 rgn->base = base;
562 rgn->size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800563 rgn->flags = flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200564 memblock_set_region_node(rgn, nid);
Tejun Heo784656f92011-07-12 11:15:55 +0200565 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800566 type->total_size += size;
Tejun Heo784656f92011-07-12 11:15:55 +0200567}
568
569/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100570 * memblock_add_range - add new memblock region
Tejun Heo784656f92011-07-12 11:15:55 +0200571 * @type: memblock type to add new region into
572 * @base: base address of the new region
573 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800574 * @nid: nid of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800575 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200576 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300577 * Add new memblock region [@base, @base + @size) into @type. The new region
Tejun Heo784656f92011-07-12 11:15:55 +0200578 * is allowed to overlap with existing ones - overlaps don't affect already
579 * existing regions. @type is guaranteed to be minimal (all neighbouring
580 * compatible regions are merged) after the addition.
581 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300582 * Return:
Tejun Heo784656f92011-07-12 11:15:55 +0200583 * 0 on success, -errno on failure.
584 */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100585int __init_memblock memblock_add_range(struct memblock_type *type,
Tang Chen66a20752014-01-21 15:49:20 -0800586 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300587 int nid, enum memblock_flags flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000588{
Tejun Heo784656f92011-07-12 11:15:55 +0200589 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800590 phys_addr_t obase = base;
591 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800592 int idx, nr_new;
593 struct memblock_region *rgn;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000594
Tejun Heob3dc6272012-04-20 08:31:34 -0700595 if (!size)
596 return 0;
597
Tejun Heo784656f92011-07-12 11:15:55 +0200598 /* special case for empty array */
599 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800600 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000601 type->regions[0].base = base;
602 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800603 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800604 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800605 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000606 return 0;
607 }
Tejun Heo784656f92011-07-12 11:15:55 +0200608repeat:
609 /*
610 * The following is executed twice. Once with %false @insert and
611 * then with %true. The first counts the number of regions needed
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700612 * to accommodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700613 */
Tejun Heo784656f92011-07-12 11:15:55 +0200614 base = obase;
615 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000616
Gioh Kim66e8b432017-11-15 17:33:42 -0800617 for_each_memblock_type(idx, type, rgn) {
Tejun Heo784656f92011-07-12 11:15:55 +0200618 phys_addr_t rbase = rgn->base;
619 phys_addr_t rend = rbase + rgn->size;
620
621 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000622 break;
Tejun Heo784656f92011-07-12 11:15:55 +0200623 if (rend <= base)
624 continue;
625 /*
626 * @rgn overlaps. If it separates the lower part of new
627 * area, insert that portion.
628 */
629 if (rbase > base) {
Wei Yangc0a29492015-09-04 15:47:38 -0700630#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
631 WARN_ON(nid != memblock_get_region_node(rgn));
632#endif
Wei Yang4fcab5f2015-09-08 14:59:53 -0700633 WARN_ON(flags != rgn->flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200634 nr_new++;
635 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800636 memblock_insert_region(type, idx++, base,
Tang Chen66a20752014-01-21 15:49:20 -0800637 rbase - base, nid,
638 flags);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000639 }
Tejun Heo784656f92011-07-12 11:15:55 +0200640 /* area below @rend is dealt with, forget about it */
641 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000642 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000643
Tejun Heo784656f92011-07-12 11:15:55 +0200644 /* insert the remaining portion */
645 if (base < end) {
646 nr_new++;
647 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800648 memblock_insert_region(type, idx, base, end - base,
Tang Chen66a20752014-01-21 15:49:20 -0800649 nid, flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200650 }
651
nimisoloef3cc4d2016-07-26 15:24:56 -0700652 if (!nr_new)
653 return 0;
654
Tejun Heo784656f92011-07-12 11:15:55 +0200655 /*
656 * If this was the first round, resize array and repeat for actual
657 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700658 */
Tejun Heo784656f92011-07-12 11:15:55 +0200659 if (!insert) {
660 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700661 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f92011-07-12 11:15:55 +0200662 return -ENOMEM;
663 insert = true;
664 goto repeat;
665 } else {
666 memblock_merge_regions(type);
667 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700668 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000669}
670
Mike Rapoport48a833c2018-06-30 17:55:03 +0300671/**
672 * memblock_add_node - add new memblock region within a NUMA node
673 * @base: base address of the new region
674 * @size: size of the new region
675 * @nid: nid of the new region
676 *
677 * Add new memblock region [@base, @base + @size) to the "memory"
678 * type. See memblock_add_range() description for mode details
679 *
680 * Return:
681 * 0 on success, -errno on failure.
682 */
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800683int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
684 int nid)
685{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100686 return memblock_add_range(&memblock.memory, base, size, nid, 0);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800687}
688
Mike Rapoport48a833c2018-06-30 17:55:03 +0300689/**
690 * memblock_add - add new memblock region
691 * @base: base address of the new region
692 * @size: size of the new region
693 *
694 * Add new memblock region [@base, @base + @size) to the "memory"
695 * type. See memblock_add_range() description for mode details
696 *
697 * Return:
698 * 0 on success, -errno on failure.
699 */
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700700int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700701{
Miles Chen5d63f812017-02-22 15:46:42 -0800702 phys_addr_t end = base + size - 1;
703
704 memblock_dbg("memblock_add: [%pa-%pa] %pF\n",
705 &base, &end, (void *)_RET_IP_);
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700706
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700707 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000708}
709
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800710/**
711 * memblock_isolate_range - isolate given range into disjoint memblocks
712 * @type: memblock type to isolate range for
713 * @base: base of range to isolate
714 * @size: size of range to isolate
715 * @start_rgn: out parameter for the start of isolated region
716 * @end_rgn: out parameter for the end of isolated region
717 *
718 * Walk @type and ensure that regions don't cross the boundaries defined by
Mike Rapoport47cec442018-06-30 17:55:02 +0300719 * [@base, @base + @size). Crossing regions are split at the boundaries,
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800720 * which may create at most two more regions. The index of the first
721 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
722 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300723 * Return:
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800724 * 0 on success, -errno on failure.
725 */
726static int __init_memblock memblock_isolate_range(struct memblock_type *type,
727 phys_addr_t base, phys_addr_t size,
728 int *start_rgn, int *end_rgn)
729{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800730 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800731 int idx;
732 struct memblock_region *rgn;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800733
734 *start_rgn = *end_rgn = 0;
735
Tejun Heob3dc6272012-04-20 08:31:34 -0700736 if (!size)
737 return 0;
738
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800739 /* we'll create at most two more regions */
740 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700741 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800742 return -ENOMEM;
743
Gioh Kim66e8b432017-11-15 17:33:42 -0800744 for_each_memblock_type(idx, type, rgn) {
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800745 phys_addr_t rbase = rgn->base;
746 phys_addr_t rend = rbase + rgn->size;
747
748 if (rbase >= end)
749 break;
750 if (rend <= base)
751 continue;
752
753 if (rbase < base) {
754 /*
755 * @rgn intersects from below. Split and continue
756 * to process the next region - the new top half.
757 */
758 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800759 rgn->size -= base - rbase;
760 type->total_size -= base - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800761 memblock_insert_region(type, idx, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800762 memblock_get_region_node(rgn),
763 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800764 } else if (rend > end) {
765 /*
766 * @rgn intersects from above. Split and redo the
767 * current region - the new bottom half.
768 */
769 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800770 rgn->size -= end - rbase;
771 type->total_size -= end - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800772 memblock_insert_region(type, idx--, rbase, end - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800773 memblock_get_region_node(rgn),
774 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800775 } else {
776 /* @rgn is fully contained, record it */
777 if (!*end_rgn)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800778 *start_rgn = idx;
779 *end_rgn = idx + 1;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800780 }
781 }
782
783 return 0;
784}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800785
Alexander Kuleshov35bd16a2015-11-05 18:47:00 -0800786static int __init_memblock memblock_remove_range(struct memblock_type *type,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100787 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000788{
Tejun Heo71936182011-12-08 10:22:07 -0800789 int start_rgn, end_rgn;
790 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000791
Tejun Heo71936182011-12-08 10:22:07 -0800792 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
793 if (ret)
794 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000795
Tejun Heo71936182011-12-08 10:22:07 -0800796 for (i = end_rgn - 1; i >= start_rgn; i--)
797 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700798 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000799}
800
Tejun Heo581adcb2011-12-08 10:22:06 -0800801int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000802{
Minchan Kim25cf23d2018-06-07 17:07:35 -0700803 phys_addr_t end = base + size - 1;
804
805 memblock_dbg("memblock_remove: [%pa-%pa] %pS\n",
806 &base, &end, (void *)_RET_IP_);
807
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100808 return memblock_remove_range(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000809}
810
Mike Rapoport4d728682018-12-28 00:35:29 -0800811/**
812 * memblock_free - free boot memory block
813 * @base: phys starting address of the boot memory block
814 * @size: size of the boot memory block in bytes
815 *
816 * Free boot memory block previously allocated by memblock_alloc_xx() API.
817 * The freeing memory will not be released to the buddy allocator.
818 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800819int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000820{
Miles Chen5d63f812017-02-22 15:46:42 -0800821 phys_addr_t end = base + size - 1;
822
823 memblock_dbg(" memblock_free: [%pa-%pa] %pF\n",
824 &base, &end, (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200825
Catalin Marinas9099dae2016-10-11 13:55:11 -0700826 kmemleak_free_part_phys(base, size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100827 return memblock_remove_range(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000828}
829
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700830int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000831{
Miles Chen5d63f812017-02-22 15:46:42 -0800832 phys_addr_t end = base + size - 1;
833
834 memblock_dbg("memblock_reserve: [%pa-%pa] %pF\n",
835 &base, &end, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000836
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700837 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000838}
839
Tejun Heo35fd0802011-07-12 11:15:59 +0200840/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300841 * memblock_setclr_flag - set or clear flag for a memory region
842 * @base: base address of the region
843 * @size: size of the region
844 * @set: set or clear the flag
845 * @flag: the flag to udpate
Tang Chen66b16ed2014-01-21 15:49:23 -0800846 *
Tony Luck4308ce12014-12-12 16:54:59 -0800847 * This function isolates region [@base, @base + @size), and sets/clears flag
Tang Chen66b16ed2014-01-21 15:49:23 -0800848 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300849 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800850 */
Tony Luck4308ce12014-12-12 16:54:59 -0800851static int __init_memblock memblock_setclr_flag(phys_addr_t base,
852 phys_addr_t size, int set, int flag)
Tang Chen66b16ed2014-01-21 15:49:23 -0800853{
854 struct memblock_type *type = &memblock.memory;
855 int i, ret, start_rgn, end_rgn;
856
857 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
858 if (ret)
859 return ret;
860
Mike Rapoportfe145122019-03-11 23:30:46 -0700861 for (i = start_rgn; i < end_rgn; i++) {
862 struct memblock_region *r = &type->regions[i];
863
Tony Luck4308ce12014-12-12 16:54:59 -0800864 if (set)
Mike Rapoportfe145122019-03-11 23:30:46 -0700865 r->flags |= flag;
Tony Luck4308ce12014-12-12 16:54:59 -0800866 else
Mike Rapoportfe145122019-03-11 23:30:46 -0700867 r->flags &= ~flag;
868 }
Tang Chen66b16ed2014-01-21 15:49:23 -0800869
870 memblock_merge_regions(type);
871 return 0;
872}
873
874/**
Tony Luck4308ce12014-12-12 16:54:59 -0800875 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
876 * @base: the base phys addr of the region
877 * @size: the size of the region
878 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300879 * Return: 0 on success, -errno on failure.
Tony Luck4308ce12014-12-12 16:54:59 -0800880 */
881int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
882{
883 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
884}
885
886/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800887 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
888 * @base: the base phys addr of the region
889 * @size: the size of the region
890 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300891 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800892 */
893int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
894{
Tony Luck4308ce12014-12-12 16:54:59 -0800895 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
Tang Chen66b16ed2014-01-21 15:49:23 -0800896}
897
898/**
Tony Lucka3f5baf2015-06-24 16:58:12 -0700899 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
900 * @base: the base phys addr of the region
901 * @size: the size of the region
902 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300903 * Return: 0 on success, -errno on failure.
Tony Lucka3f5baf2015-06-24 16:58:12 -0700904 */
905int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
906{
907 system_has_some_mirror = true;
908
909 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
910}
911
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100912/**
913 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
914 * @base: the base phys addr of the region
915 * @size: the size of the region
916 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300917 * Return: 0 on success, -errno on failure.
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100918 */
919int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
920{
921 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
922}
Tony Lucka3f5baf2015-06-24 16:58:12 -0700923
924/**
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900925 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
926 * @base: the base phys addr of the region
927 * @size: the size of the region
928 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300929 * Return: 0 on success, -errno on failure.
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900930 */
931int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
932{
933 return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
934}
935
936/**
Robin Holt8e7a7f82015-06-30 14:56:41 -0700937 * __next_reserved_mem_region - next function for for_each_reserved_region()
938 * @idx: pointer to u64 loop variable
939 * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
940 * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
941 *
942 * Iterate over all reserved memory regions.
943 */
944void __init_memblock __next_reserved_mem_region(u64 *idx,
945 phys_addr_t *out_start,
946 phys_addr_t *out_end)
947{
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700948 struct memblock_type *type = &memblock.reserved;
Robin Holt8e7a7f82015-06-30 14:56:41 -0700949
Richard Leitnercd33a762016-05-20 16:58:33 -0700950 if (*idx < type->cnt) {
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700951 struct memblock_region *r = &type->regions[*idx];
Robin Holt8e7a7f82015-06-30 14:56:41 -0700952 phys_addr_t base = r->base;
953 phys_addr_t size = r->size;
954
955 if (out_start)
956 *out_start = base;
957 if (out_end)
958 *out_end = base + size - 1;
959
960 *idx += 1;
961 return;
962 }
963
964 /* signal end of iteration */
965 *idx = ULLONG_MAX;
966}
967
968/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100969 * __next__mem_range - next function for for_each_free_mem_range() etc.
Tejun Heo35fd0802011-07-12 11:15:59 +0200970 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800971 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700972 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100973 * @type_a: pointer to memblock_type from where the range is taken
974 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -0700975 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
976 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
977 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +0200978 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100979 * Find the first area from *@idx which matches @nid, fill the out
Tejun Heo35fd0802011-07-12 11:15:59 +0200980 * parameters, and update *@idx for the next iteration. The lower 32bit of
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100981 * *@idx contains index into type_a and the upper 32bit indexes the
982 * areas before each region in type_b. For example, if type_b regions
Tejun Heo35fd0802011-07-12 11:15:59 +0200983 * look like the following,
984 *
985 * 0:[0-16), 1:[32-48), 2:[128-130)
986 *
987 * The upper 32bit indexes the following regions.
988 *
989 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
990 *
991 * As both region arrays are sorted, the function advances the two indices
992 * in lockstep and returns each intersection.
993 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300994void __init_memblock __next_mem_range(u64 *idx, int nid,
995 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100996 struct memblock_type *type_a,
997 struct memblock_type *type_b,
998 phys_addr_t *out_start,
999 phys_addr_t *out_end, int *out_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +02001000{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001001 int idx_a = *idx & 0xffffffff;
1002 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -08001003
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001004 if (WARN_ONCE(nid == MAX_NUMNODES,
1005 "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
Grygorii Strashko560dca272014-01-21 15:50:55 -08001006 nid = NUMA_NO_NODE;
Tejun Heo35fd0802011-07-12 11:15:59 +02001007
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001008 for (; idx_a < type_a->cnt; idx_a++) {
1009 struct memblock_region *m = &type_a->regions[idx_a];
1010
Tejun Heo35fd0802011-07-12 11:15:59 +02001011 phys_addr_t m_start = m->base;
1012 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001013 int m_nid = memblock_get_region_node(m);
Tejun Heo35fd0802011-07-12 11:15:59 +02001014
1015 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001016 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +02001017 continue;
1018
Xishi Qiu0a313a92014-09-09 14:50:46 -07001019 /* skip hotpluggable memory regions if needed */
1020 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
1021 continue;
1022
Tony Lucka3f5baf2015-06-24 16:58:12 -07001023 /* if we want mirror memory skip non-mirror memory regions */
1024 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1025 continue;
1026
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001027 /* skip nomap memory unless we were asked for it explicitly */
1028 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1029 continue;
1030
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001031 if (!type_b) {
1032 if (out_start)
1033 *out_start = m_start;
1034 if (out_end)
1035 *out_end = m_end;
1036 if (out_nid)
1037 *out_nid = m_nid;
1038 idx_a++;
1039 *idx = (u32)idx_a | (u64)idx_b << 32;
1040 return;
1041 }
Tejun Heo35fd0802011-07-12 11:15:59 +02001042
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001043 /* scan areas before each reservation */
1044 for (; idx_b < type_b->cnt + 1; idx_b++) {
1045 struct memblock_region *r;
1046 phys_addr_t r_start;
1047 phys_addr_t r_end;
1048
1049 r = &type_b->regions[idx_b];
1050 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1051 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001052 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001053
1054 /*
1055 * if idx_b advanced past idx_a,
1056 * break out to advance idx_a
1057 */
Tejun Heo35fd0802011-07-12 11:15:59 +02001058 if (r_start >= m_end)
1059 break;
1060 /* if the two regions intersect, we're done */
1061 if (m_start < r_end) {
1062 if (out_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001063 *out_start =
1064 max(m_start, r_start);
Tejun Heo35fd0802011-07-12 11:15:59 +02001065 if (out_end)
1066 *out_end = min(m_end, r_end);
1067 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001068 *out_nid = m_nid;
Tejun Heo35fd0802011-07-12 11:15:59 +02001069 /*
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001070 * The region which ends first is
1071 * advanced for the next iteration.
Tejun Heo35fd0802011-07-12 11:15:59 +02001072 */
1073 if (m_end <= r_end)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001074 idx_a++;
Tejun Heo35fd0802011-07-12 11:15:59 +02001075 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001076 idx_b++;
1077 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo35fd0802011-07-12 11:15:59 +02001078 return;
1079 }
1080 }
1081 }
1082
1083 /* signal end of iteration */
1084 *idx = ULLONG_MAX;
1085}
1086
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001087/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001088 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1089 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001090 * @idx: pointer to u64 loop variable
Alexander Kuleshovad5ea8c2015-09-08 15:04:22 -07001091 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -07001092 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001093 * @type_a: pointer to memblock_type from where the range is taken
1094 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -07001095 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1096 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1097 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001098 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001099 * Finds the next range from type_a which is not marked as unsuitable
1100 * in type_b.
1101 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001102 * Reverse of __next_mem_range().
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001103 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001104void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1105 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001106 struct memblock_type *type_a,
1107 struct memblock_type *type_b,
1108 phys_addr_t *out_start,
1109 phys_addr_t *out_end, int *out_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001110{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001111 int idx_a = *idx & 0xffffffff;
1112 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -08001113
Grygorii Strashko560dca272014-01-21 15:50:55 -08001114 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1115 nid = NUMA_NO_NODE;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001116
1117 if (*idx == (u64)ULLONG_MAX) {
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001118 idx_a = type_a->cnt - 1;
zijun_hue47608a2016-08-04 15:32:00 -07001119 if (type_b != NULL)
1120 idx_b = type_b->cnt;
1121 else
1122 idx_b = 0;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001123 }
1124
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001125 for (; idx_a >= 0; idx_a--) {
1126 struct memblock_region *m = &type_a->regions[idx_a];
1127
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001128 phys_addr_t m_start = m->base;
1129 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001130 int m_nid = memblock_get_region_node(m);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001131
1132 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001133 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001134 continue;
1135
Tang Chen55ac5902014-01-21 15:49:35 -08001136 /* skip hotpluggable memory regions if needed */
1137 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
1138 continue;
1139
Tony Lucka3f5baf2015-06-24 16:58:12 -07001140 /* if we want mirror memory skip non-mirror memory regions */
1141 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1142 continue;
1143
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001144 /* skip nomap memory unless we were asked for it explicitly */
1145 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1146 continue;
1147
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001148 if (!type_b) {
1149 if (out_start)
1150 *out_start = m_start;
1151 if (out_end)
1152 *out_end = m_end;
1153 if (out_nid)
1154 *out_nid = m_nid;
zijun_hufb399b42016-07-28 15:48:56 -07001155 idx_a--;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001156 *idx = (u32)idx_a | (u64)idx_b << 32;
1157 return;
1158 }
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001159
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001160 /* scan areas before each reservation */
1161 for (; idx_b >= 0; idx_b--) {
1162 struct memblock_region *r;
1163 phys_addr_t r_start;
1164 phys_addr_t r_end;
1165
1166 r = &type_b->regions[idx_b];
1167 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1168 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001169 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001170 /*
1171 * if idx_b advanced past idx_a,
1172 * break out to advance idx_a
1173 */
1174
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001175 if (r_end <= m_start)
1176 break;
1177 /* if the two regions intersect, we're done */
1178 if (m_end > r_start) {
1179 if (out_start)
1180 *out_start = max(m_start, r_start);
1181 if (out_end)
1182 *out_end = min(m_end, r_end);
1183 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001184 *out_nid = m_nid;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001185 if (m_start >= r_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001186 idx_a--;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001187 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001188 idx_b--;
1189 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001190 return;
1191 }
1192 }
1193 }
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001194 /* signal end of iteration */
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001195 *idx = ULLONG_MAX;
1196}
1197
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001198#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1199/*
Chen Chang45e79812018-11-16 15:08:57 -08001200 * Common iterator interface used to define for_each_mem_pfn_range().
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001201 */
1202void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1203 unsigned long *out_start_pfn,
1204 unsigned long *out_end_pfn, int *out_nid)
1205{
1206 struct memblock_type *type = &memblock.memory;
1207 struct memblock_region *r;
1208
1209 while (++*idx < type->cnt) {
1210 r = &type->regions[*idx];
1211
1212 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1213 continue;
1214 if (nid == MAX_NUMNODES || nid == r->nid)
1215 break;
1216 }
1217 if (*idx >= type->cnt) {
1218 *idx = -1;
1219 return;
1220 }
1221
1222 if (out_start_pfn)
1223 *out_start_pfn = PFN_UP(r->base);
1224 if (out_end_pfn)
1225 *out_end_pfn = PFN_DOWN(r->base + r->size);
1226 if (out_nid)
1227 *out_nid = r->nid;
1228}
1229
1230/**
1231 * memblock_set_node - set node ID on memblock regions
1232 * @base: base of area to set node ID for
1233 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -08001234 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001235 * @nid: node ID to set
1236 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001237 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001238 * Regions which cross the area boundaries are split as necessary.
1239 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001240 * Return:
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001241 * 0 on success, -errno on failure.
1242 */
1243int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -08001244 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001245{
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001246 int start_rgn, end_rgn;
1247 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001248
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001249 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1250 if (ret)
1251 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001252
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001253 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -07001254 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001255
1256 memblock_merge_regions(type);
1257 return 0;
1258}
1259#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1260
Mike Rapoport92d12f92019-03-11 23:29:41 -07001261/**
1262 * memblock_alloc_range_nid - allocate boot memory block
1263 * @size: size of memory block to be allocated in bytes
1264 * @align: alignment of the region and block's size
1265 * @start: the lower bound of the memory region to allocate (phys address)
1266 * @end: the upper bound of the memory region to allocate (phys address)
1267 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1268 *
1269 * The allocation is performed from memory region limited by
1270 * memblock.current_limit if @max_addr == %MEMBLOCK_ALLOC_ACCESSIBLE.
1271 *
1272 * If the specified node can not hold the requested memory the
1273 * allocation falls back to any node in the system
1274 *
1275 * For systems with memory mirroring, the allocation is attempted first
1276 * from the regions with mirroring enabled and then retried from any
1277 * memory region.
1278 *
1279 * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for
1280 * allocated boot memory block, so that it is never reported as leaks.
1281 *
1282 * Return:
1283 * Physical address of allocated memory block on success, %0 on failure.
1284 */
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001285static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1286 phys_addr_t align, phys_addr_t start,
Mike Rapoport92d12f92019-03-11 23:29:41 -07001287 phys_addr_t end, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001288{
Mike Rapoport92d12f92019-03-11 23:29:41 -07001289 enum memblock_flags flags = choose_memblock_flags();
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001290 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001291
Mike Rapoport92d12f92019-03-11 23:29:41 -07001292 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1293 nid = NUMA_NO_NODE;
1294
Mike Rapoport2f770802018-10-30 15:10:01 -07001295 if (!align) {
1296 /* Can't use WARNs this early in boot on powerpc */
1297 dump_stack();
1298 align = SMP_CACHE_BYTES;
1299 }
1300
Mike Rapoport92d12f92019-03-11 23:29:41 -07001301 if (end > memblock.current_limit)
1302 end = memblock.current_limit;
1303
1304again:
Tony Luckfc6daaf2015-06-24 16:58:09 -07001305 found = memblock_find_in_range_node(size, align, start, end, nid,
1306 flags);
Mike Rapoport92d12f92019-03-11 23:29:41 -07001307 if (found && !memblock_reserve(found, size))
1308 goto done;
1309
1310 if (nid != NUMA_NO_NODE) {
1311 found = memblock_find_in_range_node(size, align, start,
1312 end, NUMA_NO_NODE,
1313 flags);
1314 if (found && !memblock_reserve(found, size))
1315 goto done;
1316 }
1317
1318 if (flags & MEMBLOCK_MIRROR) {
1319 flags &= ~MEMBLOCK_MIRROR;
1320 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1321 &size);
1322 goto again;
1323 }
1324
1325 return 0;
1326
1327done:
1328 /* Skip kmemleak for kasan_init() due to high volume. */
1329 if (end != MEMBLOCK_ALLOC_KASAN)
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001330 /*
Mike Rapoport92d12f92019-03-11 23:29:41 -07001331 * The min_count is set to 0 so that memblock allocated
1332 * blocks are never reported as leaks. This is because many
1333 * of these blocks are only referred via the physical
1334 * address which is not looked up by kmemleak.
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001335 */
Catalin Marinas9099dae2016-10-11 13:55:11 -07001336 kmemleak_alloc_phys(found, size, 0, 0);
Mike Rapoport92d12f92019-03-11 23:29:41 -07001337
1338 return found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001339}
1340
Mike Rapoport8a770c22019-03-11 23:29:16 -07001341phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
1342 phys_addr_t align,
1343 phys_addr_t start,
1344 phys_addr_t end)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001345{
Mike Rapoport92d12f92019-03-11 23:29:41 -07001346 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001347}
1348
Mike Rapoport9a8dd702018-10-30 15:07:59 -07001349phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001350{
Mike Rapoport33755572019-03-11 23:29:21 -07001351 return memblock_alloc_range_nid(size, align, 0,
Mike Rapoport92d12f92019-03-11 23:29:41 -07001352 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001353}
1354
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001355/**
Mike Rapoporteb31d552018-10-30 15:08:04 -07001356 * memblock_alloc_internal - allocate boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001357 * @size: size of memory block to be allocated in bytes
1358 * @align: alignment of the region and block's size
1359 * @min_addr: the lower bound of the memory region to allocate (phys address)
1360 * @max_addr: the upper bound of the memory region to allocate (phys address)
1361 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1362 *
Mike Rapoport92d12f92019-03-11 23:29:41 -07001363 * Allocates memory block using memblock_alloc_range_nid() and
1364 * converts the returned physical address to virtual.
1365 *
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001366 * The @min_addr limit is dropped if it can not be satisfied and the allocation
Mike Rapoport92d12f92019-03-11 23:29:41 -07001367 * will fall back to memory below @min_addr. Other constraints, such
1368 * as node and mirrored memory will be handled again in
1369 * memblock_alloc_range_nid().
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001370 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001371 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001372 * Virtual address of allocated memory block on success, NULL on failure.
1373 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001374static void * __init memblock_alloc_internal(
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001375 phys_addr_t size, phys_addr_t align,
1376 phys_addr_t min_addr, phys_addr_t max_addr,
1377 int nid)
1378{
1379 phys_addr_t alloc;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001380
1381 /*
1382 * Detect any accidental use of these APIs after slab is ready, as at
1383 * this moment memblock may be deinitialized already and its
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -07001384 * internal data may be destroyed (after execution of memblock_free_all)
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001385 */
1386 if (WARN_ON_ONCE(slab_is_available()))
1387 return kzalloc_node(size, GFP_NOWAIT, nid);
1388
Mike Rapoport92d12f92019-03-11 23:29:41 -07001389 alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid);
Mike Rapoport2f770802018-10-30 15:10:01 -07001390
Mike Rapoport92d12f92019-03-11 23:29:41 -07001391 /* retry allocation without lower limit */
1392 if (!alloc && min_addr)
1393 alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001394
Mike Rapoport92d12f92019-03-11 23:29:41 -07001395 if (!alloc)
1396 return NULL;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001397
Mike Rapoport92d12f92019-03-11 23:29:41 -07001398 return phys_to_virt(alloc);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001399}
1400
1401/**
Mike Rapoporteb31d552018-10-30 15:08:04 -07001402 * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001403 * memory and without panicking
1404 * @size: size of memory block to be allocated in bytes
1405 * @align: alignment of the region and block's size
1406 * @min_addr: the lower bound of the memory region from where the allocation
1407 * is preferred (phys address)
1408 * @max_addr: the upper bound of the memory region from where the allocation
Mike Rapoport97ad1082018-10-30 15:09:44 -07001409 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001410 * allocate only from memory limited by memblock.current_limit value
1411 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1412 *
1413 * Public function, provides additional debug information (including caller
1414 * info), if enabled. Does not zero allocated memory, does not panic if request
1415 * cannot be satisfied.
1416 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001417 * Return:
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001418 * Virtual address of allocated memory block on success, NULL on failure.
1419 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001420void * __init memblock_alloc_try_nid_raw(
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001421 phys_addr_t size, phys_addr_t align,
1422 phys_addr_t min_addr, phys_addr_t max_addr,
1423 int nid)
1424{
1425 void *ptr;
1426
Mike Rapoporta36aab82018-08-17 15:47:17 -07001427 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1428 __func__, (u64)size, (u64)align, nid, &min_addr,
1429 &max_addr, (void *)_RET_IP_);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001430
Mike Rapoporteb31d552018-10-30 15:08:04 -07001431 ptr = memblock_alloc_internal(size, align,
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001432 min_addr, max_addr, nid);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001433 if (ptr && size > 0)
Alexander Duyckf682a972018-10-26 15:07:45 -07001434 page_init_poison(ptr, size);
1435
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001436 return ptr;
1437}
1438
1439/**
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001440 * memblock_alloc_try_nid - allocate boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001441 * @size: size of memory block to be allocated in bytes
1442 * @align: alignment of the region and block's size
1443 * @min_addr: the lower bound of the memory region from where the allocation
1444 * is preferred (phys address)
1445 * @max_addr: the upper bound of the memory region from where the allocation
Mike Rapoport97ad1082018-10-30 15:09:44 -07001446 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001447 * allocate only from memory limited by memblock.current_limit value
1448 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1449 *
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001450 * Public function, provides additional debug information (including caller
1451 * info), if enabled. This function zeroes the allocated memory.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001452 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001453 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001454 * Virtual address of allocated memory block on success, NULL on failure.
1455 */
Mike Rapoporteb31d552018-10-30 15:08:04 -07001456void * __init memblock_alloc_try_nid(
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001457 phys_addr_t size, phys_addr_t align,
1458 phys_addr_t min_addr, phys_addr_t max_addr,
1459 int nid)
1460{
1461 void *ptr;
1462
Mike Rapoporta36aab82018-08-17 15:47:17 -07001463 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n",
1464 __func__, (u64)size, (u64)align, nid, &min_addr,
1465 &max_addr, (void *)_RET_IP_);
Mike Rapoporteb31d552018-10-30 15:08:04 -07001466 ptr = memblock_alloc_internal(size, align,
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001467 min_addr, max_addr, nid);
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001468 if (ptr)
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001469 memset(ptr, 0, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001470
Mike Rapoportc0dbe822019-03-11 23:30:37 -07001471 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001472}
1473
1474/**
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001475 * __memblock_free_late - free bootmem block pages directly to buddy allocator
Mike Rapoport48a833c2018-06-30 17:55:03 +03001476 * @base: phys starting address of the boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001477 * @size: size of the boot memory block in bytes
1478 *
1479 * This is only useful when the bootmem allocator has already been torn
1480 * down, but we are still initializing the system. Pages are released directly
1481 * to the buddy allocator, no bootmem metadata is updated because it is gone.
1482 */
1483void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1484{
Mike Rapoporta36aab82018-08-17 15:47:17 -07001485 phys_addr_t cursor, end;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001486
Mike Rapoporta36aab82018-08-17 15:47:17 -07001487 end = base + size - 1;
1488 memblock_dbg("%s: [%pa-%pa] %pF\n",
1489 __func__, &base, &end, (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001490 kmemleak_free_part_phys(base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001491 cursor = PFN_UP(base);
1492 end = PFN_DOWN(base + size);
1493
1494 for (; cursor < end; cursor++) {
Mike Rapoport7c2ee342018-10-30 15:09:36 -07001495 memblock_free_pages(pfn_to_page(cursor), cursor, 0);
Arun KSca79b0c2018-12-28 00:34:29 -08001496 totalram_pages_inc();
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001497 }
1498}
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001499
1500/*
1501 * Remaining API functions
1502 */
1503
David Gibson1f1ffb8a2016-02-05 15:36:19 -08001504phys_addr_t __init_memblock memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001505{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001506 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001507}
1508
Srikar Dronamraju8907de52016-10-07 16:59:18 -07001509phys_addr_t __init_memblock memblock_reserved_size(void)
1510{
1511 return memblock.reserved.total_size;
1512}
1513
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001514phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1515{
1516 unsigned long pages = 0;
1517 struct memblock_region *r;
1518 unsigned long start_pfn, end_pfn;
1519
1520 for_each_memblock(memory, r) {
1521 start_pfn = memblock_region_memory_base_pfn(r);
1522 end_pfn = memblock_region_memory_end_pfn(r);
1523 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1524 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1525 pages += end_pfn - start_pfn;
1526 }
1527
Fabian Frederick16763232014-04-07 15:37:53 -07001528 return PFN_PHYS(pages);
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001529}
1530
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001531/* lowest address */
1532phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1533{
1534 return memblock.memory.regions[0].base;
1535}
1536
Yinghai Lu10d06432010-07-28 15:43:02 +10001537phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001538{
1539 int idx = memblock.memory.cnt - 1;
1540
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001541 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001542}
1543
Dennis Chena571d4e2016-07-28 15:48:26 -07001544static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001545{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001546 phys_addr_t max_addr = PHYS_ADDR_MAX;
Emil Medve136199f2014-04-07 15:37:52 -07001547 struct memblock_region *r;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001548
Dennis Chena571d4e2016-07-28 15:48:26 -07001549 /*
1550 * translate the memory @limit size into the max address within one of
1551 * the memory memblock regions, if the @limit exceeds the total size
Stefan Agner1c4bc432018-06-07 17:06:15 -07001552 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
Dennis Chena571d4e2016-07-28 15:48:26 -07001553 */
Emil Medve136199f2014-04-07 15:37:52 -07001554 for_each_memblock(memory, r) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001555 if (limit <= r->size) {
1556 max_addr = r->base + limit;
1557 break;
1558 }
1559 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001560 }
1561
Dennis Chena571d4e2016-07-28 15:48:26 -07001562 return max_addr;
1563}
1564
1565void __init memblock_enforce_memory_limit(phys_addr_t limit)
1566{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001567 phys_addr_t max_addr = PHYS_ADDR_MAX;
Dennis Chena571d4e2016-07-28 15:48:26 -07001568
1569 if (!limit)
1570 return;
1571
1572 max_addr = __find_max_addr(limit);
1573
1574 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001575 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001576 return;
1577
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001578 /* truncate both memory and reserved regions */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001579 memblock_remove_range(&memblock.memory, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001580 PHYS_ADDR_MAX);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001581 memblock_remove_range(&memblock.reserved, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001582 PHYS_ADDR_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001583}
1584
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001585void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1586{
1587 int start_rgn, end_rgn;
1588 int i, ret;
1589
1590 if (!size)
1591 return;
1592
1593 ret = memblock_isolate_range(&memblock.memory, base, size,
1594 &start_rgn, &end_rgn);
1595 if (ret)
1596 return;
1597
1598 /* remove all the MAP regions */
1599 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1600 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1601 memblock_remove_region(&memblock.memory, i);
1602
1603 for (i = start_rgn - 1; i >= 0; i--)
1604 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1605 memblock_remove_region(&memblock.memory, i);
1606
1607 /* truncate the reserved regions */
1608 memblock_remove_range(&memblock.reserved, 0, base);
1609 memblock_remove_range(&memblock.reserved,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001610 base + size, PHYS_ADDR_MAX);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001611}
1612
Dennis Chena571d4e2016-07-28 15:48:26 -07001613void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1614{
Dennis Chena571d4e2016-07-28 15:48:26 -07001615 phys_addr_t max_addr;
Dennis Chena571d4e2016-07-28 15:48:26 -07001616
1617 if (!limit)
1618 return;
1619
1620 max_addr = __find_max_addr(limit);
1621
1622 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001623 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001624 return;
1625
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001626 memblock_cap_memory_range(0, max_addr);
Dennis Chena571d4e2016-07-28 15:48:26 -07001627}
1628
Yinghai Lucd794812010-10-11 12:34:09 -07001629static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001630{
1631 unsigned int left = 0, right = type->cnt;
1632
1633 do {
1634 unsigned int mid = (right + left) / 2;
1635
1636 if (addr < type->regions[mid].base)
1637 right = mid;
1638 else if (addr >= (type->regions[mid].base +
1639 type->regions[mid].size))
1640 left = mid + 1;
1641 else
1642 return mid;
1643 } while (left < right);
1644 return -1;
1645}
1646
Yueyi Lif5a222d2018-12-14 14:17:06 -08001647bool __init_memblock memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001648{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001649 return memblock_search(&memblock.reserved, addr) != -1;
1650}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001651
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001652bool __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001653{
1654 return memblock_search(&memblock.memory, addr) != -1;
1655}
1656
Yaowei Bai937f0c22018-02-06 15:41:18 -08001657bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001658{
1659 int i = memblock_search(&memblock.memory, addr);
1660
1661 if (i == -1)
1662 return false;
1663 return !memblock_is_nomap(&memblock.memory.regions[i]);
1664}
1665
Yinghai Lue76b63f2013-09-11 14:22:17 -07001666#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1667int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1668 unsigned long *start_pfn, unsigned long *end_pfn)
1669{
1670 struct memblock_type *type = &memblock.memory;
Fabian Frederick16763232014-04-07 15:37:53 -07001671 int mid = memblock_search(type, PFN_PHYS(pfn));
Yinghai Lue76b63f2013-09-11 14:22:17 -07001672
1673 if (mid == -1)
1674 return -1;
1675
Fabian Frederickf7e2f7e2014-06-04 16:07:51 -07001676 *start_pfn = PFN_DOWN(type->regions[mid].base);
1677 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001678
1679 return type->regions[mid].nid;
1680}
1681#endif
1682
Stephen Boydeab30942012-05-24 00:45:21 -07001683/**
1684 * memblock_is_region_memory - check if a region is a subset of memory
1685 * @base: base of region to check
1686 * @size: size of region to check
1687 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001688 * Check if the region [@base, @base + @size) is a subset of a memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001689 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001690 * Return:
Stephen Boydeab30942012-05-24 00:45:21 -07001691 * 0 if false, non-zero if true
1692 */
Yaowei Bai937f0c22018-02-06 15:41:18 -08001693bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001694{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001695 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001696 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001697
1698 if (idx == -1)
Yaowei Bai937f0c22018-02-06 15:41:18 -08001699 return false;
Wei Yangef415ef2017-02-22 15:45:04 -08001700 return (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001701 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001702}
1703
Stephen Boydeab30942012-05-24 00:45:21 -07001704/**
1705 * memblock_is_region_reserved - check if a region intersects reserved memory
1706 * @base: base of region to check
1707 * @size: size of region to check
1708 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001709 * Check if the region [@base, @base + @size) intersects a reserved
1710 * memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001711 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001712 * Return:
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001713 * True if they intersect, false if not.
Stephen Boydeab30942012-05-24 00:45:21 -07001714 */
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001715bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001716{
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001717 memblock_cap_size(base, &size);
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001718 return memblock_overlaps_region(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001719}
1720
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001721void __init_memblock memblock_trim_memory(phys_addr_t align)
1722{
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001723 phys_addr_t start, end, orig_start, orig_end;
Emil Medve136199f2014-04-07 15:37:52 -07001724 struct memblock_region *r;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001725
Emil Medve136199f2014-04-07 15:37:52 -07001726 for_each_memblock(memory, r) {
1727 orig_start = r->base;
1728 orig_end = r->base + r->size;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001729 start = round_up(orig_start, align);
1730 end = round_down(orig_end, align);
1731
1732 if (start == orig_start && end == orig_end)
1733 continue;
1734
1735 if (start < end) {
Emil Medve136199f2014-04-07 15:37:52 -07001736 r->base = start;
1737 r->size = end - start;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001738 } else {
Emil Medve136199f2014-04-07 15:37:52 -07001739 memblock_remove_region(&memblock.memory,
1740 r - memblock.memory.regions);
1741 r--;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001742 }
1743 }
1744}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001745
Yinghai Lu3661ca62010-09-15 13:05:29 -07001746void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001747{
1748 memblock.current_limit = limit;
1749}
1750
Laura Abbottfec51012014-02-27 01:23:43 +01001751phys_addr_t __init_memblock memblock_get_current_limit(void)
1752{
1753 return memblock.current_limit;
1754}
1755
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001756static void __init_memblock memblock_dump(struct memblock_type *type)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001757{
Miles Chen5d63f812017-02-22 15:46:42 -08001758 phys_addr_t base, end, size;
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001759 enum memblock_flags flags;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001760 int idx;
1761 struct memblock_region *rgn;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001762
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001763 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001764
Gioh Kim66e8b432017-11-15 17:33:42 -08001765 for_each_memblock_type(idx, type, rgn) {
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001766 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001767
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001768 base = rgn->base;
1769 size = rgn->size;
Miles Chen5d63f812017-02-22 15:46:42 -08001770 end = base + size - 1;
Tang Chen66a20752014-01-21 15:49:20 -08001771 flags = rgn->flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001772#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1773 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1774 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1775 memblock_get_region_node(rgn));
1776#endif
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001777 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001778 type->name, idx, &base, &end, &size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001779 }
1780}
1781
Tejun Heo4ff7b822011-12-08 10:22:06 -08001782void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001783{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001784 pr_info("MEMBLOCK configuration:\n");
Miles Chen5d63f812017-02-22 15:46:42 -08001785 pr_info(" memory size = %pa reserved size = %pa\n",
1786 &memblock.memory.total_size,
1787 &memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001788
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001789 memblock_dump(&memblock.memory);
1790 memblock_dump(&memblock.reserved);
Heiko Carstens409efd42017-02-24 14:55:56 -08001791#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001792 memblock_dump(&memblock.physmem);
Heiko Carstens409efd42017-02-24 14:55:56 -08001793#endif
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001794}
1795
Tejun Heo1aadc052011-12-08 10:22:08 -08001796void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001797{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001798 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001799}
1800
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001801static int __init early_memblock(char *p)
1802{
1803 if (p && strstr(p, "debug"))
1804 memblock_debug = 1;
1805 return 0;
1806}
1807early_param("memblock", early_memblock);
1808
Mike Rapoportbda49a82018-10-30 15:09:40 -07001809static void __init __free_pages_memory(unsigned long start, unsigned long end)
1810{
1811 int order;
1812
1813 while (start < end) {
1814 order = min(MAX_ORDER - 1UL, __ffs(start));
1815
1816 while (start + (1UL << order) > end)
1817 order--;
1818
1819 memblock_free_pages(pfn_to_page(start), start, order);
1820
1821 start += (1UL << order);
1822 }
1823}
1824
1825static unsigned long __init __free_memory_core(phys_addr_t start,
1826 phys_addr_t end)
1827{
1828 unsigned long start_pfn = PFN_UP(start);
1829 unsigned long end_pfn = min_t(unsigned long,
1830 PFN_DOWN(end), max_low_pfn);
1831
1832 if (start_pfn >= end_pfn)
1833 return 0;
1834
1835 __free_pages_memory(start_pfn, end_pfn);
1836
1837 return end_pfn - start_pfn;
1838}
1839
1840static unsigned long __init free_low_memory_core_early(void)
1841{
1842 unsigned long count = 0;
1843 phys_addr_t start, end;
1844 u64 i;
1845
1846 memblock_clear_hotplug(0, -1);
1847
1848 for_each_reserved_mem_region(i, &start, &end)
1849 reserve_bootmem_region(start, end);
1850
1851 /*
1852 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
1853 * because in some case like Node0 doesn't have RAM installed
1854 * low ram will be on Node1
1855 */
1856 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
1857 NULL)
1858 count += __free_memory_core(start, end);
1859
1860 return count;
1861}
1862
1863static int reset_managed_pages_done __initdata;
1864
1865void reset_node_managed_pages(pg_data_t *pgdat)
1866{
1867 struct zone *z;
1868
1869 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
Arun KS9705bea2018-12-28 00:34:24 -08001870 atomic_long_set(&z->managed_pages, 0);
Mike Rapoportbda49a82018-10-30 15:09:40 -07001871}
1872
1873void __init reset_all_zones_managed_pages(void)
1874{
1875 struct pglist_data *pgdat;
1876
1877 if (reset_managed_pages_done)
1878 return;
1879
1880 for_each_online_pgdat(pgdat)
1881 reset_node_managed_pages(pgdat);
1882
1883 reset_managed_pages_done = 1;
1884}
1885
1886/**
1887 * memblock_free_all - release free pages to the buddy allocator
1888 *
1889 * Return: the number of pages actually released.
1890 */
1891unsigned long __init memblock_free_all(void)
1892{
1893 unsigned long pages;
1894
1895 reset_all_zones_managed_pages();
1896
1897 pages = free_low_memory_core_early();
Arun KSca79b0c2018-12-28 00:34:29 -08001898 totalram_pages_add(pages);
Mike Rapoportbda49a82018-10-30 15:09:40 -07001899
1900 return pages;
1901}
1902
Tejun Heoc378ddd2011-07-14 11:46:03 +02001903#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001904
1905static int memblock_debug_show(struct seq_file *m, void *private)
1906{
1907 struct memblock_type *type = m->private;
1908 struct memblock_region *reg;
1909 int i;
Miles Chen5d63f812017-02-22 15:46:42 -08001910 phys_addr_t end;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001911
1912 for (i = 0; i < type->cnt; i++) {
1913 reg = &type->regions[i];
Miles Chen5d63f812017-02-22 15:46:42 -08001914 end = reg->base + reg->size - 1;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001915
Miles Chen5d63f812017-02-22 15:46:42 -08001916 seq_printf(m, "%4d: ", i);
1917 seq_printf(m, "%pa..%pa\n", &reg->base, &end);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001918 }
1919 return 0;
1920}
Andy Shevchenko5ad35092018-04-05 16:23:16 -07001921DEFINE_SHOW_ATTRIBUTE(memblock_debug);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001922
1923static int __init memblock_init_debugfs(void)
1924{
1925 struct dentry *root = debugfs_create_dir("memblock", NULL);
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -08001926
Joe Perches0825a6f2018-06-14 15:27:58 -07001927 debugfs_create_file("memory", 0444, root,
1928 &memblock.memory, &memblock_debug_fops);
1929 debugfs_create_file("reserved", 0444, root,
1930 &memblock.reserved, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001931#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
Joe Perches0825a6f2018-06-14 15:27:58 -07001932 debugfs_create_file("physmem", 0444, root,
1933 &memblock.physmem, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001934#endif
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001935
1936 return 0;
1937}
1938__initcall(memblock_init_debugfs);
1939
1940#endif /* CONFIG_DEBUG_FS */