blob: 1bcd9b97056481524978ed295e25051c5bbe7536 [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
Tejun Heofe091c22011-12-08 10:22:07 -080023static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
24static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
25
26struct memblock memblock __initdata_memblock = {
27 .memory.regions = memblock_memory_init_regions,
28 .memory.cnt = 1, /* empty dummy entry */
29 .memory.max = INIT_MEMBLOCK_REGIONS,
30
31 .reserved.regions = memblock_reserved_init_regions,
32 .reserved.cnt = 1, /* empty dummy entry */
33 .reserved.max = INIT_MEMBLOCK_REGIONS,
34
35 .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
36};
Yinghai Lu95f72d12010-07-12 14:36:09 +100037
Yinghai Lu10d06432010-07-28 15:43:02 +100038int memblock_debug __initdata_memblock;
Tejun Heo1aadc052011-12-08 10:22:08 -080039static int memblock_can_resize __initdata_memblock;
Gavin Shan181eb392012-05-29 15:06:50 -070040static int memblock_memory_in_slab __initdata_memblock = 0;
41static int memblock_reserved_in_slab __initdata_memblock = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +100042
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070043/* inline so we don't get a warning when pr_debug is compiled out */
Raghavendra D Prabhuc2233112012-10-08 16:33:55 -070044static __init_memblock const char *
45memblock_type_name(struct memblock_type *type)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070046{
47 if (type == &memblock.memory)
48 return "memory";
49 else if (type == &memblock.reserved)
50 return "reserved";
51 else
52 return "unknown";
53}
54
Tejun Heoeb18f1b2011-12-08 10:22:07 -080055/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
56static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
57{
58 return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
59}
60
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100061/*
62 * Address comparison utilities
63 */
Yinghai Lu10d06432010-07-28 15:43:02 +100064static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100065 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100066{
67 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
68}
69
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -070070static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
71 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100072{
73 unsigned long i;
74
75 for (i = 0; i < type->cnt; i++) {
76 phys_addr_t rgnbase = type->regions[i].base;
77 phys_addr_t rgnsize = type->regions[i].size;
78 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
79 break;
80 }
81
82 return (i < type->cnt) ? i : -1;
83}
84
Tejun Heo7bd0b0f2011-12-08 10:22:09 -080085/**
86 * memblock_find_in_range_node - find free area in given range and node
87 * @start: start of candidate range
88 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
89 * @size: size of free area to find
90 * @align: alignment of free area to find
91 * @nid: nid of the free area to find, %MAX_NUMNODES for any node
92 *
93 * Find @size free area aligned to @align in the specified range and node.
94 *
Tang Chenf7210e62013-02-22 16:33:51 -080095 * If we have CONFIG_HAVE_MEMBLOCK_NODE_MAP defined, we need to check if the
96 * memory we found if not in hotpluggable ranges.
97 *
Tejun Heo7bd0b0f2011-12-08 10:22:09 -080098 * RETURNS:
99 * Found address on success, %0 on failure.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000100 */
Tang Chenf7210e62013-02-22 16:33:51 -0800101#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800102phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start,
103 phys_addr_t end, phys_addr_t size,
104 phys_addr_t align, int nid)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000105{
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800106 phys_addr_t this_start, this_end, cand;
107 u64 i;
Tang Chenfb06bc8e2013-02-22 16:33:42 -0800108 int curr = movablemem_map.nr_map - 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000109
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800110 /* pump up @end */
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000111 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
112 end = memblock.current_limit;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000113
Tejun Heo5d53cb22012-01-13 10:14:12 -0800114 /* avoid allocating the first page */
115 start = max_t(phys_addr_t, start, PAGE_SIZE);
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800116 end = max(start, end);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000117
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800118 for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
119 this_start = clamp(this_start, start, end);
120 this_end = clamp(this_end, start, end);
121
Tang Chenfb06bc8e2013-02-22 16:33:42 -0800122restart:
123 if (this_end <= this_start || this_end < size)
Tejun Heo5d53cb22012-01-13 10:14:12 -0800124 continue;
125
Tang Chenfb06bc8e2013-02-22 16:33:42 -0800126 for (; curr >= 0; curr--) {
127 if ((movablemem_map.map[curr].start_pfn << PAGE_SHIFT)
128 < this_end)
129 break;
130 }
131
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800132 cand = round_down(this_end - size, align);
Tang Chenfb06bc8e2013-02-22 16:33:42 -0800133 if (curr >= 0 &&
134 cand < movablemem_map.map[curr].end_pfn << PAGE_SHIFT) {
135 this_end = movablemem_map.map[curr].start_pfn
136 << PAGE_SHIFT;
137 goto restart;
138 }
139
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800140 if (cand >= this_start)
141 return cand;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000142 }
Tang Chenfb06bc8e2013-02-22 16:33:42 -0800143
Tejun Heo1f5026a2011-07-12 09:58:09 +0200144 return 0;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000145}
Tang Chenf7210e62013-02-22 16:33:51 -0800146#else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
147phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start,
148 phys_addr_t end, phys_addr_t size,
149 phys_addr_t align, int nid)
150{
151 phys_addr_t this_start, this_end, cand;
152 u64 i;
153
154 /* pump up @end */
155 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
156 end = memblock.current_limit;
157
158 /* avoid allocating the first page */
159 start = max_t(phys_addr_t, start, PAGE_SIZE);
160 end = max(start, end);
161
162 for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
163 this_start = clamp(this_start, start, end);
164 this_end = clamp(this_end, start, end);
165
166 if (this_end < size)
167 continue;
168
169 cand = round_down(this_end - size, align);
170 if (cand >= this_start)
171 return cand;
172 }
173 return 0;
174}
175#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000176
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800177/**
178 * memblock_find_in_range - find free area in given range
179 * @start: start of candidate range
180 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
181 * @size: size of free area to find
182 * @align: alignment of free area to find
183 *
184 * Find @size free area aligned to @align in the specified range.
185 *
186 * RETURNS:
187 * Found address on success, %0 on failure.
188 */
189phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
190 phys_addr_t end, phys_addr_t size,
191 phys_addr_t align)
192{
193 return memblock_find_in_range_node(start, end, size, align,
194 MAX_NUMNODES);
195}
196
Yinghai Lu10d06432010-07-28 15:43:02 +1000197static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000198{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800199 type->total_size -= type->regions[r].size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200200 memmove(&type->regions[r], &type->regions[r + 1],
201 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000202 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000203
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700204 /* Special case for empty arrays */
205 if (type->cnt == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800206 WARN_ON(type->total_size != 0);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700207 type->cnt = 1;
208 type->regions[0].base = 0;
209 type->regions[0].size = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200210 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700211 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000212}
213
Yinghai Lu29f67382012-07-11 14:02:56 -0700214phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info(
215 phys_addr_t *addr)
216{
217 if (memblock.reserved.regions == memblock_reserved_init_regions)
218 return 0;
219
220 *addr = __pa(memblock.reserved.regions);
221
222 return PAGE_ALIGN(sizeof(struct memblock_region) *
223 memblock.reserved.max);
224}
225
Greg Pearson48c3b582012-06-20 12:53:05 -0700226/**
227 * memblock_double_array - double the size of the memblock regions array
228 * @type: memblock type of the regions array being doubled
229 * @new_area_start: starting address of memory range to avoid overlap with
230 * @new_area_size: size of memory range to avoid overlap with
231 *
232 * Double the size of the @type regions array. If memblock is being used to
233 * allocate memory for a new reserved regions array and there is a previously
234 * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
235 * waiting to be reserved, ensure the memory used by the new array does
236 * not overlap.
237 *
238 * RETURNS:
239 * 0 on success, -1 on failure.
240 */
241static int __init_memblock memblock_double_array(struct memblock_type *type,
242 phys_addr_t new_area_start,
243 phys_addr_t new_area_size)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700244{
245 struct memblock_region *new_array, *old_array;
Yinghai Lu29f67382012-07-11 14:02:56 -0700246 phys_addr_t old_alloc_size, new_alloc_size;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700247 phys_addr_t old_size, new_size, addr;
248 int use_slab = slab_is_available();
Gavin Shan181eb392012-05-29 15:06:50 -0700249 int *in_slab;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700250
251 /* We don't allow resizing until we know about the reserved regions
252 * of memory that aren't suitable for allocation
253 */
254 if (!memblock_can_resize)
255 return -1;
256
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700257 /* Calculate new doubled size */
258 old_size = type->max * sizeof(struct memblock_region);
259 new_size = old_size << 1;
Yinghai Lu29f67382012-07-11 14:02:56 -0700260 /*
261 * We need to allocated new one align to PAGE_SIZE,
262 * so we can free them completely later.
263 */
264 old_alloc_size = PAGE_ALIGN(old_size);
265 new_alloc_size = PAGE_ALIGN(new_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700266
Gavin Shan181eb392012-05-29 15:06:50 -0700267 /* Retrieve the slab flag */
268 if (type == &memblock.memory)
269 in_slab = &memblock_memory_in_slab;
270 else
271 in_slab = &memblock_reserved_in_slab;
272
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700273 /* Try to find some space for it.
274 *
275 * WARNING: We assume that either slab_is_available() and we use it or
Andrew Mortonfd073832012-07-31 16:42:40 -0700276 * we use MEMBLOCK for allocations. That means that this is unsafe to
277 * use when bootmem is currently active (unless bootmem itself is
278 * implemented on top of MEMBLOCK which isn't the case yet)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700279 *
280 * This should however not be an issue for now, as we currently only
Andrew Mortonfd073832012-07-31 16:42:40 -0700281 * call into MEMBLOCK while it's still active, or much later when slab
282 * is active for memory hotplug operations
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700283 */
284 if (use_slab) {
285 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200286 addr = new_array ? __pa(new_array) : 0;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700287 } else {
Greg Pearson48c3b582012-06-20 12:53:05 -0700288 /* only exclude range when trying to double reserved.regions */
289 if (type != &memblock.reserved)
290 new_area_start = new_area_size = 0;
291
292 addr = memblock_find_in_range(new_area_start + new_area_size,
293 memblock.current_limit,
Yinghai Lu29f67382012-07-11 14:02:56 -0700294 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700295 if (!addr && new_area_size)
296 addr = memblock_find_in_range(0,
Andrew Mortonfd073832012-07-31 16:42:40 -0700297 min(new_area_start, memblock.current_limit),
298 new_alloc_size, PAGE_SIZE);
Greg Pearson48c3b582012-06-20 12:53:05 -0700299
Sachin Kamat15674862012-09-04 13:55:05 +0530300 new_array = addr ? __va(addr) : NULL;
Gavin Shan4e2f0772012-05-29 15:06:50 -0700301 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200302 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700303 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
304 memblock_type_name(type), type->max, type->max * 2);
305 return -1;
306 }
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700307
Andrew Mortonfd073832012-07-31 16:42:40 -0700308 memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
309 memblock_type_name(type), type->max * 2, (u64)addr,
310 (u64)addr + new_size - 1);
Yinghai Luea9e4372010-07-28 15:13:22 +1000311
Andrew Mortonfd073832012-07-31 16:42:40 -0700312 /*
313 * Found space, we now need to move the array over before we add the
314 * reserved region since it may be our reserved array itself that is
315 * full.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700316 */
317 memcpy(new_array, type->regions, old_size);
318 memset(new_array + type->max, 0, old_size);
319 old_array = type->regions;
320 type->regions = new_array;
321 type->max <<= 1;
322
Andrew Mortonfd073832012-07-31 16:42:40 -0700323 /* Free old array. We needn't free it if the array is the static one */
Gavin Shan181eb392012-05-29 15:06:50 -0700324 if (*in_slab)
325 kfree(old_array);
326 else if (old_array != memblock_memory_init_regions &&
327 old_array != memblock_reserved_init_regions)
Yinghai Lu29f67382012-07-11 14:02:56 -0700328 memblock_free(__pa(old_array), old_alloc_size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700329
Andrew Mortonfd073832012-07-31 16:42:40 -0700330 /*
331 * Reserve the new array if that comes from the memblock. Otherwise, we
332 * needn't do it
Gavin Shan181eb392012-05-29 15:06:50 -0700333 */
334 if (!use_slab)
Yinghai Lu29f67382012-07-11 14:02:56 -0700335 BUG_ON(memblock_reserve(addr, new_alloc_size));
Gavin Shan181eb392012-05-29 15:06:50 -0700336
337 /* Update slab flag */
338 *in_slab = use_slab;
339
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700340 return 0;
341}
342
Tejun Heo784656f92011-07-12 11:15:55 +0200343/**
344 * memblock_merge_regions - merge neighboring compatible regions
345 * @type: memblock type to scan
346 *
347 * Scan @type and merge neighboring compatible regions.
348 */
349static void __init_memblock memblock_merge_regions(struct memblock_type *type)
350{
351 int i = 0;
352
353 /* cnt never goes below 1 */
354 while (i < type->cnt - 1) {
355 struct memblock_region *this = &type->regions[i];
356 struct memblock_region *next = &type->regions[i + 1];
357
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200358 if (this->base + this->size != next->base ||
359 memblock_get_region_node(this) !=
360 memblock_get_region_node(next)) {
Tejun Heo784656f92011-07-12 11:15:55 +0200361 BUG_ON(this->base + this->size > next->base);
362 i++;
363 continue;
364 }
365
366 this->size += next->size;
Lin Fengc0232ae2013-01-11 14:31:44 -0800367 /* move forward from next + 1, index of which is i + 2 */
368 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
Tejun Heo784656f92011-07-12 11:15:55 +0200369 type->cnt--;
370 }
371}
372
373/**
374 * memblock_insert_region - insert new memblock region
375 * @type: memblock type to insert into
376 * @idx: index for the insertion point
377 * @base: base address of the new region
378 * @size: size of the new region
379 *
380 * Insert new memblock region [@base,@base+@size) into @type at @idx.
381 * @type must already have extra room to accomodate the new region.
382 */
383static void __init_memblock memblock_insert_region(struct memblock_type *type,
384 int idx, phys_addr_t base,
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200385 phys_addr_t size, int nid)
Tejun Heo784656f92011-07-12 11:15:55 +0200386{
387 struct memblock_region *rgn = &type->regions[idx];
388
389 BUG_ON(type->cnt >= type->max);
390 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
391 rgn->base = base;
392 rgn->size = size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200393 memblock_set_region_node(rgn, nid);
Tejun Heo784656f92011-07-12 11:15:55 +0200394 type->cnt++;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800395 type->total_size += size;
Tejun Heo784656f92011-07-12 11:15:55 +0200396}
397
398/**
399 * memblock_add_region - add new memblock region
400 * @type: memblock type to add new region into
401 * @base: base address of the new region
402 * @size: size of the new region
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800403 * @nid: nid of the new region
Tejun Heo784656f92011-07-12 11:15:55 +0200404 *
405 * Add new memblock region [@base,@base+@size) into @type. The new region
406 * is allowed to overlap with existing ones - overlaps don't affect already
407 * existing regions. @type is guaranteed to be minimal (all neighbouring
408 * compatible regions are merged) after the addition.
409 *
410 * RETURNS:
411 * 0 on success, -errno on failure.
412 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800413static int __init_memblock memblock_add_region(struct memblock_type *type,
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800414 phys_addr_t base, phys_addr_t size, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000415{
Tejun Heo784656f92011-07-12 11:15:55 +0200416 bool insert = false;
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800417 phys_addr_t obase = base;
418 phys_addr_t end = base + memblock_cap_size(base, &size);
Tejun Heo784656f92011-07-12 11:15:55 +0200419 int i, nr_new;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000420
Tejun Heob3dc6272012-04-20 08:31:34 -0700421 if (!size)
422 return 0;
423
Tejun Heo784656f92011-07-12 11:15:55 +0200424 /* special case for empty array */
425 if (type->regions[0].size == 0) {
Tejun Heo1440c4e2011-12-08 10:22:08 -0800426 WARN_ON(type->cnt != 1 || type->total_size);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000427 type->regions[0].base = base;
428 type->regions[0].size = size;
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800429 memblock_set_region_node(&type->regions[0], nid);
Tejun Heo1440c4e2011-12-08 10:22:08 -0800430 type->total_size = size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000431 return 0;
432 }
Tejun Heo784656f92011-07-12 11:15:55 +0200433repeat:
434 /*
435 * The following is executed twice. Once with %false @insert and
436 * then with %true. The first counts the number of regions needed
437 * to accomodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700438 */
Tejun Heo784656f92011-07-12 11:15:55 +0200439 base = obase;
440 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000441
Tejun Heo784656f92011-07-12 11:15:55 +0200442 for (i = 0; i < type->cnt; i++) {
443 struct memblock_region *rgn = &type->regions[i];
444 phys_addr_t rbase = rgn->base;
445 phys_addr_t rend = rbase + rgn->size;
446
447 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000448 break;
Tejun Heo784656f92011-07-12 11:15:55 +0200449 if (rend <= base)
450 continue;
451 /*
452 * @rgn overlaps. If it separates the lower part of new
453 * area, insert that portion.
454 */
455 if (rbase > base) {
456 nr_new++;
457 if (insert)
458 memblock_insert_region(type, i++, base,
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800459 rbase - base, nid);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000460 }
Tejun Heo784656f92011-07-12 11:15:55 +0200461 /* area below @rend is dealt with, forget about it */
462 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000463 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000464
Tejun Heo784656f92011-07-12 11:15:55 +0200465 /* insert the remaining portion */
466 if (base < end) {
467 nr_new++;
468 if (insert)
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800469 memblock_insert_region(type, i, base, end - base, nid);
Tejun Heo784656f92011-07-12 11:15:55 +0200470 }
471
472 /*
473 * If this was the first round, resize array and repeat for actual
474 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700475 */
Tejun Heo784656f92011-07-12 11:15:55 +0200476 if (!insert) {
477 while (type->cnt + nr_new > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700478 if (memblock_double_array(type, obase, size) < 0)
Tejun Heo784656f92011-07-12 11:15:55 +0200479 return -ENOMEM;
480 insert = true;
481 goto repeat;
482 } else {
483 memblock_merge_regions(type);
484 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700485 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000486}
487
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800488int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
489 int nid)
490{
491 return memblock_add_region(&memblock.memory, base, size, nid);
492}
493
Tejun Heo581adcb2011-12-08 10:22:06 -0800494int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000495{
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800496 return memblock_add_region(&memblock.memory, base, size, MAX_NUMNODES);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000497}
498
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800499/**
500 * memblock_isolate_range - isolate given range into disjoint memblocks
501 * @type: memblock type to isolate range for
502 * @base: base of range to isolate
503 * @size: size of range to isolate
504 * @start_rgn: out parameter for the start of isolated region
505 * @end_rgn: out parameter for the end of isolated region
506 *
507 * Walk @type and ensure that regions don't cross the boundaries defined by
508 * [@base,@base+@size). Crossing regions are split at the boundaries,
509 * which may create at most two more regions. The index of the first
510 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
511 *
512 * RETURNS:
513 * 0 on success, -errno on failure.
514 */
515static int __init_memblock memblock_isolate_range(struct memblock_type *type,
516 phys_addr_t base, phys_addr_t size,
517 int *start_rgn, int *end_rgn)
518{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800519 phys_addr_t end = base + memblock_cap_size(base, &size);
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800520 int i;
521
522 *start_rgn = *end_rgn = 0;
523
Tejun Heob3dc6272012-04-20 08:31:34 -0700524 if (!size)
525 return 0;
526
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800527 /* we'll create at most two more regions */
528 while (type->cnt + 2 > type->max)
Greg Pearson48c3b582012-06-20 12:53:05 -0700529 if (memblock_double_array(type, base, size) < 0)
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800530 return -ENOMEM;
531
532 for (i = 0; i < type->cnt; i++) {
533 struct memblock_region *rgn = &type->regions[i];
534 phys_addr_t rbase = rgn->base;
535 phys_addr_t rend = rbase + rgn->size;
536
537 if (rbase >= end)
538 break;
539 if (rend <= base)
540 continue;
541
542 if (rbase < base) {
543 /*
544 * @rgn intersects from below. Split and continue
545 * to process the next region - the new top half.
546 */
547 rgn->base = base;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800548 rgn->size -= base - rbase;
549 type->total_size -= base - rbase;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800550 memblock_insert_region(type, i, rbase, base - rbase,
Tejun Heo71936182011-12-08 10:22:07 -0800551 memblock_get_region_node(rgn));
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800552 } else if (rend > end) {
553 /*
554 * @rgn intersects from above. Split and redo the
555 * current region - the new bottom half.
556 */
557 rgn->base = end;
Tejun Heo1440c4e2011-12-08 10:22:08 -0800558 rgn->size -= end - rbase;
559 type->total_size -= end - rbase;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800560 memblock_insert_region(type, i--, rbase, end - rbase,
Tejun Heo71936182011-12-08 10:22:07 -0800561 memblock_get_region_node(rgn));
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800562 } else {
563 /* @rgn is fully contained, record it */
564 if (!*end_rgn)
565 *start_rgn = i;
566 *end_rgn = i + 1;
567 }
568 }
569
570 return 0;
571}
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800572
Tejun Heo581adcb2011-12-08 10:22:06 -0800573static int __init_memblock __memblock_remove(struct memblock_type *type,
574 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000575{
Tejun Heo71936182011-12-08 10:22:07 -0800576 int start_rgn, end_rgn;
577 int i, ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000578
Tejun Heo71936182011-12-08 10:22:07 -0800579 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
580 if (ret)
581 return ret;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000582
Tejun Heo71936182011-12-08 10:22:07 -0800583 for (i = end_rgn - 1; i >= start_rgn; i--)
584 memblock_remove_region(type, i);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700585 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000586}
587
Tejun Heo581adcb2011-12-08 10:22:06 -0800588int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000589{
590 return __memblock_remove(&memblock.memory, base, size);
591}
592
Tejun Heo581adcb2011-12-08 10:22:06 -0800593int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000594{
Tejun Heo24aa0782011-07-12 11:16:06 +0200595 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700596 (unsigned long long)base,
597 (unsigned long long)base + size,
598 (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200599
Yinghai Lu95f72d12010-07-12 14:36:09 +1000600 return __memblock_remove(&memblock.reserved, base, size);
601}
602
Tejun Heo581adcb2011-12-08 10:22:06 -0800603int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000604{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000605 struct memblock_type *_rgn = &memblock.reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000606
Tejun Heo24aa0782011-07-12 11:16:06 +0200607 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700608 (unsigned long long)base,
609 (unsigned long long)base + size,
610 (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000611
Tejun Heo7fb0bc32011-12-08 10:22:08 -0800612 return memblock_add_region(_rgn, base, size, MAX_NUMNODES);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000613}
614
Tejun Heo35fd0802011-07-12 11:15:59 +0200615/**
616 * __next_free_mem_range - next function for for_each_free_mem_range()
617 * @idx: pointer to u64 loop variable
618 * @nid: nid: node selector, %MAX_NUMNODES for all nodes
Wanpeng Lidad75572012-06-20 12:53:01 -0700619 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
620 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
621 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo35fd0802011-07-12 11:15:59 +0200622 *
623 * Find the first free area from *@idx which matches @nid, fill the out
624 * parameters, and update *@idx for the next iteration. The lower 32bit of
625 * *@idx contains index into memory region and the upper 32bit indexes the
626 * areas before each reserved region. For example, if reserved regions
627 * look like the following,
628 *
629 * 0:[0-16), 1:[32-48), 2:[128-130)
630 *
631 * The upper 32bit indexes the following regions.
632 *
633 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
634 *
635 * As both region arrays are sorted, the function advances the two indices
636 * in lockstep and returns each intersection.
637 */
638void __init_memblock __next_free_mem_range(u64 *idx, int nid,
639 phys_addr_t *out_start,
640 phys_addr_t *out_end, int *out_nid)
641{
642 struct memblock_type *mem = &memblock.memory;
643 struct memblock_type *rsv = &memblock.reserved;
644 int mi = *idx & 0xffffffff;
645 int ri = *idx >> 32;
646
647 for ( ; mi < mem->cnt; mi++) {
648 struct memblock_region *m = &mem->regions[mi];
649 phys_addr_t m_start = m->base;
650 phys_addr_t m_end = m->base + m->size;
651
652 /* only memory regions are associated with nodes, check it */
653 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
654 continue;
655
656 /* scan areas before each reservation for intersection */
657 for ( ; ri < rsv->cnt + 1; ri++) {
658 struct memblock_region *r = &rsv->regions[ri];
659 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
660 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
661
662 /* if ri advanced past mi, break out to advance mi */
663 if (r_start >= m_end)
664 break;
665 /* if the two regions intersect, we're done */
666 if (m_start < r_end) {
667 if (out_start)
668 *out_start = max(m_start, r_start);
669 if (out_end)
670 *out_end = min(m_end, r_end);
671 if (out_nid)
672 *out_nid = memblock_get_region_node(m);
673 /*
674 * The region which ends first is advanced
675 * for the next iteration.
676 */
677 if (m_end <= r_end)
678 mi++;
679 else
680 ri++;
681 *idx = (u32)mi | (u64)ri << 32;
682 return;
683 }
684 }
685 }
686
687 /* signal end of iteration */
688 *idx = ULLONG_MAX;
689}
690
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800691/**
692 * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
693 * @idx: pointer to u64 loop variable
694 * @nid: nid: node selector, %MAX_NUMNODES for all nodes
Wanpeng Lidad75572012-06-20 12:53:01 -0700695 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
696 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
697 * @out_nid: ptr to int for nid of the range, can be %NULL
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800698 *
699 * Reverse of __next_free_mem_range().
700 */
701void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
702 phys_addr_t *out_start,
703 phys_addr_t *out_end, int *out_nid)
704{
705 struct memblock_type *mem = &memblock.memory;
706 struct memblock_type *rsv = &memblock.reserved;
707 int mi = *idx & 0xffffffff;
708 int ri = *idx >> 32;
709
710 if (*idx == (u64)ULLONG_MAX) {
711 mi = mem->cnt - 1;
712 ri = rsv->cnt;
713 }
714
715 for ( ; mi >= 0; mi--) {
716 struct memblock_region *m = &mem->regions[mi];
717 phys_addr_t m_start = m->base;
718 phys_addr_t m_end = m->base + m->size;
719
720 /* only memory regions are associated with nodes, check it */
721 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
722 continue;
723
724 /* scan areas before each reservation for intersection */
725 for ( ; ri >= 0; ri--) {
726 struct memblock_region *r = &rsv->regions[ri];
727 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
728 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
729
730 /* if ri advanced past mi, break out to advance mi */
731 if (r_end <= m_start)
732 break;
733 /* if the two regions intersect, we're done */
734 if (m_end > r_start) {
735 if (out_start)
736 *out_start = max(m_start, r_start);
737 if (out_end)
738 *out_end = min(m_end, r_end);
739 if (out_nid)
740 *out_nid = memblock_get_region_node(m);
741
742 if (m_start >= r_start)
743 mi--;
744 else
745 ri--;
746 *idx = (u32)mi | (u64)ri << 32;
747 return;
748 }
749 }
750 }
751
752 *idx = ULLONG_MAX;
753}
754
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200755#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
756/*
757 * Common iterator interface used to define for_each_mem_range().
758 */
759void __init_memblock __next_mem_pfn_range(int *idx, int nid,
760 unsigned long *out_start_pfn,
761 unsigned long *out_end_pfn, int *out_nid)
762{
763 struct memblock_type *type = &memblock.memory;
764 struct memblock_region *r;
765
766 while (++*idx < type->cnt) {
767 r = &type->regions[*idx];
768
769 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
770 continue;
771 if (nid == MAX_NUMNODES || nid == r->nid)
772 break;
773 }
774 if (*idx >= type->cnt) {
775 *idx = -1;
776 return;
777 }
778
779 if (out_start_pfn)
780 *out_start_pfn = PFN_UP(r->base);
781 if (out_end_pfn)
782 *out_end_pfn = PFN_DOWN(r->base + r->size);
783 if (out_nid)
784 *out_nid = r->nid;
785}
786
787/**
788 * memblock_set_node - set node ID on memblock regions
789 * @base: base of area to set node ID for
790 * @size: size of area to set node ID for
791 * @nid: node ID to set
792 *
793 * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
794 * Regions which cross the area boundaries are split as necessary.
795 *
796 * RETURNS:
797 * 0 on success, -errno on failure.
798 */
799int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
800 int nid)
801{
802 struct memblock_type *type = &memblock.memory;
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800803 int start_rgn, end_rgn;
804 int i, ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200805
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800806 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
807 if (ret)
808 return ret;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200809
Tejun Heo6a9ceb32011-12-08 10:22:07 -0800810 for (i = start_rgn; i < end_rgn; i++)
Wanpeng Lie9d24ad2012-10-08 16:32:21 -0700811 memblock_set_region_node(&type->regions[i], nid);
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200812
813 memblock_merge_regions(type);
814 return 0;
815}
816#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
817
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800818static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
819 phys_addr_t align, phys_addr_t max_addr,
820 int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000821{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000822 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000823
Tejun Heo847854f2012-02-29 05:56:21 +0900824 /* align @size to avoid excessive fragmentation on reserved array */
825 size = round_up(size, align);
826
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800827 found = memblock_find_in_range_node(0, max_addr, size, align, nid);
Tejun Heo9c8c27e2011-12-08 10:22:06 -0800828 if (found && !memblock_reserve(found, size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000829 return found;
830
831 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000832}
833
Tejun Heo7bd0b0f2011-12-08 10:22:09 -0800834phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
835{
836 return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
837}
838
839phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
840{
841 return memblock_alloc_base_nid(size, align, max_addr, MAX_NUMNODES);
842}
843
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000844phys_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 +1000845{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000846 phys_addr_t alloc;
847
848 alloc = __memblock_alloc_base(size, align, max_addr);
849
850 if (alloc == 0)
851 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
852 (unsigned long long) size, (unsigned long long) max_addr);
853
854 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000855}
856
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000857phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000858{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000859 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000860}
861
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700862phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
863{
864 phys_addr_t res = memblock_alloc_nid(size, align, nid);
865
866 if (res)
867 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +0200868 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000869}
870
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700871
872/*
873 * Remaining API functions
874 */
875
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000876phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000877{
Tejun Heo1440c4e2011-12-08 10:22:08 -0800878 return memblock.memory.total_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000879}
880
Yinghai Lu595ad9a2013-01-24 12:20:09 -0800881phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
882{
883 unsigned long pages = 0;
884 struct memblock_region *r;
885 unsigned long start_pfn, end_pfn;
886
887 for_each_memblock(memory, r) {
888 start_pfn = memblock_region_memory_base_pfn(r);
889 end_pfn = memblock_region_memory_end_pfn(r);
890 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
891 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
892 pages += end_pfn - start_pfn;
893 }
894
895 return (phys_addr_t)pages << PAGE_SHIFT;
896}
897
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -0700898/* lowest address */
899phys_addr_t __init_memblock memblock_start_of_DRAM(void)
900{
901 return memblock.memory.regions[0].base;
902}
903
Yinghai Lu10d06432010-07-28 15:43:02 +1000904phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000905{
906 int idx = memblock.memory.cnt - 1;
907
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000908 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000909}
910
Tejun Heoc0ce8fe2011-12-08 10:22:07 -0800911void __init memblock_enforce_memory_limit(phys_addr_t limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000912{
913 unsigned long i;
Tejun Heoc0ce8fe2011-12-08 10:22:07 -0800914 phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000915
Tejun Heoc0ce8fe2011-12-08 10:22:07 -0800916 if (!limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000917 return;
918
Tejun Heoc0ce8fe2011-12-08 10:22:07 -0800919 /* find out max address */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000920 for (i = 0; i < memblock.memory.cnt; i++) {
Tejun Heoc0ce8fe2011-12-08 10:22:07 -0800921 struct memblock_region *r = &memblock.memory.regions[i];
Yinghai Lu95f72d12010-07-12 14:36:09 +1000922
Tejun Heoc0ce8fe2011-12-08 10:22:07 -0800923 if (limit <= r->size) {
924 max_addr = r->base + limit;
925 break;
926 }
927 limit -= r->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000928 }
929
Tejun Heoc0ce8fe2011-12-08 10:22:07 -0800930 /* truncate both memory and reserved regions */
931 __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
932 __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000933}
934
Yinghai Lucd794812010-10-11 12:34:09 -0700935static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000936{
937 unsigned int left = 0, right = type->cnt;
938
939 do {
940 unsigned int mid = (right + left) / 2;
941
942 if (addr < type->regions[mid].base)
943 right = mid;
944 else if (addr >= (type->regions[mid].base +
945 type->regions[mid].size))
946 left = mid + 1;
947 else
948 return mid;
949 } while (left < right);
950 return -1;
951}
952
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000953int __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000954{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000955 return memblock_search(&memblock.reserved, addr) != -1;
956}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000957
Yinghai Lu3661ca62010-09-15 13:05:29 -0700958int __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000959{
960 return memblock_search(&memblock.memory, addr) != -1;
961}
962
Stephen Boydeab30942012-05-24 00:45:21 -0700963/**
964 * memblock_is_region_memory - check if a region is a subset of memory
965 * @base: base of region to check
966 * @size: size of region to check
967 *
968 * Check if the region [@base, @base+@size) is a subset of a memory block.
969 *
970 * RETURNS:
971 * 0 if false, non-zero if true
972 */
Yinghai Lu3661ca62010-09-15 13:05:29 -0700973int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000974{
Tomi Valkeinenabb65272011-01-20 14:44:20 -0800975 int idx = memblock_search(&memblock.memory, base);
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800976 phys_addr_t end = base + memblock_cap_size(base, &size);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000977
978 if (idx == -1)
979 return 0;
Tomi Valkeinenabb65272011-01-20 14:44:20 -0800980 return memblock.memory.regions[idx].base <= base &&
981 (memblock.memory.regions[idx].base +
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800982 memblock.memory.regions[idx].size) >= end;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000983}
984
Stephen Boydeab30942012-05-24 00:45:21 -0700985/**
986 * memblock_is_region_reserved - check if a region intersects reserved memory
987 * @base: base of region to check
988 * @size: size of region to check
989 *
990 * Check if the region [@base, @base+@size) intersects a reserved memory block.
991 *
992 * RETURNS:
993 * 0 if false, non-zero if true
994 */
Yinghai Lu10d06432010-07-28 15:43:02 +1000995int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000996{
Tejun Heoeb18f1b2011-12-08 10:22:07 -0800997 memblock_cap_size(base, &size);
Benjamin Herrenschmidtf1c2c192010-08-04 14:17:17 +1000998 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000999}
1000
Yinghai Lu6ede1fd2012-10-22 16:35:18 -07001001void __init_memblock memblock_trim_memory(phys_addr_t align)
1002{
1003 int i;
1004 phys_addr_t start, end, orig_start, orig_end;
1005 struct memblock_type *mem = &memblock.memory;
1006
1007 for (i = 0; i < mem->cnt; i++) {
1008 orig_start = mem->regions[i].base;
1009 orig_end = mem->regions[i].base + mem->regions[i].size;
1010 start = round_up(orig_start, align);
1011 end = round_down(orig_end, align);
1012
1013 if (start == orig_start && end == orig_end)
1014 continue;
1015
1016 if (start < end) {
1017 mem->regions[i].base = start;
1018 mem->regions[i].size = end - start;
1019 } else {
1020 memblock_remove_region(mem, i);
1021 i--;
1022 }
1023 }
1024}
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001025
Yinghai Lu3661ca62010-09-15 13:05:29 -07001026void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -07001027{
1028 memblock.current_limit = limit;
1029}
1030
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001031static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001032{
1033 unsigned long long base, size;
1034 int i;
1035
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001036 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001037
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001038 for (i = 0; i < type->cnt; i++) {
1039 struct memblock_region *rgn = &type->regions[i];
1040 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001041
Tejun Heo7c0caeb2011-07-14 11:43:42 +02001042 base = rgn->base;
1043 size = rgn->size;
1044#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1045 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1046 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1047 memblock_get_region_node(rgn));
1048#endif
1049 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
1050 name, i, base, base + size - 1, size, nid_buf);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001051 }
1052}
1053
Tejun Heo4ff7b822011-12-08 10:22:06 -08001054void __init_memblock __memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001055{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001056 pr_info("MEMBLOCK configuration:\n");
Tejun Heo1440c4e2011-12-08 10:22:08 -08001057 pr_info(" memory size = %#llx reserved size = %#llx\n",
1058 (unsigned long long)memblock.memory.total_size,
1059 (unsigned long long)memblock.reserved.total_size);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001060
1061 memblock_dump(&memblock.memory, "memory");
1062 memblock_dump(&memblock.reserved, "reserved");
1063}
1064
Tejun Heo1aadc052011-12-08 10:22:08 -08001065void __init memblock_allow_resize(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001066{
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -07001067 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001068}
1069
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +10001070static int __init early_memblock(char *p)
1071{
1072 if (p && strstr(p, "debug"))
1073 memblock_debug = 1;
1074 return 0;
1075}
1076early_param("memblock", early_memblock);
1077
Tejun Heoc378ddd2011-07-14 11:46:03 +02001078#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -07001079
1080static int memblock_debug_show(struct seq_file *m, void *private)
1081{
1082 struct memblock_type *type = m->private;
1083 struct memblock_region *reg;
1084 int i;
1085
1086 for (i = 0; i < type->cnt; i++) {
1087 reg = &type->regions[i];
1088 seq_printf(m, "%4d: ", i);
1089 if (sizeof(phys_addr_t) == 4)
1090 seq_printf(m, "0x%08lx..0x%08lx\n",
1091 (unsigned long)reg->base,
1092 (unsigned long)(reg->base + reg->size - 1));
1093 else
1094 seq_printf(m, "0x%016llx..0x%016llx\n",
1095 (unsigned long long)reg->base,
1096 (unsigned long long)(reg->base + reg->size - 1));
1097
1098 }
1099 return 0;
1100}
1101
1102static int memblock_debug_open(struct inode *inode, struct file *file)
1103{
1104 return single_open(file, memblock_debug_show, inode->i_private);
1105}
1106
1107static const struct file_operations memblock_debug_fops = {
1108 .open = memblock_debug_open,
1109 .read = seq_read,
1110 .llseek = seq_lseek,
1111 .release = single_release,
1112};
1113
1114static int __init memblock_init_debugfs(void)
1115{
1116 struct dentry *root = debugfs_create_dir("memblock", NULL);
1117 if (!root)
1118 return -ENXIO;
1119 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1120 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
1121
1122 return 0;
1123}
1124__initcall(memblock_init_debugfs);
1125
1126#endif /* CONFIG_DEBUG_FS */