blob: 245263ad65fb307834a301ec7cb9b327f634fad1 [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
Tejun Heofe091c22011-12-08 10:22:07 -080029static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
30static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010031#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
32static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
33#endif
Tejun Heofe091c22011-12-08 10:22:07 -080034
35struct memblock memblock __initdata_memblock = {
36 .memory.regions = memblock_memory_init_regions,
37 .memory.cnt = 1, /* empty dummy entry */
38 .memory.max = INIT_MEMBLOCK_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -080039 .memory.name = "memory",
Tejun Heofe091c22011-12-08 10:22:07 -080040
41 .reserved.regions = memblock_reserved_init_regions,
42 .reserved.cnt = 1, /* empty dummy entry */
43 .reserved.max = INIT_MEMBLOCK_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -080044 .reserved.name = "reserved",
Tejun Heofe091c22011-12-08 10:22:07 -080045
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010046#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
47 .physmem.regions = memblock_physmem_init_regions,
48 .physmem.cnt = 1, /* empty dummy entry */
49 .physmem.max = INIT_PHYSMEM_REGIONS,
Heiko Carstens0262d9c2017-02-24 14:55:59 -080050 .physmem.name = "physmem",
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010051#endif
52
Tang Chen79442ed2013-11-12 15:07:59 -080053 .bottom_up = false,
Tejun Heofe091c22011-12-08 10:22:07 -080054 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
55};
Yinghai Lu95f72d12010-07-12 14:36:09 +100056
Yinghai Lu10d06432010-07-28 15:43:02 +100057int memblock_debug __initdata_memblock;
Tony Lucka3f5baf2015-06-24 16:58:12 -070058static bool system_has_some_mirror __initdata_memblock = false;
Tejun Heo1aadc052011-12-08 10:22:08 -080059static int memblock_can_resize __initdata_memblock;
Gavin Shan181eb392012-05-29 15:06:50 -070060static int memblock_memory_in_slab __initdata_memblock = 0;
61static int memblock_reserved_in_slab __initdata_memblock = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +100062
Mike Rapoporte1720fe2018-06-30 17:55:01 +030063enum memblock_flags __init_memblock choose_memblock_flags(void)
Tony Lucka3f5baf2015-06-24 16:58:12 -070064{
65 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
66}
67
Tejun Heoeb18f1b2011-12-08 10:22:07 -080068/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
69static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
70{
Stefan Agner1c4bc432018-06-07 17:06:15 -070071 return *size = min(*size, PHYS_ADDR_MAX - base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -080072}
73
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100074/*
75 * Address comparison utilities
76 */
Yinghai Lu10d06432010-07-28 15:43:02 +100077static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100078 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100079{
80 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
81}
82
Tang Chen95cf82e2015-09-08 15:02:03 -070083bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -070084 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100085{
86 unsigned long i;
87
Alexander Kuleshovf14516f2016-01-14 15:20:39 -080088 for (i = 0; i < type->cnt; i++)
89 if (memblock_addrs_overlap(base, size, type->regions[i].base,
90 type->regions[i].size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100091 break;
Tang Chenc5c5c9d2015-09-08 15:02:00 -070092 return i < type->cnt;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100093}
94
Mike Rapoport47cec442018-06-30 17:55:02 +030095/**
Tang Chen79442ed2013-11-12 15:07:59 -080096 * __memblock_find_range_bottom_up - find free area utility in bottom-up
97 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +030098 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
99 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen79442ed2013-11-12 15:07:59 -0800100 * @size: size of free area to find
101 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800102 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700103 * @flags: pick from blocks based on memory attributes
Tang Chen79442ed2013-11-12 15:07:59 -0800104 *
105 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
106 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300107 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800108 * Found address on success, 0 on failure.
109 */
110static phys_addr_t __init_memblock
111__memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700112 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300113 enum memblock_flags flags)
Tang Chen79442ed2013-11-12 15:07:59 -0800114{
115 phys_addr_t this_start, this_end, cand;
116 u64 i;
117
Tony Luckfc6daaf2015-06-24 16:58:09 -0700118 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
Tang Chen79442ed2013-11-12 15:07:59 -0800119 this_start = clamp(this_start, start, end);
120 this_end = clamp(this_end, start, end);
121
122 cand = round_up(this_start, align);
123 if (cand < this_end && this_end - cand >= size)
124 return cand;
125 }
126
127 return 0;
128}
129
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800130/**
Tang Chen14028992013-11-12 15:07:57 -0800131 * __memblock_find_range_top_down - find free area utility, in top-down
132 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300133 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
134 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tang Chen14028992013-11-12 15:07:57 -0800135 * @size: size of free area to find
136 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800137 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700138 * @flags: pick from blocks based on memory attributes
Tang Chen14028992013-11-12 15:07:57 -0800139 *
140 * Utility called from memblock_find_in_range_node(), find free area top-down.
141 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300142 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800143 * Found address on success, 0 on failure.
Tang Chen14028992013-11-12 15:07:57 -0800144 */
145static phys_addr_t __init_memblock
146__memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700147 phys_addr_t size, phys_addr_t align, int nid,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300148 enum memblock_flags flags)
Tang Chen14028992013-11-12 15:07:57 -0800149{
150 phys_addr_t this_start, this_end, cand;
151 u64 i;
152
Tony Luckfc6daaf2015-06-24 16:58:09 -0700153 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
154 NULL) {
Tang Chen14028992013-11-12 15:07:57 -0800155 this_start = clamp(this_start, start, end);
156 this_end = clamp(this_end, start, end);
157
158 if (this_end < size)
159 continue;
160
161 cand = round_down(this_end - size, align);
162 if (cand >= this_start)
163 return cand;
164 }
165
166 return 0;
167}
168
169/**
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800170 * memblock_find_in_range_node - find free area in given range and node
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800171 * @size: size of free area to find
172 * @align: alignment of free area to find
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800173 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300174 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
175 * %MEMBLOCK_ALLOC_ACCESSIBLE
Grygorii Strashkob1154232014-01-21 15:50:16 -0800176 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700177 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800178 *
179 * Find @size free area aligned to @align in the specified range and node.
180 *
Tang Chen79442ed2013-11-12 15:07:59 -0800181 * When allocation direction is bottom-up, the @start should be greater
182 * than the end of the kernel image. Otherwise, it will be trimmed. The
183 * reason is that we want the bottom-up allocation just near the kernel
184 * image so it is highly likely that the allocated memory and the kernel
185 * will reside in the same node.
186 *
187 * If bottom-up allocation failed, will try to allocate memory top-down.
188 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300189 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800190 * Found address on success, 0 on failure.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000191 */
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800192phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
193 phys_addr_t align, phys_addr_t start,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300194 phys_addr_t end, int nid,
195 enum memblock_flags flags)
Tang Chenf7210e62013-02-22 16:33:51 -0800196{
Tang Chen0cfb8f02014-08-29 15:18:31 -0700197 phys_addr_t kernel_end, ret;
Tang Chen79442ed2013-11-12 15:07:59 -0800198
Tang Chenf7210e62013-02-22 16:33:51 -0800199 /* pump up @end */
200 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
201 end = memblock.current_limit;
202
203 /* avoid allocating the first page */
204 start = max_t(phys_addr_t, start, PAGE_SIZE);
205 end = max(start, end);
Tang Chen79442ed2013-11-12 15:07:59 -0800206 kernel_end = __pa_symbol(_end);
207
208 /*
209 * try bottom-up allocation only when bottom-up mode
210 * is set and @end is above the kernel image.
211 */
212 if (memblock_bottom_up() && end > kernel_end) {
213 phys_addr_t bottom_up_start;
214
215 /* make sure we will allocate above the kernel */
216 bottom_up_start = max(start, kernel_end);
217
218 /* ok, try bottom-up allocation first */
219 ret = __memblock_find_range_bottom_up(bottom_up_start, end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700220 size, align, nid, flags);
Tang Chen79442ed2013-11-12 15:07:59 -0800221 if (ret)
222 return ret;
223
224 /*
225 * we always limit bottom-up allocation above the kernel,
226 * but top-down allocation doesn't have the limit, so
227 * retrying top-down allocation may succeed when bottom-up
228 * allocation failed.
229 *
230 * bottom-up allocation is expected to be fail very rarely,
231 * so we use WARN_ONCE() here to see the stack trace if
232 * fail happens.
233 */
Joe Perches756a0252016-03-17 14:19:47 -0700234 WARN_ONCE(1, "memblock: bottom-up allocation failed, memory hotunplug may be affected\n");
Tang Chen79442ed2013-11-12 15:07:59 -0800235 }
Tang Chenf7210e62013-02-22 16:33:51 -0800236
Tony Luckfc6daaf2015-06-24 16:58:09 -0700237 return __memblock_find_range_top_down(start, end, size, align, nid,
238 flags);
Tang Chenf7210e62013-02-22 16:33:51 -0800239}
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000240
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800241/**
242 * memblock_find_in_range - find free area in given range
243 * @start: start of candidate range
Mike Rapoport47cec442018-06-30 17:55:02 +0300244 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
245 * %MEMBLOCK_ALLOC_ACCESSIBLE
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800246 * @size: size of free area to find
247 * @align: alignment of free area to find
248 *
249 * Find @size free area aligned to @align in the specified range.
250 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300251 * Return:
Tang Chen79442ed2013-11-12 15:07:59 -0800252 * Found address on success, 0 on failure.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800253 */
254phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
255 phys_addr_t end, phys_addr_t size,
256 phys_addr_t align)
257{
Tony Lucka3f5baf2015-06-24 16:58:12 -0700258 phys_addr_t ret;
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300259 enum memblock_flags flags = choose_memblock_flags();
Tony Lucka3f5baf2015-06-24 16:58:12 -0700260
261again:
262 ret = memblock_find_in_range_node(size, align, start, end,
263 NUMA_NO_NODE, flags);
264
265 if (!ret && (flags & MEMBLOCK_MIRROR)) {
266 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
267 &size);
268 flags &= ~MEMBLOCK_MIRROR;
269 goto again;
270 }
271
272 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800273}
274
Yinghai Lu10d06432010-07-28 15:43:02 +1000275static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000276{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800277 type->total_size -= type->regions[r].size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200278 memmove(&type->regions[r], &type->regions[r + 1],
279 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000280 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000281
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700282 /* Special case for empty arrays */
283 if (type->cnt == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800284 WARN_ON(type->total_size != 0);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700285 type->cnt = 1;
286 type->regions[0].base = 0;
287 type->regions[0].size = 0;
Tang Chen66a20752014-01-21 15:49:20 -0800288 type->regions[0].flags = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200289 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700290 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000291}
292
Philipp Hachtmann354f17e2014-01-23 15:53:24 -0800293#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
Pavel Tatashin3010f872017-08-18 15:16:05 -0700294/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300295 * memblock_discard - discard memory and reserved arrays if they were allocated
Pavel Tatashin3010f872017-08-18 15:16:05 -0700296 */
297void __init memblock_discard(void)
Yinghai Lu29f67382012-07-11 14:02:56 -0700298{
Pavel Tatashin3010f872017-08-18 15:16:05 -0700299 phys_addr_t addr, size;
Yinghai Lu29f67382012-07-11 14:02:56 -0700300
Pavel Tatashin3010f872017-08-18 15:16:05 -0700301 if (memblock.reserved.regions != memblock_reserved_init_regions) {
302 addr = __pa(memblock.reserved.regions);
303 size = PAGE_ALIGN(sizeof(struct memblock_region) *
304 memblock.reserved.max);
305 __memblock_free_late(addr, size);
306 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700307
Pavel Tatashin91b540f2017-08-25 15:55:46 -0700308 if (memblock.memory.regions != memblock_memory_init_regions) {
Pavel Tatashin3010f872017-08-18 15:16:05 -0700309 addr = __pa(memblock.memory.regions);
310 size = PAGE_ALIGN(sizeof(struct memblock_region) *
311 memblock.memory.max);
312 __memblock_free_late(addr, size);
313 }
Yinghai Lu29f67382012-07-11 14:02:56 -0700314}
Philipp Hachtmann5e270e22014-01-23 15:53:11 -0800315#endif
316
Greg Pearson48c3b582012-06-20 12:53:05 -0700317/**
318 * memblock_double_array - double the size of the memblock regions array
319 * @type: memblock type of the regions array being doubled
320 * @new_area_start: starting address of memory range to avoid overlap with
321 * @new_area_size: size of memory range to avoid overlap with
322 *
323 * Double the size of the @type regions array. If memblock is being used to
324 * allocate memory for a new reserved regions array and there is a previously
Mike Rapoport47cec442018-06-30 17:55:02 +0300325 * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
Greg Pearson48c3b582012-06-20 12:53:05 -0700326 * waiting to be reserved, ensure the memory used by the new array does
327 * not overlap.
328 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300329 * Return:
Greg Pearson48c3b582012-06-20 12:53:05 -0700330 * 0 on success, -1 on failure.
331 */
332static int __init_memblock memblock_double_array(struct memblock_type *type,
333 phys_addr_t new_area_start,
334 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700335{
336 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700337 phys_addr_t old_alloc_size, new_alloc_size;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700338 phys_addr_t old_size, new_size, addr;
339 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700340 int *in_slab;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700341
342 /* We don't allow resizing until we know about the reserved regions
343 * of memory that aren't suitable for allocation
344 */
345 if (!memblock_can_resize)
346 return -1;
347
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700348 /* Calculate new doubled size */
349 old_size = type->max * sizeof(struct memblock_region);
350 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700351 /*
352 * We need to allocated new one align to PAGE_SIZE,
353 * so we can free them completely later.
354 */
355 old_alloc_size = PAGE_ALIGN(old_size);
356 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700357
Gavin Shan181eb392012-05-29 15:06:50 -0700358 /* Retrieve the slab flag */
359 if (type == &memblock.memory)
360 in_slab = &memblock_memory_in_slab;
361 else
362 in_slab = &memblock_reserved_in_slab;
363
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700364 /* Try to find some space for it.
365 *
366 * WARNING: We assume that either slab_is_available() and we use it or
Andrew Mortonfd073832012-07-31 16:42:40 -0700367 * we use MEMBLOCK for allocations. That means that this is unsafe to
368 * use when bootmem is currently active (unless bootmem itself is
369 * implemented on top of MEMBLOCK which isn't the case yet)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700370 *
371 * This should however not be an issue for now, as we currently only
Andrew Mortonfd073832012-07-31 16:42:40 -0700372 * call into MEMBLOCK while it's still active, or much later when slab
373 * is active for memory hotplug operations
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700374 */
375 if (use_slab) {
376 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200377 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700378 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700379 /* only exclude range when trying to double reserved.regions */
380 if (type != &memblock.reserved)
381 new_area_start = new_area_size = 0;
382
383 addr = memblock_find_in_range(new_area_start + new_area_size,
384 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700385 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700386 if (!addr && new_area_size)
387 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700388 min(new_area_start, memblock.current_limit),
389 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700390
Sachin Kamat15674862012-09-04 13:55:05 +0530391 new_array = addr ? __va(addr) : NULL;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700392 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200393 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700394 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800395 type->name, type->max, type->max * 2);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700396 return -1;
397 }
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700398
Andrew Mortonfd073832012-07-31 16:42:40 -0700399 memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
Heiko Carstens0262d9c2017-02-24 14:55:59 -0800400 type->name, type->max * 2, (u64)addr,
Andrew Mortonfd073832012-07-31 16:42:40 -0700401 (u64)addr + new_size - 1);
Yinghai Luea9e4372010-07-28 15:13:22 +1000402
Andrew Mortonfd073832012-07-31 16:42:40 -0700403 /*
404 * Found space, we now need to move the array over before we add the
405 * reserved region since it may be our reserved array itself that is
406 * full.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700407 */
408 memcpy(new_array, type->regions, old_size);
409 memset(new_array + type->max, 0, old_size);
410 old_array = type->regions;
411 type->regions = new_array;
412 type->max <<= 1;
413
Andrew Mortonfd073832012-07-31 16:42:40 -0700414 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700415 if (*in_slab)
416 kfree(old_array);
417 else if (old_array != memblock_memory_init_regions &&
418 old_array != memblock_reserved_init_regions)
Yinghai Lu29f67382012-07-11 14:02:56 -0700419 memblock_free(__pa(old_array), old_alloc_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700420
Andrew Mortonfd073832012-07-31 16:42:40 -0700421 /*
422 * Reserve the new array if that comes from the memblock. Otherwise, we
423 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700424 */
425 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700426 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700427
428 /* Update slab flag */
429 *in_slab = use_slab;
430
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700431 return 0;
432}
433
Tejun Heo784656f92011-07-12 11:15:55 +0200434/**
435 * memblock_merge_regions - merge neighboring compatible regions
436 * @type: memblock type to scan
437 *
438 * Scan @type and merge neighboring compatible regions.
439 */
440static void __init_memblock memblock_merge_regions(struct memblock_type *type)
441{
442 int i = 0;
443
444 /* cnt never goes below 1 */
445 while (i < type->cnt - 1) {
446 struct memblock_region *this = &type->regions[i];
447 struct memblock_region *next = &type->regions[i + 1];
448
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200449 if (this->base + this->size != next->base ||
450 memblock_get_region_node(this) !=
Tang Chen66a20752014-01-21 15:49:20 -0800451 memblock_get_region_node(next) ||
452 this->flags != next->flags) {
Tejun Heo784656f92011-07-12 11:15:55 +0200453 BUG_ON(this->base + this->size > next->base);
454 i++;
455 continue;
456 }
457
458 this->size += next->size;
Lin Fengc0232ae2013-01-11 14:31:44 -0800459 /* move forward from next + 1, index of which is i + 2 */
460 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
Tejun Heo784656f92011-07-12 11:15:55 +0200461 type->cnt--;
462 }
463}
464
465/**
466 * memblock_insert_region - insert new memblock region
Tang Chen209ff862013-04-29 15:08:41 -0700467 * @type: memblock type to insert into
468 * @idx: index for the insertion point
469 * @base: base address of the new region
470 * @size: size of the new region
471 * @nid: node id of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800472 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200473 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300474 * Insert new memblock region [@base, @base + @size) into @type at @idx.
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700475 * @type must already have extra room to accommodate the new region.
Tejun Heo784656f92011-07-12 11:15:55 +0200476 */
477static void __init_memblock memblock_insert_region(struct memblock_type *type,
478 int idx, phys_addr_t base,
Tang Chen66a20752014-01-21 15:49:20 -0800479 phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300480 int nid,
481 enum memblock_flags flags)
Tejun Heo784656f92011-07-12 11:15:55 +0200482{
483 struct memblock_region *rgn = &type->regions[idx];
484
485 BUG_ON(type->cnt >= type->max);
486 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
487 rgn->base = base;
488 rgn->size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800489 rgn->flags = flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200490 memblock_set_region_node(rgn, nid);
Tejun Heo784656f92011-07-12 11:15:55 +0200491 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800492 type->total_size += size;
Tejun Heo784656f92011-07-12 11:15:55 +0200493}
494
495/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100496 * memblock_add_range - add new memblock region
Tejun Heo784656f92011-07-12 11:15:55 +0200497 * @type: memblock type to add new region into
498 * @base: base address of the new region
499 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800500 * @nid: nid of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800501 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200502 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300503 * Add new memblock region [@base, @base + @size) into @type. The new region
Tejun Heo784656f92011-07-12 11:15:55 +0200504 * is allowed to overlap with existing ones - overlaps don't affect already
505 * existing regions. @type is guaranteed to be minimal (all neighbouring
506 * compatible regions are merged) after the addition.
507 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300508 * Return:
Tejun Heo784656f92011-07-12 11:15:55 +0200509 * 0 on success, -errno on failure.
510 */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100511int __init_memblock memblock_add_range(struct memblock_type *type,
Tang Chen66a20752014-01-21 15:49:20 -0800512 phys_addr_t base, phys_addr_t size,
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300513 int nid, enum memblock_flags flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000514{
Tejun Heo784656f92011-07-12 11:15:55 +0200515 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800516 phys_addr_t obase = base;
517 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800518 int idx, nr_new;
519 struct memblock_region *rgn;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000520
Tejun Heob3dc6272012-04-20 08:31:34 -0700521 if (!size)
522 return 0;
523
Tejun Heo784656f92011-07-12 11:15:55 +0200524 /* special case for empty array */
525 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800526 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000527 type->regions[0].base = base;
528 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800529 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800530 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800531 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000532 return 0;
533 }
Tejun Heo784656f92011-07-12 11:15:55 +0200534repeat:
535 /*
536 * The following is executed twice. Once with %false @insert and
537 * then with %true. The first counts the number of regions needed
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700538 * to accommodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700539 */
Tejun Heo784656f92011-07-12 11:15:55 +0200540 base = obase;
541 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000542
Gioh Kim66e8b432017-11-15 17:33:42 -0800543 for_each_memblock_type(idx, type, rgn) {
Tejun Heo784656f92011-07-12 11:15:55 +0200544 phys_addr_t rbase = rgn->base;
545 phys_addr_t rend = rbase + rgn->size;
546
547 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000548 break;
Tejun Heo784656f92011-07-12 11:15:55 +0200549 if (rend <= base)
550 continue;
551 /*
552 * @rgn overlaps. If it separates the lower part of new
553 * area, insert that portion.
554 */
555 if (rbase > base) {
Wei Yangc0a29492015-09-04 15:47:38 -0700556#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
557 WARN_ON(nid != memblock_get_region_node(rgn));
558#endif
Wei Yang4fcab5f2015-09-08 14:59:53 -0700559 WARN_ON(flags != rgn->flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200560 nr_new++;
561 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800562 memblock_insert_region(type, idx++, base,
Tang Chen66a20752014-01-21 15:49:20 -0800563 rbase - base, nid,
564 flags);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000565 }
Tejun Heo784656f92011-07-12 11:15:55 +0200566 /* area below @rend is dealt with, forget about it */
567 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000568 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000569
Tejun Heo784656f92011-07-12 11:15:55 +0200570 /* insert the remaining portion */
571 if (base < end) {
572 nr_new++;
573 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800574 memblock_insert_region(type, idx, base, end - base,
Tang Chen66a20752014-01-21 15:49:20 -0800575 nid, flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200576 }
577
nimisoloef3cc4d2016-07-26 15:24:56 -0700578 if (!nr_new)
579 return 0;
580
Tejun Heo784656f92011-07-12 11:15:55 +0200581 /*
582 * If this was the first round, resize array and repeat for actual
583 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700584 */
Tejun Heo784656f92011-07-12 11:15:55 +0200585 if (!insert) {
586 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700587 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f92011-07-12 11:15:55 +0200588 return -ENOMEM;
589 insert = true;
590 goto repeat;
591 } else {
592 memblock_merge_regions(type);
593 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700594 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000595}
596
Mike Rapoport48a833c2018-06-30 17:55:03 +0300597/**
598 * memblock_add_node - add new memblock region within a NUMA node
599 * @base: base address of the new region
600 * @size: size of the new region
601 * @nid: nid of the new region
602 *
603 * Add new memblock region [@base, @base + @size) to the "memory"
604 * type. See memblock_add_range() description for mode details
605 *
606 * Return:
607 * 0 on success, -errno on failure.
608 */
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800609int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
610 int nid)
611{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100612 return memblock_add_range(&memblock.memory, base, size, nid, 0);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800613}
614
Mike Rapoport48a833c2018-06-30 17:55:03 +0300615/**
616 * memblock_add - add new memblock region
617 * @base: base address of the new region
618 * @size: size of the new region
619 *
620 * Add new memblock region [@base, @base + @size) to the "memory"
621 * type. See memblock_add_range() description for mode details
622 *
623 * Return:
624 * 0 on success, -errno on failure.
625 */
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700626int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700627{
Miles Chen5d63f812017-02-22 15:46:42 -0800628 phys_addr_t end = base + size - 1;
629
630 memblock_dbg("memblock_add: [%pa-%pa] %pF\n",
631 &base, &end, (void *)_RET_IP_);
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700632
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700633 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000634}
635
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800636/**
637 * memblock_isolate_range - isolate given range into disjoint memblocks
638 * @type: memblock type to isolate range for
639 * @base: base of range to isolate
640 * @size: size of range to isolate
641 * @start_rgn: out parameter for the start of isolated region
642 * @end_rgn: out parameter for the end of isolated region
643 *
644 * Walk @type and ensure that regions don't cross the boundaries defined by
Mike Rapoport47cec442018-06-30 17:55:02 +0300645 * [@base, @base + @size). Crossing regions are split at the boundaries,
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800646 * which may create at most two more regions. The index of the first
647 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
648 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300649 * Return:
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800650 * 0 on success, -errno on failure.
651 */
652static int __init_memblock memblock_isolate_range(struct memblock_type *type,
653 phys_addr_t base, phys_addr_t size,
654 int *start_rgn, int *end_rgn)
655{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800656 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800657 int idx;
658 struct memblock_region *rgn;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800659
660 *start_rgn = *end_rgn = 0;
661
Tejun Heob3dc6272012-04-20 08:31:34 -0700662 if (!size)
663 return 0;
664
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800665 /* we'll create at most two more regions */
666 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700667 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800668 return -ENOMEM;
669
Gioh Kim66e8b432017-11-15 17:33:42 -0800670 for_each_memblock_type(idx, type, rgn) {
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800671 phys_addr_t rbase = rgn->base;
672 phys_addr_t rend = rbase + rgn->size;
673
674 if (rbase >= end)
675 break;
676 if (rend <= base)
677 continue;
678
679 if (rbase < base) {
680 /*
681 * @rgn intersects from below. Split and continue
682 * to process the next region - the new top half.
683 */
684 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800685 rgn->size -= base - rbase;
686 type->total_size -= base - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800687 memblock_insert_region(type, idx, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800688 memblock_get_region_node(rgn),
689 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800690 } else if (rend > end) {
691 /*
692 * @rgn intersects from above. Split and redo the
693 * current region - the new bottom half.
694 */
695 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800696 rgn->size -= end - rbase;
697 type->total_size -= end - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800698 memblock_insert_region(type, idx--, rbase, end - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800699 memblock_get_region_node(rgn),
700 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800701 } else {
702 /* @rgn is fully contained, record it */
703 if (!*end_rgn)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800704 *start_rgn = idx;
705 *end_rgn = idx + 1;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800706 }
707 }
708
709 return 0;
710}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800711
Alexander Kuleshov35bd16a2015-11-05 18:47:00 -0800712static int __init_memblock memblock_remove_range(struct memblock_type *type,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100713 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000714{
Tejun Heo71936182011-12-08 10:22:07 -0800715 int start_rgn, end_rgn;
716 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000717
Tejun Heo71936182011-12-08 10:22:07 -0800718 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
719 if (ret)
720 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000721
Tejun Heo71936182011-12-08 10:22:07 -0800722 for (i = end_rgn - 1; i >= start_rgn; i--)
723 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700724 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000725}
726
Tejun Heo581adcb2011-12-08 10:22:06 -0800727int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000728{
Minchan Kim25cf23d2018-06-07 17:07:35 -0700729 phys_addr_t end = base + size - 1;
730
731 memblock_dbg("memblock_remove: [%pa-%pa] %pS\n",
732 &base, &end, (void *)_RET_IP_);
733
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100734 return memblock_remove_range(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000735}
736
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100737
Tejun Heo581adcb2011-12-08 10:22:06 -0800738int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000739{
Miles Chen5d63f812017-02-22 15:46:42 -0800740 phys_addr_t end = base + size - 1;
741
742 memblock_dbg(" memblock_free: [%pa-%pa] %pF\n",
743 &base, &end, (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200744
Catalin Marinas9099dae2016-10-11 13:55:11 -0700745 kmemleak_free_part_phys(base, size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100746 return memblock_remove_range(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000747}
748
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700749int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000750{
Miles Chen5d63f812017-02-22 15:46:42 -0800751 phys_addr_t end = base + size - 1;
752
753 memblock_dbg("memblock_reserve: [%pa-%pa] %pF\n",
754 &base, &end, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000755
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700756 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000757}
758
Tejun Heo35fd0802011-07-12 11:15:59 +0200759/**
Mike Rapoport47cec442018-06-30 17:55:02 +0300760 * memblock_setclr_flag - set or clear flag for a memory region
761 * @base: base address of the region
762 * @size: size of the region
763 * @set: set or clear the flag
764 * @flag: the flag to udpate
Tang Chen66b16ed2014-01-21 15:49:23 -0800765 *
Tony Luck4308ce12014-12-12 16:54:59 -0800766 * This function isolates region [@base, @base + @size), and sets/clears flag
Tang Chen66b16ed2014-01-21 15:49:23 -0800767 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300768 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800769 */
Tony Luck4308ce12014-12-12 16:54:59 -0800770static int __init_memblock memblock_setclr_flag(phys_addr_t base,
771 phys_addr_t size, int set, int flag)
Tang Chen66b16ed2014-01-21 15:49:23 -0800772{
773 struct memblock_type *type = &memblock.memory;
774 int i, ret, start_rgn, end_rgn;
775
776 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
777 if (ret)
778 return ret;
779
780 for (i = start_rgn; i < end_rgn; i++)
Tony Luck4308ce12014-12-12 16:54:59 -0800781 if (set)
782 memblock_set_region_flags(&type->regions[i], flag);
783 else
784 memblock_clear_region_flags(&type->regions[i], flag);
Tang Chen66b16ed2014-01-21 15:49:23 -0800785
786 memblock_merge_regions(type);
787 return 0;
788}
789
790/**
Tony Luck4308ce12014-12-12 16:54:59 -0800791 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
792 * @base: the base phys addr of the region
793 * @size: the size of the region
794 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300795 * Return: 0 on success, -errno on failure.
Tony Luck4308ce12014-12-12 16:54:59 -0800796 */
797int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
798{
799 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
800}
801
802/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800803 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
804 * @base: the base phys addr of the region
805 * @size: the size of the region
806 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300807 * Return: 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800808 */
809int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
810{
Tony Luck4308ce12014-12-12 16:54:59 -0800811 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
Tang Chen66b16ed2014-01-21 15:49:23 -0800812}
813
814/**
Tony Lucka3f5baf2015-06-24 16:58:12 -0700815 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
816 * @base: the base phys addr of the region
817 * @size: the size of the region
818 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300819 * Return: 0 on success, -errno on failure.
Tony Lucka3f5baf2015-06-24 16:58:12 -0700820 */
821int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
822{
823 system_has_some_mirror = true;
824
825 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
826}
827
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100828/**
829 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
830 * @base: the base phys addr of the region
831 * @size: the size of the region
832 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300833 * Return: 0 on success, -errno on failure.
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100834 */
835int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
836{
837 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
838}
Tony Lucka3f5baf2015-06-24 16:58:12 -0700839
840/**
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900841 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
842 * @base: the base phys addr of the region
843 * @size: the size of the region
844 *
Mike Rapoport47cec442018-06-30 17:55:02 +0300845 * Return: 0 on success, -errno on failure.
AKASHI Takahiro4c546b82017-04-03 11:23:54 +0900846 */
847int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
848{
849 return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
850}
851
852/**
Robin Holt8e7a7f82015-06-30 14:56:41 -0700853 * __next_reserved_mem_region - next function for for_each_reserved_region()
854 * @idx: pointer to u64 loop variable
855 * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
856 * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
857 *
858 * Iterate over all reserved memory regions.
859 */
860void __init_memblock __next_reserved_mem_region(u64 *idx,
861 phys_addr_t *out_start,
862 phys_addr_t *out_end)
863{
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700864 struct memblock_type *type = &memblock.reserved;
Robin Holt8e7a7f82015-06-30 14:56:41 -0700865
Richard Leitnercd33a762016-05-20 16:58:33 -0700866 if (*idx < type->cnt) {
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700867 struct memblock_region *r = &type->regions[*idx];
Robin Holt8e7a7f82015-06-30 14:56:41 -0700868 phys_addr_t base = r->base;
869 phys_addr_t size = r->size;
870
871 if (out_start)
872 *out_start = base;
873 if (out_end)
874 *out_end = base + size - 1;
875
876 *idx += 1;
877 return;
878 }
879
880 /* signal end of iteration */
881 *idx = ULLONG_MAX;
882}
883
884/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100885 * __next__mem_range - next function for for_each_free_mem_range() etc.
Tejun Heo35fd0802011-07-12 11:15:59 +0200886 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800887 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700888 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100889 * @type_a: pointer to memblock_type from where the range is taken
890 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -0700891 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
892 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
893 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +0200894 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100895 * Find the first area from *@idx which matches @nid, fill the out
Tejun Heo35fd0802011-07-12 11:15:59 +0200896 * parameters, and update *@idx for the next iteration. The lower 32bit of
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100897 * *@idx contains index into type_a and the upper 32bit indexes the
898 * areas before each region in type_b. For example, if type_b regions
Tejun Heo35fd0802011-07-12 11:15:59 +0200899 * look like the following,
900 *
901 * 0:[0-16), 1:[32-48), 2:[128-130)
902 *
903 * The upper 32bit indexes the following regions.
904 *
905 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
906 *
907 * As both region arrays are sorted, the function advances the two indices
908 * in lockstep and returns each intersection.
909 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +0300910void __init_memblock __next_mem_range(u64 *idx, int nid,
911 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100912 struct memblock_type *type_a,
913 struct memblock_type *type_b,
914 phys_addr_t *out_start,
915 phys_addr_t *out_end, int *out_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200916{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100917 int idx_a = *idx & 0xffffffff;
918 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -0800919
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100920 if (WARN_ONCE(nid == MAX_NUMNODES,
921 "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
Grygorii Strashko560dca272014-01-21 15:50:55 -0800922 nid = NUMA_NO_NODE;
Tejun Heo35fd0802011-07-12 11:15:59 +0200923
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100924 for (; idx_a < type_a->cnt; idx_a++) {
925 struct memblock_region *m = &type_a->regions[idx_a];
926
Tejun Heo35fd0802011-07-12 11:15:59 +0200927 phys_addr_t m_start = m->base;
928 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100929 int m_nid = memblock_get_region_node(m);
Tejun Heo35fd0802011-07-12 11:15:59 +0200930
931 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100932 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200933 continue;
934
Xishi Qiu0a313a92014-09-09 14:50:46 -0700935 /* skip hotpluggable memory regions if needed */
936 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
937 continue;
938
Tony Lucka3f5baf2015-06-24 16:58:12 -0700939 /* if we want mirror memory skip non-mirror memory regions */
940 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
941 continue;
942
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100943 /* skip nomap memory unless we were asked for it explicitly */
944 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
945 continue;
946
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100947 if (!type_b) {
948 if (out_start)
949 *out_start = m_start;
950 if (out_end)
951 *out_end = m_end;
952 if (out_nid)
953 *out_nid = m_nid;
954 idx_a++;
955 *idx = (u32)idx_a | (u64)idx_b << 32;
956 return;
957 }
Tejun Heo35fd0802011-07-12 11:15:59 +0200958
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100959 /* scan areas before each reservation */
960 for (; idx_b < type_b->cnt + 1; idx_b++) {
961 struct memblock_region *r;
962 phys_addr_t r_start;
963 phys_addr_t r_end;
964
965 r = &type_b->regions[idx_b];
966 r_start = idx_b ? r[-1].base + r[-1].size : 0;
967 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -0700968 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100969
970 /*
971 * if idx_b advanced past idx_a,
972 * break out to advance idx_a
973 */
Tejun Heo35fd0802011-07-12 11:15:59 +0200974 if (r_start >= m_end)
975 break;
976 /* if the two regions intersect, we're done */
977 if (m_start < r_end) {
978 if (out_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100979 *out_start =
980 max(m_start, r_start);
Tejun Heo35fd0802011-07-12 11:15:59 +0200981 if (out_end)
982 *out_end = min(m_end, r_end);
983 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100984 *out_nid = m_nid;
Tejun Heo35fd0802011-07-12 11:15:59 +0200985 /*
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100986 * The region which ends first is
987 * advanced for the next iteration.
Tejun Heo35fd0802011-07-12 11:15:59 +0200988 */
989 if (m_end <= r_end)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100990 idx_a++;
Tejun Heo35fd0802011-07-12 11:15:59 +0200991 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100992 idx_b++;
993 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo35fd0802011-07-12 11:15:59 +0200994 return;
995 }
996 }
997 }
998
999 /* signal end of iteration */
1000 *idx = ULLONG_MAX;
1001}
1002
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001003/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001004 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1005 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001006 * @idx: pointer to u64 loop variable
Alexander Kuleshovad5ea8c2015-09-08 15:04:22 -07001007 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -07001008 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001009 * @type_a: pointer to memblock_type from where the range is taken
1010 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -07001011 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1012 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1013 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001014 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001015 * Finds the next range from type_a which is not marked as unsuitable
1016 * in type_b.
1017 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001018 * Reverse of __next_mem_range().
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001019 */
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001020void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
1021 enum memblock_flags flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001022 struct memblock_type *type_a,
1023 struct memblock_type *type_b,
1024 phys_addr_t *out_start,
1025 phys_addr_t *out_end, int *out_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001026{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001027 int idx_a = *idx & 0xffffffff;
1028 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -08001029
Grygorii Strashko560dca272014-01-21 15:50:55 -08001030 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1031 nid = NUMA_NO_NODE;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001032
1033 if (*idx == (u64)ULLONG_MAX) {
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001034 idx_a = type_a->cnt - 1;
zijun_hue47608a2016-08-04 15:32:00 -07001035 if (type_b != NULL)
1036 idx_b = type_b->cnt;
1037 else
1038 idx_b = 0;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001039 }
1040
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001041 for (; idx_a >= 0; idx_a--) {
1042 struct memblock_region *m = &type_a->regions[idx_a];
1043
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001044 phys_addr_t m_start = m->base;
1045 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001046 int m_nid = memblock_get_region_node(m);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001047
1048 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001049 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001050 continue;
1051
Tang Chen55ac5902014-01-21 15:49:35 -08001052 /* skip hotpluggable memory regions if needed */
1053 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
1054 continue;
1055
Tony Lucka3f5baf2015-06-24 16:58:12 -07001056 /* if we want mirror memory skip non-mirror memory regions */
1057 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1058 continue;
1059
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001060 /* skip nomap memory unless we were asked for it explicitly */
1061 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1062 continue;
1063
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001064 if (!type_b) {
1065 if (out_start)
1066 *out_start = m_start;
1067 if (out_end)
1068 *out_end = m_end;
1069 if (out_nid)
1070 *out_nid = m_nid;
zijun_hufb399b42016-07-28 15:48:56 -07001071 idx_a--;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001072 *idx = (u32)idx_a | (u64)idx_b << 32;
1073 return;
1074 }
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001075
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001076 /* scan areas before each reservation */
1077 for (; idx_b >= 0; idx_b--) {
1078 struct memblock_region *r;
1079 phys_addr_t r_start;
1080 phys_addr_t r_end;
1081
1082 r = &type_b->regions[idx_b];
1083 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1084 r_end = idx_b < type_b->cnt ?
Stefan Agner1c4bc432018-06-07 17:06:15 -07001085 r->base : PHYS_ADDR_MAX;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001086 /*
1087 * if idx_b advanced past idx_a,
1088 * break out to advance idx_a
1089 */
1090
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001091 if (r_end <= m_start)
1092 break;
1093 /* if the two regions intersect, we're done */
1094 if (m_end > r_start) {
1095 if (out_start)
1096 *out_start = max(m_start, r_start);
1097 if (out_end)
1098 *out_end = min(m_end, r_end);
1099 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001100 *out_nid = m_nid;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001101 if (m_start >= r_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001102 idx_a--;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001103 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001104 idx_b--;
1105 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001106 return;
1107 }
1108 }
1109 }
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001110 /* signal end of iteration */
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001111 *idx = ULLONG_MAX;
1112}
1113
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001114#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1115/*
1116 * Common iterator interface used to define for_each_mem_range().
1117 */
1118void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1119 unsigned long *out_start_pfn,
1120 unsigned long *out_end_pfn, int *out_nid)
1121{
1122 struct memblock_type *type = &memblock.memory;
1123 struct memblock_region *r;
1124
1125 while (++*idx < type->cnt) {
1126 r = &type->regions[*idx];
1127
1128 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1129 continue;
1130 if (nid == MAX_NUMNODES || nid == r->nid)
1131 break;
1132 }
1133 if (*idx >= type->cnt) {
1134 *idx = -1;
1135 return;
1136 }
1137
1138 if (out_start_pfn)
1139 *out_start_pfn = PFN_UP(r->base);
1140 if (out_end_pfn)
1141 *out_end_pfn = PFN_DOWN(r->base + r->size);
1142 if (out_nid)
1143 *out_nid = r->nid;
1144}
1145
1146/**
1147 * memblock_set_node - set node ID on memblock regions
1148 * @base: base of area to set node ID for
1149 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -08001150 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001151 * @nid: node ID to set
1152 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001153 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001154 * Regions which cross the area boundaries are split as necessary.
1155 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001156 * Return:
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001157 * 0 on success, -errno on failure.
1158 */
1159int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -08001160 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001161{
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001162 int start_rgn, end_rgn;
1163 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001164
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001165 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1166 if (ret)
1167 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001168
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001169 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -07001170 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001171
1172 memblock_merge_regions(type);
1173 return 0;
1174}
1175#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1176
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001177static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1178 phys_addr_t align, phys_addr_t start,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001179 phys_addr_t end, int nid,
1180 enum memblock_flags flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001181{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001182 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001183
Grygorii Strashko79f40fa2014-01-21 15:50:12 -08001184 if (!align)
1185 align = SMP_CACHE_BYTES;
Vineet Gupta94f3d3a2013-04-29 15:06:15 -07001186
Tony Luckfc6daaf2015-06-24 16:58:09 -07001187 found = memblock_find_in_range_node(size, align, start, end, nid,
1188 flags);
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001189 if (found && !memblock_reserve(found, size)) {
1190 /*
1191 * The min_count is set to 0 so that memblock allocations are
1192 * never reported as leaks.
1193 */
Catalin Marinas9099dae2016-10-11 13:55:11 -07001194 kmemleak_alloc_phys(found, size, 0, 0);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001195 return found;
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001196 }
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001197 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001198}
1199
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001200phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001201 phys_addr_t start, phys_addr_t end,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001202 enum memblock_flags flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001203{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001204 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1205 flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001206}
1207
Nicholas Pigginb575454f2018-02-14 01:08:15 +10001208phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001209 phys_addr_t align, phys_addr_t max_addr,
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001210 int nid, enum memblock_flags flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001211{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001212 return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001213}
1214
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001215phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
1216{
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001217 enum memblock_flags flags = choose_memblock_flags();
Tony Lucka3f5baf2015-06-24 16:58:12 -07001218 phys_addr_t ret;
1219
1220again:
1221 ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE,
1222 nid, flags);
1223
1224 if (!ret && (flags & MEMBLOCK_MIRROR)) {
1225 flags &= ~MEMBLOCK_MIRROR;
1226 goto again;
1227 }
1228 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001229}
1230
1231phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1232{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001233 return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE,
1234 MEMBLOCK_NONE);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001235}
1236
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001237phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001238{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001239 phys_addr_t alloc;
1240
1241 alloc = __memblock_alloc_base(size, align, max_addr);
1242
1243 if (alloc == 0)
Miles Chen5d63f812017-02-22 15:46:42 -08001244 panic("ERROR: Failed to allocate %pa bytes below %pa.\n",
1245 &size, &max_addr);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001246
1247 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001248}
1249
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001250phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001251{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001252 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001253}
1254
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001255phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1256{
1257 phys_addr_t res = memblock_alloc_nid(size, align, nid);
1258
1259 if (res)
1260 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +02001261 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001262}
1263
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001264/**
1265 * memblock_virt_alloc_internal - allocate boot memory block
1266 * @size: size of memory block to be allocated in bytes
1267 * @align: alignment of the region and block's size
1268 * @min_addr: the lower bound of the memory region to allocate (phys address)
1269 * @max_addr: the upper bound of the memory region to allocate (phys address)
1270 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1271 *
1272 * The @min_addr limit is dropped if it can not be satisfied and the allocation
1273 * will fall back to memory below @min_addr. Also, allocation may fall back
1274 * to any node in the system if the specified node can not
1275 * hold the requested memory.
1276 *
1277 * The allocation is performed from memory region limited by
1278 * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE.
1279 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001280 * The memory block is aligned on %SMP_CACHE_BYTES if @align == 0.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001281 *
1282 * The phys address of allocated boot memory block is converted to virtual and
1283 * allocated memory is reset to 0.
1284 *
1285 * In addition, function sets the min_count to 0 using kmemleak_alloc for
1286 * allocated boot memory block, so that it is never reported as leaks.
1287 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001288 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001289 * Virtual address of allocated memory block on success, NULL on failure.
1290 */
1291static void * __init memblock_virt_alloc_internal(
1292 phys_addr_t size, phys_addr_t align,
1293 phys_addr_t min_addr, phys_addr_t max_addr,
1294 int nid)
1295{
1296 phys_addr_t alloc;
1297 void *ptr;
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001298 enum memblock_flags flags = choose_memblock_flags();
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001299
Grygorii Strashko560dca272014-01-21 15:50:55 -08001300 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1301 nid = NUMA_NO_NODE;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001302
1303 /*
1304 * Detect any accidental use of these APIs after slab is ready, as at
1305 * this moment memblock may be deinitialized already and its
1306 * internal data may be destroyed (after execution of free_all_bootmem)
1307 */
1308 if (WARN_ON_ONCE(slab_is_available()))
1309 return kzalloc_node(size, GFP_NOWAIT, nid);
1310
1311 if (!align)
1312 align = SMP_CACHE_BYTES;
1313
Yinghai Luf544e142014-01-29 14:05:52 -08001314 if (max_addr > memblock.current_limit)
1315 max_addr = memblock.current_limit;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001316again:
1317 alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001318 nid, flags);
Wei Yang7d41c032017-02-22 15:45:07 -08001319 if (alloc && !memblock_reserve(alloc, size))
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001320 goto done;
1321
1322 if (nid != NUMA_NO_NODE) {
1323 alloc = memblock_find_in_range_node(size, align, min_addr,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001324 max_addr, NUMA_NO_NODE,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001325 flags);
Wei Yang7d41c032017-02-22 15:45:07 -08001326 if (alloc && !memblock_reserve(alloc, size))
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001327 goto done;
1328 }
1329
1330 if (min_addr) {
1331 min_addr = 0;
1332 goto again;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001333 }
1334
Tony Lucka3f5baf2015-06-24 16:58:12 -07001335 if (flags & MEMBLOCK_MIRROR) {
1336 flags &= ~MEMBLOCK_MIRROR;
1337 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1338 &size);
1339 goto again;
1340 }
1341
1342 return NULL;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001343done:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001344 ptr = phys_to_virt(alloc);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001345
1346 /*
1347 * The min_count is set to 0 so that bootmem allocated blocks
1348 * are never reported as leaks. This is because many of these blocks
1349 * are only referred via the physical address which is not
1350 * looked up by kmemleak.
1351 */
1352 kmemleak_alloc(ptr, size, 0, 0);
1353
1354 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001355}
1356
1357/**
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001358 * memblock_virt_alloc_try_nid_raw - allocate boot memory block without zeroing
1359 * memory and without panicking
1360 * @size: size of memory block to be allocated in bytes
1361 * @align: alignment of the region and block's size
1362 * @min_addr: the lower bound of the memory region from where the allocation
1363 * is preferred (phys address)
1364 * @max_addr: the upper bound of the memory region from where the allocation
1365 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1366 * allocate only from memory limited by memblock.current_limit value
1367 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1368 *
1369 * Public function, provides additional debug information (including caller
1370 * info), if enabled. Does not zero allocated memory, does not panic if request
1371 * cannot be satisfied.
1372 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001373 * Return:
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001374 * Virtual address of allocated memory block on success, NULL on failure.
1375 */
1376void * __init memblock_virt_alloc_try_nid_raw(
1377 phys_addr_t size, phys_addr_t align,
1378 phys_addr_t min_addr, phys_addr_t max_addr,
1379 int nid)
1380{
1381 void *ptr;
1382
1383 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1384 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1385 (u64)max_addr, (void *)_RET_IP_);
1386
1387 ptr = memblock_virt_alloc_internal(size, align,
1388 min_addr, max_addr, nid);
1389#ifdef CONFIG_DEBUG_VM
1390 if (ptr && size > 0)
Pavel Tatashinf165b372018-04-05 16:22:47 -07001391 memset(ptr, PAGE_POISON_PATTERN, size);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001392#endif
1393 return ptr;
1394}
1395
1396/**
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001397 * memblock_virt_alloc_try_nid_nopanic - allocate boot memory block
1398 * @size: size of memory block to be allocated in bytes
1399 * @align: alignment of the region and block's size
1400 * @min_addr: the lower bound of the memory region from where the allocation
1401 * is preferred (phys address)
1402 * @max_addr: the upper bound of the memory region from where the allocation
1403 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1404 * allocate only from memory limited by memblock.current_limit value
1405 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1406 *
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001407 * Public function, provides additional debug information (including caller
1408 * info), if enabled. This function zeroes the allocated memory.
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001409 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001410 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001411 * Virtual address of allocated memory block on success, NULL on failure.
1412 */
1413void * __init memblock_virt_alloc_try_nid_nopanic(
1414 phys_addr_t size, phys_addr_t align,
1415 phys_addr_t min_addr, phys_addr_t max_addr,
1416 int nid)
1417{
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001418 void *ptr;
1419
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001420 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1421 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1422 (u64)max_addr, (void *)_RET_IP_);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001423
1424 ptr = memblock_virt_alloc_internal(size, align,
1425 min_addr, max_addr, nid);
1426 if (ptr)
1427 memset(ptr, 0, size);
1428 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001429}
1430
1431/**
1432 * memblock_virt_alloc_try_nid - allocate boot memory block with panicking
1433 * @size: size of memory block to be allocated in bytes
1434 * @align: alignment of the region and block's size
1435 * @min_addr: the lower bound of the memory region from where the allocation
1436 * is preferred (phys address)
1437 * @max_addr: the upper bound of the memory region from where the allocation
1438 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1439 * allocate only from memory limited by memblock.current_limit value
1440 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1441 *
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001442 * Public panicking version of memblock_virt_alloc_try_nid_nopanic()
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001443 * which provides debug information (including caller info), if enabled,
1444 * and panics if the request can not be satisfied.
1445 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001446 * Return:
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001447 * Virtual address of allocated memory block on success, NULL on failure.
1448 */
1449void * __init memblock_virt_alloc_try_nid(
1450 phys_addr_t size, phys_addr_t align,
1451 phys_addr_t min_addr, phys_addr_t max_addr,
1452 int nid)
1453{
1454 void *ptr;
1455
1456 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1457 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1458 (u64)max_addr, (void *)_RET_IP_);
1459 ptr = memblock_virt_alloc_internal(size, align,
1460 min_addr, max_addr, nid);
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001461 if (ptr) {
1462 memset(ptr, 0, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001463 return ptr;
Pavel Tatashinea1f5f32017-11-15 17:36:27 -08001464 }
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001465
1466 panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx\n",
1467 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1468 (u64)max_addr);
1469 return NULL;
1470}
1471
1472/**
1473 * __memblock_free_early - free boot memory block
1474 * @base: phys starting address of the boot memory block
1475 * @size: size of the boot memory block in bytes
1476 *
1477 * Free boot memory block previously allocated by memblock_virt_alloc_xx() API.
1478 * The freeing memory will not be released to the buddy allocator.
1479 */
1480void __init __memblock_free_early(phys_addr_t base, phys_addr_t size)
1481{
1482 memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1483 __func__, (u64)base, (u64)base + size - 1,
1484 (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001485 kmemleak_free_part_phys(base, size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001486 memblock_remove_range(&memblock.reserved, base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001487}
1488
Mike Rapoport48a833c2018-06-30 17:55:03 +03001489/**
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001490 * __memblock_free_late - free bootmem block pages directly to buddy allocator
Mike Rapoport48a833c2018-06-30 17:55:03 +03001491 * @base: phys starting address of the boot memory block
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001492 * @size: size of the boot memory block in bytes
1493 *
1494 * This is only useful when the bootmem allocator has already been torn
1495 * down, but we are still initializing the system. Pages are released directly
1496 * to the buddy allocator, no bootmem metadata is updated because it is gone.
1497 */
1498void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1499{
1500 u64 cursor, end;
1501
1502 memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1503 __func__, (u64)base, (u64)base + size - 1,
1504 (void *)_RET_IP_);
Catalin Marinas9099dae2016-10-11 13:55:11 -07001505 kmemleak_free_part_phys(base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001506 cursor = PFN_UP(base);
1507 end = PFN_DOWN(base + size);
1508
1509 for (; cursor < end; cursor++) {
Mel Gormand70ddd72015-06-30 14:56:52 -07001510 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001511 totalram_pages++;
1512 }
1513}
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001514
1515/*
1516 * Remaining API functions
1517 */
1518
David Gibson1f1ffb8a2016-02-05 15:36:19 -08001519phys_addr_t __init_memblock memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001520{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001521 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001522}
1523
Srikar Dronamraju8907de52016-10-07 16:59:18 -07001524phys_addr_t __init_memblock memblock_reserved_size(void)
1525{
1526 return memblock.reserved.total_size;
1527}
1528
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001529phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1530{
1531 unsigned long pages = 0;
1532 struct memblock_region *r;
1533 unsigned long start_pfn, end_pfn;
1534
1535 for_each_memblock(memory, r) {
1536 start_pfn = memblock_region_memory_base_pfn(r);
1537 end_pfn = memblock_region_memory_end_pfn(r);
1538 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1539 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1540 pages += end_pfn - start_pfn;
1541 }
1542
Fabian Frederick16763232014-04-07 15:37:53 -07001543 return PFN_PHYS(pages);
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001544}
1545
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001546/* lowest address */
1547phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1548{
1549 return memblock.memory.regions[0].base;
1550}
1551
Yinghai Lu10d06432010-07-28 15:43:02 +10001552phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001553{
1554 int idx = memblock.memory.cnt - 1;
1555
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001556 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001557}
1558
Dennis Chena571d4e2016-07-28 15:48:26 -07001559static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001560{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001561 phys_addr_t max_addr = PHYS_ADDR_MAX;
Emil Medve136199f2014-04-07 15:37:52 -07001562 struct memblock_region *r;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001563
Dennis Chena571d4e2016-07-28 15:48:26 -07001564 /*
1565 * translate the memory @limit size into the max address within one of
1566 * the memory memblock regions, if the @limit exceeds the total size
Stefan Agner1c4bc432018-06-07 17:06:15 -07001567 * of those regions, max_addr will keep original value PHYS_ADDR_MAX
Dennis Chena571d4e2016-07-28 15:48:26 -07001568 */
Emil Medve136199f2014-04-07 15:37:52 -07001569 for_each_memblock(memory, r) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001570 if (limit <= r->size) {
1571 max_addr = r->base + limit;
1572 break;
1573 }
1574 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001575 }
1576
Dennis Chena571d4e2016-07-28 15:48:26 -07001577 return max_addr;
1578}
1579
1580void __init memblock_enforce_memory_limit(phys_addr_t limit)
1581{
Stefan Agner1c4bc432018-06-07 17:06:15 -07001582 phys_addr_t max_addr = PHYS_ADDR_MAX;
Dennis Chena571d4e2016-07-28 15:48:26 -07001583
1584 if (!limit)
1585 return;
1586
1587 max_addr = __find_max_addr(limit);
1588
1589 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001590 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001591 return;
1592
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001593 /* truncate both memory and reserved regions */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001594 memblock_remove_range(&memblock.memory, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001595 PHYS_ADDR_MAX);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001596 memblock_remove_range(&memblock.reserved, max_addr,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001597 PHYS_ADDR_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001598}
1599
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001600void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1601{
1602 int start_rgn, end_rgn;
1603 int i, ret;
1604
1605 if (!size)
1606 return;
1607
1608 ret = memblock_isolate_range(&memblock.memory, base, size,
1609 &start_rgn, &end_rgn);
1610 if (ret)
1611 return;
1612
1613 /* remove all the MAP regions */
1614 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1615 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1616 memblock_remove_region(&memblock.memory, i);
1617
1618 for (i = start_rgn - 1; i >= 0; i--)
1619 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1620 memblock_remove_region(&memblock.memory, i);
1621
1622 /* truncate the reserved regions */
1623 memblock_remove_range(&memblock.reserved, 0, base);
1624 memblock_remove_range(&memblock.reserved,
Stefan Agner1c4bc432018-06-07 17:06:15 -07001625 base + size, PHYS_ADDR_MAX);
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001626}
1627
Dennis Chena571d4e2016-07-28 15:48:26 -07001628void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1629{
Dennis Chena571d4e2016-07-28 15:48:26 -07001630 phys_addr_t max_addr;
Dennis Chena571d4e2016-07-28 15:48:26 -07001631
1632 if (!limit)
1633 return;
1634
1635 max_addr = __find_max_addr(limit);
1636
1637 /* @limit exceeds the total size of the memory, do nothing */
Stefan Agner1c4bc432018-06-07 17:06:15 -07001638 if (max_addr == PHYS_ADDR_MAX)
Dennis Chena571d4e2016-07-28 15:48:26 -07001639 return;
1640
AKASHI Takahiroc9ca9b42017-04-03 11:23:55 +09001641 memblock_cap_memory_range(0, max_addr);
Dennis Chena571d4e2016-07-28 15:48:26 -07001642}
1643
Yinghai Lucd794812010-10-11 12:34:09 -07001644static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001645{
1646 unsigned int left = 0, right = type->cnt;
1647
1648 do {
1649 unsigned int mid = (right + left) / 2;
1650
1651 if (addr < type->regions[mid].base)
1652 right = mid;
1653 else if (addr >= (type->regions[mid].base +
1654 type->regions[mid].size))
1655 left = mid + 1;
1656 else
1657 return mid;
1658 } while (left < right);
1659 return -1;
1660}
1661
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001662bool __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001663{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001664 return memblock_search(&memblock.reserved, addr) != -1;
1665}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001666
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001667bool __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001668{
1669 return memblock_search(&memblock.memory, addr) != -1;
1670}
1671
Yaowei Bai937f0c22018-02-06 15:41:18 -08001672bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001673{
1674 int i = memblock_search(&memblock.memory, addr);
1675
1676 if (i == -1)
1677 return false;
1678 return !memblock_is_nomap(&memblock.memory.regions[i]);
1679}
1680
Yinghai Lue76b63f2013-09-11 14:22:17 -07001681#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1682int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1683 unsigned long *start_pfn, unsigned long *end_pfn)
1684{
1685 struct memblock_type *type = &memblock.memory;
Fabian Frederick16763232014-04-07 15:37:53 -07001686 int mid = memblock_search(type, PFN_PHYS(pfn));
Yinghai Lue76b63f2013-09-11 14:22:17 -07001687
1688 if (mid == -1)
1689 return -1;
1690
Fabian Frederickf7e2f7e2014-06-04 16:07:51 -07001691 *start_pfn = PFN_DOWN(type->regions[mid].base);
1692 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001693
1694 return type->regions[mid].nid;
1695}
1696#endif
1697
Stephen Boydeab30942012-05-24 00:45:21 -07001698/**
1699 * memblock_is_region_memory - check if a region is a subset of memory
1700 * @base: base of region to check
1701 * @size: size of region to check
1702 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001703 * Check if the region [@base, @base + @size) is a subset of a memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001704 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001705 * Return:
Stephen Boydeab30942012-05-24 00:45:21 -07001706 * 0 if false, non-zero if true
1707 */
Yaowei Bai937f0c22018-02-06 15:41:18 -08001708bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001709{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001710 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001711 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001712
1713 if (idx == -1)
Yaowei Bai937f0c22018-02-06 15:41:18 -08001714 return false;
Wei Yangef415ef2017-02-22 15:45:04 -08001715 return (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001716 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001717}
1718
Stephen Boydeab30942012-05-24 00:45:21 -07001719/**
1720 * memblock_is_region_reserved - check if a region intersects reserved memory
1721 * @base: base of region to check
1722 * @size: size of region to check
1723 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001724 * Check if the region [@base, @base + @size) intersects a reserved
1725 * memory block.
Stephen Boydeab30942012-05-24 00:45:21 -07001726 *
Mike Rapoport47cec442018-06-30 17:55:02 +03001727 * Return:
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001728 * True if they intersect, false if not.
Stephen Boydeab30942012-05-24 00:45:21 -07001729 */
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001730bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001731{
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001732 memblock_cap_size(base, &size);
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001733 return memblock_overlaps_region(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001734}
1735
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001736void __init_memblock memblock_trim_memory(phys_addr_t align)
1737{
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001738 phys_addr_t start, end, orig_start, orig_end;
Emil Medve136199f2014-04-07 15:37:52 -07001739 struct memblock_region *r;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001740
Emil Medve136199f2014-04-07 15:37:52 -07001741 for_each_memblock(memory, r) {
1742 orig_start = r->base;
1743 orig_end = r->base + r->size;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001744 start = round_up(orig_start, align);
1745 end = round_down(orig_end, align);
1746
1747 if (start == orig_start && end == orig_end)
1748 continue;
1749
1750 if (start < end) {
Emil Medve136199f2014-04-07 15:37:52 -07001751 r->base = start;
1752 r->size = end - start;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001753 } else {
Emil Medve136199f2014-04-07 15:37:52 -07001754 memblock_remove_region(&memblock.memory,
1755 r - memblock.memory.regions);
1756 r--;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001757 }
1758 }
1759}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001760
Yinghai Lu3661ca62010-09-15 13:05:29 -07001761void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001762{
1763 memblock.current_limit = limit;
1764}
1765
Laura Abbottfec51012014-02-27 01:23:43 +01001766phys_addr_t __init_memblock memblock_get_current_limit(void)
1767{
1768 return memblock.current_limit;
1769}
1770
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001771static void __init_memblock memblock_dump(struct memblock_type *type)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001772{
Miles Chen5d63f812017-02-22 15:46:42 -08001773 phys_addr_t base, end, size;
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001774 enum memblock_flags flags;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001775 int idx;
1776 struct memblock_region *rgn;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001777
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001778 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001779
Gioh Kim66e8b432017-11-15 17:33:42 -08001780 for_each_memblock_type(idx, type, rgn) {
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001781 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001782
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001783 base = rgn->base;
1784 size = rgn->size;
Miles Chen5d63f812017-02-22 15:46:42 -08001785 end = base + size - 1;
Tang Chen66a20752014-01-21 15:49:20 -08001786 flags = rgn->flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001787#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1788 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1789 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1790 memblock_get_region_node(rgn));
1791#endif
Mike Rapoporte1720fe2018-06-30 17:55:01 +03001792 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001793 type->name, idx, &base, &end, &size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001794 }
1795}
1796
Tejun Heo4ff7b822011-12-08 10:22:06 -08001797void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001798{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001799 pr_info("MEMBLOCK configuration:\n");
Miles Chen5d63f812017-02-22 15:46:42 -08001800 pr_info(" memory size = %pa reserved size = %pa\n",
1801 &memblock.memory.total_size,
1802 &memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001803
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001804 memblock_dump(&memblock.memory);
1805 memblock_dump(&memblock.reserved);
Heiko Carstens409efd42017-02-24 14:55:56 -08001806#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
Heiko Carstens0262d9c2017-02-24 14:55:59 -08001807 memblock_dump(&memblock.physmem);
Heiko Carstens409efd42017-02-24 14:55:56 -08001808#endif
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001809}
1810
Tejun Heo1aadc052011-12-08 10:22:08 -08001811void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001812{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001813 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001814}
1815
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001816static int __init early_memblock(char *p)
1817{
1818 if (p && strstr(p, "debug"))
1819 memblock_debug = 1;
1820 return 0;
1821}
1822early_param("memblock", early_memblock);
1823
Tejun Heoc378ddd2011-07-14 11:46:03 +02001824#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001825
1826static int memblock_debug_show(struct seq_file *m, void *private)
1827{
1828 struct memblock_type *type = m->private;
1829 struct memblock_region *reg;
1830 int i;
Miles Chen5d63f812017-02-22 15:46:42 -08001831 phys_addr_t end;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001832
1833 for (i = 0; i < type->cnt; i++) {
1834 reg = &type->regions[i];
Miles Chen5d63f812017-02-22 15:46:42 -08001835 end = reg->base + reg->size - 1;
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001836
Miles Chen5d63f812017-02-22 15:46:42 -08001837 seq_printf(m, "%4d: ", i);
1838 seq_printf(m, "%pa..%pa\n", &reg->base, &end);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001839 }
1840 return 0;
1841}
Andy Shevchenko5ad35092018-04-05 16:23:16 -07001842DEFINE_SHOW_ATTRIBUTE(memblock_debug);
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001843
1844static int __init memblock_init_debugfs(void)
1845{
1846 struct dentry *root = debugfs_create_dir("memblock", NULL);
1847 if (!root)
1848 return -ENXIO;
Joe Perches0825a6f2018-06-14 15:27:58 -07001849 debugfs_create_file("memory", 0444, root,
1850 &memblock.memory, &memblock_debug_fops);
1851 debugfs_create_file("reserved", 0444, root,
1852 &memblock.reserved, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001853#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
Joe Perches0825a6f2018-06-14 15:27:58 -07001854 debugfs_create_file("physmem", 0444, root,
1855 &memblock.physmem, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001856#endif
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001857
1858 return 0;
1859}
1860__initcall(memblock_init_debugfs);
1861
1862#endif /* CONFIG_DEBUG_FS */