blob: 79498417567d62e2be383dd6b31798067a3c9612 [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>
20#include <linux/seq_file.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100021#include <linux/memblock.h>
Shiraz Hashime1525702016-02-04 14:53:33 +053022#include <linux/preempt.h>
23#include <linux/seqlock.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100024
Christoph Hellwigc4c5ad62016-07-28 15:48:06 -070025#include <asm/sections.h>
Santosh Shilimkar26f09e92014-01-21 15:50:19 -080026#include <linux/io.h>
27
28#include "internal.h"
Tang Chen79442ed2013-11-12 15:07:59 -080029
Tejun Heofe091c22011-12-08 10:22:07 -080030static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
31static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010032#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
33static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
34#endif
Tejun Heofe091c22011-12-08 10:22:07 -080035
Shiraz Hashime1525702016-02-04 14:53:33 +053036static seqcount_t memblock_seq;
Tejun Heofe091c22011-12-08 10:22:07 -080037struct memblock memblock __initdata_memblock = {
38 .memory.regions = memblock_memory_init_regions,
39 .memory.cnt = 1, /* empty dummy entry */
40 .memory.max = INIT_MEMBLOCK_REGIONS,
41
42 .reserved.regions = memblock_reserved_init_regions,
43 .reserved.cnt = 1, /* empty dummy entry */
44 .reserved.max = INIT_MEMBLOCK_REGIONS,
45
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,
50#endif
51
Tang Chen79442ed2013-11-12 15:07:59 -080052 .bottom_up = false,
Tejun Heofe091c22011-12-08 10:22:07 -080053 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
54};
Yinghai Lu95f72d12010-07-12 14:36:09 +100055
Yinghai Lu10d06432010-07-28 15:43:02 +100056int memblock_debug __initdata_memblock;
Tang Chen55ac5902014-01-21 15:49:35 -080057#ifdef CONFIG_MOVABLE_NODE
58bool movable_node_enabled __initdata_memblock = false;
59#endif
Tony Lucka3f5baf2015-06-24 16:58:12 -070060static bool system_has_some_mirror __initdata_memblock = false;
Tejun Heo1aadc052011-12-08 10:22:08 -080061static int memblock_can_resize __initdata_memblock;
Gavin Shan181eb392012-05-29 15:06:50 -070062static int memblock_memory_in_slab __initdata_memblock = 0;
63static int memblock_reserved_in_slab __initdata_memblock = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +100064
Tony Lucka3f5baf2015-06-24 16:58:12 -070065ulong __init_memblock choose_memblock_flags(void)
66{
67 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
68}
69
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070070/* inline so we don't get a warning when pr_debug is compiled out */
Raghavendra D Prabhuc2233112012-10-08 16:33:55 -070071static __init_memblock const char *
72memblock_type_name(struct memblock_type *type)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070073{
74 if (type == &memblock.memory)
75 return "memory";
76 else if (type == &memblock.reserved)
77 return "reserved";
78 else
79 return "unknown";
80}
81
Tejun Heoeb18f1b2011-12-08 10:22:07 -080082/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
83static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
84{
85 return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
86}
87
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100088/*
89 * Address comparison utilities
90 */
Yinghai Lu10d06432010-07-28 15:43:02 +100091static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100092 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100093{
94 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
95}
96
Tang Chen95cf82e2015-09-08 15:02:03 -070097bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -070098 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100099{
100 unsigned long i;
101
Alexander Kuleshovf14516f2016-01-14 15:20:39 -0800102 for (i = 0; i < type->cnt; i++)
103 if (memblock_addrs_overlap(base, size, type->regions[i].base,
104 type->regions[i].size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000105 break;
Tang Chenc5c5c9d2015-09-08 15:02:00 -0700106 return i < type->cnt;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000107}
108
Tang Chen79442ed2013-11-12 15:07:59 -0800109/*
110 * __memblock_find_range_bottom_up - find free area utility in bottom-up
111 * @start: start of candidate range
112 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
113 * @size: size of free area to find
114 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800115 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700116 * @flags: pick from blocks based on memory attributes
Tang Chen79442ed2013-11-12 15:07:59 -0800117 *
118 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
119 *
120 * RETURNS:
121 * Found address on success, 0 on failure.
122 */
123static phys_addr_t __init_memblock
124__memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700125 phys_addr_t size, phys_addr_t align, int nid,
126 ulong flags)
Tang Chen79442ed2013-11-12 15:07:59 -0800127{
128 phys_addr_t this_start, this_end, cand;
129 u64 i;
130
Tony Luckfc6daaf2015-06-24 16:58:09 -0700131 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
Tang Chen79442ed2013-11-12 15:07:59 -0800132 this_start = clamp(this_start, start, end);
133 this_end = clamp(this_end, start, end);
134
135 cand = round_up(this_start, align);
136 if (cand < this_end && this_end - cand >= size)
137 return cand;
138 }
139
140 return 0;
141}
142
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800143/**
Tang Chen14028992013-11-12 15:07:57 -0800144 * __memblock_find_range_top_down - find free area utility, in top-down
145 * @start: start of candidate range
146 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
147 * @size: size of free area to find
148 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800149 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700150 * @flags: pick from blocks based on memory attributes
Tang Chen14028992013-11-12 15:07:57 -0800151 *
152 * Utility called from memblock_find_in_range_node(), find free area top-down.
153 *
154 * RETURNS:
Tang Chen79442ed2013-11-12 15:07:59 -0800155 * Found address on success, 0 on failure.
Tang Chen14028992013-11-12 15:07:57 -0800156 */
157static phys_addr_t __init_memblock
158__memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700159 phys_addr_t size, phys_addr_t align, int nid,
160 ulong flags)
Tang Chen14028992013-11-12 15:07:57 -0800161{
162 phys_addr_t this_start, this_end, cand;
163 u64 i;
164
Tony Luckfc6daaf2015-06-24 16:58:09 -0700165 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
166 NULL) {
Tang Chen14028992013-11-12 15:07:57 -0800167 this_start = clamp(this_start, start, end);
168 this_end = clamp(this_end, start, end);
169
170 if (this_end < size)
171 continue;
172
173 cand = round_down(this_end - size, align);
174 if (cand >= this_start)
175 return cand;
176 }
177
178 return 0;
179}
180
181/**
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800182 * memblock_find_in_range_node - find free area in given range and node
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800183 * @size: size of free area to find
184 * @align: alignment of free area to find
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800185 * @start: start of candidate range
186 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
Grygorii Strashkob1154232014-01-21 15:50:16 -0800187 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tony Luckfc6daaf2015-06-24 16:58:09 -0700188 * @flags: pick from blocks based on memory attributes
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800189 *
190 * Find @size free area aligned to @align in the specified range and node.
191 *
Tang Chen79442ed2013-11-12 15:07:59 -0800192 * When allocation direction is bottom-up, the @start should be greater
193 * than the end of the kernel image. Otherwise, it will be trimmed. The
194 * reason is that we want the bottom-up allocation just near the kernel
195 * image so it is highly likely that the allocated memory and the kernel
196 * will reside in the same node.
197 *
198 * If bottom-up allocation failed, will try to allocate memory top-down.
199 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800200 * RETURNS:
Tang Chen79442ed2013-11-12 15:07:59 -0800201 * Found address on success, 0 on failure.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000202 */
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800203phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
204 phys_addr_t align, phys_addr_t start,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700205 phys_addr_t end, int nid, ulong flags)
Tang Chenf7210e62013-02-22 16:33:51 -0800206{
Tang Chen0cfb8f02014-08-29 15:18:31 -0700207 phys_addr_t kernel_end, ret;
Tang Chen79442ed2013-11-12 15:07:59 -0800208
Tang Chenf7210e62013-02-22 16:33:51 -0800209 /* pump up @end */
210 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
211 end = memblock.current_limit;
212
213 /* avoid allocating the first page */
214 start = max_t(phys_addr_t, start, PAGE_SIZE);
215 end = max(start, end);
Tang Chen79442ed2013-11-12 15:07:59 -0800216 kernel_end = __pa_symbol(_end);
217
218 /*
219 * try bottom-up allocation only when bottom-up mode
220 * is set and @end is above the kernel image.
221 */
222 if (memblock_bottom_up() && end > kernel_end) {
223 phys_addr_t bottom_up_start;
224
225 /* make sure we will allocate above the kernel */
226 bottom_up_start = max(start, kernel_end);
227
228 /* ok, try bottom-up allocation first */
229 ret = __memblock_find_range_bottom_up(bottom_up_start, end,
Tony Luckfc6daaf2015-06-24 16:58:09 -0700230 size, align, nid, flags);
Tang Chen79442ed2013-11-12 15:07:59 -0800231 if (ret)
232 return ret;
233
234 /*
235 * we always limit bottom-up allocation above the kernel,
236 * but top-down allocation doesn't have the limit, so
237 * retrying top-down allocation may succeed when bottom-up
238 * allocation failed.
239 *
240 * bottom-up allocation is expected to be fail very rarely,
241 * so we use WARN_ONCE() here to see the stack trace if
242 * fail happens.
243 */
Joe Perches756a025f02016-03-17 14:19:47 -0700244 WARN_ONCE(1, "memblock: bottom-up allocation failed, memory hotunplug may be affected\n");
Tang Chen79442ed2013-11-12 15:07:59 -0800245 }
Tang Chenf7210e62013-02-22 16:33:51 -0800246
Tony Luckfc6daaf2015-06-24 16:58:09 -0700247 return __memblock_find_range_top_down(start, end, size, align, nid,
248 flags);
Tang Chenf7210e62013-02-22 16:33:51 -0800249}
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000250
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800251/**
252 * memblock_find_in_range - find free area in given range
253 * @start: start of candidate range
254 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
255 * @size: size of free area to find
256 * @align: alignment of free area to find
257 *
258 * Find @size free area aligned to @align in the specified range.
259 *
260 * RETURNS:
Tang Chen79442ed2013-11-12 15:07:59 -0800261 * Found address on success, 0 on failure.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800262 */
263phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
264 phys_addr_t end, phys_addr_t size,
265 phys_addr_t align)
266{
Tony Lucka3f5baf2015-06-24 16:58:12 -0700267 phys_addr_t ret;
268 ulong flags = choose_memblock_flags();
269
270again:
271 ret = memblock_find_in_range_node(size, align, start, end,
272 NUMA_NO_NODE, flags);
273
274 if (!ret && (flags & MEMBLOCK_MIRROR)) {
275 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
276 &size);
277 flags &= ~MEMBLOCK_MIRROR;
278 goto again;
279 }
280
281 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800282}
283
Yinghai Lu10d06432010-07-28 15:43:02 +1000284static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000285{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800286 type->total_size -= type->regions[r].size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200287 memmove(&type->regions[r], &type->regions[r + 1],
288 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000289 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000290
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700291 /* Special case for empty arrays */
292 if (type->cnt == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800293 WARN_ON(type->total_size != 0);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700294 type->cnt = 1;
295 type->regions[0].base = 0;
296 type->regions[0].size = 0;
Tang Chen66a20752014-01-21 15:49:20 -0800297 type->regions[0].flags = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200298 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700299 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000300}
301
Philipp Hachtmann354f17e2014-01-23 15:53:24 -0800302#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
303
Yinghai Lu29f67382012-07-11 14:02:56 -0700304phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info(
305 phys_addr_t *addr)
306{
307 if (memblock.reserved.regions == memblock_reserved_init_regions)
308 return 0;
309
310 *addr = __pa(memblock.reserved.regions);
311
312 return PAGE_ALIGN(sizeof(struct memblock_region) *
313 memblock.reserved.max);
314}
315
Philipp Hachtmann5e270e22014-01-23 15:53:11 -0800316phys_addr_t __init_memblock get_allocated_memblock_memory_regions_info(
317 phys_addr_t *addr)
318{
319 if (memblock.memory.regions == memblock_memory_init_regions)
320 return 0;
321
322 *addr = __pa(memblock.memory.regions);
323
324 return PAGE_ALIGN(sizeof(struct memblock_region) *
325 memblock.memory.max);
326}
327
328#endif
329
Greg Pearson48c3b582012-06-20 12:53:05 -0700330/**
331 * memblock_double_array - double the size of the memblock regions array
332 * @type: memblock type of the regions array being doubled
333 * @new_area_start: starting address of memory range to avoid overlap with
334 * @new_area_size: size of memory range to avoid overlap with
335 *
336 * Double the size of the @type regions array. If memblock is being used to
337 * allocate memory for a new reserved regions array and there is a previously
338 * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
339 * waiting to be reserved, ensure the memory used by the new array does
340 * not overlap.
341 *
342 * RETURNS:
343 * 0 on success, -1 on failure.
344 */
345static int __init_memblock memblock_double_array(struct memblock_type *type,
346 phys_addr_t new_area_start,
347 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700348{
349 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700350 phys_addr_t old_alloc_size, new_alloc_size;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700351 phys_addr_t old_size, new_size, addr;
352 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700353 int *in_slab;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700354
355 /* We don't allow resizing until we know about the reserved regions
356 * of memory that aren't suitable for allocation
357 */
358 if (!memblock_can_resize)
359 return -1;
360
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700361 /* Calculate new doubled size */
362 old_size = type->max * sizeof(struct memblock_region);
363 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700364 /*
365 * We need to allocated new one align to PAGE_SIZE,
366 * so we can free them completely later.
367 */
368 old_alloc_size = PAGE_ALIGN(old_size);
369 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700370
Gavin Shan181eb392012-05-29 15:06:50 -0700371 /* Retrieve the slab flag */
372 if (type == &memblock.memory)
373 in_slab = &memblock_memory_in_slab;
374 else
375 in_slab = &memblock_reserved_in_slab;
376
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700377 /* Try to find some space for it.
378 *
379 * WARNING: We assume that either slab_is_available() and we use it or
Andrew Mortonfd073832012-07-31 16:42:40 -0700380 * we use MEMBLOCK for allocations. That means that this is unsafe to
381 * use when bootmem is currently active (unless bootmem itself is
382 * implemented on top of MEMBLOCK which isn't the case yet)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700383 *
384 * This should however not be an issue for now, as we currently only
Andrew Mortonfd073832012-07-31 16:42:40 -0700385 * call into MEMBLOCK while it's still active, or much later when slab
386 * is active for memory hotplug operations
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700387 */
388 if (use_slab) {
389 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200390 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700391 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700392 /* only exclude range when trying to double reserved.regions */
393 if (type != &memblock.reserved)
394 new_area_start = new_area_size = 0;
395
396 addr = memblock_find_in_range(new_area_start + new_area_size,
397 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700398 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700399 if (!addr && new_area_size)
400 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700401 min(new_area_start, memblock.current_limit),
402 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700403
Sachin Kamat15674862012-09-04 13:55:05 +0530404 new_array = addr ? __va(addr) : NULL;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700405 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200406 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700407 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
408 memblock_type_name(type), type->max, type->max * 2);
409 return -1;
410 }
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700411
Andrew Mortonfd073832012-07-31 16:42:40 -0700412 memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
413 memblock_type_name(type), type->max * 2, (u64)addr,
414 (u64)addr + new_size - 1);
Yinghai Luea9e4372010-07-28 15:13:22 +1000415
Andrew Mortonfd073832012-07-31 16:42:40 -0700416 /*
417 * Found space, we now need to move the array over before we add the
418 * reserved region since it may be our reserved array itself that is
419 * full.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700420 */
421 memcpy(new_array, type->regions, old_size);
422 memset(new_array + type->max, 0, old_size);
423 old_array = type->regions;
424 type->regions = new_array;
425 type->max <<= 1;
426
Andrew Mortonfd073832012-07-31 16:42:40 -0700427 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700428 if (*in_slab)
429 kfree(old_array);
430 else if (old_array != memblock_memory_init_regions &&
431 old_array != memblock_reserved_init_regions)
Yinghai Lu29f67382012-07-11 14:02:56 -0700432 memblock_free(__pa(old_array), old_alloc_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700433
Andrew Mortonfd073832012-07-31 16:42:40 -0700434 /*
435 * Reserve the new array if that comes from the memblock. Otherwise, we
436 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700437 */
438 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700439 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700440
441 /* Update slab flag */
442 *in_slab = use_slab;
443
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700444 return 0;
445}
446
Tejun Heo784656f92011-07-12 11:15:55 +0200447/**
448 * memblock_merge_regions - merge neighboring compatible regions
449 * @type: memblock type to scan
450 *
451 * Scan @type and merge neighboring compatible regions.
452 */
453static void __init_memblock memblock_merge_regions(struct memblock_type *type)
454{
455 int i = 0;
456
457 /* cnt never goes below 1 */
458 while (i < type->cnt - 1) {
459 struct memblock_region *this = &type->regions[i];
460 struct memblock_region *next = &type->regions[i + 1];
461
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200462 if (this->base + this->size != next->base ||
463 memblock_get_region_node(this) !=
Tang Chen66a20752014-01-21 15:49:20 -0800464 memblock_get_region_node(next) ||
465 this->flags != next->flags) {
Tejun Heo784656f92011-07-12 11:15:55 +0200466 BUG_ON(this->base + this->size > next->base);
467 i++;
468 continue;
469 }
470
471 this->size += next->size;
Lin Fengc0232ae2013-01-11 14:31:44 -0800472 /* move forward from next + 1, index of which is i + 2 */
473 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
Tejun Heo784656f92011-07-12 11:15:55 +0200474 type->cnt--;
475 }
476}
477
478/**
479 * memblock_insert_region - insert new memblock region
Tang Chen209ff862013-04-29 15:08:41 -0700480 * @type: memblock type to insert into
481 * @idx: index for the insertion point
482 * @base: base address of the new region
483 * @size: size of the new region
484 * @nid: node id of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800485 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200486 *
487 * Insert new memblock region [@base,@base+@size) into @type at @idx.
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700488 * @type must already have extra room to accommodate the new region.
Tejun Heo784656f92011-07-12 11:15:55 +0200489 */
490static void __init_memblock memblock_insert_region(struct memblock_type *type,
491 int idx, phys_addr_t base,
Tang Chen66a20752014-01-21 15:49:20 -0800492 phys_addr_t size,
493 int nid, unsigned long flags)
Tejun Heo784656f92011-07-12 11:15:55 +0200494{
495 struct memblock_region *rgn = &type->regions[idx];
496
497 BUG_ON(type->cnt >= type->max);
498 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
499 rgn->base = base;
500 rgn->size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800501 rgn->flags = flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200502 memblock_set_region_node(rgn, nid);
Tejun Heo784656f92011-07-12 11:15:55 +0200503 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800504 type->total_size += size;
Tejun Heo784656f92011-07-12 11:15:55 +0200505}
506
507/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100508 * memblock_add_range - add new memblock region
Tejun Heo784656f92011-07-12 11:15:55 +0200509 * @type: memblock type to add new region into
510 * @base: base address of the new region
511 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800512 * @nid: nid of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800513 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200514 *
515 * Add new memblock region [@base,@base+@size) into @type. The new region
516 * is allowed to overlap with existing ones - overlaps don't affect already
517 * existing regions. @type is guaranteed to be minimal (all neighbouring
518 * compatible regions are merged) after the addition.
519 *
520 * RETURNS:
521 * 0 on success, -errno on failure.
522 */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100523int __init_memblock memblock_add_range(struct memblock_type *type,
Tang Chen66a20752014-01-21 15:49:20 -0800524 phys_addr_t base, phys_addr_t size,
525 int nid, unsigned long flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000526{
Tejun Heo784656f92011-07-12 11:15:55 +0200527 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800528 phys_addr_t obase = base;
529 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800530 int idx, nr_new;
531 struct memblock_region *rgn;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000532
Tejun Heob3dc6272012-04-20 08:31:34 -0700533 if (!size)
534 return 0;
535
Tejun Heo784656f92011-07-12 11:15:55 +0200536 /* special case for empty array */
537 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800538 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000539 type->regions[0].base = base;
540 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800541 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800542 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800543 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000544 return 0;
545 }
Tejun Heo784656f92011-07-12 11:15:55 +0200546repeat:
547 /*
548 * The following is executed twice. Once with %false @insert and
549 * then with %true. The first counts the number of regions needed
Alexander Kuleshov412d0002016-08-04 15:31:46 -0700550 * to accommodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700551 */
Tejun Heo784656f92011-07-12 11:15:55 +0200552 base = obase;
553 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000554
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800555 for_each_memblock_type(type, rgn) {
Tejun Heo784656f92011-07-12 11:15:55 +0200556 phys_addr_t rbase = rgn->base;
557 phys_addr_t rend = rbase + rgn->size;
558
559 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000560 break;
Tejun Heo784656f92011-07-12 11:15:55 +0200561 if (rend <= base)
562 continue;
563 /*
564 * @rgn overlaps. If it separates the lower part of new
565 * area, insert that portion.
566 */
567 if (rbase > base) {
Wei Yangc0a29492015-09-04 15:47:38 -0700568#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
569 WARN_ON(nid != memblock_get_region_node(rgn));
570#endif
Wei Yang4fcab5f2015-09-08 14:59:53 -0700571 WARN_ON(flags != rgn->flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200572 nr_new++;
573 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800574 memblock_insert_region(type, idx++, base,
Tang Chen66a20752014-01-21 15:49:20 -0800575 rbase - base, nid,
576 flags);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000577 }
Tejun Heo784656f92011-07-12 11:15:55 +0200578 /* area below @rend is dealt with, forget about it */
579 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000580 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000581
Tejun Heo784656f92011-07-12 11:15:55 +0200582 /* insert the remaining portion */
583 if (base < end) {
584 nr_new++;
585 if (insert)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800586 memblock_insert_region(type, idx, base, end - base,
Tang Chen66a20752014-01-21 15:49:20 -0800587 nid, flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200588 }
589
nimisoloef3cc4d2016-07-26 15:24:56 -0700590 if (!nr_new)
591 return 0;
592
Tejun Heo784656f92011-07-12 11:15:55 +0200593 /*
594 * If this was the first round, resize array and repeat for actual
595 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700596 */
Tejun Heo784656f92011-07-12 11:15:55 +0200597 if (!insert) {
598 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700599 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f92011-07-12 11:15:55 +0200600 return -ENOMEM;
601 insert = true;
602 goto repeat;
603 } else {
604 memblock_merge_regions(type);
605 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700606 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000607}
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
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700615int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700616{
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700617 memblock_dbg("memblock_add: [%#016llx-%#016llx] flags %#02lx %pF\n",
618 (unsigned long long)base,
619 (unsigned long long)base + size - 1,
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700620 0UL, (void *)_RET_IP_);
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700621
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700622 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000623}
624
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800625/**
626 * memblock_isolate_range - isolate given range into disjoint memblocks
627 * @type: memblock type to isolate range for
628 * @base: base of range to isolate
629 * @size: size of range to isolate
630 * @start_rgn: out parameter for the start of isolated region
631 * @end_rgn: out parameter for the end of isolated region
632 *
633 * Walk @type and ensure that regions don't cross the boundaries defined by
634 * [@base,@base+@size). Crossing regions are split at the boundaries,
635 * which may create at most two more regions. The index of the first
636 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
637 *
638 * RETURNS:
639 * 0 on success, -errno on failure.
640 */
641static int __init_memblock memblock_isolate_range(struct memblock_type *type,
642 phys_addr_t base, phys_addr_t size,
643 int *start_rgn, int *end_rgn)
644{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800645 phys_addr_t end = base + memblock_cap_size(base, &size);
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800646 int idx;
647 struct memblock_region *rgn;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800648
649 *start_rgn = *end_rgn = 0;
650
Tejun Heob3dc6272012-04-20 08:31:34 -0700651 if (!size)
652 return 0;
653
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800654 /* we'll create at most two more regions */
655 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700656 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800657 return -ENOMEM;
658
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800659 for_each_memblock_type(type, rgn) {
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800660 phys_addr_t rbase = rgn->base;
661 phys_addr_t rend = rbase + rgn->size;
662
663 if (rbase >= end)
664 break;
665 if (rend <= base)
666 continue;
667
668 if (rbase < base) {
669 /*
670 * @rgn intersects from below. Split and continue
671 * to process the next region - the new top half.
672 */
673 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800674 rgn->size -= base - rbase;
675 type->total_size -= base - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800676 memblock_insert_region(type, idx, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800677 memblock_get_region_node(rgn),
678 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800679 } else if (rend > end) {
680 /*
681 * @rgn intersects from above. Split and redo the
682 * current region - the new bottom half.
683 */
684 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800685 rgn->size -= end - rbase;
686 type->total_size -= end - rbase;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800687 memblock_insert_region(type, idx--, rbase, end - 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 {
691 /* @rgn is fully contained, record it */
692 if (!*end_rgn)
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -0800693 *start_rgn = idx;
694 *end_rgn = idx + 1;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800695 }
696 }
697
698 return 0;
699}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800700
Alexander Kuleshov35bd16a2015-11-05 18:47:00 -0800701static int __init_memblock memblock_remove_range(struct memblock_type *type,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100702 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000703{
Tejun Heo71936182011-12-08 10:22:07 -0800704 int start_rgn, end_rgn;
705 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000706
Tejun Heo71936182011-12-08 10:22:07 -0800707 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
708 if (ret)
709 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000710
Tejun Heo71936182011-12-08 10:22:07 -0800711 for (i = end_rgn - 1; i >= start_rgn; i--)
712 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700713 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000714}
715
Tejun Heo581adcb2011-12-08 10:22:06 -0800716int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000717{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100718 return memblock_remove_range(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000719}
720
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100721
Tejun Heo581adcb2011-12-08 10:22:06 -0800722int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000723{
Tejun Heo24aa0782011-07-12 11:16:06 +0200724 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700725 (unsigned long long)base,
Grygorii Strashko931d13f2014-01-21 15:49:17 -0800726 (unsigned long long)base + size - 1,
H. Peter Anvina1504392011-07-14 11:57:10 -0700727 (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200728
Shiraz Hashimcb3a9222016-01-19 15:11:10 +0530729 if (base < memblock.current_limit)
730 kmemleak_free_part(__va(base), size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100731 return memblock_remove_range(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000732}
733
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700734int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000735{
Tang Chen66a20752014-01-21 15:49:20 -0800736 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] flags %#02lx %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700737 (unsigned long long)base,
Grygorii Strashko931d13f2014-01-21 15:49:17 -0800738 (unsigned long long)base + size - 1,
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700739 0UL, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000740
Alexander Kuleshovf705ac42016-05-20 16:57:35 -0700741 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000742}
743
Tejun Heo35fd0802011-07-12 11:15:59 +0200744/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800745 *
Tony Luck4308ce12014-12-12 16:54:59 -0800746 * This function isolates region [@base, @base + @size), and sets/clears flag
Tang Chen66b16ed2014-01-21 15:49:23 -0800747 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700748 * Return 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800749 */
Tony Luck4308ce12014-12-12 16:54:59 -0800750static int __init_memblock memblock_setclr_flag(phys_addr_t base,
751 phys_addr_t size, int set, int flag)
Tang Chen66b16ed2014-01-21 15:49:23 -0800752{
753 struct memblock_type *type = &memblock.memory;
754 int i, ret, start_rgn, end_rgn;
755
756 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
757 if (ret)
758 return ret;
759
760 for (i = start_rgn; i < end_rgn; i++)
Tony Luck4308ce12014-12-12 16:54:59 -0800761 if (set)
762 memblock_set_region_flags(&type->regions[i], flag);
763 else
764 memblock_clear_region_flags(&type->regions[i], flag);
Tang Chen66b16ed2014-01-21 15:49:23 -0800765
766 memblock_merge_regions(type);
767 return 0;
768}
769
770/**
Tony Luck4308ce12014-12-12 16:54:59 -0800771 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
772 * @base: the base phys addr of the region
773 * @size: the size of the region
774 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700775 * Return 0 on success, -errno on failure.
Tony Luck4308ce12014-12-12 16:54:59 -0800776 */
777int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
778{
779 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
780}
781
782/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800783 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
784 * @base: the base phys addr of the region
785 * @size: the size of the region
786 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700787 * Return 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800788 */
789int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
790{
Tony Luck4308ce12014-12-12 16:54:59 -0800791 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
Tang Chen66b16ed2014-01-21 15:49:23 -0800792}
793
794/**
Tony Lucka3f5baf2015-06-24 16:58:12 -0700795 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
796 * @base: the base phys addr of the region
797 * @size: the size of the region
798 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700799 * Return 0 on success, -errno on failure.
Tony Lucka3f5baf2015-06-24 16:58:12 -0700800 */
801int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
802{
803 system_has_some_mirror = true;
804
805 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
806}
807
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100808/**
809 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
810 * @base: the base phys addr of the region
811 * @size: the size of the region
812 *
813 * Return 0 on success, -errno on failure.
814 */
815int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
816{
817 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
818}
Tony Lucka3f5baf2015-06-24 16:58:12 -0700819
820/**
Robin Holt8e7a7f82015-06-30 14:56:41 -0700821 * __next_reserved_mem_region - next function for for_each_reserved_region()
822 * @idx: pointer to u64 loop variable
823 * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
824 * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
825 *
826 * Iterate over all reserved memory regions.
827 */
828void __init_memblock __next_reserved_mem_region(u64 *idx,
829 phys_addr_t *out_start,
830 phys_addr_t *out_end)
831{
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700832 struct memblock_type *type = &memblock.reserved;
Robin Holt8e7a7f82015-06-30 14:56:41 -0700833
Richard Leitnercd33a762016-05-20 16:58:33 -0700834 if (*idx < type->cnt) {
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700835 struct memblock_region *r = &type->regions[*idx];
Robin Holt8e7a7f82015-06-30 14:56:41 -0700836 phys_addr_t base = r->base;
837 phys_addr_t size = r->size;
838
839 if (out_start)
840 *out_start = base;
841 if (out_end)
842 *out_end = base + size - 1;
843
844 *idx += 1;
845 return;
846 }
847
848 /* signal end of iteration */
849 *idx = ULLONG_MAX;
850}
851
852/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100853 * __next__mem_range - next function for for_each_free_mem_range() etc.
Tejun Heo35fd0802011-07-12 11:15:59 +0200854 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800855 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700856 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100857 * @type_a: pointer to memblock_type from where the range is taken
858 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -0700859 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
860 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
861 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +0200862 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100863 * Find the first area from *@idx which matches @nid, fill the out
Tejun Heo35fd0802011-07-12 11:15:59 +0200864 * parameters, and update *@idx for the next iteration. The lower 32bit of
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100865 * *@idx contains index into type_a and the upper 32bit indexes the
866 * areas before each region in type_b. For example, if type_b regions
Tejun Heo35fd0802011-07-12 11:15:59 +0200867 * look like the following,
868 *
869 * 0:[0-16), 1:[32-48), 2:[128-130)
870 *
871 * The upper 32bit indexes the following regions.
872 *
873 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
874 *
875 * As both region arrays are sorted, the function advances the two indices
876 * in lockstep and returns each intersection.
877 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700878void __init_memblock __next_mem_range(u64 *idx, int nid, ulong flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100879 struct memblock_type *type_a,
880 struct memblock_type *type_b,
881 phys_addr_t *out_start,
882 phys_addr_t *out_end, int *out_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200883{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100884 int idx_a = *idx & 0xffffffff;
885 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -0800886
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100887 if (WARN_ONCE(nid == MAX_NUMNODES,
888 "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
Grygorii Strashko560dca272014-01-21 15:50:55 -0800889 nid = NUMA_NO_NODE;
Tejun Heo35fd0802011-07-12 11:15:59 +0200890
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100891 for (; idx_a < type_a->cnt; idx_a++) {
892 struct memblock_region *m = &type_a->regions[idx_a];
893
Tejun Heo35fd0802011-07-12 11:15:59 +0200894 phys_addr_t m_start = m->base;
895 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100896 int m_nid = memblock_get_region_node(m);
Tejun Heo35fd0802011-07-12 11:15:59 +0200897
898 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100899 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200900 continue;
901
Xishi Qiu0a313a92014-09-09 14:50:46 -0700902 /* skip hotpluggable memory regions if needed */
903 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
904 continue;
905
Tony Lucka3f5baf2015-06-24 16:58:12 -0700906 /* if we want mirror memory skip non-mirror memory regions */
907 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
908 continue;
909
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +0100910 /* skip nomap memory unless we were asked for it explicitly */
911 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
912 continue;
913
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100914 if (!type_b) {
915 if (out_start)
916 *out_start = m_start;
917 if (out_end)
918 *out_end = m_end;
919 if (out_nid)
920 *out_nid = m_nid;
921 idx_a++;
922 *idx = (u32)idx_a | (u64)idx_b << 32;
923 return;
924 }
Tejun Heo35fd0802011-07-12 11:15:59 +0200925
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100926 /* scan areas before each reservation */
927 for (; idx_b < type_b->cnt + 1; idx_b++) {
928 struct memblock_region *r;
929 phys_addr_t r_start;
930 phys_addr_t r_end;
931
932 r = &type_b->regions[idx_b];
933 r_start = idx_b ? r[-1].base + r[-1].size : 0;
934 r_end = idx_b < type_b->cnt ?
935 r->base : ULLONG_MAX;
936
937 /*
938 * if idx_b advanced past idx_a,
939 * break out to advance idx_a
940 */
Tejun Heo35fd0802011-07-12 11:15:59 +0200941 if (r_start >= m_end)
942 break;
943 /* if the two regions intersect, we're done */
944 if (m_start < r_end) {
945 if (out_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100946 *out_start =
947 max(m_start, r_start);
Tejun Heo35fd0802011-07-12 11:15:59 +0200948 if (out_end)
949 *out_end = min(m_end, r_end);
950 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100951 *out_nid = m_nid;
Tejun Heo35fd0802011-07-12 11:15:59 +0200952 /*
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100953 * The region which ends first is
954 * advanced for the next iteration.
Tejun Heo35fd0802011-07-12 11:15:59 +0200955 */
956 if (m_end <= r_end)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100957 idx_a++;
Tejun Heo35fd0802011-07-12 11:15:59 +0200958 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100959 idx_b++;
960 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo35fd0802011-07-12 11:15:59 +0200961 return;
962 }
963 }
964 }
965
966 /* signal end of iteration */
967 *idx = ULLONG_MAX;
968}
969
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800970/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100971 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
972 *
973 * Finds the next range from type_a which is not marked as unsuitable
974 * in type_b.
975 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800976 * @idx: pointer to u64 loop variable
Alexander Kuleshovad5ea8c2015-09-08 15:04:22 -0700977 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700978 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100979 * @type_a: pointer to memblock_type from where the range is taken
980 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -0700981 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
982 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
983 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800984 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100985 * Reverse of __next_mem_range().
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800986 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700987void __init_memblock __next_mem_range_rev(u64 *idx, int nid, ulong flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100988 struct memblock_type *type_a,
989 struct memblock_type *type_b,
990 phys_addr_t *out_start,
991 phys_addr_t *out_end, int *out_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800992{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100993 int idx_a = *idx & 0xffffffff;
994 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -0800995
Grygorii Strashko560dca272014-01-21 15:50:55 -0800996 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
997 nid = NUMA_NO_NODE;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800998
999 if (*idx == (u64)ULLONG_MAX) {
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001000 idx_a = type_a->cnt - 1;
zijun_hue47608a2016-08-04 15:32:00 -07001001 if (type_b != NULL)
1002 idx_b = type_b->cnt;
1003 else
1004 idx_b = 0;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001005 }
1006
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001007 for (; idx_a >= 0; idx_a--) {
1008 struct memblock_region *m = &type_a->regions[idx_a];
1009
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001010 phys_addr_t m_start = m->base;
1011 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001012 int m_nid = memblock_get_region_node(m);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001013
1014 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001015 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001016 continue;
1017
Tang Chen55ac5902014-01-21 15:49:35 -08001018 /* skip hotpluggable memory regions if needed */
1019 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
1020 continue;
1021
Tony Lucka3f5baf2015-06-24 16:58:12 -07001022 /* if we want mirror memory skip non-mirror memory regions */
1023 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1024 continue;
1025
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001026 /* skip nomap memory unless we were asked for it explicitly */
1027 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1028 continue;
1029
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001030 if (!type_b) {
1031 if (out_start)
1032 *out_start = m_start;
1033 if (out_end)
1034 *out_end = m_end;
1035 if (out_nid)
1036 *out_nid = m_nid;
zijun_hufb399b42016-07-28 15:48:56 -07001037 idx_a--;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001038 *idx = (u32)idx_a | (u64)idx_b << 32;
1039 return;
1040 }
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001041
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001042 /* scan areas before each reservation */
1043 for (; idx_b >= 0; idx_b--) {
1044 struct memblock_region *r;
1045 phys_addr_t r_start;
1046 phys_addr_t r_end;
1047
1048 r = &type_b->regions[idx_b];
1049 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1050 r_end = idx_b < type_b->cnt ?
1051 r->base : ULLONG_MAX;
1052 /*
1053 * if idx_b advanced past idx_a,
1054 * break out to advance idx_a
1055 */
1056
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001057 if (r_end <= m_start)
1058 break;
1059 /* if the two regions intersect, we're done */
1060 if (m_end > r_start) {
1061 if (out_start)
1062 *out_start = max(m_start, r_start);
1063 if (out_end)
1064 *out_end = min(m_end, r_end);
1065 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001066 *out_nid = m_nid;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001067 if (m_start >= r_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001068 idx_a--;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001069 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001070 idx_b--;
1071 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001072 return;
1073 }
1074 }
1075 }
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001076 /* signal end of iteration */
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001077 *idx = ULLONG_MAX;
1078}
1079
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001080#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1081/*
1082 * Common iterator interface used to define for_each_mem_range().
1083 */
1084void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1085 unsigned long *out_start_pfn,
1086 unsigned long *out_end_pfn, int *out_nid)
1087{
1088 struct memblock_type *type = &memblock.memory;
1089 struct memblock_region *r;
1090
1091 while (++*idx < type->cnt) {
1092 r = &type->regions[*idx];
1093
1094 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1095 continue;
1096 if (nid == MAX_NUMNODES || nid == r->nid)
1097 break;
1098 }
1099 if (*idx >= type->cnt) {
1100 *idx = -1;
1101 return;
1102 }
1103
1104 if (out_start_pfn)
1105 *out_start_pfn = PFN_UP(r->base);
1106 if (out_end_pfn)
1107 *out_end_pfn = PFN_DOWN(r->base + r->size);
1108 if (out_nid)
1109 *out_nid = r->nid;
1110}
1111
1112/**
1113 * memblock_set_node - set node ID on memblock regions
1114 * @base: base of area to set node ID for
1115 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -08001116 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001117 * @nid: node ID to set
1118 *
Tang Chene7e8de52014-01-21 15:49:26 -08001119 * Set the nid of memblock @type regions in [@base,@base+@size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001120 * Regions which cross the area boundaries are split as necessary.
1121 *
1122 * RETURNS:
1123 * 0 on success, -errno on failure.
1124 */
1125int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -08001126 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001127{
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001128 int start_rgn, end_rgn;
1129 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001130
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001131 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1132 if (ret)
1133 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001134
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001135 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -07001136 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001137
1138 memblock_merge_regions(type);
1139 return 0;
1140}
1141#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1142
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001143static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1144 phys_addr_t align, phys_addr_t start,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001145 phys_addr_t end, int nid, ulong flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001146{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001147 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001148
Grygorii Strashko79f40fa2014-01-21 15:50:12 -08001149 if (!align)
1150 align = SMP_CACHE_BYTES;
Vineet Gupta94f3d3a2013-04-29 15:06:15 -07001151
Tony Luckfc6daaf2015-06-24 16:58:09 -07001152 found = memblock_find_in_range_node(size, align, start, end, nid,
1153 flags);
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001154 if (found && !memblock_reserve(found, size)) {
1155 /*
1156 * The min_count is set to 0 so that memblock allocations are
1157 * never reported as leaks.
1158 */
Shiraz Hashimcb3a9222016-01-19 15:11:10 +05301159 if (found < memblock.current_limit)
1160 kmemleak_alloc(__va(found), size, 0, 0);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001161 return found;
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001162 }
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001163 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001164}
1165
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001166phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001167 phys_addr_t start, phys_addr_t end,
1168 ulong flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001169{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001170 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1171 flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001172}
1173
1174static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
1175 phys_addr_t align, phys_addr_t max_addr,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001176 int nid, ulong flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001177{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001178 return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001179}
1180
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001181phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
1182{
Tony Lucka3f5baf2015-06-24 16:58:12 -07001183 ulong flags = choose_memblock_flags();
1184 phys_addr_t ret;
1185
1186again:
1187 ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE,
1188 nid, flags);
1189
1190 if (!ret && (flags & MEMBLOCK_MIRROR)) {
1191 flags &= ~MEMBLOCK_MIRROR;
1192 goto again;
1193 }
1194 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001195}
1196
1197phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1198{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001199 return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE,
1200 MEMBLOCK_NONE);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001201}
1202
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001203phys_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 +10001204{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001205 phys_addr_t alloc;
1206
1207 alloc = __memblock_alloc_base(size, align, max_addr);
1208
1209 if (alloc == 0)
1210 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
1211 (unsigned long long) size, (unsigned long long) max_addr);
1212
1213 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001214}
1215
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001216phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001217{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001218 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001219}
1220
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001221phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1222{
1223 phys_addr_t res = memblock_alloc_nid(size, align, nid);
1224
1225 if (res)
1226 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +02001227 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001228}
1229
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001230/**
1231 * memblock_virt_alloc_internal - allocate boot memory block
1232 * @size: size of memory block to be allocated in bytes
1233 * @align: alignment of the region and block's size
1234 * @min_addr: the lower bound of the memory region to allocate (phys address)
1235 * @max_addr: the upper bound of the memory region to allocate (phys address)
1236 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1237 *
1238 * The @min_addr limit is dropped if it can not be satisfied and the allocation
1239 * will fall back to memory below @min_addr. Also, allocation may fall back
1240 * to any node in the system if the specified node can not
1241 * hold the requested memory.
1242 *
1243 * The allocation is performed from memory region limited by
1244 * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE.
1245 *
1246 * The memory block is aligned on SMP_CACHE_BYTES if @align == 0.
1247 *
1248 * The phys address of allocated boot memory block is converted to virtual and
1249 * allocated memory is reset to 0.
1250 *
1251 * In addition, function sets the min_count to 0 using kmemleak_alloc for
1252 * allocated boot memory block, so that it is never reported as leaks.
1253 *
1254 * RETURNS:
1255 * Virtual address of allocated memory block on success, NULL on failure.
1256 */
1257static void * __init memblock_virt_alloc_internal(
1258 phys_addr_t size, phys_addr_t align,
1259 phys_addr_t min_addr, phys_addr_t max_addr,
1260 int nid)
1261{
1262 phys_addr_t alloc;
1263 void *ptr;
Tony Lucka3f5baf2015-06-24 16:58:12 -07001264 ulong flags = choose_memblock_flags();
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001265
Grygorii Strashko560dca272014-01-21 15:50:55 -08001266 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1267 nid = NUMA_NO_NODE;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001268
1269 /*
1270 * Detect any accidental use of these APIs after slab is ready, as at
1271 * this moment memblock may be deinitialized already and its
1272 * internal data may be destroyed (after execution of free_all_bootmem)
1273 */
1274 if (WARN_ON_ONCE(slab_is_available()))
1275 return kzalloc_node(size, GFP_NOWAIT, nid);
1276
1277 if (!align)
1278 align = SMP_CACHE_BYTES;
1279
Yinghai Luf544e142014-01-29 14:05:52 -08001280 if (max_addr > memblock.current_limit)
1281 max_addr = memblock.current_limit;
1282
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001283again:
1284 alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001285 nid, flags);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001286 if (alloc)
1287 goto done;
1288
1289 if (nid != NUMA_NO_NODE) {
1290 alloc = memblock_find_in_range_node(size, align, min_addr,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001291 max_addr, NUMA_NO_NODE,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001292 flags);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001293 if (alloc)
1294 goto done;
1295 }
1296
1297 if (min_addr) {
1298 min_addr = 0;
1299 goto again;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001300 }
1301
Tony Lucka3f5baf2015-06-24 16:58:12 -07001302 if (flags & MEMBLOCK_MIRROR) {
1303 flags &= ~MEMBLOCK_MIRROR;
1304 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1305 &size);
1306 goto again;
1307 }
1308
1309 return NULL;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001310done:
1311 memblock_reserve(alloc, size);
1312 ptr = phys_to_virt(alloc);
1313 memset(ptr, 0, size);
1314
1315 /*
1316 * The min_count is set to 0 so that bootmem allocated blocks
1317 * are never reported as leaks. This is because many of these blocks
1318 * are only referred via the physical address which is not
1319 * looked up by kmemleak.
1320 */
1321 kmemleak_alloc(ptr, size, 0, 0);
1322
1323 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001324}
1325
1326/**
1327 * memblock_virt_alloc_try_nid_nopanic - allocate boot memory block
1328 * @size: size of memory block to be allocated in bytes
1329 * @align: alignment of the region and block's size
1330 * @min_addr: the lower bound of the memory region from where the allocation
1331 * is preferred (phys address)
1332 * @max_addr: the upper bound of the memory region from where the allocation
1333 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1334 * allocate only from memory limited by memblock.current_limit value
1335 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1336 *
1337 * Public version of _memblock_virt_alloc_try_nid_nopanic() which provides
1338 * additional debug information (including caller info), if enabled.
1339 *
1340 * RETURNS:
1341 * Virtual address of allocated memory block on success, NULL on failure.
1342 */
1343void * __init memblock_virt_alloc_try_nid_nopanic(
1344 phys_addr_t size, phys_addr_t align,
1345 phys_addr_t min_addr, phys_addr_t max_addr,
1346 int nid)
1347{
1348 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1349 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1350 (u64)max_addr, (void *)_RET_IP_);
1351 return memblock_virt_alloc_internal(size, align, min_addr,
1352 max_addr, nid);
1353}
1354
1355/**
1356 * memblock_virt_alloc_try_nid - allocate boot memory block with panicking
1357 * @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 from where the allocation
1360 * is preferred (phys address)
1361 * @max_addr: the upper bound of the memory region from where the allocation
1362 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1363 * allocate only from memory limited by memblock.current_limit value
1364 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1365 *
1366 * Public panicking version of _memblock_virt_alloc_try_nid_nopanic()
1367 * which provides debug information (including caller info), if enabled,
1368 * and panics if the request can not be satisfied.
1369 *
1370 * RETURNS:
1371 * Virtual address of allocated memory block on success, NULL on failure.
1372 */
1373void * __init memblock_virt_alloc_try_nid(
1374 phys_addr_t size, phys_addr_t align,
1375 phys_addr_t min_addr, phys_addr_t max_addr,
1376 int nid)
1377{
1378 void *ptr;
1379
1380 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1381 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1382 (u64)max_addr, (void *)_RET_IP_);
1383 ptr = memblock_virt_alloc_internal(size, align,
1384 min_addr, max_addr, nid);
1385 if (ptr)
1386 return ptr;
1387
1388 panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx\n",
1389 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1390 (u64)max_addr);
1391 return NULL;
1392}
1393
1394/**
1395 * __memblock_free_early - free boot memory block
1396 * @base: phys starting address of the boot memory block
1397 * @size: size of the boot memory block in bytes
1398 *
1399 * Free boot memory block previously allocated by memblock_virt_alloc_xx() API.
1400 * The freeing memory will not be released to the buddy allocator.
1401 */
1402void __init __memblock_free_early(phys_addr_t base, phys_addr_t size)
1403{
1404 memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1405 __func__, (u64)base, (u64)base + size - 1,
1406 (void *)_RET_IP_);
1407 kmemleak_free_part(__va(base), size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001408 memblock_remove_range(&memblock.reserved, base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001409}
1410
1411/*
1412 * __memblock_free_late - free bootmem block pages directly to buddy allocator
1413 * @addr: phys starting address of the boot memory block
1414 * @size: size of the boot memory block in bytes
1415 *
1416 * This is only useful when the bootmem allocator has already been torn
1417 * down, but we are still initializing the system. Pages are released directly
1418 * to the buddy allocator, no bootmem metadata is updated because it is gone.
1419 */
1420void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1421{
1422 u64 cursor, end;
1423
1424 memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1425 __func__, (u64)base, (u64)base + size - 1,
1426 (void *)_RET_IP_);
1427 kmemleak_free_part(__va(base), size);
1428 cursor = PFN_UP(base);
1429 end = PFN_DOWN(base + size);
1430
1431 for (; cursor < end; cursor++) {
Mel Gormand70ddd72015-06-30 14:56:52 -07001432 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001433 totalram_pages++;
1434 }
1435}
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001436
1437/*
1438 * Remaining API functions
1439 */
1440
David Gibson1f1ffb8a2016-02-05 15:36:19 -08001441phys_addr_t __init_memblock memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001442{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001443 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001444}
1445
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001446phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1447{
1448 unsigned long pages = 0;
1449 struct memblock_region *r;
1450 unsigned long start_pfn, end_pfn;
1451
1452 for_each_memblock(memory, r) {
1453 start_pfn = memblock_region_memory_base_pfn(r);
1454 end_pfn = memblock_region_memory_end_pfn(r);
1455 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1456 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1457 pages += end_pfn - start_pfn;
1458 }
1459
Fabian Frederick16763232014-04-07 15:37:53 -07001460 return PFN_PHYS(pages);
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001461}
1462
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001463/* lowest address */
1464phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1465{
1466 return memblock.memory.regions[0].base;
1467}
1468
Yinghai Lu10d06432010-07-28 15:43:02 +10001469phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001470{
1471 int idx = memblock.memory.cnt - 1;
1472
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001473 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001474}
1475
Dennis Chena571d4e2016-07-28 15:48:26 -07001476static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001477{
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001478 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
Emil Medve136199f2014-04-07 15:37:52 -07001479 struct memblock_region *r;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001480
Dennis Chena571d4e2016-07-28 15:48:26 -07001481 /*
1482 * translate the memory @limit size into the max address within one of
1483 * the memory memblock regions, if the @limit exceeds the total size
1484 * of those regions, max_addr will keep original value ULLONG_MAX
1485 */
Emil Medve136199f2014-04-07 15:37:52 -07001486 for_each_memblock(memory, r) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001487 if (limit <= r->size) {
1488 max_addr = r->base + limit;
1489 break;
1490 }
1491 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001492 }
1493
Dennis Chena571d4e2016-07-28 15:48:26 -07001494 return max_addr;
1495}
1496
1497void __init memblock_enforce_memory_limit(phys_addr_t limit)
1498{
1499 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
1500
1501 if (!limit)
1502 return;
1503
1504 max_addr = __find_max_addr(limit);
1505
1506 /* @limit exceeds the total size of the memory, do nothing */
1507 if (max_addr == (phys_addr_t)ULLONG_MAX)
1508 return;
1509
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001510 /* truncate both memory and reserved regions */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001511 memblock_remove_range(&memblock.memory, max_addr,
1512 (phys_addr_t)ULLONG_MAX);
1513 memblock_remove_range(&memblock.reserved, max_addr,
1514 (phys_addr_t)ULLONG_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001515}
1516
Dennis Chena571d4e2016-07-28 15:48:26 -07001517void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1518{
1519 struct memblock_type *type = &memblock.memory;
1520 phys_addr_t max_addr;
1521 int i, ret, start_rgn, end_rgn;
1522
1523 if (!limit)
1524 return;
1525
1526 max_addr = __find_max_addr(limit);
1527
1528 /* @limit exceeds the total size of the memory, do nothing */
1529 if (max_addr == (phys_addr_t)ULLONG_MAX)
1530 return;
1531
1532 ret = memblock_isolate_range(type, max_addr, (phys_addr_t)ULLONG_MAX,
1533 &start_rgn, &end_rgn);
1534 if (ret)
1535 return;
1536
1537 /* remove all the MAP regions above the limit */
1538 for (i = end_rgn - 1; i >= start_rgn; i--) {
1539 if (!memblock_is_nomap(&type->regions[i]))
1540 memblock_remove_region(type, i);
1541 }
1542 /* truncate the reserved regions */
1543 memblock_remove_range(&memblock.reserved, max_addr,
1544 (phys_addr_t)ULLONG_MAX);
1545}
1546
Shiraz Hashime1525702016-02-04 14:53:33 +05301547static int __init_memblock __memblock_search(struct memblock_type *type,
1548 phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001549{
1550 unsigned int left = 0, right = type->cnt;
1551
1552 do {
1553 unsigned int mid = (right + left) / 2;
1554
1555 if (addr < type->regions[mid].base)
1556 right = mid;
1557 else if (addr >= (type->regions[mid].base +
1558 type->regions[mid].size))
1559 left = mid + 1;
1560 else
1561 return mid;
1562 } while (left < right);
1563 return -1;
1564}
1565
Shiraz Hashime1525702016-02-04 14:53:33 +05301566static int __init_memblock memblock_search(struct memblock_type *type,
1567 phys_addr_t addr)
1568{
1569 int ret;
1570 unsigned long seq;
1571
1572 do {
1573 seq = raw_read_seqcount_begin(&memblock_seq);
1574 ret = __memblock_search(type, addr);
1575 } while (unlikely(read_seqcount_retry(&memblock_seq, seq)));
1576
1577 return ret;
1578}
1579
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001580bool __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001581{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001582 return memblock_search(&memblock.reserved, addr) != -1;
1583}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001584
Yaowei Baib4ad0c72016-01-14 15:18:54 -08001585bool __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001586{
1587 return memblock_search(&memblock.memory, addr) != -1;
1588}
1589
Ard Biesheuvelbf3d3cc2015-11-30 13:28:15 +01001590int __init_memblock memblock_is_map_memory(phys_addr_t addr)
1591{
1592 int i = memblock_search(&memblock.memory, addr);
1593
1594 if (i == -1)
1595 return false;
1596 return !memblock_is_nomap(&memblock.memory.regions[i]);
1597}
1598
Yinghai Lue76b63f2013-09-11 14:22:17 -07001599#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1600int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1601 unsigned long *start_pfn, unsigned long *end_pfn)
1602{
1603 struct memblock_type *type = &memblock.memory;
Fabian Frederick16763232014-04-07 15:37:53 -07001604 int mid = memblock_search(type, PFN_PHYS(pfn));
Yinghai Lue76b63f2013-09-11 14:22:17 -07001605
1606 if (mid == -1)
1607 return -1;
1608
Fabian Frederickf7e2f7e2014-06-04 16:07:51 -07001609 *start_pfn = PFN_DOWN(type->regions[mid].base);
1610 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001611
1612 return type->regions[mid].nid;
1613}
1614#endif
1615
Stephen Boydeab30942012-05-24 00:45:21 -07001616/**
1617 * memblock_is_region_memory - check if a region is a subset of memory
1618 * @base: base of region to check
1619 * @size: size of region to check
1620 *
1621 * Check if the region [@base, @base+@size) is a subset of a memory block.
1622 *
1623 * RETURNS:
1624 * 0 if false, non-zero if true
1625 */
Yinghai Lu3661ca62010-09-15 13:05:29 -07001626int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001627{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001628 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001629 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001630
1631 if (idx == -1)
1632 return 0;
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001633 return memblock.memory.regions[idx].base <= base &&
1634 (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001635 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001636}
1637
Stephen Boyd39ea1a32012-05-14 18:55:50 -07001638int __init_memblock memblock_overlaps_memory(phys_addr_t base, phys_addr_t size)
1639{
1640 memblock_cap_size(base, &size);
1641
1642 return memblock_overlaps_region(&memblock.memory, base, size) >= 0;
1643}
1644
Stephen Boydeab30942012-05-24 00:45:21 -07001645/**
1646 * memblock_is_region_reserved - check if a region intersects reserved memory
1647 * @base: base of region to check
1648 * @size: size of region to check
1649 *
1650 * Check if the region [@base, @base+@size) intersects a reserved memory block.
1651 *
1652 * RETURNS:
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001653 * True if they intersect, false if not.
Stephen Boydeab30942012-05-24 00:45:21 -07001654 */
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001655bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001656{
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001657 memblock_cap_size(base, &size);
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001658 return memblock_overlaps_region(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001659}
1660
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001661void __init_memblock memblock_trim_memory(phys_addr_t align)
1662{
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001663 phys_addr_t start, end, orig_start, orig_end;
Emil Medve136199f2014-04-07 15:37:52 -07001664 struct memblock_region *r;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001665
Emil Medve136199f2014-04-07 15:37:52 -07001666 for_each_memblock(memory, r) {
1667 orig_start = r->base;
1668 orig_end = r->base + r->size;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001669 start = round_up(orig_start, align);
1670 end = round_down(orig_end, align);
1671
1672 if (start == orig_start && end == orig_end)
1673 continue;
1674
1675 if (start < end) {
Emil Medve136199f2014-04-07 15:37:52 -07001676 r->base = start;
1677 r->size = end - start;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001678 } else {
Emil Medve136199f2014-04-07 15:37:52 -07001679 memblock_remove_region(&memblock.memory,
1680 r - memblock.memory.regions);
1681 r--;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001682 }
1683 }
1684}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001685
Yinghai Lu3661ca62010-09-15 13:05:29 -07001686void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001687{
1688 memblock.current_limit = limit;
1689}
1690
Laura Abbottfec51012014-02-27 01:23:43 +01001691phys_addr_t __init_memblock memblock_get_current_limit(void)
1692{
1693 return memblock.current_limit;
1694}
1695
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001696static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001697{
1698 unsigned long long base, size;
Tang Chen66a20752014-01-21 15:49:20 -08001699 unsigned long flags;
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001700 int idx;
1701 struct memblock_region *rgn;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001702
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001703 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001704
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001705 for_each_memblock_type(type, rgn) {
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001706 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001707
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001708 base = rgn->base;
1709 size = rgn->size;
Tang Chen66a20752014-01-21 15:49:20 -08001710 flags = rgn->flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001711#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1712 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1713 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1714 memblock_get_region_node(rgn));
1715#endif
Tang Chen66a20752014-01-21 15:49:20 -08001716 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n",
Alexander Kuleshov8c9c1702016-01-14 15:20:42 -08001717 name, idx, base, base + size - 1, size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001718 }
1719}
1720
Tejun Heo4ff7b822011-12-08 10:22:06 -08001721void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001722{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001723 pr_info("MEMBLOCK configuration:\n");
Tejun Heo1440c4e2011-12-08 10:22:08 -08001724 pr_info(" memory size = %#llx reserved size = %#llx\n",
1725 (unsigned long long)memblock.memory.total_size,
1726 (unsigned long long)memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001727
1728 memblock_dump(&memblock.memory, "memory");
1729 memblock_dump(&memblock.reserved, "reserved");
1730}
1731
Tejun Heo1aadc052011-12-08 10:22:08 -08001732void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001733{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001734 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001735}
1736
Shiraz Hashime1525702016-02-04 14:53:33 +05301737static void __init_memblock memblock_resize_late(int begin)
1738{
1739 static int memblock_can_resize_old;
1740
1741 if (begin) {
1742 preempt_disable();
1743 memblock_can_resize_old = memblock_can_resize;
1744 memblock_can_resize = 0;
1745 raw_write_seqcount_begin(&memblock_seq);
1746 } else {
1747 raw_write_seqcount_end(&memblock_seq);
1748 memblock_can_resize = memblock_can_resize_old;
1749 preempt_enable();
1750 }
1751}
1752
1753void __init_memblock memblock_region_resize_late_begin(void)
1754{
1755 memblock_resize_late(1);
1756}
1757
1758void __init_memblock memblock_region_resize_late_end(void)
1759{
1760 memblock_resize_late(0);
1761}
1762
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001763static int __init early_memblock(char *p)
1764{
1765 if (p && strstr(p, "debug"))
1766 memblock_debug = 1;
1767 return 0;
1768}
1769early_param("memblock", early_memblock);
1770
Tejun Heoc378ddd2011-07-14 11:46:03 +02001771#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001772
1773static int memblock_debug_show(struct seq_file *m, void *private)
1774{
1775 struct memblock_type *type = m->private;
1776 struct memblock_region *reg;
1777 int i;
1778
1779 for (i = 0; i < type->cnt; i++) {
1780 reg = &type->regions[i];
1781 seq_printf(m, "%4d: ", i);
1782 if (sizeof(phys_addr_t) == 4)
1783 seq_printf(m, "0x%08lx..0x%08lx\n",
1784 (unsigned long)reg->base,
1785 (unsigned long)(reg->base + reg->size - 1));
1786 else
1787 seq_printf(m, "0x%016llx..0x%016llx\n",
1788 (unsigned long long)reg->base,
1789 (unsigned long long)(reg->base + reg->size - 1));
1790
1791 }
1792 return 0;
1793}
1794
1795static int memblock_debug_open(struct inode *inode, struct file *file)
1796{
1797 return single_open(file, memblock_debug_show, inode->i_private);
1798}
1799
1800static const struct file_operations memblock_debug_fops = {
1801 .open = memblock_debug_open,
1802 .read = seq_read,
1803 .llseek = seq_lseek,
1804 .release = single_release,
1805};
1806
1807static int __init memblock_init_debugfs(void)
1808{
1809 struct dentry *root = debugfs_create_dir("memblock", NULL);
1810 if (!root)
1811 return -ENXIO;
1812 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1813 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001814#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
1815 debugfs_create_file("physmem", S_IRUGO, root, &memblock.physmem, &memblock_debug_fops);
1816#endif
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001817
1818 return 0;
1819}
1820__initcall(memblock_init_debugfs);
1821
1822#endif /* CONFIG_DEBUG_FS */