blob: 03f1dc7b663ca028788e8333ce39a2b0c6ddbfcf [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>
24
Tejun Heofe091c22011-12-08 10:22:07 -080025static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
26static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
27
28struct memblock memblock __initdata_memblock = {
29 .memory.regions = memblock_memory_init_regions,
30 .memory.cnt = 1, /* empty dummy entry */
31 .memory.max = INIT_MEMBLOCK_REGIONS,
32
33 .reserved.regions = memblock_reserved_init_regions,
34 .reserved.cnt = 1, /* empty dummy entry */
35 .reserved.max = INIT_MEMBLOCK_REGIONS,
36
Tang Chen79442ed2013-11-12 15:07:59 -080037 .bottom_up = false,
Tejun Heofe091c22011-12-08 10:22:07 -080038 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
39};
Yinghai Lu95f72d12010-07-12 14:36:09 +100040
Yinghai Lu10d06432010-07-28 15:43:02 +100041int memblock_debug __initdata_memblock;
Tang Chen55ac5902014-01-21 15:49:35 -080042#ifdef CONFIG_MOVABLE_NODE
43bool movable_node_enabled __initdata_memblock = false;
44#endif
Tejun Heo1aadc052011-12-08 10:22:08 -080045static int memblock_can_resize __initdata_memblock;
Gavin Shan181eb392012-05-29 15:06:50 -070046static int memblock_memory_in_slab __initdata_memblock = 0;
47static int memblock_reserved_in_slab __initdata_memblock = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +100048
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070049/* inline so we don't get a warning when pr_debug is compiled out */
Raghavendra D Prabhuc2233112012-10-08 16:33:55 -070050static __init_memblock const char *
51memblock_type_name(struct memblock_type *type)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070052{
53 if (type == &memblock.memory)
54 return "memory";
55 else if (type == &memblock.reserved)
56 return "reserved";
57 else
58 return "unknown";
59}
60
Tejun Heoeb18f1b2011-12-08 10:22:07 -080061/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
62static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
63{
64 return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
65}
66
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100067/*
68 * Address comparison utilities
69 */
Yinghai Lu10d06432010-07-28 15:43:02 +100070static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100071 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100072{
73 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
74}
75
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -070076static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
77 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100078{
79 unsigned long i;
80
81 for (i = 0; i < type->cnt; i++) {
82 phys_addr_t rgnbase = type->regions[i].base;
83 phys_addr_t rgnsize = type->regions[i].size;
84 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
85 break;
86 }
87
88 return (i < type->cnt) ? i : -1;
89}
90
Tang Chen79442ed2013-11-12 15:07:59 -080091/*
92 * __memblock_find_range_bottom_up - find free area utility in bottom-up
93 * @start: start of candidate range
94 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
95 * @size: size of free area to find
96 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -080097 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tang Chen79442ed2013-11-12 15:07:59 -080098 *
99 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
100 *
101 * RETURNS:
102 * Found address on success, 0 on failure.
103 */
104static phys_addr_t __init_memblock
105__memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
106 phys_addr_t size, phys_addr_t align, int nid)
107{
108 phys_addr_t this_start, this_end, cand;
109 u64 i;
110
111 for_each_free_mem_range(i, nid, &this_start, &this_end, NULL) {
112 this_start = clamp(this_start, start, end);
113 this_end = clamp(this_end, start, end);
114
115 cand = round_up(this_start, align);
116 if (cand < this_end && this_end - cand >= size)
117 return cand;
118 }
119
120 return 0;
121}
122
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800123/**
Tang Chen14028992013-11-12 15:07:57 -0800124 * __memblock_find_range_top_down - find free area utility, in top-down
125 * @start: start of candidate range
126 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
127 * @size: size of free area to find
128 * @align: alignment of free area to find
Grygorii Strashkob1154232014-01-21 15:50:16 -0800129 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tang Chen14028992013-11-12 15:07:57 -0800130 *
131 * Utility called from memblock_find_in_range_node(), find free area top-down.
132 *
133 * RETURNS:
Tang Chen79442ed2013-11-12 15:07:59 -0800134 * Found address on success, 0 on failure.
Tang Chen14028992013-11-12 15:07:57 -0800135 */
136static phys_addr_t __init_memblock
137__memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
138 phys_addr_t size, phys_addr_t align, int nid)
139{
140 phys_addr_t this_start, this_end, cand;
141 u64 i;
142
143 for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
144 this_start = clamp(this_start, start, end);
145 this_end = clamp(this_end, start, end);
146
147 if (this_end < size)
148 continue;
149
150 cand = round_down(this_end - size, align);
151 if (cand >= this_start)
152 return cand;
153 }
154
155 return 0;
156}
157
158/**
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800159 * memblock_find_in_range_node - find free area in given range and node
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800160 * @size: size of free area to find
161 * @align: alignment of free area to find
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800162 * @start: start of candidate range
163 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
Grygorii Strashkob1154232014-01-21 15:50:16 -0800164 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800165 *
166 * Find @size free area aligned to @align in the specified range and node.
167 *
Tang Chen79442ed2013-11-12 15:07:59 -0800168 * When allocation direction is bottom-up, the @start should be greater
169 * than the end of the kernel image. Otherwise, it will be trimmed. The
170 * reason is that we want the bottom-up allocation just near the kernel
171 * image so it is highly likely that the allocated memory and the kernel
172 * will reside in the same node.
173 *
174 * If bottom-up allocation failed, will try to allocate memory top-down.
175 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800176 * RETURNS:
Tang Chen79442ed2013-11-12 15:07:59 -0800177 * Found address on success, 0 on failure.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000178 */
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800179phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
180 phys_addr_t align, phys_addr_t start,
181 phys_addr_t end, int nid)
Tang Chenf7210e62013-02-22 16:33:51 -0800182{
Tang Chen79442ed2013-11-12 15:07:59 -0800183 int ret;
184 phys_addr_t kernel_end;
185
Tang Chenf7210e62013-02-22 16:33:51 -0800186 /* pump up @end */
187 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
188 end = memblock.current_limit;
189
190 /* avoid allocating the first page */
191 start = max_t(phys_addr_t, start, PAGE_SIZE);
192 end = max(start, end);
Tang Chen79442ed2013-11-12 15:07:59 -0800193 kernel_end = __pa_symbol(_end);
194
195 /*
196 * try bottom-up allocation only when bottom-up mode
197 * is set and @end is above the kernel image.
198 */
199 if (memblock_bottom_up() && end > kernel_end) {
200 phys_addr_t bottom_up_start;
201
202 /* make sure we will allocate above the kernel */
203 bottom_up_start = max(start, kernel_end);
204
205 /* ok, try bottom-up allocation first */
206 ret = __memblock_find_range_bottom_up(bottom_up_start, end,
207 size, align, nid);
208 if (ret)
209 return ret;
210
211 /*
212 * we always limit bottom-up allocation above the kernel,
213 * but top-down allocation doesn't have the limit, so
214 * retrying top-down allocation may succeed when bottom-up
215 * allocation failed.
216 *
217 * bottom-up allocation is expected to be fail very rarely,
218 * so we use WARN_ONCE() here to see the stack trace if
219 * fail happens.
220 */
221 WARN_ONCE(1, "memblock: bottom-up allocation failed, "
222 "memory hotunplug may be affected\n");
223 }
Tang Chenf7210e62013-02-22 16:33:51 -0800224
Tang Chen14028992013-11-12 15:07:57 -0800225 return __memblock_find_range_top_down(start, end, size, align, nid);
Tang Chenf7210e62013-02-22 16:33:51 -0800226}
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000227
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800228/**
229 * memblock_find_in_range - find free area in given range
230 * @start: start of candidate range
231 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
232 * @size: size of free area to find
233 * @align: alignment of free area to find
234 *
235 * Find @size free area aligned to @align in the specified range.
236 *
237 * RETURNS:
Tang Chen79442ed2013-11-12 15:07:59 -0800238 * Found address on success, 0 on failure.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800239 */
240phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
241 phys_addr_t end, phys_addr_t size,
242 phys_addr_t align)
243{
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800244 return memblock_find_in_range_node(size, align, start, end,
Grygorii Strashkob1154232014-01-21 15:50:16 -0800245 NUMA_NO_NODE);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800246}
247
Yinghai Lu10d06432010-07-28 15:43:02 +1000248static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000249{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800250 type->total_size -= type->regions[r].size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200251 memmove(&type->regions[r], &type->regions[r + 1],
252 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000253 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000254
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700255 /* Special case for empty arrays */
256 if (type->cnt == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800257 WARN_ON(type->total_size != 0);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700258 type->cnt = 1;
259 type->regions[0].base = 0;
260 type->regions[0].size = 0;
Tang Chen66a20752014-01-21 15:49:20 -0800261 type->regions[0].flags = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200262 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700263 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000264}
265
Yinghai Lu29f67382012-07-11 14:02:56 -0700266phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info(
267 phys_addr_t *addr)
268{
269 if (memblock.reserved.regions == memblock_reserved_init_regions)
270 return 0;
271
Grygorii Strashkofd615c42014-01-21 15:50:05 -0800272 /*
273 * Don't allow nobootmem allocator to free reserved memory regions
274 * array if
275 * - CONFIG_DEBUG_FS is enabled;
276 * - CONFIG_ARCH_DISCARD_MEMBLOCK is not enabled;
277 * - reserved memory regions array have been resized during boot.
278 * Otherwise debug_fs entry "sys/kernel/debug/memblock/reserved"
279 * will show garbage instead of state of memory reservations.
280 */
281 if (IS_ENABLED(CONFIG_DEBUG_FS) &&
282 !IS_ENABLED(CONFIG_ARCH_DISCARD_MEMBLOCK))
283 return 0;
284
Yinghai Lu29f67382012-07-11 14:02:56 -0700285 *addr = __pa(memblock.reserved.regions);
286
287 return PAGE_ALIGN(sizeof(struct memblock_region) *
288 memblock.reserved.max);
289}
290
Greg Pearson48c3b582012-06-20 12:53:05 -0700291/**
292 * memblock_double_array - double the size of the memblock regions array
293 * @type: memblock type of the regions array being doubled
294 * @new_area_start: starting address of memory range to avoid overlap with
295 * @new_area_size: size of memory range to avoid overlap with
296 *
297 * Double the size of the @type regions array. If memblock is being used to
298 * allocate memory for a new reserved regions array and there is a previously
299 * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
300 * waiting to be reserved, ensure the memory used by the new array does
301 * not overlap.
302 *
303 * RETURNS:
304 * 0 on success, -1 on failure.
305 */
306static int __init_memblock memblock_double_array(struct memblock_type *type,
307 phys_addr_t new_area_start,
308 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700309{
310 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700311 phys_addr_t old_alloc_size, new_alloc_size;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700312 phys_addr_t old_size, new_size, addr;
313 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700314 int *in_slab;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700315
316 /* We don't allow resizing until we know about the reserved regions
317 * of memory that aren't suitable for allocation
318 */
319 if (!memblock_can_resize)
320 return -1;
321
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700322 /* Calculate new doubled size */
323 old_size = type->max * sizeof(struct memblock_region);
324 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700325 /*
326 * We need to allocated new one align to PAGE_SIZE,
327 * so we can free them completely later.
328 */
329 old_alloc_size = PAGE_ALIGN(old_size);
330 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700331
Gavin Shan181eb392012-05-29 15:06:50 -0700332 /* Retrieve the slab flag */
333 if (type == &memblock.memory)
334 in_slab = &memblock_memory_in_slab;
335 else
336 in_slab = &memblock_reserved_in_slab;
337
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700338 /* Try to find some space for it.
339 *
340 * WARNING: We assume that either slab_is_available() and we use it or
Andrew Mortonfd073832012-07-31 16:42:40 -0700341 * we use MEMBLOCK for allocations. That means that this is unsafe to
342 * use when bootmem is currently active (unless bootmem itself is
343 * implemented on top of MEMBLOCK which isn't the case yet)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700344 *
345 * This should however not be an issue for now, as we currently only
Andrew Mortonfd073832012-07-31 16:42:40 -0700346 * call into MEMBLOCK while it's still active, or much later when slab
347 * is active for memory hotplug operations
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700348 */
349 if (use_slab) {
350 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200351 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700352 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700353 /* only exclude range when trying to double reserved.regions */
354 if (type != &memblock.reserved)
355 new_area_start = new_area_size = 0;
356
357 addr = memblock_find_in_range(new_area_start + new_area_size,
358 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700359 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700360 if (!addr && new_area_size)
361 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700362 min(new_area_start, memblock.current_limit),
363 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700364
Sachin Kamat15674862012-09-04 13:55:05 +0530365 new_array = addr ? __va(addr) : NULL;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700366 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200367 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700368 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
369 memblock_type_name(type), type->max, type->max * 2);
370 return -1;
371 }
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700372
Andrew Mortonfd073832012-07-31 16:42:40 -0700373 memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
374 memblock_type_name(type), type->max * 2, (u64)addr,
375 (u64)addr + new_size - 1);
Yinghai Luea9e4372010-07-28 15:13:22 +1000376
Andrew Mortonfd073832012-07-31 16:42:40 -0700377 /*
378 * Found space, we now need to move the array over before we add the
379 * reserved region since it may be our reserved array itself that is
380 * full.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700381 */
382 memcpy(new_array, type->regions, old_size);
383 memset(new_array + type->max, 0, old_size);
384 old_array = type->regions;
385 type->regions = new_array;
386 type->max <<= 1;
387
Andrew Mortonfd073832012-07-31 16:42:40 -0700388 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700389 if (*in_slab)
390 kfree(old_array);
391 else if (old_array != memblock_memory_init_regions &&
392 old_array != memblock_reserved_init_regions)
Yinghai Lu29f67382012-07-11 14:02:56 -0700393 memblock_free(__pa(old_array), old_alloc_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700394
Andrew Mortonfd073832012-07-31 16:42:40 -0700395 /*
396 * Reserve the new array if that comes from the memblock. Otherwise, we
397 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700398 */
399 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700400 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700401
402 /* Update slab flag */
403 *in_slab = use_slab;
404
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700405 return 0;
406}
407
Tejun Heo784656f2011-07-12 11:15:55 +0200408/**
409 * memblock_merge_regions - merge neighboring compatible regions
410 * @type: memblock type to scan
411 *
412 * Scan @type and merge neighboring compatible regions.
413 */
414static void __init_memblock memblock_merge_regions(struct memblock_type *type)
415{
416 int i = 0;
417
418 /* cnt never goes below 1 */
419 while (i < type->cnt - 1) {
420 struct memblock_region *this = &type->regions[i];
421 struct memblock_region *next = &type->regions[i + 1];
422
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200423 if (this->base + this->size != next->base ||
424 memblock_get_region_node(this) !=
Tang Chen66a20752014-01-21 15:49:20 -0800425 memblock_get_region_node(next) ||
426 this->flags != next->flags) {
Tejun Heo784656f2011-07-12 11:15:55 +0200427 BUG_ON(this->base + this->size > next->base);
428 i++;
429 continue;
430 }
431
432 this->size += next->size;
Lin Fengc0232ae2013-01-11 14:31:44 -0800433 /* move forward from next + 1, index of which is i + 2 */
434 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
Tejun Heo784656f2011-07-12 11:15:55 +0200435 type->cnt--;
436 }
437}
438
439/**
440 * memblock_insert_region - insert new memblock region
Tang Chen209ff862013-04-29 15:08:41 -0700441 * @type: memblock type to insert into
442 * @idx: index for the insertion point
443 * @base: base address of the new region
444 * @size: size of the new region
445 * @nid: node id of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800446 * @flags: flags of the new region
Tejun Heo784656f2011-07-12 11:15:55 +0200447 *
448 * Insert new memblock region [@base,@base+@size) into @type at @idx.
449 * @type must already have extra room to accomodate the new region.
450 */
451static void __init_memblock memblock_insert_region(struct memblock_type *type,
452 int idx, phys_addr_t base,
Tang Chen66a20752014-01-21 15:49:20 -0800453 phys_addr_t size,
454 int nid, unsigned long flags)
Tejun Heo784656f2011-07-12 11:15:55 +0200455{
456 struct memblock_region *rgn = &type->regions[idx];
457
458 BUG_ON(type->cnt >= type->max);
459 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
460 rgn->base = base;
461 rgn->size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800462 rgn->flags = flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200463 memblock_set_region_node(rgn, nid);
Tejun Heo784656f2011-07-12 11:15:55 +0200464 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800465 type->total_size += size;
Tejun Heo784656f2011-07-12 11:15:55 +0200466}
467
468/**
469 * memblock_add_region - add new memblock region
470 * @type: memblock type to add new region into
471 * @base: base address of the new region
472 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800473 * @nid: nid of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800474 * @flags: flags of the new region
Tejun Heo784656f2011-07-12 11:15:55 +0200475 *
476 * Add new memblock region [@base,@base+@size) into @type. The new region
477 * is allowed to overlap with existing ones - overlaps don't affect already
478 * existing regions. @type is guaranteed to be minimal (all neighbouring
479 * compatible regions are merged) after the addition.
480 *
481 * RETURNS:
482 * 0 on success, -errno on failure.
483 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800484static int __init_memblock memblock_add_region(struct memblock_type *type,
Tang Chen66a20752014-01-21 15:49:20 -0800485 phys_addr_t base, phys_addr_t size,
486 int nid, unsigned long flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000487{
Tejun Heo784656f2011-07-12 11:15:55 +0200488 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800489 phys_addr_t obase = base;
490 phys_addr_t end = base + memblock_cap_size(base, &size);
Tejun Heo784656f2011-07-12 11:15:55 +0200491 int i, nr_new;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000492
Tejun Heob3dc6272012-04-20 08:31:34 -0700493 if (!size)
494 return 0;
495
Tejun Heo784656f2011-07-12 11:15:55 +0200496 /* special case for empty array */
497 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800498 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000499 type->regions[0].base = base;
500 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800501 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800502 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800503 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000504 return 0;
505 }
Tejun Heo784656f2011-07-12 11:15:55 +0200506repeat:
507 /*
508 * The following is executed twice. Once with %false @insert and
509 * then with %true. The first counts the number of regions needed
510 * to accomodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700511 */
Tejun Heo784656f2011-07-12 11:15:55 +0200512 base = obase;
513 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000514
Tejun Heo784656f2011-07-12 11:15:55 +0200515 for (i = 0; i < type->cnt; i++) {
516 struct memblock_region *rgn = &type->regions[i];
517 phys_addr_t rbase = rgn->base;
518 phys_addr_t rend = rbase + rgn->size;
519
520 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000521 break;
Tejun Heo784656f2011-07-12 11:15:55 +0200522 if (rend <= base)
523 continue;
524 /*
525 * @rgn overlaps. If it separates the lower part of new
526 * area, insert that portion.
527 */
528 if (rbase > base) {
529 nr_new++;
530 if (insert)
531 memblock_insert_region(type, i++, base,
Tang Chen66a20752014-01-21 15:49:20 -0800532 rbase - base, nid,
533 flags);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000534 }
Tejun Heo784656f2011-07-12 11:15:55 +0200535 /* area below @rend is dealt with, forget about it */
536 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000537 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000538
Tejun Heo784656f2011-07-12 11:15:55 +0200539 /* insert the remaining portion */
540 if (base < end) {
541 nr_new++;
542 if (insert)
Tang Chen66a20752014-01-21 15:49:20 -0800543 memblock_insert_region(type, i, base, end - base,
544 nid, flags);
Tejun Heo784656f2011-07-12 11:15:55 +0200545 }
546
547 /*
548 * If this was the first round, resize array and repeat for actual
549 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700550 */
Tejun Heo784656f2011-07-12 11:15:55 +0200551 if (!insert) {
552 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700553 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f2011-07-12 11:15:55 +0200554 return -ENOMEM;
555 insert = true;
556 goto repeat;
557 } else {
558 memblock_merge_regions(type);
559 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700560 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000561}
562
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800563int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
564 int nid)
565{
Tang Chen66a20752014-01-21 15:49:20 -0800566 return memblock_add_region(&memblock.memory, base, size, nid, 0);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800567}
568
Tejun Heo581adcb2011-12-08 10:22:06 -0800569int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000570{
Tang Chen66a20752014-01-21 15:49:20 -0800571 return memblock_add_region(&memblock.memory, base, size,
572 MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000573}
574
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800575/**
576 * memblock_isolate_range - isolate given range into disjoint memblocks
577 * @type: memblock type to isolate range for
578 * @base: base of range to isolate
579 * @size: size of range to isolate
580 * @start_rgn: out parameter for the start of isolated region
581 * @end_rgn: out parameter for the end of isolated region
582 *
583 * Walk @type and ensure that regions don't cross the boundaries defined by
584 * [@base,@base+@size). Crossing regions are split at the boundaries,
585 * which may create at most two more regions. The index of the first
586 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
587 *
588 * RETURNS:
589 * 0 on success, -errno on failure.
590 */
591static int __init_memblock memblock_isolate_range(struct memblock_type *type,
592 phys_addr_t base, phys_addr_t size,
593 int *start_rgn, int *end_rgn)
594{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800595 phys_addr_t end = base + memblock_cap_size(base, &size);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800596 int i;
597
598 *start_rgn = *end_rgn = 0;
599
Tejun Heob3dc6272012-04-20 08:31:34 -0700600 if (!size)
601 return 0;
602
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800603 /* we'll create at most two more regions */
604 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700605 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800606 return -ENOMEM;
607
608 for (i = 0; i < type->cnt; i++) {
609 struct memblock_region *rgn = &type->regions[i];
610 phys_addr_t rbase = rgn->base;
611 phys_addr_t rend = rbase + rgn->size;
612
613 if (rbase >= end)
614 break;
615 if (rend <= base)
616 continue;
617
618 if (rbase < base) {
619 /*
620 * @rgn intersects from below. Split and continue
621 * to process the next region - the new top half.
622 */
623 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800624 rgn->size -= base - rbase;
625 type->total_size -= base - rbase;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800626 memblock_insert_region(type, i, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800627 memblock_get_region_node(rgn),
628 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800629 } else if (rend > end) {
630 /*
631 * @rgn intersects from above. Split and redo the
632 * current region - the new bottom half.
633 */
634 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800635 rgn->size -= end - rbase;
636 type->total_size -= end - rbase;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800637 memblock_insert_region(type, i--, rbase, end - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800638 memblock_get_region_node(rgn),
639 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800640 } else {
641 /* @rgn is fully contained, record it */
642 if (!*end_rgn)
643 *start_rgn = i;
644 *end_rgn = i + 1;
645 }
646 }
647
648 return 0;
649}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800650
Tejun Heo581adcb2011-12-08 10:22:06 -0800651static int __init_memblock __memblock_remove(struct memblock_type *type,
652 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000653{
Tejun Heo71936182011-12-08 10:22:07 -0800654 int start_rgn, end_rgn;
655 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000656
Tejun Heo71936182011-12-08 10:22:07 -0800657 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
658 if (ret)
659 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000660
Tejun Heo71936182011-12-08 10:22:07 -0800661 for (i = end_rgn - 1; i >= start_rgn; i--)
662 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700663 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000664}
665
Tejun Heo581adcb2011-12-08 10:22:06 -0800666int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000667{
668 return __memblock_remove(&memblock.memory, base, size);
669}
670
Tejun Heo581adcb2011-12-08 10:22:06 -0800671int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000672{
Tejun Heo24aa0782011-07-12 11:16:06 +0200673 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700674 (unsigned long long)base,
Grygorii Strashko931d13f2014-01-21 15:49:17 -0800675 (unsigned long long)base + size - 1,
H. Peter Anvina1504392011-07-14 11:57:10 -0700676 (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200677
Yinghai Lu95f72d12010-07-12 14:36:09 +1000678 return __memblock_remove(&memblock.reserved, base, size);
679}
680
Tang Chen66a20752014-01-21 15:49:20 -0800681static int __init_memblock memblock_reserve_region(phys_addr_t base,
682 phys_addr_t size,
683 int nid,
684 unsigned long flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000685{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000686 struct memblock_type *_rgn = &memblock.reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000687
Tang Chen66a20752014-01-21 15:49:20 -0800688 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] flags %#02lx %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700689 (unsigned long long)base,
Grygorii Strashko931d13f2014-01-21 15:49:17 -0800690 (unsigned long long)base + size - 1,
Tang Chen66a20752014-01-21 15:49:20 -0800691 flags, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000692
Tang Chen66a20752014-01-21 15:49:20 -0800693 return memblock_add_region(_rgn, base, size, nid, flags);
694}
695
696int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
697{
698 return memblock_reserve_region(base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000699}
700
Tejun Heo35fd0802011-07-12 11:15:59 +0200701/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800702 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
703 * @base: the base phys addr of the region
704 * @size: the size of the region
705 *
706 * This function isolates region [@base, @base + @size), and mark it with flag
707 * MEMBLOCK_HOTPLUG.
708 *
709 * Return 0 on succees, -errno on failure.
710 */
711int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
712{
713 struct memblock_type *type = &memblock.memory;
714 int i, ret, start_rgn, end_rgn;
715
716 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
717 if (ret)
718 return ret;
719
720 for (i = start_rgn; i < end_rgn; i++)
721 memblock_set_region_flags(&type->regions[i], MEMBLOCK_HOTPLUG);
722
723 memblock_merge_regions(type);
724 return 0;
725}
726
727/**
728 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
729 * @base: the base phys addr of the region
730 * @size: the size of the region
731 *
732 * This function isolates region [@base, @base + @size), and clear flag
733 * MEMBLOCK_HOTPLUG for the isolated regions.
734 *
735 * Return 0 on succees, -errno on failure.
736 */
737int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
738{
739 struct memblock_type *type = &memblock.memory;
740 int i, ret, start_rgn, end_rgn;
741
742 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
743 if (ret)
744 return ret;
745
746 for (i = start_rgn; i < end_rgn; i++)
747 memblock_clear_region_flags(&type->regions[i],
748 MEMBLOCK_HOTPLUG);
749
750 memblock_merge_regions(type);
751 return 0;
752}
753
754/**
Tejun Heo35fd0802011-07-12 11:15:59 +0200755 * __next_free_mem_range - next function for for_each_free_mem_range()
756 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800757 * @nid: node selector, %NUMA_NO_NODE for all nodes
Wanpeng Lidad75572012-06-20 12:53:01 -0700758 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
759 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
760 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +0200761 *
762 * Find the first free area from *@idx which matches @nid, fill the out
763 * parameters, and update *@idx for the next iteration. The lower 32bit of
764 * *@idx contains index into memory region and the upper 32bit indexes the
765 * areas before each reserved region. For example, if reserved regions
766 * look like the following,
767 *
768 * 0:[0-16), 1:[32-48), 2:[128-130)
769 *
770 * The upper 32bit indexes the following regions.
771 *
772 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
773 *
774 * As both region arrays are sorted, the function advances the two indices
775 * in lockstep and returns each intersection.
776 */
777void __init_memblock __next_free_mem_range(u64 *idx, int nid,
778 phys_addr_t *out_start,
779 phys_addr_t *out_end, int *out_nid)
780{
781 struct memblock_type *mem = &memblock.memory;
782 struct memblock_type *rsv = &memblock.reserved;
783 int mi = *idx & 0xffffffff;
784 int ri = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -0800785 bool check_node = (nid != NUMA_NO_NODE) && (nid != MAX_NUMNODES);
786
787 if (nid == MAX_NUMNODES)
788 pr_warn_once("%s: Usage of MAX_NUMNODES is depricated. Use NUMA_NO_NODE instead\n",
789 __func__);
Tejun Heo35fd0802011-07-12 11:15:59 +0200790
791 for ( ; mi < mem->cnt; mi++) {
792 struct memblock_region *m = &mem->regions[mi];
793 phys_addr_t m_start = m->base;
794 phys_addr_t m_end = m->base + m->size;
795
796 /* only memory regions are associated with nodes, check it */
Grygorii Strashkob1154232014-01-21 15:50:16 -0800797 if (check_node && nid != memblock_get_region_node(m))
Tejun Heo35fd0802011-07-12 11:15:59 +0200798 continue;
799
800 /* scan areas before each reservation for intersection */
801 for ( ; ri < rsv->cnt + 1; ri++) {
802 struct memblock_region *r = &rsv->regions[ri];
803 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
804 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
805
806 /* if ri advanced past mi, break out to advance mi */
807 if (r_start >= m_end)
808 break;
809 /* if the two regions intersect, we're done */
810 if (m_start < r_end) {
811 if (out_start)
812 *out_start = max(m_start, r_start);
813 if (out_end)
814 *out_end = min(m_end, r_end);
815 if (out_nid)
816 *out_nid = memblock_get_region_node(m);
817 /*
818 * The region which ends first is advanced
819 * for the next iteration.
820 */
821 if (m_end <= r_end)
822 mi++;
823 else
824 ri++;
825 *idx = (u32)mi | (u64)ri << 32;
826 return;
827 }
828 }
829 }
830
831 /* signal end of iteration */
832 *idx = ULLONG_MAX;
833}
834
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800835/**
836 * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
837 * @idx: pointer to u64 loop variable
Grygorii Strashkob1154232014-01-21 15:50:16 -0800838 * @nid: nid: node selector, %NUMA_NO_NODE for all nodes
Wanpeng Lidad75572012-06-20 12:53:01 -0700839 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
840 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
841 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800842 *
843 * Reverse of __next_free_mem_range().
Tang Chen55ac5902014-01-21 15:49:35 -0800844 *
845 * Linux kernel cannot migrate pages used by itself. Memory hotplug users won't
846 * be able to hot-remove hotpluggable memory used by the kernel. So this
847 * function skip hotpluggable regions if needed when allocating memory for the
848 * kernel.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800849 */
850void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
851 phys_addr_t *out_start,
852 phys_addr_t *out_end, int *out_nid)
853{
854 struct memblock_type *mem = &memblock.memory;
855 struct memblock_type *rsv = &memblock.reserved;
856 int mi = *idx & 0xffffffff;
857 int ri = *idx >> 32;
Grygorii Strashkob1154232014-01-21 15:50:16 -0800858 bool check_node = (nid != NUMA_NO_NODE) && (nid != MAX_NUMNODES);
859
860 if (nid == MAX_NUMNODES)
861 pr_warn_once("%s: Usage of MAX_NUMNODES is depricated. Use NUMA_NO_NODE instead\n",
862 __func__);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800863
864 if (*idx == (u64)ULLONG_MAX) {
865 mi = mem->cnt - 1;
866 ri = rsv->cnt;
867 }
868
869 for ( ; mi >= 0; mi--) {
870 struct memblock_region *m = &mem->regions[mi];
871 phys_addr_t m_start = m->base;
872 phys_addr_t m_end = m->base + m->size;
873
874 /* only memory regions are associated with nodes, check it */
Grygorii Strashkob1154232014-01-21 15:50:16 -0800875 if (check_node && nid != memblock_get_region_node(m))
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800876 continue;
877
Tang Chen55ac5902014-01-21 15:49:35 -0800878 /* skip hotpluggable memory regions if needed */
879 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
880 continue;
881
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800882 /* scan areas before each reservation for intersection */
883 for ( ; ri >= 0; ri--) {
884 struct memblock_region *r = &rsv->regions[ri];
885 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
886 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
887
888 /* if ri advanced past mi, break out to advance mi */
889 if (r_end <= m_start)
890 break;
891 /* if the two regions intersect, we're done */
892 if (m_end > r_start) {
893 if (out_start)
894 *out_start = max(m_start, r_start);
895 if (out_end)
896 *out_end = min(m_end, r_end);
897 if (out_nid)
898 *out_nid = memblock_get_region_node(m);
899
900 if (m_start >= r_start)
901 mi--;
902 else
903 ri--;
904 *idx = (u32)mi | (u64)ri << 32;
905 return;
906 }
907 }
908 }
909
910 *idx = ULLONG_MAX;
911}
912
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200913#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
914/*
915 * Common iterator interface used to define for_each_mem_range().
916 */
917void __init_memblock __next_mem_pfn_range(int *idx, int nid,
918 unsigned long *out_start_pfn,
919 unsigned long *out_end_pfn, int *out_nid)
920{
921 struct memblock_type *type = &memblock.memory;
922 struct memblock_region *r;
923
924 while (++*idx < type->cnt) {
925 r = &type->regions[*idx];
926
927 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
928 continue;
929 if (nid == MAX_NUMNODES || nid == r->nid)
930 break;
931 }
932 if (*idx >= type->cnt) {
933 *idx = -1;
934 return;
935 }
936
937 if (out_start_pfn)
938 *out_start_pfn = PFN_UP(r->base);
939 if (out_end_pfn)
940 *out_end_pfn = PFN_DOWN(r->base + r->size);
941 if (out_nid)
942 *out_nid = r->nid;
943}
944
945/**
946 * memblock_set_node - set node ID on memblock regions
947 * @base: base of area to set node ID for
948 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -0800949 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200950 * @nid: node ID to set
951 *
Tang Chene7e8de52014-01-21 15:49:26 -0800952 * Set the nid of memblock @type regions in [@base,@base+@size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200953 * Regions which cross the area boundaries are split as necessary.
954 *
955 * RETURNS:
956 * 0 on success, -errno on failure.
957 */
958int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -0800959 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200960{
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800961 int start_rgn, end_rgn;
962 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200963
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800964 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
965 if (ret)
966 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200967
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800968 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -0700969 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200970
971 memblock_merge_regions(type);
972 return 0;
973}
974#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
975
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800976static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
977 phys_addr_t align, phys_addr_t max_addr,
978 int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000979{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000980 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000981
Grygorii Strashko79f40fa2014-01-21 15:50:12 -0800982 if (!align)
983 align = SMP_CACHE_BYTES;
Vineet Gupta94f3d3a2013-04-29 15:06:15 -0700984
Tejun Heo847854f2012-02-29 05:56:21 +0900985 /* align @size to avoid excessive fragmentation on reserved array */
986 size = round_up(size, align);
987
Grygorii Strashko87029ee2014-01-21 15:50:14 -0800988 found = memblock_find_in_range_node(size, align, 0, max_addr, nid);
Tejun Heo9c8c27e2011-12-08 10:22:06 -0800989 if (found && !memblock_reserve(found, size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000990 return found;
991
992 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000993}
994
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800995phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
996{
997 return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
998}
999
1000phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1001{
Grygorii Strashkob1154232014-01-21 15:50:16 -08001002 return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -08001003}
1004
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001005phys_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 +10001006{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001007 phys_addr_t alloc;
1008
1009 alloc = __memblock_alloc_base(size, align, max_addr);
1010
1011 if (alloc == 0)
1012 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
1013 (unsigned long long) size, (unsigned long long) max_addr);
1014
1015 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001016}
1017
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001018phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001019{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001020 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001021}
1022
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001023phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1024{
1025 phys_addr_t res = memblock_alloc_nid(size, align, nid);
1026
1027 if (res)
1028 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +02001029 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001030}
1031
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001032
1033/*
1034 * Remaining API functions
1035 */
1036
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +10001037phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001038{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001039 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001040}
1041
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001042phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1043{
1044 unsigned long pages = 0;
1045 struct memblock_region *r;
1046 unsigned long start_pfn, end_pfn;
1047
1048 for_each_memblock(memory, r) {
1049 start_pfn = memblock_region_memory_base_pfn(r);
1050 end_pfn = memblock_region_memory_end_pfn(r);
1051 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1052 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1053 pages += end_pfn - start_pfn;
1054 }
1055
1056 return (phys_addr_t)pages << PAGE_SHIFT;
1057}
1058
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001059/* lowest address */
1060phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1061{
1062 return memblock.memory.regions[0].base;
1063}
1064
Yinghai Lu10d06432010-07-28 15:43:02 +10001065phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001066{
1067 int idx = memblock.memory.cnt - 1;
1068
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001069 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001070}
1071
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001072void __init memblock_enforce_memory_limit(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001073{
1074 unsigned long i;
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001075 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001076
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001077 if (!limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001078 return;
1079
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001080 /* find out max address */
Yinghai Lu95f72d12010-07-12 14:36:09 +10001081 for (i = 0; i < memblock.memory.cnt; i++) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001082 struct memblock_region *r = &memblock.memory.regions[i];
Yinghai Lu95f72d12010-07-12 14:36:09 +10001083
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001084 if (limit <= r->size) {
1085 max_addr = r->base + limit;
1086 break;
1087 }
1088 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001089 }
1090
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001091 /* truncate both memory and reserved regions */
1092 __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
1093 __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001094}
1095
Yinghai Lucd794812010-10-11 12:34:09 -07001096static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001097{
1098 unsigned int left = 0, right = type->cnt;
1099
1100 do {
1101 unsigned int mid = (right + left) / 2;
1102
1103 if (addr < type->regions[mid].base)
1104 right = mid;
1105 else if (addr >= (type->regions[mid].base +
1106 type->regions[mid].size))
1107 left = mid + 1;
1108 else
1109 return mid;
1110 } while (left < right);
1111 return -1;
1112}
1113
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +10001114int __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001115{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001116 return memblock_search(&memblock.reserved, addr) != -1;
1117}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001118
Yinghai Lu3661ca62010-09-15 13:05:29 -07001119int __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001120{
1121 return memblock_search(&memblock.memory, addr) != -1;
1122}
1123
Yinghai Lue76b63f2013-09-11 14:22:17 -07001124#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1125int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1126 unsigned long *start_pfn, unsigned long *end_pfn)
1127{
1128 struct memblock_type *type = &memblock.memory;
1129 int mid = memblock_search(type, (phys_addr_t)pfn << PAGE_SHIFT);
1130
1131 if (mid == -1)
1132 return -1;
1133
1134 *start_pfn = type->regions[mid].base >> PAGE_SHIFT;
1135 *end_pfn = (type->regions[mid].base + type->regions[mid].size)
1136 >> PAGE_SHIFT;
1137
1138 return type->regions[mid].nid;
1139}
1140#endif
1141
Stephen Boydeab30942012-05-24 00:45:21 -07001142/**
1143 * memblock_is_region_memory - check if a region is a subset of memory
1144 * @base: base of region to check
1145 * @size: size of region to check
1146 *
1147 * Check if the region [@base, @base+@size) is a subset of a memory block.
1148 *
1149 * RETURNS:
1150 * 0 if false, non-zero if true
1151 */
Yinghai Lu3661ca62010-09-15 13:05:29 -07001152int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001153{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001154 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001155 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001156
1157 if (idx == -1)
1158 return 0;
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001159 return memblock.memory.regions[idx].base <= base &&
1160 (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001161 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001162}
1163
Stephen Boydeab30942012-05-24 00:45:21 -07001164/**
1165 * memblock_is_region_reserved - check if a region intersects reserved memory
1166 * @base: base of region to check
1167 * @size: size of region to check
1168 *
1169 * Check if the region [@base, @base+@size) intersects a reserved memory block.
1170 *
1171 * RETURNS:
1172 * 0 if false, non-zero if true
1173 */
Yinghai Lu10d06432010-07-28 15:43:02 +10001174int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001175{
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001176 memblock_cap_size(base, &size);
Benjamin Herrenschmidtf1c2c192010-08-04 14:17:17 +10001177 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001178}
1179
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001180void __init_memblock memblock_trim_memory(phys_addr_t align)
1181{
1182 int i;
1183 phys_addr_t start, end, orig_start, orig_end;
1184 struct memblock_type *mem = &memblock.memory;
1185
1186 for (i = 0; i < mem->cnt; i++) {
1187 orig_start = mem->regions[i].base;
1188 orig_end = mem->regions[i].base + mem->regions[i].size;
1189 start = round_up(orig_start, align);
1190 end = round_down(orig_end, align);
1191
1192 if (start == orig_start && end == orig_end)
1193 continue;
1194
1195 if (start < end) {
1196 mem->regions[i].base = start;
1197 mem->regions[i].size = end - start;
1198 } else {
1199 memblock_remove_region(mem, i);
1200 i--;
1201 }
1202 }
1203}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001204
Yinghai Lu3661ca62010-09-15 13:05:29 -07001205void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001206{
1207 memblock.current_limit = limit;
1208}
1209
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001210static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001211{
1212 unsigned long long base, size;
Tang Chen66a20752014-01-21 15:49:20 -08001213 unsigned long flags;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001214 int i;
1215
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001216 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001217
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001218 for (i = 0; i < type->cnt; i++) {
1219 struct memblock_region *rgn = &type->regions[i];
1220 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001221
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001222 base = rgn->base;
1223 size = rgn->size;
Tang Chen66a20752014-01-21 15:49:20 -08001224 flags = rgn->flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001225#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1226 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1227 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1228 memblock_get_region_node(rgn));
1229#endif
Tang Chen66a20752014-01-21 15:49:20 -08001230 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n",
1231 name, i, base, base + size - 1, size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001232 }
1233}
1234
Tejun Heo4ff7b822011-12-08 10:22:06 -08001235void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001236{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001237 pr_info("MEMBLOCK configuration:\n");
Tejun Heo1440c4e2011-12-08 10:22:08 -08001238 pr_info(" memory size = %#llx reserved size = %#llx\n",
1239 (unsigned long long)memblock.memory.total_size,
1240 (unsigned long long)memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001241
1242 memblock_dump(&memblock.memory, "memory");
1243 memblock_dump(&memblock.reserved, "reserved");
1244}
1245
Tejun Heo1aadc052011-12-08 10:22:08 -08001246void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001247{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001248 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001249}
1250
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001251static int __init early_memblock(char *p)
1252{
1253 if (p && strstr(p, "debug"))
1254 memblock_debug = 1;
1255 return 0;
1256}
1257early_param("memblock", early_memblock);
1258
Tejun Heoc378ddd2011-07-14 11:46:03 +02001259#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001260
1261static int memblock_debug_show(struct seq_file *m, void *private)
1262{
1263 struct memblock_type *type = m->private;
1264 struct memblock_region *reg;
1265 int i;
1266
1267 for (i = 0; i < type->cnt; i++) {
1268 reg = &type->regions[i];
1269 seq_printf(m, "%4d: ", i);
1270 if (sizeof(phys_addr_t) == 4)
1271 seq_printf(m, "0x%08lx..0x%08lx\n",
1272 (unsigned long)reg->base,
1273 (unsigned long)(reg->base + reg->size - 1));
1274 else
1275 seq_printf(m, "0x%016llx..0x%016llx\n",
1276 (unsigned long long)reg->base,
1277 (unsigned long long)(reg->base + reg->size - 1));
1278
1279 }
1280 return 0;
1281}
1282
1283static int memblock_debug_open(struct inode *inode, struct file *file)
1284{
1285 return single_open(file, memblock_debug_show, inode->i_private);
1286}
1287
1288static const struct file_operations memblock_debug_fops = {
1289 .open = memblock_debug_open,
1290 .read = seq_read,
1291 .llseek = seq_lseek,
1292 .release = single_release,
1293};
1294
1295static int __init memblock_init_debugfs(void)
1296{
1297 struct dentry *root = debugfs_create_dir("memblock", NULL);
1298 if (!root)
1299 return -ENXIO;
1300 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1301 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
1302
1303 return 0;
1304}
1305__initcall(memblock_init_debugfs);
1306
1307#endif /* CONFIG_DEBUG_FS */