blob: de4d9c352fd64dfd8a072db1504c6b3bbd9bbf15 [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
97 * @nid: nid of the free area to find, %MAX_NUMNODES for any node
98 *
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
129 * @nid: nid of the free area to find, %MAX_NUMNODES for any node
130 *
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
160 * @start: start of candidate range
161 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
162 * @size: size of free area to find
163 * @align: alignment of free area to find
164 * @nid: nid of the free area to find, %MAX_NUMNODES for any node
165 *
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 */
Tang Chenf7210e62013-02-22 16:33:51 -0800179phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start,
180 phys_addr_t end, phys_addr_t size,
181 phys_addr_t align, int nid)
182{
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{
244 return memblock_find_in_range_node(start, end, size, align,
245 MAX_NUMNODES);
246}
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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-07-12 11:15:55 +0200464 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800465 type->total_size += size;
Tejun Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-07-12 11:15:55 +0200512 base = obase;
513 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000514
Tejun Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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 Heo784656f92011-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
Tang Chend8bbdd72013-07-08 16:00:22 -0700757 * @nid: node selector, %MAX_NUMNODES 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;
785
786 for ( ; mi < mem->cnt; mi++) {
787 struct memblock_region *m = &mem->regions[mi];
788 phys_addr_t m_start = m->base;
789 phys_addr_t m_end = m->base + m->size;
790
791 /* only memory regions are associated with nodes, check it */
792 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
793 continue;
794
795 /* scan areas before each reservation for intersection */
796 for ( ; ri < rsv->cnt + 1; ri++) {
797 struct memblock_region *r = &rsv->regions[ri];
798 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
799 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
800
801 /* if ri advanced past mi, break out to advance mi */
802 if (r_start >= m_end)
803 break;
804 /* if the two regions intersect, we're done */
805 if (m_start < r_end) {
806 if (out_start)
807 *out_start = max(m_start, r_start);
808 if (out_end)
809 *out_end = min(m_end, r_end);
810 if (out_nid)
811 *out_nid = memblock_get_region_node(m);
812 /*
813 * The region which ends first is advanced
814 * for the next iteration.
815 */
816 if (m_end <= r_end)
817 mi++;
818 else
819 ri++;
820 *idx = (u32)mi | (u64)ri << 32;
821 return;
822 }
823 }
824 }
825
826 /* signal end of iteration */
827 *idx = ULLONG_MAX;
828}
829
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800830/**
831 * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
832 * @idx: pointer to u64 loop variable
833 * @nid: nid: node selector, %MAX_NUMNODES for all nodes
Wanpeng Lidad75572012-06-20 12:53:01 -0700834 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
835 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
836 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800837 *
838 * Reverse of __next_free_mem_range().
Tang Chen55ac5902014-01-21 15:49:35 -0800839 *
840 * Linux kernel cannot migrate pages used by itself. Memory hotplug users won't
841 * be able to hot-remove hotpluggable memory used by the kernel. So this
842 * function skip hotpluggable regions if needed when allocating memory for the
843 * kernel.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800844 */
845void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
846 phys_addr_t *out_start,
847 phys_addr_t *out_end, int *out_nid)
848{
849 struct memblock_type *mem = &memblock.memory;
850 struct memblock_type *rsv = &memblock.reserved;
851 int mi = *idx & 0xffffffff;
852 int ri = *idx >> 32;
853
854 if (*idx == (u64)ULLONG_MAX) {
855 mi = mem->cnt - 1;
856 ri = rsv->cnt;
857 }
858
859 for ( ; mi >= 0; mi--) {
860 struct memblock_region *m = &mem->regions[mi];
861 phys_addr_t m_start = m->base;
862 phys_addr_t m_end = m->base + m->size;
863
864 /* only memory regions are associated with nodes, check it */
865 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
866 continue;
867
Tang Chen55ac5902014-01-21 15:49:35 -0800868 /* skip hotpluggable memory regions if needed */
869 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
870 continue;
871
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800872 /* scan areas before each reservation for intersection */
873 for ( ; ri >= 0; ri--) {
874 struct memblock_region *r = &rsv->regions[ri];
875 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
876 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
877
878 /* if ri advanced past mi, break out to advance mi */
879 if (r_end <= m_start)
880 break;
881 /* if the two regions intersect, we're done */
882 if (m_end > r_start) {
883 if (out_start)
884 *out_start = max(m_start, r_start);
885 if (out_end)
886 *out_end = min(m_end, r_end);
887 if (out_nid)
888 *out_nid = memblock_get_region_node(m);
889
890 if (m_start >= r_start)
891 mi--;
892 else
893 ri--;
894 *idx = (u32)mi | (u64)ri << 32;
895 return;
896 }
897 }
898 }
899
900 *idx = ULLONG_MAX;
901}
902
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200903#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
904/*
905 * Common iterator interface used to define for_each_mem_range().
906 */
907void __init_memblock __next_mem_pfn_range(int *idx, int nid,
908 unsigned long *out_start_pfn,
909 unsigned long *out_end_pfn, int *out_nid)
910{
911 struct memblock_type *type = &memblock.memory;
912 struct memblock_region *r;
913
914 while (++*idx < type->cnt) {
915 r = &type->regions[*idx];
916
917 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
918 continue;
919 if (nid == MAX_NUMNODES || nid == r->nid)
920 break;
921 }
922 if (*idx >= type->cnt) {
923 *idx = -1;
924 return;
925 }
926
927 if (out_start_pfn)
928 *out_start_pfn = PFN_UP(r->base);
929 if (out_end_pfn)
930 *out_end_pfn = PFN_DOWN(r->base + r->size);
931 if (out_nid)
932 *out_nid = r->nid;
933}
934
935/**
936 * memblock_set_node - set node ID on memblock regions
937 * @base: base of area to set node ID for
938 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -0800939 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200940 * @nid: node ID to set
941 *
Tang Chene7e8de52014-01-21 15:49:26 -0800942 * Set the nid of memblock @type regions in [@base,@base+@size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200943 * Regions which cross the area boundaries are split as necessary.
944 *
945 * RETURNS:
946 * 0 on success, -errno on failure.
947 */
948int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -0800949 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200950{
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800951 int start_rgn, end_rgn;
952 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200953
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800954 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
955 if (ret)
956 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200957
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800958 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -0700959 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200960
961 memblock_merge_regions(type);
962 return 0;
963}
964#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
965
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800966static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
967 phys_addr_t align, phys_addr_t max_addr,
968 int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000969{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000970 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000971
Vineet Gupta94f3d3a2013-04-29 15:06:15 -0700972 if (WARN_ON(!align))
973 align = __alignof__(long long);
974
Tejun Heo847854f2012-02-29 05:56:21 +0900975 /* align @size to avoid excessive fragmentation on reserved array */
976 size = round_up(size, align);
977
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800978 found = memblock_find_in_range_node(0, max_addr, size, align, nid);
Tejun Heo9c8c27e2011-12-08 10:22:06 -0800979 if (found && !memblock_reserve(found, size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000980 return found;
981
982 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000983}
984
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800985phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
986{
987 return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
988}
989
990phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
991{
992 return memblock_alloc_base_nid(size, align, max_addr, MAX_NUMNODES);
993}
994
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000995phys_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 +1000996{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000997 phys_addr_t alloc;
998
999 alloc = __memblock_alloc_base(size, align, max_addr);
1000
1001 if (alloc == 0)
1002 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
1003 (unsigned long long) size, (unsigned long long) max_addr);
1004
1005 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001006}
1007
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001008phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001009{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001010 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001011}
1012
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001013phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1014{
1015 phys_addr_t res = memblock_alloc_nid(size, align, nid);
1016
1017 if (res)
1018 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +02001019 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001020}
1021
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001022
1023/*
1024 * Remaining API functions
1025 */
1026
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +10001027phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001028{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001029 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001030}
1031
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001032phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1033{
1034 unsigned long pages = 0;
1035 struct memblock_region *r;
1036 unsigned long start_pfn, end_pfn;
1037
1038 for_each_memblock(memory, r) {
1039 start_pfn = memblock_region_memory_base_pfn(r);
1040 end_pfn = memblock_region_memory_end_pfn(r);
1041 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1042 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1043 pages += end_pfn - start_pfn;
1044 }
1045
1046 return (phys_addr_t)pages << PAGE_SHIFT;
1047}
1048
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001049/* lowest address */
1050phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1051{
1052 return memblock.memory.regions[0].base;
1053}
1054
Yinghai Lu10d06432010-07-28 15:43:02 +10001055phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001056{
1057 int idx = memblock.memory.cnt - 1;
1058
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001059 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001060}
1061
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001062void __init memblock_enforce_memory_limit(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001063{
1064 unsigned long i;
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001065 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001066
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001067 if (!limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001068 return;
1069
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001070 /* find out max address */
Yinghai Lu95f72d12010-07-12 14:36:09 +10001071 for (i = 0; i < memblock.memory.cnt; i++) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001072 struct memblock_region *r = &memblock.memory.regions[i];
Yinghai Lu95f72d12010-07-12 14:36:09 +10001073
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001074 if (limit <= r->size) {
1075 max_addr = r->base + limit;
1076 break;
1077 }
1078 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001079 }
1080
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001081 /* truncate both memory and reserved regions */
1082 __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
1083 __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001084}
1085
Yinghai Lucd794812010-10-11 12:34:09 -07001086static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001087{
1088 unsigned int left = 0, right = type->cnt;
1089
1090 do {
1091 unsigned int mid = (right + left) / 2;
1092
1093 if (addr < type->regions[mid].base)
1094 right = mid;
1095 else if (addr >= (type->regions[mid].base +
1096 type->regions[mid].size))
1097 left = mid + 1;
1098 else
1099 return mid;
1100 } while (left < right);
1101 return -1;
1102}
1103
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +10001104int __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001105{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001106 return memblock_search(&memblock.reserved, addr) != -1;
1107}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001108
Yinghai Lu3661ca62010-09-15 13:05:29 -07001109int __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001110{
1111 return memblock_search(&memblock.memory, addr) != -1;
1112}
1113
Yinghai Lue76b63f2013-09-11 14:22:17 -07001114#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1115int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1116 unsigned long *start_pfn, unsigned long *end_pfn)
1117{
1118 struct memblock_type *type = &memblock.memory;
1119 int mid = memblock_search(type, (phys_addr_t)pfn << PAGE_SHIFT);
1120
1121 if (mid == -1)
1122 return -1;
1123
1124 *start_pfn = type->regions[mid].base >> PAGE_SHIFT;
1125 *end_pfn = (type->regions[mid].base + type->regions[mid].size)
1126 >> PAGE_SHIFT;
1127
1128 return type->regions[mid].nid;
1129}
1130#endif
1131
Stephen Boydeab30942012-05-24 00:45:21 -07001132/**
1133 * memblock_is_region_memory - check if a region is a subset of memory
1134 * @base: base of region to check
1135 * @size: size of region to check
1136 *
1137 * Check if the region [@base, @base+@size) is a subset of a memory block.
1138 *
1139 * RETURNS:
1140 * 0 if false, non-zero if true
1141 */
Yinghai Lu3661ca62010-09-15 13:05:29 -07001142int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001143{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001144 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001145 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001146
1147 if (idx == -1)
1148 return 0;
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001149 return memblock.memory.regions[idx].base <= base &&
1150 (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001151 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001152}
1153
Stephen Boydeab30942012-05-24 00:45:21 -07001154/**
1155 * memblock_is_region_reserved - check if a region intersects reserved memory
1156 * @base: base of region to check
1157 * @size: size of region to check
1158 *
1159 * Check if the region [@base, @base+@size) intersects a reserved memory block.
1160 *
1161 * RETURNS:
1162 * 0 if false, non-zero if true
1163 */
Yinghai Lu10d06432010-07-28 15:43:02 +10001164int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001165{
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001166 memblock_cap_size(base, &size);
Benjamin Herrenschmidtf1c2c192010-08-04 14:17:17 +10001167 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001168}
1169
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001170void __init_memblock memblock_trim_memory(phys_addr_t align)
1171{
1172 int i;
1173 phys_addr_t start, end, orig_start, orig_end;
1174 struct memblock_type *mem = &memblock.memory;
1175
1176 for (i = 0; i < mem->cnt; i++) {
1177 orig_start = mem->regions[i].base;
1178 orig_end = mem->regions[i].base + mem->regions[i].size;
1179 start = round_up(orig_start, align);
1180 end = round_down(orig_end, align);
1181
1182 if (start == orig_start && end == orig_end)
1183 continue;
1184
1185 if (start < end) {
1186 mem->regions[i].base = start;
1187 mem->regions[i].size = end - start;
1188 } else {
1189 memblock_remove_region(mem, i);
1190 i--;
1191 }
1192 }
1193}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001194
Yinghai Lu3661ca62010-09-15 13:05:29 -07001195void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001196{
1197 memblock.current_limit = limit;
1198}
1199
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001200static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001201{
1202 unsigned long long base, size;
Tang Chen66a20752014-01-21 15:49:20 -08001203 unsigned long flags;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001204 int i;
1205
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001206 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001207
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001208 for (i = 0; i < type->cnt; i++) {
1209 struct memblock_region *rgn = &type->regions[i];
1210 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001211
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001212 base = rgn->base;
1213 size = rgn->size;
Tang Chen66a20752014-01-21 15:49:20 -08001214 flags = rgn->flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001215#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1216 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1217 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1218 memblock_get_region_node(rgn));
1219#endif
Tang Chen66a20752014-01-21 15:49:20 -08001220 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n",
1221 name, i, base, base + size - 1, size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001222 }
1223}
1224
Tejun Heo4ff7b822011-12-08 10:22:06 -08001225void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001226{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001227 pr_info("MEMBLOCK configuration:\n");
Tejun Heo1440c4e2011-12-08 10:22:08 -08001228 pr_info(" memory size = %#llx reserved size = %#llx\n",
1229 (unsigned long long)memblock.memory.total_size,
1230 (unsigned long long)memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001231
1232 memblock_dump(&memblock.memory, "memory");
1233 memblock_dump(&memblock.reserved, "reserved");
1234}
1235
Tejun Heo1aadc052011-12-08 10:22:08 -08001236void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001237{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001238 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001239}
1240
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001241static int __init early_memblock(char *p)
1242{
1243 if (p && strstr(p, "debug"))
1244 memblock_debug = 1;
1245 return 0;
1246}
1247early_param("memblock", early_memblock);
1248
Tejun Heoc378ddd2011-07-14 11:46:03 +02001249#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001250
1251static int memblock_debug_show(struct seq_file *m, void *private)
1252{
1253 struct memblock_type *type = m->private;
1254 struct memblock_region *reg;
1255 int i;
1256
1257 for (i = 0; i < type->cnt; i++) {
1258 reg = &type->regions[i];
1259 seq_printf(m, "%4d: ", i);
1260 if (sizeof(phys_addr_t) == 4)
1261 seq_printf(m, "0x%08lx..0x%08lx\n",
1262 (unsigned long)reg->base,
1263 (unsigned long)(reg->base + reg->size - 1));
1264 else
1265 seq_printf(m, "0x%016llx..0x%016llx\n",
1266 (unsigned long long)reg->base,
1267 (unsigned long long)(reg->base + reg->size - 1));
1268
1269 }
1270 return 0;
1271}
1272
1273static int memblock_debug_open(struct inode *inode, struct file *file)
1274{
1275 return single_open(file, memblock_debug_show, inode->i_private);
1276}
1277
1278static const struct file_operations memblock_debug_fops = {
1279 .open = memblock_debug_open,
1280 .read = seq_read,
1281 .llseek = seq_lseek,
1282 .release = single_release,
1283};
1284
1285static int __init memblock_init_debugfs(void)
1286{
1287 struct dentry *root = debugfs_create_dir("memblock", NULL);
1288 if (!root)
1289 return -ENXIO;
1290 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1291 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
1292
1293 return 0;
1294}
1295__initcall(memblock_init_debugfs);
1296
1297#endif /* CONFIG_DEBUG_FS */