blob: e799ac0f123f58579bcf3f59de209d9030999a57 [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>
22
Tang Chen79442ed2013-11-12 15:07:59 -080023#include <asm-generic/sections.h>
Santosh Shilimkar26f09e92014-01-21 15:50:19 -080024#include <linux/io.h>
25
26#include "internal.h"
Tang Chen79442ed2013-11-12 15:07:59 -080027
Tejun Heofe091c22011-12-08 10:22:07 -080028static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
29static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010030#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
31static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
32#endif
Tejun Heofe091c22011-12-08 10:22:07 -080033
34struct memblock memblock __initdata_memblock = {
35 .memory.regions = memblock_memory_init_regions,
36 .memory.cnt = 1, /* empty dummy entry */
37 .memory.max = INIT_MEMBLOCK_REGIONS,
38
39 .reserved.regions = memblock_reserved_init_regions,
40 .reserved.cnt = 1, /* empty dummy entry */
41 .reserved.max = INIT_MEMBLOCK_REGIONS,
42
Philipp Hachtmann70210ed2014-01-29 18:16:01 +010043#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
44 .physmem.regions = memblock_physmem_init_regions,
45 .physmem.cnt = 1, /* empty dummy entry */
46 .physmem.max = INIT_PHYSMEM_REGIONS,
47#endif
48
Tang Chen79442ed2013-11-12 15:07:59 -080049 .bottom_up = false,
Tejun Heofe091c22011-12-08 10:22:07 -080050 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
51};
Yinghai Lu95f72d12010-07-12 14:36:09 +100052
Yinghai Lu10d06432010-07-28 15:43:02 +100053int memblock_debug __initdata_memblock;
Tang Chen55ac5902014-01-21 15:49:35 -080054#ifdef CONFIG_MOVABLE_NODE
55bool movable_node_enabled __initdata_memblock = false;
56#endif
Tony Lucka3f5baf2015-06-24 16:58:12 -070057static bool system_has_some_mirror __initdata_memblock = false;
Tejun Heo1aadc052011-12-08 10:22:08 -080058static int memblock_can_resize __initdata_memblock;
Gavin Shan181eb392012-05-29 15:06:50 -070059static int memblock_memory_in_slab __initdata_memblock = 0;
60static int memblock_reserved_in_slab __initdata_memblock = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +100061
Tony Lucka3f5baf2015-06-24 16:58:12 -070062ulong __init_memblock choose_memblock_flags(void)
63{
64 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
65}
66
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070067/* inline so we don't get a warning when pr_debug is compiled out */
Raghavendra D Prabhuc2233112012-10-08 16:33:55 -070068static __init_memblock const char *
69memblock_type_name(struct memblock_type *type)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070070{
71 if (type == &memblock.memory)
72 return "memory";
73 else if (type == &memblock.reserved)
74 return "reserved";
75 else
76 return "unknown";
77}
78
Tejun Heoeb18f1b52011-12-08 10:22:07 -080079/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
80static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
81{
82 return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
83}
84
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100085/*
86 * Address comparison utilities
87 */
Yinghai Lu10d06432010-07-28 15:43:02 +100088static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100089 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100090{
91 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
92}
93
Tang Chen95cf82e2015-09-08 15:02:03 -070094bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -070095 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100096{
97 unsigned long i;
98
99 for (i = 0; i < type->cnt; i++) {
100 phys_addr_t rgnbase = type->regions[i].base;
101 phys_addr_t rgnsize = type->regions[i].size;
102 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
103 break;
104 }
105
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 Perches40ae2222016-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.
488 * @type must already have extra room to accomodate the new region.
489 */
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 Heoeb18f1b52011-12-08 10:22:07 -0800528 phys_addr_t obase = base;
529 phys_addr_t end = base + memblock_cap_size(base, &size);
Tejun Heo784656f92011-07-12 11:15:55 +0200530 int i, nr_new;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000531
Tejun Heob3dc6272012-04-20 08:31:34 -0700532 if (!size)
533 return 0;
534
Tejun Heo784656f92011-07-12 11:15:55 +0200535 /* special case for empty array */
536 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800537 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000538 type->regions[0].base = base;
539 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800540 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800541 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800542 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000543 return 0;
544 }
Tejun Heo784656f92011-07-12 11:15:55 +0200545repeat:
546 /*
547 * The following is executed twice. Once with %false @insert and
548 * then with %true. The first counts the number of regions needed
549 * to accomodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700550 */
Tejun Heo784656f92011-07-12 11:15:55 +0200551 base = obase;
552 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000553
Tejun Heo784656f92011-07-12 11:15:55 +0200554 for (i = 0; i < type->cnt; i++) {
555 struct memblock_region *rgn = &type->regions[i];
556 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)
574 memblock_insert_region(type, i++, 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)
Tang Chen66a20752014-01-21 15:49:20 -0800586 memblock_insert_region(type, i, base, end - base,
587 nid, flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200588 }
589
590 /*
591 * If this was the first round, resize array and repeat for actual
592 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700593 */
Tejun Heo784656f92011-07-12 11:15:55 +0200594 if (!insert) {
595 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700596 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f92011-07-12 11:15:55 +0200597 return -ENOMEM;
598 insert = true;
599 goto repeat;
600 } else {
601 memblock_merge_regions(type);
602 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700603 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000604}
605
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800606int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
607 int nid)
608{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100609 return memblock_add_range(&memblock.memory, base, size, nid, 0);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800610}
611
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700612static int __init_memblock memblock_add_region(phys_addr_t base,
613 phys_addr_t size,
614 int nid,
615 unsigned long flags)
616{
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700617 struct memblock_type *type = &memblock.memory;
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700618
619 memblock_dbg("memblock_add: [%#016llx-%#016llx] flags %#02lx %pF\n",
620 (unsigned long long)base,
621 (unsigned long long)base + size - 1,
622 flags, (void *)_RET_IP_);
623
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700624 return memblock_add_range(type, base, size, nid, flags);
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700625}
626
Tejun Heo581adcb2011-12-08 10:22:06 -0800627int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000628{
Alexander Kuleshov6a4055b2015-04-15 16:14:44 -0700629 return memblock_add_region(base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000630}
631
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800632/**
633 * memblock_isolate_range - isolate given range into disjoint memblocks
634 * @type: memblock type to isolate range for
635 * @base: base of range to isolate
636 * @size: size of range to isolate
637 * @start_rgn: out parameter for the start of isolated region
638 * @end_rgn: out parameter for the end of isolated region
639 *
640 * Walk @type and ensure that regions don't cross the boundaries defined by
641 * [@base,@base+@size). Crossing regions are split at the boundaries,
642 * which may create at most two more regions. The index of the first
643 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
644 *
645 * RETURNS:
646 * 0 on success, -errno on failure.
647 */
648static int __init_memblock memblock_isolate_range(struct memblock_type *type,
649 phys_addr_t base, phys_addr_t size,
650 int *start_rgn, int *end_rgn)
651{
Tejun Heoeb18f1b52011-12-08 10:22:07 -0800652 phys_addr_t end = base + memblock_cap_size(base, &size);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800653 int i;
654
655 *start_rgn = *end_rgn = 0;
656
Tejun Heob3dc6272012-04-20 08:31:34 -0700657 if (!size)
658 return 0;
659
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800660 /* we'll create at most two more regions */
661 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700662 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800663 return -ENOMEM;
664
665 for (i = 0; i < type->cnt; i++) {
666 struct memblock_region *rgn = &type->regions[i];
667 phys_addr_t rbase = rgn->base;
668 phys_addr_t rend = rbase + rgn->size;
669
670 if (rbase >= end)
671 break;
672 if (rend <= base)
673 continue;
674
675 if (rbase < base) {
676 /*
677 * @rgn intersects from below. Split and continue
678 * to process the next region - the new top half.
679 */
680 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800681 rgn->size -= base - rbase;
682 type->total_size -= base - rbase;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800683 memblock_insert_region(type, i, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800684 memblock_get_region_node(rgn),
685 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800686 } else if (rend > end) {
687 /*
688 * @rgn intersects from above. Split and redo the
689 * current region - the new bottom half.
690 */
691 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800692 rgn->size -= end - rbase;
693 type->total_size -= end - rbase;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800694 memblock_insert_region(type, i--, rbase, end - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800695 memblock_get_region_node(rgn),
696 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800697 } else {
698 /* @rgn is fully contained, record it */
699 if (!*end_rgn)
700 *start_rgn = i;
701 *end_rgn = i + 1;
702 }
703 }
704
705 return 0;
706}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800707
Alexander Kuleshov35bd16a2015-11-05 18:47:00 -0800708static int __init_memblock memblock_remove_range(struct memblock_type *type,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100709 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000710{
Tejun Heo71936182011-12-08 10:22:07 -0800711 int start_rgn, end_rgn;
712 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000713
Tejun Heo71936182011-12-08 10:22:07 -0800714 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
715 if (ret)
716 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000717
Tejun Heo71936182011-12-08 10:22:07 -0800718 for (i = end_rgn - 1; i >= start_rgn; i--)
719 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700720 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000721}
722
Tejun Heo581adcb2011-12-08 10:22:06 -0800723int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000724{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100725 return memblock_remove_range(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000726}
727
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100728
Tejun Heo581adcb2011-12-08 10:22:06 -0800729int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000730{
Tejun Heo24aa0782011-07-12 11:16:06 +0200731 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700732 (unsigned long long)base,
Grygorii Strashko931d13f2014-01-21 15:49:17 -0800733 (unsigned long long)base + size - 1,
H. Peter Anvina1504392011-07-14 11:57:10 -0700734 (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200735
Catalin Marinasaedf95e2014-06-06 14:38:20 -0700736 kmemleak_free_part(__va(base), size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100737 return memblock_remove_range(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000738}
739
Tang Chen66a20752014-01-21 15:49:20 -0800740static int __init_memblock memblock_reserve_region(phys_addr_t base,
741 phys_addr_t size,
742 int nid,
743 unsigned long flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000744{
Baoquan He7fc825b2015-04-14 15:44:48 -0700745 struct memblock_type *type = &memblock.reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000746
Tang Chen66a20752014-01-21 15:49:20 -0800747 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] flags %#02lx %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700748 (unsigned long long)base,
Grygorii Strashko931d13f2014-01-21 15:49:17 -0800749 (unsigned long long)base + size - 1,
Tang Chen66a20752014-01-21 15:49:20 -0800750 flags, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000751
Baoquan He7fc825b2015-04-14 15:44:48 -0700752 return memblock_add_range(type, base, size, nid, flags);
Tang Chen66a20752014-01-21 15:49:20 -0800753}
754
755int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
756{
757 return memblock_reserve_region(base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000758}
759
Tejun Heo35fd0802011-07-12 11:15:59 +0200760/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800761 *
Tony Luck4308ce12014-12-12 16:54:59 -0800762 * This function isolates region [@base, @base + @size), and sets/clears flag
Tang Chen66b16ed2014-01-21 15:49:23 -0800763 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700764 * Return 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800765 */
Tony Luck4308ce12014-12-12 16:54:59 -0800766static int __init_memblock memblock_setclr_flag(phys_addr_t base,
767 phys_addr_t size, int set, int flag)
Tang Chen66b16ed2014-01-21 15:49:23 -0800768{
769 struct memblock_type *type = &memblock.memory;
770 int i, ret, start_rgn, end_rgn;
771
772 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
773 if (ret)
774 return ret;
775
776 for (i = start_rgn; i < end_rgn; i++)
Tony Luck4308ce12014-12-12 16:54:59 -0800777 if (set)
778 memblock_set_region_flags(&type->regions[i], flag);
779 else
780 memblock_clear_region_flags(&type->regions[i], flag);
Tang Chen66b16ed2014-01-21 15:49:23 -0800781
782 memblock_merge_regions(type);
783 return 0;
784}
785
786/**
Tony Luck4308ce12014-12-12 16:54:59 -0800787 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
788 * @base: the base phys addr of the region
789 * @size: the size of the region
790 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700791 * Return 0 on success, -errno on failure.
Tony Luck4308ce12014-12-12 16:54:59 -0800792 */
793int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
794{
795 return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
796}
797
798/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800799 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
800 * @base: the base phys addr of the region
801 * @size: the size of the region
802 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700803 * Return 0 on success, -errno on failure.
Tang Chen66b16ed2014-01-21 15:49:23 -0800804 */
805int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
806{
Tony Luck4308ce12014-12-12 16:54:59 -0800807 return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
Tang Chen66b16ed2014-01-21 15:49:23 -0800808}
809
810/**
Tony Lucka3f5baf2015-06-24 16:58:12 -0700811 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
812 * @base: the base phys addr of the region
813 * @size: the size of the region
814 *
Alexander Kuleshovc1153932015-09-08 15:04:02 -0700815 * Return 0 on success, -errno on failure.
Tony Lucka3f5baf2015-06-24 16:58:12 -0700816 */
817int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
818{
819 system_has_some_mirror = true;
820
821 return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
822}
823
Ard Biesheuvel02dd1892015-11-30 13:28:15 +0100824/**
825 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
826 * @base: the base phys addr of the region
827 * @size: the size of the region
828 *
829 * Return 0 on success, -errno on failure.
830 */
831int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
832{
833 return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
834}
Tony Lucka3f5baf2015-06-24 16:58:12 -0700835
836/**
AKASHI Takahirofc7b7e92017-04-03 11:23:54 +0900837 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
838 * @base: the base phys addr of the region
839 * @size: the size of the region
840 *
841 * Return 0 on success, -errno on failure.
842 */
843int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
844{
845 return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
846}
Tejun Heo35fd0802011-07-12 11:15:59 +0200847
848/**
Robin Holt8e7a7f82015-06-30 14:56:41 -0700849 * __next_reserved_mem_region - next function for for_each_reserved_region()
850 * @idx: pointer to u64 loop variable
851 * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
852 * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
853 *
854 * Iterate over all reserved memory regions.
855 */
856void __init_memblock __next_reserved_mem_region(u64 *idx,
857 phys_addr_t *out_start,
858 phys_addr_t *out_end)
859{
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700860 struct memblock_type *type = &memblock.reserved;
Robin Holt8e7a7f82015-06-30 14:56:41 -0700861
Alexander Kuleshov567d1172015-09-08 15:03:33 -0700862 if (*idx >= 0 && *idx < type->cnt) {
863 struct memblock_region *r = &type->regions[*idx];
Robin Holt8e7a7f82015-06-30 14:56:41 -0700864 phys_addr_t base = r->base;
865 phys_addr_t size = r->size;
866
867 if (out_start)
868 *out_start = base;
869 if (out_end)
870 *out_end = base + size - 1;
871
872 *idx += 1;
873 return;
874 }
875
876 /* signal end of iteration */
877 *idx = ULLONG_MAX;
878}
879
880/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100881 * __next__mem_range - next function for for_each_free_mem_range() etc.
Tejun Heo35fd0802011-07-12 11:15:59 +0200882 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800883 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -0700884 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100885 * @type_a: pointer to memblock_type from where the range is taken
886 * @type_b: pointer to memblock_type which excludes memory from being taken
Tejun Heo35fd0802011-07-12 11:15:59 +0200887 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
888 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
889 * @out_nid: ptr to int for nid of the range, can be %NULL
890 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100891 * Find the first area from *@idx which matches @nid, fill the out
Tejun Heo35fd0802011-07-12 11:15:59 +0200892 * parameters, and update *@idx for the next iteration. The lower 32bit of
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100893 * *@idx contains index into type_a and the upper 32bit indexes the
894 * areas before each region in type_b. For example, if type_b regions
Tejun Heo35fd0802011-07-12 11:15:59 +0200895 * look like the following,
896 *
897 * 0:[0-16), 1:[32-48), 2:[128-130)
898 *
899 * The upper 32bit indexes the following regions.
900 *
901 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
902 *
903 * As both region arrays are sorted, the function advances the two indices
904 * in lockstep and returns each intersection.
905 */
Tony Luckfc6daaf2015-06-24 16:58:09 -0700906void __init_memblock __next_mem_range(u64 *idx, int nid, ulong flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100907 struct memblock_type *type_a,
908 struct memblock_type *type_b,
909 phys_addr_t *out_start,
910 phys_addr_t *out_end, int *out_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200911{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100912 int idx_a = *idx & 0xffffffff;
913 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -0800914
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100915 if (WARN_ONCE(nid == MAX_NUMNODES,
916 "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
Grygorii Strashko560dca272014-01-21 15:50:55 -0800917 nid = NUMA_NO_NODE;
Tejun Heo35fd0802011-07-12 11:15:59 +0200918
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100919 for (; idx_a < type_a->cnt; idx_a++) {
920 struct memblock_region *m = &type_a->regions[idx_a];
921
Tejun Heo35fd0802011-07-12 11:15:59 +0200922 phys_addr_t m_start = m->base;
923 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100924 int m_nid = memblock_get_region_node(m);
Tejun Heo35fd0802011-07-12 11:15:59 +0200925
926 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100927 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo35fd0802011-07-12 11:15:59 +0200928 continue;
929
Xishi Qiu0a313a92014-09-09 14:50:46 -0700930 /* skip hotpluggable memory regions if needed */
931 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
932 continue;
933
Tony Lucka3f5baf2015-06-24 16:58:12 -0700934 /* if we want mirror memory skip non-mirror memory regions */
935 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
936 continue;
937
Ard Biesheuvel02dd1892015-11-30 13:28:15 +0100938 /* skip nomap memory unless we were asked for it explicitly */
939 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
940 continue;
941
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100942 if (!type_b) {
943 if (out_start)
944 *out_start = m_start;
945 if (out_end)
946 *out_end = m_end;
947 if (out_nid)
948 *out_nid = m_nid;
949 idx_a++;
950 *idx = (u32)idx_a | (u64)idx_b << 32;
951 return;
952 }
Tejun Heo35fd0802011-07-12 11:15:59 +0200953
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100954 /* scan areas before each reservation */
955 for (; idx_b < type_b->cnt + 1; idx_b++) {
956 struct memblock_region *r;
957 phys_addr_t r_start;
958 phys_addr_t r_end;
959
960 r = &type_b->regions[idx_b];
961 r_start = idx_b ? r[-1].base + r[-1].size : 0;
962 r_end = idx_b < type_b->cnt ?
963 r->base : ULLONG_MAX;
964
965 /*
966 * if idx_b advanced past idx_a,
967 * break out to advance idx_a
968 */
Tejun Heo35fd0802011-07-12 11:15:59 +0200969 if (r_start >= m_end)
970 break;
971 /* if the two regions intersect, we're done */
972 if (m_start < r_end) {
973 if (out_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100974 *out_start =
975 max(m_start, r_start);
Tejun Heo35fd0802011-07-12 11:15:59 +0200976 if (out_end)
977 *out_end = min(m_end, r_end);
978 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100979 *out_nid = m_nid;
Tejun Heo35fd0802011-07-12 11:15:59 +0200980 /*
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100981 * The region which ends first is
982 * advanced for the next iteration.
Tejun Heo35fd0802011-07-12 11:15:59 +0200983 */
984 if (m_end <= r_end)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100985 idx_a++;
Tejun Heo35fd0802011-07-12 11:15:59 +0200986 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100987 idx_b++;
988 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo35fd0802011-07-12 11:15:59 +0200989 return;
990 }
991 }
992 }
993
994 /* signal end of iteration */
995 *idx = ULLONG_MAX;
996}
997
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800998/**
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +0100999 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
1000 *
1001 * Finds the next range from type_a which is not marked as unsuitable
1002 * in type_b.
1003 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001004 * @idx: pointer to u64 loop variable
Alexander Kuleshovad5ea8c2015-09-08 15:04:22 -07001005 * @nid: node selector, %NUMA_NO_NODE for all nodes
Tony Luckfc6daaf2015-06-24 16:58:09 -07001006 * @flags: pick from blocks based on memory attributes
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001007 * @type_a: pointer to memblock_type from where the range is taken
1008 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Lidad75572012-06-20 12:53:01 -07001009 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
1010 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
1011 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001012 *
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001013 * Reverse of __next_mem_range().
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001014 */
Tony Luckfc6daaf2015-06-24 16:58:09 -07001015void __init_memblock __next_mem_range_rev(u64 *idx, int nid, ulong flags,
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001016 struct memblock_type *type_a,
1017 struct memblock_type *type_b,
1018 phys_addr_t *out_start,
1019 phys_addr_t *out_end, int *out_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001020{
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001021 int idx_a = *idx & 0xffffffff;
1022 int idx_b = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -08001023
Grygorii Strashko560dca272014-01-21 15:50:55 -08001024 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1025 nid = NUMA_NO_NODE;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001026
1027 if (*idx == (u64)ULLONG_MAX) {
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001028 idx_a = type_a->cnt - 1;
1029 idx_b = type_b->cnt;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001030 }
1031
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001032 for (; idx_a >= 0; idx_a--) {
1033 struct memblock_region *m = &type_a->regions[idx_a];
1034
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001035 phys_addr_t m_start = m->base;
1036 phys_addr_t m_end = m->base + m->size;
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001037 int m_nid = memblock_get_region_node(m);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001038
1039 /* only memory regions are associated with nodes, check it */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001040 if (nid != NUMA_NO_NODE && nid != m_nid)
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001041 continue;
1042
Tang Chen55ac5902014-01-21 15:49:35 -08001043 /* skip hotpluggable memory regions if needed */
1044 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
1045 continue;
1046
Tony Lucka3f5baf2015-06-24 16:58:12 -07001047 /* if we want mirror memory skip non-mirror memory regions */
1048 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1049 continue;
1050
Ard Biesheuvel02dd1892015-11-30 13:28:15 +01001051 /* skip nomap memory unless we were asked for it explicitly */
1052 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1053 continue;
1054
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001055 if (!type_b) {
1056 if (out_start)
1057 *out_start = m_start;
1058 if (out_end)
1059 *out_end = m_end;
1060 if (out_nid)
1061 *out_nid = m_nid;
1062 idx_a++;
1063 *idx = (u32)idx_a | (u64)idx_b << 32;
1064 return;
1065 }
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001066
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001067 /* scan areas before each reservation */
1068 for (; idx_b >= 0; idx_b--) {
1069 struct memblock_region *r;
1070 phys_addr_t r_start;
1071 phys_addr_t r_end;
1072
1073 r = &type_b->regions[idx_b];
1074 r_start = idx_b ? r[-1].base + r[-1].size : 0;
1075 r_end = idx_b < type_b->cnt ?
1076 r->base : ULLONG_MAX;
1077 /*
1078 * if idx_b advanced past idx_a,
1079 * break out to advance idx_a
1080 */
1081
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001082 if (r_end <= m_start)
1083 break;
1084 /* if the two regions intersect, we're done */
1085 if (m_end > r_start) {
1086 if (out_start)
1087 *out_start = max(m_start, r_start);
1088 if (out_end)
1089 *out_end = min(m_end, r_end);
1090 if (out_nid)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001091 *out_nid = m_nid;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001092 if (m_start >= r_start)
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001093 idx_a--;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001094 else
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001095 idx_b--;
1096 *idx = (u32)idx_a | (u64)idx_b << 32;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001097 return;
1098 }
1099 }
1100 }
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001101 /* signal end of iteration */
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001102 *idx = ULLONG_MAX;
1103}
1104
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001105#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1106/*
1107 * Common iterator interface used to define for_each_mem_range().
1108 */
1109void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1110 unsigned long *out_start_pfn,
1111 unsigned long *out_end_pfn, int *out_nid)
1112{
1113 struct memblock_type *type = &memblock.memory;
1114 struct memblock_region *r;
1115
1116 while (++*idx < type->cnt) {
1117 r = &type->regions[*idx];
1118
1119 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1120 continue;
1121 if (nid == MAX_NUMNODES || nid == r->nid)
1122 break;
1123 }
1124 if (*idx >= type->cnt) {
1125 *idx = -1;
1126 return;
1127 }
1128
1129 if (out_start_pfn)
1130 *out_start_pfn = PFN_UP(r->base);
1131 if (out_end_pfn)
1132 *out_end_pfn = PFN_DOWN(r->base + r->size);
1133 if (out_nid)
1134 *out_nid = r->nid;
1135}
1136
1137/**
1138 * memblock_set_node - set node ID on memblock regions
1139 * @base: base of area to set node ID for
1140 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -08001141 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001142 * @nid: node ID to set
1143 *
Tang Chene7e8de52014-01-21 15:49:26 -08001144 * Set the nid of memblock @type regions in [@base,@base+@size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001145 * Regions which cross the area boundaries are split as necessary.
1146 *
1147 * RETURNS:
1148 * 0 on success, -errno on failure.
1149 */
1150int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -08001151 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001152{
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001153 int start_rgn, end_rgn;
1154 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001155
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001156 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1157 if (ret)
1158 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001159
Tejun Heo6a9ceb32011-12-08 10:22:07 -08001160 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -07001161 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001162
1163 memblock_merge_regions(type);
1164 return 0;
1165}
1166#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1167
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001168static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1169 phys_addr_t align, phys_addr_t start,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001170 phys_addr_t end, int nid, ulong flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001171{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001172 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001173
Grygorii Strashko79f40fa2014-01-21 15:50:12 -08001174 if (!align)
1175 align = SMP_CACHE_BYTES;
Vineet Gupta94f3d3a2013-04-29 15:06:15 -07001176
Tony Luckfc6daaf2015-06-24 16:58:09 -07001177 found = memblock_find_in_range_node(size, align, start, end, nid,
1178 flags);
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001179 if (found && !memblock_reserve(found, size)) {
1180 /*
1181 * The min_count is set to 0 so that memblock allocations are
1182 * never reported as leaks.
1183 */
1184 kmemleak_alloc(__va(found), size, 0, 0);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001185 return found;
Catalin Marinasaedf95e2014-06-06 14:38:20 -07001186 }
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001187 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001188}
1189
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001190phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001191 phys_addr_t start, phys_addr_t end,
1192 ulong flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001193{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001194 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1195 flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001196}
1197
1198static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
1199 phys_addr_t align, phys_addr_t max_addr,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001200 int nid, ulong flags)
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001201{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001202 return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags);
Akinobu Mita2bfc2862014-06-04 16:06:53 -07001203}
1204
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001205phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
1206{
Tony Lucka3f5baf2015-06-24 16:58:12 -07001207 ulong flags = choose_memblock_flags();
1208 phys_addr_t ret;
1209
1210again:
1211 ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE,
1212 nid, flags);
1213
1214 if (!ret && (flags & MEMBLOCK_MIRROR)) {
1215 flags &= ~MEMBLOCK_MIRROR;
1216 goto again;
1217 }
1218 return ret;
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001219}
1220
1221phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1222{
Tony Luckfc6daaf2015-06-24 16:58:09 -07001223 return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE,
1224 MEMBLOCK_NONE);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001225}
1226
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001227phys_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 +10001228{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001229 phys_addr_t alloc;
1230
1231 alloc = __memblock_alloc_base(size, align, max_addr);
1232
1233 if (alloc == 0)
1234 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
1235 (unsigned long long) size, (unsigned long long) max_addr);
1236
1237 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001238}
1239
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001240phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001241{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001242 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001243}
1244
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001245phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1246{
1247 phys_addr_t res = memblock_alloc_nid(size, align, nid);
1248
1249 if (res)
1250 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +02001251 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001252}
1253
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001254/**
1255 * memblock_virt_alloc_internal - allocate boot memory block
1256 * @size: size of memory block to be allocated in bytes
1257 * @align: alignment of the region and block's size
1258 * @min_addr: the lower bound of the memory region to allocate (phys address)
1259 * @max_addr: the upper bound of the memory region to allocate (phys address)
1260 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1261 *
1262 * The @min_addr limit is dropped if it can not be satisfied and the allocation
1263 * will fall back to memory below @min_addr. Also, allocation may fall back
1264 * to any node in the system if the specified node can not
1265 * hold the requested memory.
1266 *
1267 * The allocation is performed from memory region limited by
1268 * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE.
1269 *
1270 * The memory block is aligned on SMP_CACHE_BYTES if @align == 0.
1271 *
1272 * The phys address of allocated boot memory block is converted to virtual and
1273 * allocated memory is reset to 0.
1274 *
1275 * In addition, function sets the min_count to 0 using kmemleak_alloc for
1276 * allocated boot memory block, so that it is never reported as leaks.
1277 *
1278 * RETURNS:
1279 * Virtual address of allocated memory block on success, NULL on failure.
1280 */
1281static void * __init memblock_virt_alloc_internal(
1282 phys_addr_t size, phys_addr_t align,
1283 phys_addr_t min_addr, phys_addr_t max_addr,
1284 int nid)
1285{
1286 phys_addr_t alloc;
1287 void *ptr;
Tony Lucka3f5baf2015-06-24 16:58:12 -07001288 ulong flags = choose_memblock_flags();
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001289
Grygorii Strashko560dca272014-01-21 15:50:55 -08001290 if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1291 nid = NUMA_NO_NODE;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001292
1293 /*
1294 * Detect any accidental use of these APIs after slab is ready, as at
1295 * this moment memblock may be deinitialized already and its
1296 * internal data may be destroyed (after execution of free_all_bootmem)
1297 */
1298 if (WARN_ON_ONCE(slab_is_available()))
1299 return kzalloc_node(size, GFP_NOWAIT, nid);
1300
1301 if (!align)
1302 align = SMP_CACHE_BYTES;
1303
Yinghai Luf544e142014-01-29 14:05:52 -08001304 if (max_addr > memblock.current_limit)
1305 max_addr = memblock.current_limit;
1306
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001307again:
1308 alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001309 nid, flags);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001310 if (alloc)
1311 goto done;
1312
1313 if (nid != NUMA_NO_NODE) {
1314 alloc = memblock_find_in_range_node(size, align, min_addr,
Tony Luckfc6daaf2015-06-24 16:58:09 -07001315 max_addr, NUMA_NO_NODE,
Tony Lucka3f5baf2015-06-24 16:58:12 -07001316 flags);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001317 if (alloc)
1318 goto done;
1319 }
1320
1321 if (min_addr) {
1322 min_addr = 0;
1323 goto again;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001324 }
1325
Tony Lucka3f5baf2015-06-24 16:58:12 -07001326 if (flags & MEMBLOCK_MIRROR) {
1327 flags &= ~MEMBLOCK_MIRROR;
1328 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1329 &size);
1330 goto again;
1331 }
1332
1333 return NULL;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001334done:
1335 memblock_reserve(alloc, size);
1336 ptr = phys_to_virt(alloc);
1337 memset(ptr, 0, size);
1338
1339 /*
1340 * The min_count is set to 0 so that bootmem allocated blocks
1341 * are never reported as leaks. This is because many of these blocks
1342 * are only referred via the physical address which is not
1343 * looked up by kmemleak.
1344 */
1345 kmemleak_alloc(ptr, size, 0, 0);
1346
1347 return ptr;
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001348}
1349
1350/**
1351 * memblock_virt_alloc_try_nid_nopanic - allocate boot memory block
1352 * @size: size of memory block to be allocated in bytes
1353 * @align: alignment of the region and block's size
1354 * @min_addr: the lower bound of the memory region from where the allocation
1355 * is preferred (phys address)
1356 * @max_addr: the upper bound of the memory region from where the allocation
1357 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1358 * allocate only from memory limited by memblock.current_limit value
1359 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1360 *
1361 * Public version of _memblock_virt_alloc_try_nid_nopanic() which provides
1362 * additional debug information (including caller info), if enabled.
1363 *
1364 * RETURNS:
1365 * Virtual address of allocated memory block on success, NULL on failure.
1366 */
1367void * __init memblock_virt_alloc_try_nid_nopanic(
1368 phys_addr_t size, phys_addr_t align,
1369 phys_addr_t min_addr, phys_addr_t max_addr,
1370 int nid)
1371{
1372 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1373 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1374 (u64)max_addr, (void *)_RET_IP_);
1375 return memblock_virt_alloc_internal(size, align, min_addr,
1376 max_addr, nid);
1377}
1378
1379/**
1380 * memblock_virt_alloc_try_nid - allocate boot memory block with panicking
1381 * @size: size of memory block to be allocated in bytes
1382 * @align: alignment of the region and block's size
1383 * @min_addr: the lower bound of the memory region from where the allocation
1384 * is preferred (phys address)
1385 * @max_addr: the upper bound of the memory region from where the allocation
1386 * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1387 * allocate only from memory limited by memblock.current_limit value
1388 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1389 *
1390 * Public panicking version of _memblock_virt_alloc_try_nid_nopanic()
1391 * which provides debug information (including caller info), if enabled,
1392 * and panics if the request can not be satisfied.
1393 *
1394 * RETURNS:
1395 * Virtual address of allocated memory block on success, NULL on failure.
1396 */
1397void * __init memblock_virt_alloc_try_nid(
1398 phys_addr_t size, phys_addr_t align,
1399 phys_addr_t min_addr, phys_addr_t max_addr,
1400 int nid)
1401{
1402 void *ptr;
1403
1404 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1405 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1406 (u64)max_addr, (void *)_RET_IP_);
1407 ptr = memblock_virt_alloc_internal(size, align,
1408 min_addr, max_addr, nid);
1409 if (ptr)
1410 return ptr;
1411
1412 panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx\n",
1413 __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1414 (u64)max_addr);
1415 return NULL;
1416}
1417
1418/**
1419 * __memblock_free_early - free boot memory block
1420 * @base: phys starting address of the boot memory block
1421 * @size: size of the boot memory block in bytes
1422 *
1423 * Free boot memory block previously allocated by memblock_virt_alloc_xx() API.
1424 * The freeing memory will not be released to the buddy allocator.
1425 */
1426void __init __memblock_free_early(phys_addr_t base, phys_addr_t size)
1427{
1428 memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1429 __func__, (u64)base, (u64)base + size - 1,
1430 (void *)_RET_IP_);
1431 kmemleak_free_part(__va(base), size);
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001432 memblock_remove_range(&memblock.reserved, base, size);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001433}
1434
1435/*
1436 * __memblock_free_late - free bootmem block pages directly to buddy allocator
1437 * @addr: phys starting address of the boot memory block
1438 * @size: size of the boot memory block in bytes
1439 *
1440 * This is only useful when the bootmem allocator has already been torn
1441 * down, but we are still initializing the system. Pages are released directly
1442 * to the buddy allocator, no bootmem metadata is updated because it is gone.
1443 */
1444void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1445{
1446 u64 cursor, end;
1447
1448 memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1449 __func__, (u64)base, (u64)base + size - 1,
1450 (void *)_RET_IP_);
1451 kmemleak_free_part(__va(base), size);
1452 cursor = PFN_UP(base);
1453 end = PFN_DOWN(base + size);
1454
1455 for (; cursor < end; cursor++) {
Mel Gormand70ddd72015-06-30 14:56:52 -07001456 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
Santosh Shilimkar26f09e92014-01-21 15:50:19 -08001457 totalram_pages++;
1458 }
1459}
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001460
1461/*
1462 * Remaining API functions
1463 */
1464
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +10001465phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001466{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001467 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001468}
1469
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001470phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1471{
1472 unsigned long pages = 0;
1473 struct memblock_region *r;
1474 unsigned long start_pfn, end_pfn;
1475
1476 for_each_memblock(memory, r) {
1477 start_pfn = memblock_region_memory_base_pfn(r);
1478 end_pfn = memblock_region_memory_end_pfn(r);
1479 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1480 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1481 pages += end_pfn - start_pfn;
1482 }
1483
Fabian Frederick16763232014-04-07 15:37:53 -07001484 return PFN_PHYS(pages);
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001485}
1486
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001487/* lowest address */
1488phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1489{
1490 return memblock.memory.regions[0].base;
1491}
1492
Yinghai Lu10d06432010-07-28 15:43:02 +10001493phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001494{
1495 int idx = memblock.memory.cnt - 1;
1496
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001497 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001498}
1499
Dennis Chen520bf992016-07-28 15:48:26 -07001500static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001501{
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001502 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
Emil Medve136199f2014-04-07 15:37:52 -07001503 struct memblock_region *r;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001504
Dennis Chen520bf992016-07-28 15:48:26 -07001505 /*
1506 * translate the memory @limit size into the max address within one of
1507 * the memory memblock regions, if the @limit exceeds the total size
1508 * of those regions, max_addr will keep original value ULLONG_MAX
1509 */
Emil Medve136199f2014-04-07 15:37:52 -07001510 for_each_memblock(memory, r) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001511 if (limit <= r->size) {
1512 max_addr = r->base + limit;
1513 break;
1514 }
1515 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001516 }
1517
Dennis Chen520bf992016-07-28 15:48:26 -07001518 return max_addr;
1519}
1520
1521void __init memblock_enforce_memory_limit(phys_addr_t limit)
1522{
1523 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
1524
1525 if (!limit)
1526 return;
1527
1528 max_addr = __find_max_addr(limit);
1529
1530 /* @limit exceeds the total size of the memory, do nothing */
1531 if (max_addr == (phys_addr_t)ULLONG_MAX)
1532 return;
1533
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001534 /* truncate both memory and reserved regions */
Philipp Hachtmannf1af9d32014-01-29 18:16:01 +01001535 memblock_remove_range(&memblock.memory, max_addr,
1536 (phys_addr_t)ULLONG_MAX);
1537 memblock_remove_range(&memblock.reserved, max_addr,
1538 (phys_addr_t)ULLONG_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001539}
1540
AKASHI Takahiro8512530e2017-04-03 11:23:55 +09001541void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
1542{
1543 int start_rgn, end_rgn;
1544 int i, ret;
1545
1546 if (!size)
1547 return;
1548
1549 ret = memblock_isolate_range(&memblock.memory, base, size,
1550 &start_rgn, &end_rgn);
1551 if (ret)
1552 return;
1553
1554 /* remove all the MAP regions */
1555 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
1556 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1557 memblock_remove_region(&memblock.memory, i);
1558
1559 for (i = start_rgn - 1; i >= 0; i--)
1560 if (!memblock_is_nomap(&memblock.memory.regions[i]))
1561 memblock_remove_region(&memblock.memory, i);
1562
1563 /* truncate the reserved regions */
1564 memblock_remove_range(&memblock.reserved, 0, base);
1565 memblock_remove_range(&memblock.reserved,
1566 base + size, (phys_addr_t)ULLONG_MAX);
1567}
1568
Dennis Chen520bf992016-07-28 15:48:26 -07001569void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1570{
Dennis Chen520bf992016-07-28 15:48:26 -07001571 phys_addr_t max_addr;
Dennis Chen520bf992016-07-28 15:48:26 -07001572
1573 if (!limit)
1574 return;
1575
1576 max_addr = __find_max_addr(limit);
1577
1578 /* @limit exceeds the total size of the memory, do nothing */
1579 if (max_addr == (phys_addr_t)ULLONG_MAX)
1580 return;
1581
AKASHI Takahiro8512530e2017-04-03 11:23:55 +09001582 memblock_cap_memory_range(0, max_addr);
Dennis Chen520bf992016-07-28 15:48:26 -07001583}
1584
Yinghai Lucd794812010-10-11 12:34:09 -07001585static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001586{
1587 unsigned int left = 0, right = type->cnt;
1588
1589 do {
1590 unsigned int mid = (right + left) / 2;
1591
1592 if (addr < type->regions[mid].base)
1593 right = mid;
1594 else if (addr >= (type->regions[mid].base +
1595 type->regions[mid].size))
1596 left = mid + 1;
1597 else
1598 return mid;
1599 } while (left < right);
1600 return -1;
1601}
1602
Yaowei Bai3cd7d782016-01-14 15:18:54 -08001603bool __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001604{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001605 return memblock_search(&memblock.reserved, addr) != -1;
1606}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001607
Yaowei Bai3cd7d782016-01-14 15:18:54 -08001608bool __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001609{
1610 return memblock_search(&memblock.memory, addr) != -1;
1611}
1612
Ard Biesheuvel02dd1892015-11-30 13:28:15 +01001613int __init_memblock memblock_is_map_memory(phys_addr_t addr)
1614{
1615 int i = memblock_search(&memblock.memory, addr);
1616
1617 if (i == -1)
1618 return false;
1619 return !memblock_is_nomap(&memblock.memory.regions[i]);
1620}
1621
Yinghai Lue76b63f2013-09-11 14:22:17 -07001622#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1623int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1624 unsigned long *start_pfn, unsigned long *end_pfn)
1625{
1626 struct memblock_type *type = &memblock.memory;
Fabian Frederick16763232014-04-07 15:37:53 -07001627 int mid = memblock_search(type, PFN_PHYS(pfn));
Yinghai Lue76b63f2013-09-11 14:22:17 -07001628
1629 if (mid == -1)
1630 return -1;
1631
Fabian Frederickf7e2f7e2014-06-04 16:07:51 -07001632 *start_pfn = PFN_DOWN(type->regions[mid].base);
1633 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
Yinghai Lue76b63f2013-09-11 14:22:17 -07001634
1635 return type->regions[mid].nid;
1636}
1637#endif
1638
Stephen Boydeab30942012-05-24 00:45:21 -07001639/**
1640 * memblock_is_region_memory - check if a region is a subset of memory
1641 * @base: base of region to check
1642 * @size: size of region to check
1643 *
1644 * Check if the region [@base, @base+@size) is a subset of a memory block.
1645 *
1646 * RETURNS:
1647 * 0 if false, non-zero if true
1648 */
Yinghai Lu3661ca62010-09-15 13:05:29 -07001649int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001650{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001651 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b52011-12-08 10:22:07 -08001652 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001653
1654 if (idx == -1)
1655 return 0;
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001656 return memblock.memory.regions[idx].base <= base &&
1657 (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b52011-12-08 10:22:07 -08001658 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001659}
1660
Stephen Boydeab30942012-05-24 00:45:21 -07001661/**
1662 * memblock_is_region_reserved - check if a region intersects reserved memory
1663 * @base: base of region to check
1664 * @size: size of region to check
1665 *
1666 * Check if the region [@base, @base+@size) intersects a reserved memory block.
1667 *
1668 * RETURNS:
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001669 * True if they intersect, false if not.
Stephen Boydeab30942012-05-24 00:45:21 -07001670 */
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001671bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001672{
Tejun Heoeb18f1b52011-12-08 10:22:07 -08001673 memblock_cap_size(base, &size);
Tang Chenc5c5c9d2015-09-08 15:02:00 -07001674 return memblock_overlaps_region(&memblock.reserved, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001675}
1676
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001677void __init_memblock memblock_trim_memory(phys_addr_t align)
1678{
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001679 phys_addr_t start, end, orig_start, orig_end;
Emil Medve136199f2014-04-07 15:37:52 -07001680 struct memblock_region *r;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001681
Emil Medve136199f2014-04-07 15:37:52 -07001682 for_each_memblock(memory, r) {
1683 orig_start = r->base;
1684 orig_end = r->base + r->size;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001685 start = round_up(orig_start, align);
1686 end = round_down(orig_end, align);
1687
1688 if (start == orig_start && end == orig_end)
1689 continue;
1690
1691 if (start < end) {
Emil Medve136199f2014-04-07 15:37:52 -07001692 r->base = start;
1693 r->size = end - start;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001694 } else {
Emil Medve136199f2014-04-07 15:37:52 -07001695 memblock_remove_region(&memblock.memory,
1696 r - memblock.memory.regions);
1697 r--;
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001698 }
1699 }
1700}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001701
Yinghai Lu3661ca62010-09-15 13:05:29 -07001702void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001703{
1704 memblock.current_limit = limit;
1705}
1706
Laura Abbottfec51012014-02-27 01:23:43 +01001707phys_addr_t __init_memblock memblock_get_current_limit(void)
1708{
1709 return memblock.current_limit;
1710}
1711
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001712static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001713{
1714 unsigned long long base, size;
Tang Chen66a20752014-01-21 15:49:20 -08001715 unsigned long flags;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001716 int i;
1717
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001718 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001719
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001720 for (i = 0; i < type->cnt; i++) {
1721 struct memblock_region *rgn = &type->regions[i];
1722 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001723
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001724 base = rgn->base;
1725 size = rgn->size;
Tang Chen66a20752014-01-21 15:49:20 -08001726 flags = rgn->flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001727#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1728 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1729 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1730 memblock_get_region_node(rgn));
1731#endif
Tang Chen66a20752014-01-21 15:49:20 -08001732 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n",
1733 name, i, base, base + size - 1, size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001734 }
1735}
1736
Michal Hockocb1fb152017-06-02 14:46:49 -07001737extern unsigned long __init_memblock
1738memblock_reserved_memory_within(phys_addr_t start_addr, phys_addr_t end_addr)
1739{
1740 struct memblock_type *type = &memblock.reserved;
1741 unsigned long size = 0;
1742 int idx;
1743
1744 for (idx = 0; idx < type->cnt; idx++) {
1745 struct memblock_region *rgn = &type->regions[idx];
1746 phys_addr_t start, end;
1747
1748 if (rgn->base + rgn->size < start_addr)
1749 continue;
1750 if (rgn->base > end_addr)
1751 continue;
1752
1753 start = rgn->base;
1754 end = start + rgn->size;
1755 size += end - start;
1756 }
1757
1758 return size;
1759}
1760
Tejun Heo4ff7b822011-12-08 10:22:06 -08001761void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001762{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001763 pr_info("MEMBLOCK configuration:\n");
Tejun Heo1440c4e2011-12-08 10:22:08 -08001764 pr_info(" memory size = %#llx reserved size = %#llx\n",
1765 (unsigned long long)memblock.memory.total_size,
1766 (unsigned long long)memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001767
1768 memblock_dump(&memblock.memory, "memory");
1769 memblock_dump(&memblock.reserved, "reserved");
1770}
1771
Tejun Heo1aadc052011-12-08 10:22:08 -08001772void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001773{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001774 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001775}
1776
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001777static int __init early_memblock(char *p)
1778{
1779 if (p && strstr(p, "debug"))
1780 memblock_debug = 1;
1781 return 0;
1782}
1783early_param("memblock", early_memblock);
1784
Tejun Heoc378ddd2011-07-14 11:46:03 +02001785#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001786
1787static int memblock_debug_show(struct seq_file *m, void *private)
1788{
1789 struct memblock_type *type = m->private;
1790 struct memblock_region *reg;
1791 int i;
1792
1793 for (i = 0; i < type->cnt; i++) {
1794 reg = &type->regions[i];
1795 seq_printf(m, "%4d: ", i);
1796 if (sizeof(phys_addr_t) == 4)
1797 seq_printf(m, "0x%08lx..0x%08lx\n",
1798 (unsigned long)reg->base,
1799 (unsigned long)(reg->base + reg->size - 1));
1800 else
1801 seq_printf(m, "0x%016llx..0x%016llx\n",
1802 (unsigned long long)reg->base,
1803 (unsigned long long)(reg->base + reg->size - 1));
1804
1805 }
1806 return 0;
1807}
1808
1809static int memblock_debug_open(struct inode *inode, struct file *file)
1810{
1811 return single_open(file, memblock_debug_show, inode->i_private);
1812}
1813
1814static const struct file_operations memblock_debug_fops = {
1815 .open = memblock_debug_open,
1816 .read = seq_read,
1817 .llseek = seq_lseek,
1818 .release = single_release,
1819};
1820
1821static int __init memblock_init_debugfs(void)
1822{
1823 struct dentry *root = debugfs_create_dir("memblock", NULL);
1824 if (!root)
1825 return -ENXIO;
1826 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1827 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
Philipp Hachtmann70210ed2014-01-29 18:16:01 +01001828#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
1829 debugfs_create_file("physmem", S_IRUGO, root, &memblock.physmem, &memblock_debug_fops);
1830#endif
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001831
1832 return 0;
1833}
1834__initcall(memblock_init_debugfs);
1835
1836#endif /* CONFIG_DEBUG_FS */