blob: 94803671824563c0fc926937f0b293756853278e [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
Yinghai Lu10d06432010-07-28 15:43:02 +100023struct memblock memblock __initdata_memblock;
Yinghai Lu95f72d12010-07-12 14:36:09 +100024
Yinghai Lu10d06432010-07-28 15:43:02 +100025int memblock_debug __initdata_memblock;
26int memblock_can_resize __initdata_memblock;
27static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1] __initdata_memblock;
28static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1] __initdata_memblock;
Yinghai Lu95f72d12010-07-12 14:36:09 +100029
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -070030/* inline so we don't get a warning when pr_debug is compiled out */
31static inline const char *memblock_type_name(struct memblock_type *type)
32{
33 if (type == &memblock.memory)
34 return "memory";
35 else if (type == &memblock.reserved)
36 return "reserved";
37 else
38 return "unknown";
39}
40
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100041/*
42 * Address comparison utilities
43 */
Yinghai Lu10d06432010-07-28 15:43:02 +100044static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +100045 phys_addr_t base2, phys_addr_t size2)
Yinghai Lu95f72d12010-07-12 14:36:09 +100046{
47 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
48}
49
H Hartley Sweeten2d7d3eb2011-10-31 17:09:15 -070050static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
51 phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100052{
53 unsigned long i;
54
55 for (i = 0; i < type->cnt; i++) {
56 phys_addr_t rgnbase = type->regions[i].base;
57 phys_addr_t rgnsize = type->regions[i].size;
58 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
59 break;
60 }
61
62 return (i < type->cnt) ? i : -1;
63}
64
65/*
66 * Find, allocate, deallocate or reserve unreserved regions. All allocations
67 * are top-down.
68 */
69
Yinghai Lucd794812010-10-11 12:34:09 -070070static phys_addr_t __init_memblock memblock_find_region(phys_addr_t start, phys_addr_t end,
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100071 phys_addr_t size, phys_addr_t align)
72{
73 phys_addr_t base, res_base;
74 long j;
75
Yinghai Luf1af98c2010-10-04 14:57:39 -070076 /* In case, huge size is requested */
77 if (end < size)
Tejun Heo1f5026a2011-07-12 09:58:09 +020078 return 0;
Yinghai Luf1af98c2010-10-04 14:57:39 -070079
Tejun Heo348968e2011-07-12 09:58:08 +020080 base = round_down(end - size, align);
Yinghai Luf1af98c2010-10-04 14:57:39 -070081
Benjamin Herrenschmidt25818f02010-07-28 15:25:10 +100082 /* Prevent allocations returning 0 as it's also used to
83 * indicate an allocation failure
84 */
85 if (start == 0)
86 start = PAGE_SIZE;
87
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100088 while (start <= base) {
89 j = memblock_overlaps_region(&memblock.reserved, base, size);
90 if (j < 0)
91 return base;
92 res_base = memblock.reserved.regions[j].base;
93 if (res_base < size)
94 break;
Tejun Heo348968e2011-07-12 09:58:08 +020095 base = round_down(res_base - size, align);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100096 }
97
Tejun Heo1f5026a2011-07-12 09:58:09 +020098 return 0;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +100099}
100
Tejun Heofc769a82011-07-12 09:58:10 +0200101/*
102 * Find a free area with specified alignment in a specific range.
103 */
104phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, phys_addr_t end,
105 phys_addr_t size, phys_addr_t align)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000106{
107 long i;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000108
109 BUG_ON(0 == size);
110
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000111 /* Pump up max_addr */
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000112 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
113 end = memblock.current_limit;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000114
115 /* We do a top-down search, this tends to limit memory
116 * fragmentation by keeping early boot allocs near the
117 * top of memory
118 */
119 for (i = memblock.memory.cnt - 1; i >= 0; i--) {
120 phys_addr_t memblockbase = memblock.memory.regions[i].base;
121 phys_addr_t memblocksize = memblock.memory.regions[i].size;
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000122 phys_addr_t bottom, top, found;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000123
124 if (memblocksize < size)
125 continue;
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000126 if ((memblockbase + memblocksize) <= start)
127 break;
128 bottom = max(memblockbase, start);
129 top = min(memblockbase + memblocksize, end);
130 if (bottom >= top)
131 continue;
132 found = memblock_find_region(bottom, top, size, align);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200133 if (found)
Benjamin Herrenschmidtfef501d2010-07-12 15:00:34 +1000134 return found;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000135 }
Tejun Heo1f5026a2011-07-12 09:58:09 +0200136 return 0;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000137}
138
Yinghai Lu5303b682010-07-28 15:38:40 +1000139/*
Yinghai Lu7950c402010-08-25 13:39:14 -0700140 * Free memblock.reserved.regions
141 */
142int __init_memblock memblock_free_reserved_regions(void)
143{
144 if (memblock.reserved.regions == memblock_reserved_init_regions)
145 return 0;
146
147 return memblock_free(__pa(memblock.reserved.regions),
148 sizeof(struct memblock_region) * memblock.reserved.max);
149}
150
151/*
152 * Reserve memblock.reserved.regions
153 */
154int __init_memblock memblock_reserve_reserved_regions(void)
155{
156 if (memblock.reserved.regions == memblock_reserved_init_regions)
157 return 0;
158
159 return memblock_reserve(__pa(memblock.reserved.regions),
160 sizeof(struct memblock_region) * memblock.reserved.max);
161}
162
Yinghai Lu10d06432010-07-28 15:43:02 +1000163static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000164{
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200165 memmove(&type->regions[r], &type->regions[r + 1],
166 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000167 type->cnt--;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000168
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700169 /* Special case for empty arrays */
170 if (type->cnt == 0) {
171 type->cnt = 1;
172 type->regions[0].base = 0;
173 type->regions[0].size = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200174 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700175 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000176}
177
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700178/* Defined below but needed now */
Tejun Heo581adcb2011-12-08 10:22:06 -0800179static int memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size);
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700180
Yinghai Lu10d06432010-07-28 15:43:02 +1000181static int __init_memblock memblock_double_array(struct memblock_type *type)
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700182{
183 struct memblock_region *new_array, *old_array;
184 phys_addr_t old_size, new_size, addr;
185 int use_slab = slab_is_available();
186
187 /* We don't allow resizing until we know about the reserved regions
188 * of memory that aren't suitable for allocation
189 */
190 if (!memblock_can_resize)
191 return -1;
192
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700193 /* Calculate new doubled size */
194 old_size = type->max * sizeof(struct memblock_region);
195 new_size = old_size << 1;
196
197 /* Try to find some space for it.
198 *
199 * WARNING: We assume that either slab_is_available() and we use it or
200 * we use MEMBLOCK for allocations. That means that this is unsafe to use
201 * when bootmem is currently active (unless bootmem itself is implemented
202 * on top of MEMBLOCK which isn't the case yet)
203 *
204 * This should however not be an issue for now, as we currently only
205 * call into MEMBLOCK while it's still active, or much later when slab is
206 * active for memory hotplug operations
207 */
208 if (use_slab) {
209 new_array = kmalloc(new_size, GFP_KERNEL);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200210 addr = new_array ? __pa(new_array) : 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700211 } else
Tejun Heofc769a82011-07-12 09:58:10 +0200212 addr = memblock_find_in_range(0, MEMBLOCK_ALLOC_ACCESSIBLE, new_size, sizeof(phys_addr_t));
Tejun Heo1f5026a2011-07-12 09:58:09 +0200213 if (!addr) {
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700214 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
215 memblock_type_name(type), type->max, type->max * 2);
216 return -1;
217 }
218 new_array = __va(addr);
219
Yinghai Luea9e4372010-07-28 15:13:22 +1000220 memblock_dbg("memblock: %s array is doubled to %ld at [%#010llx-%#010llx]",
221 memblock_type_name(type), type->max * 2, (u64)addr, (u64)addr + new_size - 1);
222
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700223 /* Found space, we now need to move the array over before
224 * we add the reserved region since it may be our reserved
225 * array itself that is full.
226 */
227 memcpy(new_array, type->regions, old_size);
228 memset(new_array + type->max, 0, old_size);
229 old_array = type->regions;
230 type->regions = new_array;
231 type->max <<= 1;
232
233 /* If we use SLAB that's it, we are done */
234 if (use_slab)
235 return 0;
236
237 /* Add the new reserved region now. Should not fail ! */
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700238 BUG_ON(memblock_add_region(&memblock.reserved, addr, new_size));
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700239
240 /* If the array wasn't our static init one, then free it. We only do
241 * that before SLAB is available as later on, we don't know whether
242 * to use kfree or free_bootmem_pages(). Shouldn't be a big deal
243 * anyways
244 */
245 if (old_array != memblock_memory_init_regions &&
246 old_array != memblock_reserved_init_regions)
247 memblock_free(__pa(old_array), old_size);
248
249 return 0;
250}
251
Tejun Heo784656f2011-07-12 11:15:55 +0200252/**
253 * memblock_merge_regions - merge neighboring compatible regions
254 * @type: memblock type to scan
255 *
256 * Scan @type and merge neighboring compatible regions.
257 */
258static void __init_memblock memblock_merge_regions(struct memblock_type *type)
259{
260 int i = 0;
261
262 /* cnt never goes below 1 */
263 while (i < type->cnt - 1) {
264 struct memblock_region *this = &type->regions[i];
265 struct memblock_region *next = &type->regions[i + 1];
266
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200267 if (this->base + this->size != next->base ||
268 memblock_get_region_node(this) !=
269 memblock_get_region_node(next)) {
Tejun Heo784656f2011-07-12 11:15:55 +0200270 BUG_ON(this->base + this->size > next->base);
271 i++;
272 continue;
273 }
274
275 this->size += next->size;
276 memmove(next, next + 1, (type->cnt - (i + 1)) * sizeof(*next));
277 type->cnt--;
278 }
279}
280
281/**
282 * memblock_insert_region - insert new memblock region
283 * @type: memblock type to insert into
284 * @idx: index for the insertion point
285 * @base: base address of the new region
286 * @size: size of the new region
287 *
288 * Insert new memblock region [@base,@base+@size) into @type at @idx.
289 * @type must already have extra room to accomodate the new region.
290 */
291static void __init_memblock memblock_insert_region(struct memblock_type *type,
292 int idx, phys_addr_t base,
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200293 phys_addr_t size, int nid)
Tejun Heo784656f2011-07-12 11:15:55 +0200294{
295 struct memblock_region *rgn = &type->regions[idx];
296
297 BUG_ON(type->cnt >= type->max);
298 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
299 rgn->base = base;
300 rgn->size = size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200301 memblock_set_region_node(rgn, nid);
Tejun Heo784656f2011-07-12 11:15:55 +0200302 type->cnt++;
303}
304
305/**
306 * memblock_add_region - add new memblock region
307 * @type: memblock type to add new region into
308 * @base: base address of the new region
309 * @size: size of the new region
310 *
311 * Add new memblock region [@base,@base+@size) into @type. The new region
312 * is allowed to overlap with existing ones - overlaps don't affect already
313 * existing regions. @type is guaranteed to be minimal (all neighbouring
314 * compatible regions are merged) after the addition.
315 *
316 * RETURNS:
317 * 0 on success, -errno on failure.
318 */
Tejun Heo581adcb2011-12-08 10:22:06 -0800319static int __init_memblock memblock_add_region(struct memblock_type *type,
320 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000321{
Tejun Heo784656f2011-07-12 11:15:55 +0200322 bool insert = false;
323 phys_addr_t obase = base, end = base + size;
324 int i, nr_new;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000325
Tejun Heo784656f2011-07-12 11:15:55 +0200326 /* special case for empty array */
327 if (type->regions[0].size == 0) {
328 WARN_ON(type->cnt != 1);
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000329 type->regions[0].base = base;
330 type->regions[0].size = size;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200331 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000332 return 0;
333 }
Tejun Heo784656f2011-07-12 11:15:55 +0200334repeat:
335 /*
336 * The following is executed twice. Once with %false @insert and
337 * then with %true. The first counts the number of regions needed
338 * to accomodate the new area. The second actually inserts them.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700339 */
Tejun Heo784656f2011-07-12 11:15:55 +0200340 base = obase;
341 nr_new = 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000342
Tejun Heo784656f2011-07-12 11:15:55 +0200343 for (i = 0; i < type->cnt; i++) {
344 struct memblock_region *rgn = &type->regions[i];
345 phys_addr_t rbase = rgn->base;
346 phys_addr_t rend = rbase + rgn->size;
347
348 if (rbase >= end)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000349 break;
Tejun Heo784656f2011-07-12 11:15:55 +0200350 if (rend <= base)
351 continue;
352 /*
353 * @rgn overlaps. If it separates the lower part of new
354 * area, insert that portion.
355 */
356 if (rbase > base) {
357 nr_new++;
358 if (insert)
359 memblock_insert_region(type, i++, base,
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200360 rbase - base, MAX_NUMNODES);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000361 }
Tejun Heo784656f2011-07-12 11:15:55 +0200362 /* area below @rend is dealt with, forget about it */
363 base = min(rend, end);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000364 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000365
Tejun Heo784656f2011-07-12 11:15:55 +0200366 /* insert the remaining portion */
367 if (base < end) {
368 nr_new++;
369 if (insert)
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200370 memblock_insert_region(type, i, base, end - base,
371 MAX_NUMNODES);
Tejun Heo784656f2011-07-12 11:15:55 +0200372 }
373
374 /*
375 * If this was the first round, resize array and repeat for actual
376 * insertions; otherwise, merge and return.
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700377 */
Tejun Heo784656f2011-07-12 11:15:55 +0200378 if (!insert) {
379 while (type->cnt + nr_new > type->max)
380 if (memblock_double_array(type) < 0)
381 return -ENOMEM;
382 insert = true;
383 goto repeat;
384 } else {
385 memblock_merge_regions(type);
386 return 0;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700387 }
Yinghai Lu95f72d12010-07-12 14:36:09 +1000388}
389
Tejun Heo581adcb2011-12-08 10:22:06 -0800390int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000391{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000392 return memblock_add_region(&memblock.memory, base, size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000393}
394
Tejun Heo581adcb2011-12-08 10:22:06 -0800395static int __init_memblock __memblock_remove(struct memblock_type *type,
396 phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000397{
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000398 phys_addr_t end = base + size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000399 int i;
400
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700401 /* Walk through the array for collisions */
402 for (i = 0; i < type->cnt; i++) {
403 struct memblock_region *rgn = &type->regions[i];
404 phys_addr_t rend = rgn->base + rgn->size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000405
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700406 /* Nothing more to do, exit */
407 if (rgn->base > end || rgn->size == 0)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000408 break;
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700409
410 /* If we fully enclose the block, drop it */
411 if (base <= rgn->base && end >= rend) {
412 memblock_remove_region(type, i--);
413 continue;
414 }
415
416 /* If we are fully enclosed within a block
417 * then we need to split it and we are done
418 */
419 if (base > rgn->base && end < rend) {
420 rgn->size = base - rgn->base;
421 if (!memblock_add_region(type, end, rend - end))
422 return 0;
423 /* Failure to split is bad, we at least
424 * restore the block before erroring
425 */
426 rgn->size = rend - rgn->base;
427 WARN_ON(1);
428 return -1;
429 }
430
431 /* Check if we need to trim the bottom of a block */
432 if (rgn->base < end && rend > end) {
433 rgn->size -= end - rgn->base;
434 rgn->base = end;
435 break;
436 }
437
438 /* And check if we need to trim the top of a block */
439 if (base < rend)
440 rgn->size -= rend - base;
441
Yinghai Lu95f72d12010-07-12 14:36:09 +1000442 }
Benjamin Herrenschmidt8f7a6602011-03-22 16:33:43 -0700443 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000444}
445
Tejun Heo581adcb2011-12-08 10:22:06 -0800446int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000447{
448 return __memblock_remove(&memblock.memory, base, size);
449}
450
Tejun Heo581adcb2011-12-08 10:22:06 -0800451int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000452{
Tejun Heo24aa0782011-07-12 11:16:06 +0200453 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700454 (unsigned long long)base,
455 (unsigned long long)base + size,
456 (void *)_RET_IP_);
Tejun Heo24aa0782011-07-12 11:16:06 +0200457
Yinghai Lu95f72d12010-07-12 14:36:09 +1000458 return __memblock_remove(&memblock.reserved, base, size);
459}
460
Tejun Heo581adcb2011-12-08 10:22:06 -0800461int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000462{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000463 struct memblock_type *_rgn = &memblock.reserved;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000464
Tejun Heo24aa0782011-07-12 11:16:06 +0200465 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
H. Peter Anvina1504392011-07-14 11:57:10 -0700466 (unsigned long long)base,
467 (unsigned long long)base + size,
468 (void *)_RET_IP_);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000469 BUG_ON(0 == size);
470
471 return memblock_add_region(_rgn, base, size);
472}
473
Tejun Heo35fd0802011-07-12 11:15:59 +0200474/**
475 * __next_free_mem_range - next function for for_each_free_mem_range()
476 * @idx: pointer to u64 loop variable
477 * @nid: nid: node selector, %MAX_NUMNODES for all nodes
478 * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
479 * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
480 * @p_nid: ptr to int for nid of the range, can be %NULL
481 *
482 * Find the first free area from *@idx which matches @nid, fill the out
483 * parameters, and update *@idx for the next iteration. The lower 32bit of
484 * *@idx contains index into memory region and the upper 32bit indexes the
485 * areas before each reserved region. For example, if reserved regions
486 * look like the following,
487 *
488 * 0:[0-16), 1:[32-48), 2:[128-130)
489 *
490 * The upper 32bit indexes the following regions.
491 *
492 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
493 *
494 * As both region arrays are sorted, the function advances the two indices
495 * in lockstep and returns each intersection.
496 */
497void __init_memblock __next_free_mem_range(u64 *idx, int nid,
498 phys_addr_t *out_start,
499 phys_addr_t *out_end, int *out_nid)
500{
501 struct memblock_type *mem = &memblock.memory;
502 struct memblock_type *rsv = &memblock.reserved;
503 int mi = *idx & 0xffffffff;
504 int ri = *idx >> 32;
505
506 for ( ; mi < mem->cnt; mi++) {
507 struct memblock_region *m = &mem->regions[mi];
508 phys_addr_t m_start = m->base;
509 phys_addr_t m_end = m->base + m->size;
510
511 /* only memory regions are associated with nodes, check it */
512 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
513 continue;
514
515 /* scan areas before each reservation for intersection */
516 for ( ; ri < rsv->cnt + 1; ri++) {
517 struct memblock_region *r = &rsv->regions[ri];
518 phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
519 phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
520
521 /* if ri advanced past mi, break out to advance mi */
522 if (r_start >= m_end)
523 break;
524 /* if the two regions intersect, we're done */
525 if (m_start < r_end) {
526 if (out_start)
527 *out_start = max(m_start, r_start);
528 if (out_end)
529 *out_end = min(m_end, r_end);
530 if (out_nid)
531 *out_nid = memblock_get_region_node(m);
532 /*
533 * The region which ends first is advanced
534 * for the next iteration.
535 */
536 if (m_end <= r_end)
537 mi++;
538 else
539 ri++;
540 *idx = (u32)mi | (u64)ri << 32;
541 return;
542 }
543 }
544 }
545
546 /* signal end of iteration */
547 *idx = ULLONG_MAX;
548}
549
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200550#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
551/*
552 * Common iterator interface used to define for_each_mem_range().
553 */
554void __init_memblock __next_mem_pfn_range(int *idx, int nid,
555 unsigned long *out_start_pfn,
556 unsigned long *out_end_pfn, int *out_nid)
557{
558 struct memblock_type *type = &memblock.memory;
559 struct memblock_region *r;
560
561 while (++*idx < type->cnt) {
562 r = &type->regions[*idx];
563
564 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
565 continue;
566 if (nid == MAX_NUMNODES || nid == r->nid)
567 break;
568 }
569 if (*idx >= type->cnt) {
570 *idx = -1;
571 return;
572 }
573
574 if (out_start_pfn)
575 *out_start_pfn = PFN_UP(r->base);
576 if (out_end_pfn)
577 *out_end_pfn = PFN_DOWN(r->base + r->size);
578 if (out_nid)
579 *out_nid = r->nid;
580}
581
582/**
583 * memblock_set_node - set node ID on memblock regions
584 * @base: base of area to set node ID for
585 * @size: size of area to set node ID for
586 * @nid: node ID to set
587 *
588 * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
589 * Regions which cross the area boundaries are split as necessary.
590 *
591 * RETURNS:
592 * 0 on success, -errno on failure.
593 */
594int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
595 int nid)
596{
597 struct memblock_type *type = &memblock.memory;
598 phys_addr_t end = base + size;
599 int i;
600
601 /* we'll create at most two more regions */
602 while (type->cnt + 2 > type->max)
603 if (memblock_double_array(type) < 0)
604 return -ENOMEM;
605
606 for (i = 0; i < type->cnt; i++) {
607 struct memblock_region *rgn = &type->regions[i];
608 phys_addr_t rbase = rgn->base;
609 phys_addr_t rend = rbase + rgn->size;
610
611 if (rbase >= end)
612 break;
613 if (rend <= base)
614 continue;
615
616 if (rbase < base) {
617 /*
618 * @rgn intersects from below. Split and continue
619 * to process the next region - the new top half.
620 */
621 rgn->base = base;
622 rgn->size = rend - rgn->base;
623 memblock_insert_region(type, i, rbase, base - rbase,
624 rgn->nid);
625 } else if (rend > end) {
626 /*
627 * @rgn intersects from above. Split and redo the
628 * current region - the new bottom half.
629 */
630 rgn->base = end;
631 rgn->size = rend - rgn->base;
632 memblock_insert_region(type, i--, rbase, end - rbase,
633 rgn->nid);
634 } else {
635 /* @rgn is fully contained, set ->nid */
636 rgn->nid = nid;
637 }
638 }
639
640 memblock_merge_regions(type);
641 return 0;
642}
643#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
644
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000645phys_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 +1000646{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000647 phys_addr_t found;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000648
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000649 /* We align the size to limit fragmentation. Without this, a lot of
650 * small allocs quickly eat up the whole reserve array on sparc
651 */
Tejun Heo348968e2011-07-12 09:58:08 +0200652 size = round_up(size, align);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000653
Tejun Heofc769a82011-07-12 09:58:10 +0200654 found = memblock_find_in_range(0, max_addr, size, align);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200655 if (found && !memblock_add_region(&memblock.reserved, found, size))
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000656 return found;
657
658 return 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000659}
660
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000661phys_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 +1000662{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000663 phys_addr_t alloc;
664
665 alloc = __memblock_alloc_base(size, align, max_addr);
666
667 if (alloc == 0)
668 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
669 (unsigned long long) size, (unsigned long long) max_addr);
670
671 return alloc;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000672}
673
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000674phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000675{
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000676 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000677}
678
Yinghai Lu95f72d12010-07-12 14:36:09 +1000679
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000680/*
Tejun Heo34e18452011-07-12 10:46:33 +0200681 * Additional node-local top-down allocators.
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -0700682 *
683 * WARNING: Only available after early_node_map[] has been populated,
684 * on some architectures, that is after all the calls to add_active_range()
685 * have been done to populate it.
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000686 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000687
Tejun Heo34e18452011-07-12 10:46:33 +0200688static phys_addr_t __init memblock_nid_range_rev(phys_addr_t start,
689 phys_addr_t end, int *nid)
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700690{
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -0700691#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -0700692 unsigned long start_pfn, end_pfn;
693 int i;
694
Tejun Heob2fea982011-07-12 10:46:31 +0200695 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, nid)
Tejun Heo34e18452011-07-12 10:46:33 +0200696 if (end > PFN_PHYS(start_pfn) && end <= PFN_PHYS(end_pfn))
697 return max(start, PFN_PHYS(start_pfn));
Benjamin Herrenschmidtc196f762010-07-06 15:39:16 -0700698#endif
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700699 *nid = 0;
Tejun Heo34e18452011-07-12 10:46:33 +0200700 return start;
Benjamin Herrenschmidtc3f72b52010-07-06 15:38:59 -0700701}
702
Tejun Heoe6498042011-07-12 10:46:34 +0200703phys_addr_t __init memblock_find_in_range_node(phys_addr_t start,
704 phys_addr_t end,
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000705 phys_addr_t size,
706 phys_addr_t align, int nid)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000707{
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000708 struct memblock_type *mem = &memblock.memory;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000709 int i;
710
711 BUG_ON(0 == size);
712
Tejun Heoe6498042011-07-12 10:46:34 +0200713 /* Pump up max_addr */
714 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
715 end = memblock.current_limit;
716
717 for (i = mem->cnt - 1; i >= 0; i--) {
718 struct memblock_region *r = &mem->regions[i];
719 phys_addr_t base = max(start, r->base);
720 phys_addr_t top = min(end, r->base + r->size);
721
722 while (base < top) {
723 phys_addr_t tbase, ret;
724 int tnid;
725
726 tbase = memblock_nid_range_rev(base, top, &tnid);
727 if (nid == MAX_NUMNODES || tnid == nid) {
728 ret = memblock_find_region(tbase, top, size, align);
729 if (ret)
730 return ret;
731 }
732 top = tbase;
733 }
734 }
735
736 return 0;
737}
738
739phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
740{
741 phys_addr_t found;
742
743 /*
744 * We align the size to limit fragmentation. Without this, a lot of
Benjamin Herrenschmidt7f219c72010-07-12 14:24:57 +1000745 * small allocs quickly eat up the whole reserve array on sparc
746 */
Tejun Heo348968e2011-07-12 09:58:08 +0200747 size = round_up(size, align);
Benjamin Herrenschmidt7f219c72010-07-12 14:24:57 +1000748
Tejun Heoe6498042011-07-12 10:46:34 +0200749 found = memblock_find_in_range_node(0, MEMBLOCK_ALLOC_ACCESSIBLE,
750 size, align, nid);
751 if (found && !memblock_add_region(&memblock.reserved, found, size))
752 return found;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000753
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700754 return 0;
755}
756
757phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
758{
759 phys_addr_t res = memblock_alloc_nid(size, align, nid);
760
761 if (res)
762 return res;
Tejun Heo15fb0972011-07-12 09:58:07 +0200763 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000764}
765
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700766
767/*
768 * Remaining API functions
769 */
770
Yinghai Lu95f72d12010-07-12 14:36:09 +1000771/* You must call memblock_analyze() before this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000772phys_addr_t __init memblock_phys_mem_size(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000773{
Benjamin Herrenschmidt4734b592010-07-28 14:31:29 +1000774 return memblock.memory_size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000775}
776
Sam Ravnborg0a93ebe2011-10-31 17:08:16 -0700777/* lowest address */
778phys_addr_t __init_memblock memblock_start_of_DRAM(void)
779{
780 return memblock.memory.regions[0].base;
781}
782
Yinghai Lu10d06432010-07-28 15:43:02 +1000783phys_addr_t __init_memblock memblock_end_of_DRAM(void)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000784{
785 int idx = memblock.memory.cnt - 1;
786
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000787 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000788}
789
790/* You must call memblock_analyze() after this. */
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000791void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000792{
793 unsigned long i;
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000794 phys_addr_t limit;
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000795 struct memblock_region *p;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000796
797 if (!memory_limit)
798 return;
799
800 /* Truncate the memblock regions to satisfy the memory limit. */
801 limit = memory_limit;
802 for (i = 0; i < memblock.memory.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000803 if (limit > memblock.memory.regions[i].size) {
804 limit -= memblock.memory.regions[i].size;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000805 continue;
806 }
807
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000808 memblock.memory.regions[i].size = limit;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000809 memblock.memory.cnt = i + 1;
810 break;
811 }
812
Yinghai Lu95f72d12010-07-12 14:36:09 +1000813 memory_limit = memblock_end_of_DRAM();
814
815 /* And truncate any reserves above the limit also. */
816 for (i = 0; i < memblock.reserved.cnt; i++) {
Benjamin Herrenschmidte3239ff2010-08-04 14:06:41 +1000817 p = &memblock.reserved.regions[i];
Yinghai Lu95f72d12010-07-12 14:36:09 +1000818
819 if (p->base > memory_limit)
820 p->size = 0;
821 else if ((p->base + p->size) > memory_limit)
822 p->size = memory_limit - p->base;
823
824 if (p->size == 0) {
825 memblock_remove_region(&memblock.reserved, i);
826 i--;
827 }
828 }
829}
830
Yinghai Lucd794812010-10-11 12:34:09 -0700831static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000832{
833 unsigned int left = 0, right = type->cnt;
834
835 do {
836 unsigned int mid = (right + left) / 2;
837
838 if (addr < type->regions[mid].base)
839 right = mid;
840 else if (addr >= (type->regions[mid].base +
841 type->regions[mid].size))
842 left = mid + 1;
843 else
844 return mid;
845 } while (left < right);
846 return -1;
847}
848
Benjamin Herrenschmidt2898cc42010-08-04 13:34:42 +1000849int __init memblock_is_reserved(phys_addr_t addr)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000850{
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000851 return memblock_search(&memblock.reserved, addr) != -1;
852}
Yinghai Lu95f72d12010-07-12 14:36:09 +1000853
Yinghai Lu3661ca62010-09-15 13:05:29 -0700854int __init_memblock memblock_is_memory(phys_addr_t addr)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000855{
856 return memblock_search(&memblock.memory, addr) != -1;
857}
858
Yinghai Lu3661ca62010-09-15 13:05:29 -0700859int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000860{
Tomi Valkeinenabb65272011-01-20 14:44:20 -0800861 int idx = memblock_search(&memblock.memory, base);
Benjamin Herrenschmidt72d4b0b2010-08-04 14:38:47 +1000862
863 if (idx == -1)
864 return 0;
Tomi Valkeinenabb65272011-01-20 14:44:20 -0800865 return memblock.memory.regions[idx].base <= base &&
866 (memblock.memory.regions[idx].base +
867 memblock.memory.regions[idx].size) >= (base + size);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000868}
869
Yinghai Lu10d06432010-07-28 15:43:02 +1000870int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
Yinghai Lu95f72d12010-07-12 14:36:09 +1000871{
Benjamin Herrenschmidtf1c2c192010-08-04 14:17:17 +1000872 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
Yinghai Lu95f72d12010-07-12 14:36:09 +1000873}
874
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700875
Yinghai Lu3661ca62010-09-15 13:05:29 -0700876void __init_memblock memblock_set_current_limit(phys_addr_t limit)
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700877{
878 memblock.current_limit = limit;
879}
880
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200881static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000882{
883 unsigned long long base, size;
884 int i;
885
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200886 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000887
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200888 for (i = 0; i < type->cnt; i++) {
889 struct memblock_region *rgn = &type->regions[i];
890 char nid_buf[32] = "";
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000891
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200892 base = rgn->base;
893 size = rgn->size;
894#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
895 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
896 snprintf(nid_buf, sizeof(nid_buf), " on node %d",
897 memblock_get_region_node(rgn));
898#endif
899 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
900 name, i, base, base + size - 1, size, nid_buf);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000901 }
902}
903
Yinghai Lu10d06432010-07-28 15:43:02 +1000904void __init_memblock memblock_dump_all(void)
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000905{
906 if (!memblock_debug)
907 return;
908
909 pr_info("MEMBLOCK configuration:\n");
910 pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
911
912 memblock_dump(&memblock.memory, "memory");
913 memblock_dump(&memblock.reserved, "reserved");
914}
915
916void __init memblock_analyze(void)
917{
918 int i;
919
920 /* Check marker in the unused last array entry */
921 WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
Andrew Mortonc9d8c3d2011-07-25 17:12:18 -0700922 != MEMBLOCK_INACTIVE);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000923 WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
Andrew Mortonc9d8c3d2011-07-25 17:12:18 -0700924 != MEMBLOCK_INACTIVE);
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000925
926 memblock.memory_size = 0;
927
928 for (i = 0; i < memblock.memory.cnt; i++)
929 memblock.memory_size += memblock.memory.regions[i].size;
Benjamin Herrenschmidt142b45a2010-07-06 15:39:13 -0700930
931 /* We allow resizing from there */
932 memblock_can_resize = 1;
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000933}
934
Benjamin Herrenschmidt7590abe2010-07-06 15:39:10 -0700935void __init memblock_init(void)
936{
Jeremy Fitzhardinge236260b2010-10-06 15:52:29 -0700937 static int init_done __initdata = 0;
938
939 if (init_done)
940 return;
941 init_done = 1;
942
Benjamin Herrenschmidt7590abe2010-07-06 15:39:10 -0700943 /* Hookup the initial arrays */
944 memblock.memory.regions = memblock_memory_init_regions;
945 memblock.memory.max = INIT_MEMBLOCK_REGIONS;
946 memblock.reserved.regions = memblock_reserved_init_regions;
947 memblock.reserved.max = INIT_MEMBLOCK_REGIONS;
948
949 /* Write a marker in the unused last array entry */
Andrew Mortonc9d8c3d2011-07-25 17:12:18 -0700950 memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = MEMBLOCK_INACTIVE;
951 memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = MEMBLOCK_INACTIVE;
Benjamin Herrenschmidt7590abe2010-07-06 15:39:10 -0700952
953 /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
954 * This simplifies the memblock_add() code below...
955 */
956 memblock.memory.regions[0].base = 0;
957 memblock.memory.regions[0].size = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200958 memblock_set_region_node(&memblock.memory.regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt7590abe2010-07-06 15:39:10 -0700959 memblock.memory.cnt = 1;
960
961 /* Ditto. */
962 memblock.reserved.regions[0].base = 0;
963 memblock.reserved.regions[0].size = 0;
Tejun Heo7c0caeb2011-07-14 11:43:42 +0200964 memblock_set_region_node(&memblock.reserved.regions[0], MAX_NUMNODES);
Benjamin Herrenschmidt7590abe2010-07-06 15:39:10 -0700965 memblock.reserved.cnt = 1;
966
967 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
968}
969
Benjamin Herrenschmidt6ed311b2010-07-12 14:36:48 +1000970static int __init early_memblock(char *p)
971{
972 if (p && strstr(p, "debug"))
973 memblock_debug = 1;
974 return 0;
975}
976early_param("memblock", early_memblock);
977
Tejun Heoc378ddd2011-07-14 11:46:03 +0200978#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
Benjamin Herrenschmidt6d03b882010-07-06 15:39:19 -0700979
980static int memblock_debug_show(struct seq_file *m, void *private)
981{
982 struct memblock_type *type = m->private;
983 struct memblock_region *reg;
984 int i;
985
986 for (i = 0; i < type->cnt; i++) {
987 reg = &type->regions[i];
988 seq_printf(m, "%4d: ", i);
989 if (sizeof(phys_addr_t) == 4)
990 seq_printf(m, "0x%08lx..0x%08lx\n",
991 (unsigned long)reg->base,
992 (unsigned long)(reg->base + reg->size - 1));
993 else
994 seq_printf(m, "0x%016llx..0x%016llx\n",
995 (unsigned long long)reg->base,
996 (unsigned long long)(reg->base + reg->size - 1));
997
998 }
999 return 0;
1000}
1001
1002static int memblock_debug_open(struct inode *inode, struct file *file)
1003{
1004 return single_open(file, memblock_debug_show, inode->i_private);
1005}
1006
1007static const struct file_operations memblock_debug_fops = {
1008 .open = memblock_debug_open,
1009 .read = seq_read,
1010 .llseek = seq_lseek,
1011 .release = single_release,
1012};
1013
1014static int __init memblock_init_debugfs(void)
1015{
1016 struct dentry *root = debugfs_create_dir("memblock", NULL);
1017 if (!root)
1018 return -ENXIO;
1019 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1020 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
1021
1022 return 0;
1023}
1024__initcall(memblock_init_debugfs);
1025
1026#endif /* CONFIG_DEBUG_FS */