blob: 6a2a48a122a91928561677f7b66b329aab0dc201 [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
272 *addr = __pa(memblock.reserved.regions);
273
274 return PAGE_ALIGN(sizeof(struct memblock_region) *
275 memblock.reserved.max);
276}
277
Greg Pearson48c3b582012-06-20 12:53:05 -0700278/**
279 * memblock_double_array - double the size of the memblock regions array
280 * @type: memblock type of the regions array being doubled
281 * @new_area_start: starting address of memory range to avoid overlap with
282 * @new_area_size: size of memory range to avoid overlap with
283 *
284 * Double the size of the @type regions array. If memblock is being used to
285 * allocate memory for a new reserved regions array and there is a previously
286 * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
287 * waiting to be reserved, ensure the memory used by the new array does
288 * not overlap.
289 *
290 * RETURNS:
291 * 0 on success, -1 on failure.
292 */
293static int __init_memblock memblock_double_array(struct memblock_type *type,
294 phys_addr_t new_area_start,
295 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700296{
297 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700298 phys_addr_t old_alloc_size, new_alloc_size;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700299 phys_addr_t old_size, new_size, addr;
300 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700301 int *in_slab;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700302
303 /* We don't allow resizing until we know about the reserved regions
304 * of memory that aren't suitable for allocation
305 */
306 if (!memblock_can_resize)
307 return -1;
308
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700309 /* Calculate new doubled size */
310 old_size = type->max * sizeof(struct memblock_region);
311 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700312 /*
313 * We need to allocated new one align to PAGE_SIZE,
314 * so we can free them completely later.
315 */
316 old_alloc_size = PAGE_ALIGN(old_size);
317 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700318
Gavin Shan181eb392012-05-29 15:06:50 -0700319 /* Retrieve the slab flag */
320 if (type == &memblock.memory)
321 in_slab = &memblock_memory_in_slab;
322 else
323 in_slab = &memblock_reserved_in_slab;
324
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700325 /* Try to find some space for it.
326 *
327 * WARNING: We assume that either slab_is_available() and we use it or
Andrew Mortonfd073832012-07-31 16:42:40 -0700328 * we use MEMBLOCK for allocations. That means that this is unsafe to
329 * use when bootmem is currently active (unless bootmem itself is
330 * implemented on top of MEMBLOCK which isn't the case yet)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700331 *
332 * This should however not be an issue for now, as we currently only
Andrew Mortonfd073832012-07-31 16:42:40 -0700333 * call into MEMBLOCK while it's still active, or much later when slab
334 * is active for memory hotplug operations
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700335 */
336 if (use_slab) {
337 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200338 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700339 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700340 /* only exclude range when trying to double reserved.regions */
341 if (type != &memblock.reserved)
342 new_area_start = new_area_size = 0;
343
344 addr = memblock_find_in_range(new_area_start + new_area_size,
345 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700346 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700347 if (!addr && new_area_size)
348 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700349 min(new_area_start, memblock.current_limit),
350 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700351
Sachin Kamat15674862012-09-04 13:55:05 +0530352 new_array = addr ? __va(addr) : NULL;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700353 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200354 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700355 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
356 memblock_type_name(type), type->max, type->max * 2);
357 return -1;
358 }
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700359
Andrew Mortonfd073832012-07-31 16:42:40 -0700360 memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
361 memblock_type_name(type), type->max * 2, (u64)addr,
362 (u64)addr + new_size - 1);
Yinghai Luea9e4372010-07-28 15:13:22 +1000363
Andrew Mortonfd073832012-07-31 16:42:40 -0700364 /*
365 * Found space, we now need to move the array over before we add the
366 * reserved region since it may be our reserved array itself that is
367 * full.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700368 */
369 memcpy(new_array, type->regions, old_size);
370 memset(new_array + type->max, 0, old_size);
371 old_array = type->regions;
372 type->regions = new_array;
373 type->max <<= 1;
374
Andrew Mortonfd073832012-07-31 16:42:40 -0700375 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700376 if (*in_slab)
377 kfree(old_array);
378 else if (old_array != memblock_memory_init_regions &&
379 old_array != memblock_reserved_init_regions)
Yinghai Lu29f67382012-07-11 14:02:56 -0700380 memblock_free(__pa(old_array), old_alloc_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700381
Andrew Mortonfd073832012-07-31 16:42:40 -0700382 /*
383 * Reserve the new array if that comes from the memblock. Otherwise, we
384 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700385 */
386 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700387 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700388
389 /* Update slab flag */
390 *in_slab = use_slab;
391
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700392 return 0;
393}
394
Tejun Heo784656f92011-07-12 11:15:55 +0200395/**
396 * memblock_merge_regions - merge neighboring compatible regions
397 * @type: memblock type to scan
398 *
399 * Scan @type and merge neighboring compatible regions.
400 */
401static void __init_memblock memblock_merge_regions(struct memblock_type *type)
402{
403 int i = 0;
404
405 /* cnt never goes below 1 */
406 while (i < type->cnt - 1) {
407 struct memblock_region *this = &type->regions[i];
408 struct memblock_region *next = &type->regions[i + 1];
409
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200410 if (this->base + this->size != next->base ||
411 memblock_get_region_node(this) !=
Tang Chen66a20752014-01-21 15:49:20 -0800412 memblock_get_region_node(next) ||
413 this->flags != next->flags) {
Tejun Heo784656f92011-07-12 11:15:55 +0200414 BUG_ON(this->base + this->size > next->base);
415 i++;
416 continue;
417 }
418
419 this->size += next->size;
Lin Fengc0232ae2013-01-11 14:31:44 -0800420 /* move forward from next + 1, index of which is i + 2 */
421 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
Tejun Heo784656f92011-07-12 11:15:55 +0200422 type->cnt--;
423 }
424}
425
426/**
427 * memblock_insert_region - insert new memblock region
Tang Chen209ff862013-04-29 15:08:41 -0700428 * @type: memblock type to insert into
429 * @idx: index for the insertion point
430 * @base: base address of the new region
431 * @size: size of the new region
432 * @nid: node id of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800433 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200434 *
435 * Insert new memblock region [@base,@base+@size) into @type at @idx.
436 * @type must already have extra room to accomodate the new region.
437 */
438static void __init_memblock memblock_insert_region(struct memblock_type *type,
439 int idx, phys_addr_t base,
Tang Chen66a20752014-01-21 15:49:20 -0800440 phys_addr_t size,
441 int nid, unsigned long flags)
Tejun Heo784656f92011-07-12 11:15:55 +0200442{
443 struct memblock_region *rgn = &type->regions[idx];
444
445 BUG_ON(type->cnt >= type->max);
446 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
447 rgn->base = base;
448 rgn->size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800449 rgn->flags = flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200450 memblock_set_region_node(rgn, nid);
Tejun Heo784656f92011-07-12 11:15:55 +0200451 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800452 type->total_size += size;
Tejun Heo784656f92011-07-12 11:15:55 +0200453}
454
455/**
456 * memblock_add_region - add new memblock region
457 * @type: memblock type to add new region into
458 * @base: base address of the new region
459 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800460 * @nid: nid of the new region
Tang Chen66a20752014-01-21 15:49:20 -0800461 * @flags: flags of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200462 *
463 * Add new memblock region [@base,@base+@size) into @type. The new region
464 * is allowed to overlap with existing ones - overlaps don't affect already
465 * existing regions. @type is guaranteed to be minimal (all neighbouring
466 * compatible regions are merged) after the addition.
467 *
468 * RETURNS:
469 * 0 on success, -errno on failure.
470 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800471static int __init_memblock memblock_add_region(struct memblock_type *type,
Tang Chen66a20752014-01-21 15:49:20 -0800472 phys_addr_t base, phys_addr_t size,
473 int nid, unsigned long flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000474{
Tejun Heo784656f92011-07-12 11:15:55 +0200475 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800476 phys_addr_t obase = base;
477 phys_addr_t end = base + memblock_cap_size(base, &size);
Tejun Heo784656f92011-07-12 11:15:55 +0200478 int i, nr_new;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000479
Tejun Heob3dc6272012-04-20 08:31:34 -0700480 if (!size)
481 return 0;
482
Tejun Heo784656f92011-07-12 11:15:55 +0200483 /* special case for empty array */
484 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800485 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000486 type->regions[0].base = base;
487 type->regions[0].size = size;
Tang Chen66a20752014-01-21 15:49:20 -0800488 type->regions[0].flags = flags;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800489 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800490 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000491 return 0;
492 }
Tejun Heo784656f92011-07-12 11:15:55 +0200493repeat:
494 /*
495 * The following is executed twice. Once with %false @insert and
496 * then with %true. The first counts the number of regions needed
497 * to accomodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700498 */
Tejun Heo784656f92011-07-12 11:15:55 +0200499 base = obase;
500 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000501
Tejun Heo784656f92011-07-12 11:15:55 +0200502 for (i = 0; i < type->cnt; i++) {
503 struct memblock_region *rgn = &type->regions[i];
504 phys_addr_t rbase = rgn->base;
505 phys_addr_t rend = rbase + rgn->size;
506
507 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000508 break;
Tejun Heo784656f92011-07-12 11:15:55 +0200509 if (rend <= base)
510 continue;
511 /*
512 * @rgn overlaps. If it separates the lower part of new
513 * area, insert that portion.
514 */
515 if (rbase > base) {
516 nr_new++;
517 if (insert)
518 memblock_insert_region(type, i++, base,
Tang Chen66a20752014-01-21 15:49:20 -0800519 rbase - base, nid,
520 flags);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000521 }
Tejun Heo784656f92011-07-12 11:15:55 +0200522 /* area below @rend is dealt with, forget about it */
523 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000524 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000525
Tejun Heo784656f92011-07-12 11:15:55 +0200526 /* insert the remaining portion */
527 if (base < end) {
528 nr_new++;
529 if (insert)
Tang Chen66a20752014-01-21 15:49:20 -0800530 memblock_insert_region(type, i, base, end - base,
531 nid, flags);
Tejun Heo784656f92011-07-12 11:15:55 +0200532 }
533
534 /*
535 * If this was the first round, resize array and repeat for actual
536 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700537 */
Tejun Heo784656f92011-07-12 11:15:55 +0200538 if (!insert) {
539 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700540 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f92011-07-12 11:15:55 +0200541 return -ENOMEM;
542 insert = true;
543 goto repeat;
544 } else {
545 memblock_merge_regions(type);
546 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700547 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000548}
549
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800550int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
551 int nid)
552{
Tang Chen66a20752014-01-21 15:49:20 -0800553 return memblock_add_region(&memblock.memory, base, size, nid, 0);
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800554}
555
Tejun Heo581adcb2011-12-08 10:22:06 -0800556int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000557{
Tang Chen66a20752014-01-21 15:49:20 -0800558 return memblock_add_region(&memblock.memory, base, size,
559 MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000560}
561
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800562/**
563 * memblock_isolate_range - isolate given range into disjoint memblocks
564 * @type: memblock type to isolate range for
565 * @base: base of range to isolate
566 * @size: size of range to isolate
567 * @start_rgn: out parameter for the start of isolated region
568 * @end_rgn: out parameter for the end of isolated region
569 *
570 * Walk @type and ensure that regions don't cross the boundaries defined by
571 * [@base,@base+@size). Crossing regions are split at the boundaries,
572 * which may create at most two more regions. The index of the first
573 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
574 *
575 * RETURNS:
576 * 0 on success, -errno on failure.
577 */
578static int __init_memblock memblock_isolate_range(struct memblock_type *type,
579 phys_addr_t base, phys_addr_t size,
580 int *start_rgn, int *end_rgn)
581{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800582 phys_addr_t end = base + memblock_cap_size(base, &size);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800583 int i;
584
585 *start_rgn = *end_rgn = 0;
586
Tejun Heob3dc6272012-04-20 08:31:34 -0700587 if (!size)
588 return 0;
589
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800590 /* we'll create at most two more regions */
591 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700592 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800593 return -ENOMEM;
594
595 for (i = 0; i < type->cnt; i++) {
596 struct memblock_region *rgn = &type->regions[i];
597 phys_addr_t rbase = rgn->base;
598 phys_addr_t rend = rbase + rgn->size;
599
600 if (rbase >= end)
601 break;
602 if (rend <= base)
603 continue;
604
605 if (rbase < base) {
606 /*
607 * @rgn intersects from below. Split and continue
608 * to process the next region - the new top half.
609 */
610 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800611 rgn->size -= base - rbase;
612 type->total_size -= base - rbase;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800613 memblock_insert_region(type, i, rbase, base - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800614 memblock_get_region_node(rgn),
615 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800616 } else if (rend > end) {
617 /*
618 * @rgn intersects from above. Split and redo the
619 * current region - the new bottom half.
620 */
621 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800622 rgn->size -= end - rbase;
623 type->total_size -= end - rbase;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800624 memblock_insert_region(type, i--, rbase, end - rbase,
Tang Chen66a20752014-01-21 15:49:20 -0800625 memblock_get_region_node(rgn),
626 rgn->flags);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800627 } else {
628 /* @rgn is fully contained, record it */
629 if (!*end_rgn)
630 *start_rgn = i;
631 *end_rgn = i + 1;
632 }
633 }
634
635 return 0;
636}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800637
Tejun Heo581adcb2011-12-08 10:22:06 -0800638static int __init_memblock __memblock_remove(struct memblock_type *type,
639 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000640{
Tejun Heo71936182011-12-08 10:22:07 -0800641 int start_rgn, end_rgn;
642 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000643
Tejun Heo71936182011-12-08 10:22:07 -0800644 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
645 if (ret)
646 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000647
Tejun Heo71936182011-12-08 10:22:07 -0800648 for (i = end_rgn - 1; i >= start_rgn; i--)
649 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700650 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000651}
652
Tejun Heo581adcb2011-12-08 10:22:06 -0800653int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000654{
655 return __memblock_remove(&memblock.memory, base, size);
656}
657
Tejun Heo581adcb2011-12-08 10:22:06 -0800658int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000659{
Tejun Heo24aa0782011-07-12 11:16:06 +0200660 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700661 (unsigned long long)base,
Grygorii Strashko931d13f2014-01-21 15:49:17 -0800662 (unsigned long long)base + size - 1,
H. Peter Anvina1504392011-07-14 11:57:10 -0700663 (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200664
Yinghai Lu95f72d12010-07-12 14:36:09 +1000665 return __memblock_remove(&memblock.reserved, base, size);
666}
667
Tang Chen66a20752014-01-21 15:49:20 -0800668static int __init_memblock memblock_reserve_region(phys_addr_t base,
669 phys_addr_t size,
670 int nid,
671 unsigned long flags)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000672{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000673 struct memblock_type *_rgn = &memblock.reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000674
Tang Chen66a20752014-01-21 15:49:20 -0800675 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] flags %#02lx %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700676 (unsigned long long)base,
Grygorii Strashko931d13f2014-01-21 15:49:17 -0800677 (unsigned long long)base + size - 1,
Tang Chen66a20752014-01-21 15:49:20 -0800678 flags, (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000679
Tang Chen66a20752014-01-21 15:49:20 -0800680 return memblock_add_region(_rgn, base, size, nid, flags);
681}
682
683int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
684{
685 return memblock_reserve_region(base, size, MAX_NUMNODES, 0);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000686}
687
Tejun Heo35fd0802011-07-12 11:15:59 +0200688/**
Tang Chen66b16ed2014-01-21 15:49:23 -0800689 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
690 * @base: the base phys addr of the region
691 * @size: the size of the region
692 *
693 * This function isolates region [@base, @base + @size), and mark it with flag
694 * MEMBLOCK_HOTPLUG.
695 *
696 * Return 0 on succees, -errno on failure.
697 */
698int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
699{
700 struct memblock_type *type = &memblock.memory;
701 int i, ret, start_rgn, end_rgn;
702
703 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
704 if (ret)
705 return ret;
706
707 for (i = start_rgn; i < end_rgn; i++)
708 memblock_set_region_flags(&type->regions[i], MEMBLOCK_HOTPLUG);
709
710 memblock_merge_regions(type);
711 return 0;
712}
713
714/**
715 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
716 * @base: the base phys addr of the region
717 * @size: the size of the region
718 *
719 * This function isolates region [@base, @base + @size), and clear flag
720 * MEMBLOCK_HOTPLUG for the isolated regions.
721 *
722 * Return 0 on succees, -errno on failure.
723 */
724int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
725{
726 struct memblock_type *type = &memblock.memory;
727 int i, ret, start_rgn, end_rgn;
728
729 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
730 if (ret)
731 return ret;
732
733 for (i = start_rgn; i < end_rgn; i++)
734 memblock_clear_region_flags(&type->regions[i],
735 MEMBLOCK_HOTPLUG);
736
737 memblock_merge_regions(type);
738 return 0;
739}
740
741/**
Tejun Heo35fd0802011-07-12 11:15:59 +0200742 * __next_free_mem_range - next function for for_each_free_mem_range()
743 * @idx: pointer to u64 loop variable
Tang Chend8bbdd72013-07-08 16:00:22 -0700744 * @nid: node selector, %MAX_NUMNODES for all nodes
Wanpeng Lidad75572012-06-20 12:53:01 -0700745 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
746 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
747 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +0200748 *
749 * Find the first free area from *@idx which matches @nid, fill the out
750 * parameters, and update *@idx for the next iteration. The lower 32bit of
751 * *@idx contains index into memory region and the upper 32bit indexes the
752 * areas before each reserved region. For example, if reserved regions
753 * look like the following,
754 *
755 * 0:[0-16), 1:[32-48), 2:[128-130)
756 *
757 * The upper 32bit indexes the following regions.
758 *
759 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
760 *
761 * As both region arrays are sorted, the function advances the two indices
762 * in lockstep and returns each intersection.
763 */
764void __init_memblock __next_free_mem_range(u64 *idx, int nid,
765 phys_addr_t *out_start,
766 phys_addr_t *out_end, int *out_nid)
767{
768 struct memblock_type *mem = &memblock.memory;
769 struct memblock_type *rsv = &memblock.reserved;
770 int mi = *idx & 0xffffffff;
771 int ri = *idx >> 32;
772
773 for ( ; mi < mem->cnt; mi++) {
774 struct memblock_region *m = &mem->regions[mi];
775 phys_addr_t m_start = m->base;
776 phys_addr_t m_end = m->base + m->size;
777
778 /* only memory regions are associated with nodes, check it */
779 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
780 continue;
781
782 /* scan areas before each reservation for intersection */
783 for ( ; ri < rsv->cnt + 1; ri++) {
784 struct memblock_region *r = &rsv->regions[ri];
785 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
786 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
787
788 /* if ri advanced past mi, break out to advance mi */
789 if (r_start >= m_end)
790 break;
791 /* if the two regions intersect, we're done */
792 if (m_start < r_end) {
793 if (out_start)
794 *out_start = max(m_start, r_start);
795 if (out_end)
796 *out_end = min(m_end, r_end);
797 if (out_nid)
798 *out_nid = memblock_get_region_node(m);
799 /*
800 * The region which ends first is advanced
801 * for the next iteration.
802 */
803 if (m_end <= r_end)
804 mi++;
805 else
806 ri++;
807 *idx = (u32)mi | (u64)ri << 32;
808 return;
809 }
810 }
811 }
812
813 /* signal end of iteration */
814 *idx = ULLONG_MAX;
815}
816
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800817/**
818 * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
819 * @idx: pointer to u64 loop variable
820 * @nid: nid: node selector, %MAX_NUMNODES for all nodes
Wanpeng Lidad75572012-06-20 12:53:01 -0700821 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
822 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
823 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800824 *
825 * Reverse of __next_free_mem_range().
Tang Chen55ac5902014-01-21 15:49:35 -0800826 *
827 * Linux kernel cannot migrate pages used by itself. Memory hotplug users won't
828 * be able to hot-remove hotpluggable memory used by the kernel. So this
829 * function skip hotpluggable regions if needed when allocating memory for the
830 * kernel.
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800831 */
832void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
833 phys_addr_t *out_start,
834 phys_addr_t *out_end, int *out_nid)
835{
836 struct memblock_type *mem = &memblock.memory;
837 struct memblock_type *rsv = &memblock.reserved;
838 int mi = *idx & 0xffffffff;
839 int ri = *idx >> 32;
840
841 if (*idx == (u64)ULLONG_MAX) {
842 mi = mem->cnt - 1;
843 ri = rsv->cnt;
844 }
845
846 for ( ; mi >= 0; mi--) {
847 struct memblock_region *m = &mem->regions[mi];
848 phys_addr_t m_start = m->base;
849 phys_addr_t m_end = m->base + m->size;
850
851 /* only memory regions are associated with nodes, check it */
852 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
853 continue;
854
Tang Chen55ac5902014-01-21 15:49:35 -0800855 /* skip hotpluggable memory regions if needed */
856 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
857 continue;
858
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800859 /* scan areas before each reservation for intersection */
860 for ( ; ri >= 0; ri--) {
861 struct memblock_region *r = &rsv->regions[ri];
862 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
863 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
864
865 /* if ri advanced past mi, break out to advance mi */
866 if (r_end <= m_start)
867 break;
868 /* if the two regions intersect, we're done */
869 if (m_end > r_start) {
870 if (out_start)
871 *out_start = max(m_start, r_start);
872 if (out_end)
873 *out_end = min(m_end, r_end);
874 if (out_nid)
875 *out_nid = memblock_get_region_node(m);
876
877 if (m_start >= r_start)
878 mi--;
879 else
880 ri--;
881 *idx = (u32)mi | (u64)ri << 32;
882 return;
883 }
884 }
885 }
886
887 *idx = ULLONG_MAX;
888}
889
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200890#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
891/*
892 * Common iterator interface used to define for_each_mem_range().
893 */
894void __init_memblock __next_mem_pfn_range(int *idx, int nid,
895 unsigned long *out_start_pfn,
896 unsigned long *out_end_pfn, int *out_nid)
897{
898 struct memblock_type *type = &memblock.memory;
899 struct memblock_region *r;
900
901 while (++*idx < type->cnt) {
902 r = &type->regions[*idx];
903
904 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
905 continue;
906 if (nid == MAX_NUMNODES || nid == r->nid)
907 break;
908 }
909 if (*idx >= type->cnt) {
910 *idx = -1;
911 return;
912 }
913
914 if (out_start_pfn)
915 *out_start_pfn = PFN_UP(r->base);
916 if (out_end_pfn)
917 *out_end_pfn = PFN_DOWN(r->base + r->size);
918 if (out_nid)
919 *out_nid = r->nid;
920}
921
922/**
923 * memblock_set_node - set node ID on memblock regions
924 * @base: base of area to set node ID for
925 * @size: size of area to set node ID for
Tang Chene7e8de52014-01-21 15:49:26 -0800926 * @type: memblock type to set node ID for
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200927 * @nid: node ID to set
928 *
Tang Chene7e8de52014-01-21 15:49:26 -0800929 * Set the nid of memblock @type regions in [@base,@base+@size) to @nid.
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200930 * Regions which cross the area boundaries are split as necessary.
931 *
932 * RETURNS:
933 * 0 on success, -errno on failure.
934 */
935int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
Tang Chene7e8de52014-01-21 15:49:26 -0800936 struct memblock_type *type, int nid)
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200937{
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800938 int start_rgn, end_rgn;
939 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200940
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800941 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
942 if (ret)
943 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200944
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800945 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -0700946 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200947
948 memblock_merge_regions(type);
949 return 0;
950}
951#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
952
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800953static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
954 phys_addr_t align, phys_addr_t max_addr,
955 int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000956{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000957 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000958
Vineet Gupta94f3d3a2013-04-29 15:06:15 -0700959 if (WARN_ON(!align))
960 align = __alignof__(long long);
961
Tejun Heo847854f2012-02-29 05:56:21 +0900962 /* align @size to avoid excessive fragmentation on reserved array */
963 size = round_up(size, align);
964
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800965 found = memblock_find_in_range_node(0, max_addr, size, align, nid);
Tejun Heo9c8c27e2011-12-08 10:22:06 -0800966 if (found && !memblock_reserve(found, size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000967 return found;
968
969 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000970}
971
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800972phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
973{
974 return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
975}
976
977phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
978{
979 return memblock_alloc_base_nid(size, align, max_addr, MAX_NUMNODES);
980}
981
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000982phys_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 +1000983{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000984 phys_addr_t alloc;
985
986 alloc = __memblock_alloc_base(size, align, max_addr);
987
988 if (alloc == 0)
989 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
990 (unsigned long long) size, (unsigned long long) max_addr);
991
992 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000993}
994
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000995phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000996{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000997 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000998}
999
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001000phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1001{
1002 phys_addr_t res = memblock_alloc_nid(size, align, nid);
1003
1004 if (res)
1005 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +02001006 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001007}
1008
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001009
1010/*
1011 * Remaining API functions
1012 */
1013
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +10001014phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001015{
Tejun Heo1440c4e2011-12-08 10:22:08 -08001016 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001017}
1018
Yinghai Lu595ad9a2013-01-24 12:20:09 -08001019phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1020{
1021 unsigned long pages = 0;
1022 struct memblock_region *r;
1023 unsigned long start_pfn, end_pfn;
1024
1025 for_each_memblock(memory, r) {
1026 start_pfn = memblock_region_memory_base_pfn(r);
1027 end_pfn = memblock_region_memory_end_pfn(r);
1028 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1029 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1030 pages += end_pfn - start_pfn;
1031 }
1032
1033 return (phys_addr_t)pages << PAGE_SHIFT;
1034}
1035
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -07001036/* lowest address */
1037phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1038{
1039 return memblock.memory.regions[0].base;
1040}
1041
Yinghai Lu10d06432010-07-28 15:43:02 +10001042phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001043{
1044 int idx = memblock.memory.cnt - 1;
1045
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +10001046 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001047}
1048
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001049void __init memblock_enforce_memory_limit(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001050{
1051 unsigned long i;
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001052 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001053
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001054 if (!limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001055 return;
1056
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001057 /* find out max address */
Yinghai Lu95f72d12010-07-12 14:36:09 +10001058 for (i = 0; i < memblock.memory.cnt; i++) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001059 struct memblock_region *r = &memblock.memory.regions[i];
Yinghai Lu95f72d12010-07-12 14:36:09 +10001060
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001061 if (limit <= r->size) {
1062 max_addr = r->base + limit;
1063 break;
1064 }
1065 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001066 }
1067
Tejun Heoc0ce8fe2011-12-08 10:22:07 -08001068 /* truncate both memory and reserved regions */
1069 __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
1070 __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001071}
1072
Yinghai Lucd794812010-10-11 12:34:09 -07001073static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001074{
1075 unsigned int left = 0, right = type->cnt;
1076
1077 do {
1078 unsigned int mid = (right + left) / 2;
1079
1080 if (addr < type->regions[mid].base)
1081 right = mid;
1082 else if (addr >= (type->regions[mid].base +
1083 type->regions[mid].size))
1084 left = mid + 1;
1085 else
1086 return mid;
1087 } while (left < right);
1088 return -1;
1089}
1090
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +10001091int __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001092{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001093 return memblock_search(&memblock.reserved, addr) != -1;
1094}
Yinghai Lu95f72d12010-07-12 14:36:09 +10001095
Yinghai Lu3661ca62010-09-15 13:05:29 -07001096int __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001097{
1098 return memblock_search(&memblock.memory, addr) != -1;
1099}
1100
Yinghai Lue76b63f2013-09-11 14:22:17 -07001101#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1102int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1103 unsigned long *start_pfn, unsigned long *end_pfn)
1104{
1105 struct memblock_type *type = &memblock.memory;
1106 int mid = memblock_search(type, (phys_addr_t)pfn << PAGE_SHIFT);
1107
1108 if (mid == -1)
1109 return -1;
1110
1111 *start_pfn = type->regions[mid].base >> PAGE_SHIFT;
1112 *end_pfn = (type->regions[mid].base + type->regions[mid].size)
1113 >> PAGE_SHIFT;
1114
1115 return type->regions[mid].nid;
1116}
1117#endif
1118
Stephen Boydeab30942012-05-24 00:45:21 -07001119/**
1120 * memblock_is_region_memory - check if a region is a subset of memory
1121 * @base: base of region to check
1122 * @size: size of region to check
1123 *
1124 * Check if the region [@base, @base+@size) is a subset of a memory block.
1125 *
1126 * RETURNS:
1127 * 0 if false, non-zero if true
1128 */
Yinghai Lu3661ca62010-09-15 13:05:29 -07001129int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001130{
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001131 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001132 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +10001133
1134 if (idx == -1)
1135 return 0;
Tomi Valkeinenabb65272011-01-20 14:44:20 -08001136 return memblock.memory.regions[idx].base <= base &&
1137 (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001138 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001139}
1140
Stephen Boydeab30942012-05-24 00:45:21 -07001141/**
1142 * memblock_is_region_reserved - check if a region intersects reserved memory
1143 * @base: base of region to check
1144 * @size: size of region to check
1145 *
1146 * Check if the region [@base, @base+@size) intersects a reserved memory block.
1147 *
1148 * RETURNS:
1149 * 0 if false, non-zero if true
1150 */
Yinghai Lu10d06432010-07-28 15:43:02 +10001151int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +10001152{
Tejun Heoeb18f1b2011-12-08 10:22:07 -08001153 memblock_cap_size(base, &size);
Benjamin Herrenschmidtf1c2c192010-08-04 14:17:17 +10001154 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +10001155}
1156
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001157void __init_memblock memblock_trim_memory(phys_addr_t align)
1158{
1159 int i;
1160 phys_addr_t start, end, orig_start, orig_end;
1161 struct memblock_type *mem = &memblock.memory;
1162
1163 for (i = 0; i < mem->cnt; i++) {
1164 orig_start = mem->regions[i].base;
1165 orig_end = mem->regions[i].base + mem->regions[i].size;
1166 start = round_up(orig_start, align);
1167 end = round_down(orig_end, align);
1168
1169 if (start == orig_start && end == orig_end)
1170 continue;
1171
1172 if (start < end) {
1173 mem->regions[i].base = start;
1174 mem->regions[i].size = end - start;
1175 } else {
1176 memblock_remove_region(mem, i);
1177 i--;
1178 }
1179 }
1180}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001181
Yinghai Lu3661ca62010-09-15 13:05:29 -07001182void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001183{
1184 memblock.current_limit = limit;
1185}
1186
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001187static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001188{
1189 unsigned long long base, size;
Tang Chen66a20752014-01-21 15:49:20 -08001190 unsigned long flags;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001191 int i;
1192
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001193 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001194
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001195 for (i = 0; i < type->cnt; i++) {
1196 struct memblock_region *rgn = &type->regions[i];
1197 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001198
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001199 base = rgn->base;
1200 size = rgn->size;
Tang Chen66a20752014-01-21 15:49:20 -08001201 flags = rgn->flags;
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001202#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1203 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1204 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1205 memblock_get_region_node(rgn));
1206#endif
Tang Chen66a20752014-01-21 15:49:20 -08001207 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n",
1208 name, i, base, base + size - 1, size, nid_buf, flags);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001209 }
1210}
1211
Tejun Heo4ff7b822011-12-08 10:22:06 -08001212void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001213{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001214 pr_info("MEMBLOCK configuration:\n");
Tejun Heo1440c4e2011-12-08 10:22:08 -08001215 pr_info(" memory size = %#llx reserved size = %#llx\n",
1216 (unsigned long long)memblock.memory.total_size,
1217 (unsigned long long)memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001218
1219 memblock_dump(&memblock.memory, "memory");
1220 memblock_dump(&memblock.reserved, "reserved");
1221}
1222
Tejun Heo1aadc052011-12-08 10:22:08 -08001223void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001224{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001225 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001226}
1227
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001228static int __init early_memblock(char *p)
1229{
1230 if (p && strstr(p, "debug"))
1231 memblock_debug = 1;
1232 return 0;
1233}
1234early_param("memblock", early_memblock);
1235
Tejun Heoc378ddd2011-07-14 11:46:03 +02001236#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001237
1238static int memblock_debug_show(struct seq_file *m, void *private)
1239{
1240 struct memblock_type *type = m->private;
1241 struct memblock_region *reg;
1242 int i;
1243
1244 for (i = 0; i < type->cnt; i++) {
1245 reg = &type->regions[i];
1246 seq_printf(m, "%4d: ", i);
1247 if (sizeof(phys_addr_t) == 4)
1248 seq_printf(m, "0x%08lx..0x%08lx\n",
1249 (unsigned long)reg->base,
1250 (unsigned long)(reg->base + reg->size - 1));
1251 else
1252 seq_printf(m, "0x%016llx..0x%016llx\n",
1253 (unsigned long long)reg->base,
1254 (unsigned long long)(reg->base + reg->size - 1));
1255
1256 }
1257 return 0;
1258}
1259
1260static int memblock_debug_open(struct inode *inode, struct file *file)
1261{
1262 return single_open(file, memblock_debug_show, inode->i_private);
1263}
1264
1265static const struct file_operations memblock_debug_fops = {
1266 .open = memblock_debug_open,
1267 .read = seq_read,
1268 .llseek = seq_lseek,
1269 .release = single_release,
1270};
1271
1272static int __init memblock_init_debugfs(void)
1273{
1274 struct dentry *root = debugfs_create_dir("memblock", NULL);
1275 if (!root)
1276 return -ENXIO;
1277 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1278 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
1279
1280 return 0;
1281}
1282__initcall(memblock_init_debugfs);
1283
1284#endif /* CONFIG_DEBUG_FS */