]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/memblock.c
memblock: Export MEMBLOCK_ERROR
[net-next-2.6.git] / mm / memblock.c
CommitLineData
95f72d1e
YL
1/*
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>
142b45a7 14#include <linux/slab.h>
95f72d1e
YL
15#include <linux/init.h>
16#include <linux/bitops.h>
449e8df3 17#include <linux/poison.h>
c196f76f 18#include <linux/pfn.h>
6d03b885
BH
19#include <linux/debugfs.h>
20#include <linux/seq_file.h>
95f72d1e
YL
21#include <linux/memblock.h>
22
95f72d1e
YL
23struct memblock memblock;
24
5e63cf43
YL
25int memblock_debug;
26int memblock_can_resize;
bf23c51f
BH
27static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS + 1];
28static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS + 1];
95f72d1e 29
142b45a7
BH
30/* 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
6ed311b2
BH
41/*
42 * Address comparison utilities
43 */
95f72d1e 44
6ed311b2 45static phys_addr_t memblock_align_down(phys_addr_t addr, phys_addr_t size)
95f72d1e 46{
6ed311b2 47 return addr & ~(size - 1);
95f72d1e
YL
48}
49
6ed311b2 50static phys_addr_t memblock_align_up(phys_addr_t addr, phys_addr_t size)
95f72d1e 51{
6ed311b2 52 return (addr + (size - 1)) & ~(size - 1);
95f72d1e
YL
53}
54
2898cc4c
BH
55static unsigned long memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
56 phys_addr_t base2, phys_addr_t size2)
95f72d1e
YL
57{
58 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
59}
60
2898cc4c
BH
61static long memblock_addrs_adjacent(phys_addr_t base1, phys_addr_t size1,
62 phys_addr_t base2, phys_addr_t size2)
95f72d1e
YL
63{
64 if (base2 == base1 + size1)
65 return 1;
66 else if (base1 == base2 + size2)
67 return -1;
68
69 return 0;
70}
71
e3239ff9 72static long memblock_regions_adjacent(struct memblock_type *type,
2898cc4c 73 unsigned long r1, unsigned long r2)
95f72d1e 74{
2898cc4c
BH
75 phys_addr_t base1 = type->regions[r1].base;
76 phys_addr_t size1 = type->regions[r1].size;
77 phys_addr_t base2 = type->regions[r2].base;
78 phys_addr_t size2 = type->regions[r2].size;
95f72d1e
YL
79
80 return memblock_addrs_adjacent(base1, size1, base2, size2);
81}
82
6ed311b2
BH
83long memblock_overlaps_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
84{
85 unsigned long i;
86
87 for (i = 0; i < type->cnt; i++) {
88 phys_addr_t rgnbase = type->regions[i].base;
89 phys_addr_t rgnsize = type->regions[i].size;
90 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
91 break;
92 }
93
94 return (i < type->cnt) ? i : -1;
95}
96
97/*
98 * Find, allocate, deallocate or reserve unreserved regions. All allocations
99 * are top-down.
100 */
101
102static phys_addr_t __init memblock_find_region(phys_addr_t start, phys_addr_t end,
103 phys_addr_t size, phys_addr_t align)
104{
105 phys_addr_t base, res_base;
106 long j;
107
108 base = memblock_align_down((end - size), align);
109 while (start <= base) {
110 j = memblock_overlaps_region(&memblock.reserved, base, size);
111 if (j < 0)
112 return base;
113 res_base = memblock.reserved.regions[j].base;
114 if (res_base < size)
115 break;
116 base = memblock_align_down(res_base - size, align);
117 }
118
119 return MEMBLOCK_ERROR;
120}
121
fef501d4
BH
122static phys_addr_t __init memblock_find_base(phys_addr_t size, phys_addr_t align,
123 phys_addr_t start, phys_addr_t end)
6ed311b2
BH
124{
125 long i;
6ed311b2
BH
126
127 BUG_ON(0 == size);
128
129 size = memblock_align_up(size, align);
130
131 /* Pump up max_addr */
fef501d4
BH
132 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
133 end = memblock.current_limit;
6ed311b2
BH
134
135 /* We do a top-down search, this tends to limit memory
136 * fragmentation by keeping early boot allocs near the
137 * top of memory
138 */
139 for (i = memblock.memory.cnt - 1; i >= 0; i--) {
140 phys_addr_t memblockbase = memblock.memory.regions[i].base;
141 phys_addr_t memblocksize = memblock.memory.regions[i].size;
fef501d4 142 phys_addr_t bottom, top, found;
6ed311b2
BH
143
144 if (memblocksize < size)
145 continue;
fef501d4
BH
146 if ((memblockbase + memblocksize) <= start)
147 break;
148 bottom = max(memblockbase, start);
149 top = min(memblockbase + memblocksize, end);
150 if (bottom >= top)
151 continue;
152 found = memblock_find_region(bottom, top, size, align);
153 if (found != MEMBLOCK_ERROR)
154 return found;
6ed311b2
BH
155 }
156 return MEMBLOCK_ERROR;
157}
158
e3239ff9 159static void memblock_remove_region(struct memblock_type *type, unsigned long r)
95f72d1e
YL
160{
161 unsigned long i;
162
e3239ff9
BH
163 for (i = r; i < type->cnt - 1; i++) {
164 type->regions[i].base = type->regions[i + 1].base;
165 type->regions[i].size = type->regions[i + 1].size;
95f72d1e 166 }
e3239ff9 167 type->cnt--;
95f72d1e
YL
168}
169
170/* Assumption: base addr of region 1 < base addr of region 2 */
e3239ff9 171static void memblock_coalesce_regions(struct memblock_type *type,
95f72d1e
YL
172 unsigned long r1, unsigned long r2)
173{
e3239ff9
BH
174 type->regions[r1].size += type->regions[r2].size;
175 memblock_remove_region(type, r2);
95f72d1e
YL
176}
177
142b45a7
BH
178/* Defined below but needed now */
179static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size);
180
181static int memblock_double_array(struct memblock_type *type)
182{
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
142b45a7
BH
193 /* 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);
210 addr = new_array == NULL ? MEMBLOCK_ERROR : __pa(new_array);
211 } else
fef501d4 212 addr = memblock_find_base(new_size, sizeof(phys_addr_t), 0, MEMBLOCK_ALLOC_ACCESSIBLE);
142b45a7
BH
213 if (addr == MEMBLOCK_ERROR) {
214 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
ea9e4376
YL
220 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
142b45a7
BH
223 /* 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 ! */
238 BUG_ON(memblock_add_region(&memblock.reserved, addr, new_size) < 0);
239
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
d2cd563b
BH
252extern int __weak memblock_memory_can_coalesce(phys_addr_t addr1, phys_addr_t size1,
253 phys_addr_t addr2, phys_addr_t size2)
254{
255 return 1;
256}
257
2898cc4c 258static long memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
95f72d1e
YL
259{
260 unsigned long coalesced = 0;
261 long adjacent, i;
262
e3239ff9
BH
263 if ((type->cnt == 1) && (type->regions[0].size == 0)) {
264 type->regions[0].base = base;
265 type->regions[0].size = size;
95f72d1e
YL
266 return 0;
267 }
268
269 /* First try and coalesce this MEMBLOCK with another. */
e3239ff9 270 for (i = 0; i < type->cnt; i++) {
2898cc4c
BH
271 phys_addr_t rgnbase = type->regions[i].base;
272 phys_addr_t rgnsize = type->regions[i].size;
95f72d1e
YL
273
274 if ((rgnbase == base) && (rgnsize == size))
275 /* Already have this region, so we're done */
276 return 0;
277
278 adjacent = memblock_addrs_adjacent(base, size, rgnbase, rgnsize);
d2cd563b
BH
279 /* Check if arch allows coalescing */
280 if (adjacent != 0 && type == &memblock.memory &&
281 !memblock_memory_can_coalesce(base, size, rgnbase, rgnsize))
282 break;
95f72d1e 283 if (adjacent > 0) {
e3239ff9
BH
284 type->regions[i].base -= size;
285 type->regions[i].size += size;
95f72d1e
YL
286 coalesced++;
287 break;
288 } else if (adjacent < 0) {
e3239ff9 289 type->regions[i].size += size;
95f72d1e
YL
290 coalesced++;
291 break;
292 }
293 }
294
d2cd563b
BH
295 /* If we plugged a hole, we may want to also coalesce with the
296 * next region
297 */
298 if ((i < type->cnt - 1) && memblock_regions_adjacent(type, i, i+1) &&
299 ((type != &memblock.memory || memblock_memory_can_coalesce(type->regions[i].base,
300 type->regions[i].size,
301 type->regions[i+1].base,
302 type->regions[i+1].size)))) {
e3239ff9 303 memblock_coalesce_regions(type, i, i+1);
95f72d1e
YL
304 coalesced++;
305 }
306
307 if (coalesced)
308 return coalesced;
142b45a7
BH
309
310 /* If we are out of space, we fail. It's too late to resize the array
311 * but then this shouldn't have happened in the first place.
312 */
313 if (WARN_ON(type->cnt >= type->max))
95f72d1e
YL
314 return -1;
315
316 /* Couldn't coalesce the MEMBLOCK, so add it to the sorted table. */
e3239ff9
BH
317 for (i = type->cnt - 1; i >= 0; i--) {
318 if (base < type->regions[i].base) {
319 type->regions[i+1].base = type->regions[i].base;
320 type->regions[i+1].size = type->regions[i].size;
95f72d1e 321 } else {
e3239ff9
BH
322 type->regions[i+1].base = base;
323 type->regions[i+1].size = size;
95f72d1e
YL
324 break;
325 }
326 }
327
e3239ff9
BH
328 if (base < type->regions[0].base) {
329 type->regions[0].base = base;
330 type->regions[0].size = size;
95f72d1e 331 }
e3239ff9 332 type->cnt++;
95f72d1e 333
142b45a7
BH
334 /* The array is full ? Try to resize it. If that fails, we undo
335 * our allocation and return an error
336 */
337 if (type->cnt == type->max && memblock_double_array(type)) {
338 type->cnt--;
339 return -1;
340 }
341
95f72d1e
YL
342 return 0;
343}
344
2898cc4c 345long memblock_add(phys_addr_t base, phys_addr_t size)
95f72d1e 346{
e3239ff9 347 return memblock_add_region(&memblock.memory, base, size);
95f72d1e
YL
348
349}
350
2898cc4c 351static long __memblock_remove(struct memblock_type *type, phys_addr_t base, phys_addr_t size)
95f72d1e 352{
2898cc4c
BH
353 phys_addr_t rgnbegin, rgnend;
354 phys_addr_t end = base + size;
95f72d1e
YL
355 int i;
356
357 rgnbegin = rgnend = 0; /* supress gcc warnings */
358
359 /* Find the region where (base, size) belongs to */
e3239ff9
BH
360 for (i=0; i < type->cnt; i++) {
361 rgnbegin = type->regions[i].base;
362 rgnend = rgnbegin + type->regions[i].size;
95f72d1e
YL
363
364 if ((rgnbegin <= base) && (end <= rgnend))
365 break;
366 }
367
368 /* Didn't find the region */
e3239ff9 369 if (i == type->cnt)
95f72d1e
YL
370 return -1;
371
372 /* Check to see if we are removing entire region */
373 if ((rgnbegin == base) && (rgnend == end)) {
e3239ff9 374 memblock_remove_region(type, i);
95f72d1e
YL
375 return 0;
376 }
377
378 /* Check to see if region is matching at the front */
379 if (rgnbegin == base) {
e3239ff9
BH
380 type->regions[i].base = end;
381 type->regions[i].size -= size;
95f72d1e
YL
382 return 0;
383 }
384
385 /* Check to see if the region is matching at the end */
386 if (rgnend == end) {
e3239ff9 387 type->regions[i].size -= size;
95f72d1e
YL
388 return 0;
389 }
390
391 /*
392 * We need to split the entry - adjust the current one to the
393 * beginging of the hole and add the region after hole.
394 */
e3239ff9
BH
395 type->regions[i].size = base - type->regions[i].base;
396 return memblock_add_region(type, end, rgnend - end);
95f72d1e
YL
397}
398
2898cc4c 399long memblock_remove(phys_addr_t base, phys_addr_t size)
95f72d1e
YL
400{
401 return __memblock_remove(&memblock.memory, base, size);
402}
403
2898cc4c 404long __init memblock_free(phys_addr_t base, phys_addr_t size)
95f72d1e
YL
405{
406 return __memblock_remove(&memblock.reserved, base, size);
407}
408
2898cc4c 409long __init memblock_reserve(phys_addr_t base, phys_addr_t size)
95f72d1e 410{
e3239ff9 411 struct memblock_type *_rgn = &memblock.reserved;
95f72d1e
YL
412
413 BUG_ON(0 == size);
414
415 return memblock_add_region(_rgn, base, size);
416}
417
6ed311b2 418phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
95f72d1e 419{
6ed311b2 420 phys_addr_t found;
95f72d1e 421
6ed311b2
BH
422 /* We align the size to limit fragmentation. Without this, a lot of
423 * small allocs quickly eat up the whole reserve array on sparc
424 */
425 size = memblock_align_up(size, align);
95f72d1e 426
fef501d4 427 found = memblock_find_base(size, align, 0, max_addr);
6ed311b2
BH
428 if (found != MEMBLOCK_ERROR &&
429 memblock_add_region(&memblock.reserved, found, size) >= 0)
430 return found;
95f72d1e 431
6ed311b2 432 return 0;
95f72d1e
YL
433}
434
6ed311b2 435phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
95f72d1e 436{
6ed311b2
BH
437 phys_addr_t alloc;
438
439 alloc = __memblock_alloc_base(size, align, max_addr);
440
441 if (alloc == 0)
442 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
443 (unsigned long long) size, (unsigned long long) max_addr);
444
445 return alloc;
95f72d1e
YL
446}
447
6ed311b2 448phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
95f72d1e 449{
6ed311b2
BH
450 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
451}
95f72d1e 452
95f72d1e 453
6ed311b2
BH
454/*
455 * Additional node-local allocators. Search for node memory is bottom up
456 * and walks memblock regions within that node bottom-up as well, but allocation
c196f76f
BH
457 * within an memblock region is top-down. XXX I plan to fix that at some stage
458 *
459 * WARNING: Only available after early_node_map[] has been populated,
460 * on some architectures, that is after all the calls to add_active_range()
461 * have been done to populate it.
6ed311b2 462 */
95f72d1e 463
2898cc4c 464phys_addr_t __weak __init memblock_nid_range(phys_addr_t start, phys_addr_t end, int *nid)
c3f72b57 465{
c196f76f
BH
466#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
467 /*
468 * This code originates from sparc which really wants use to walk by addresses
469 * and returns the nid. This is not very convenient for early_pfn_map[] users
470 * as the map isn't sorted yet, and it really wants to be walked by nid.
471 *
472 * For now, I implement the inefficient method below which walks the early
473 * map multiple times. Eventually we may want to use an ARCH config option
474 * to implement a completely different method for both case.
475 */
476 unsigned long start_pfn, end_pfn;
477 int i;
478
479 for (i = 0; i < MAX_NUMNODES; i++) {
480 get_pfn_range_for_nid(i, &start_pfn, &end_pfn);
481 if (start < PFN_PHYS(start_pfn) || start >= PFN_PHYS(end_pfn))
482 continue;
483 *nid = i;
484 return min(end, PFN_PHYS(end_pfn));
485 }
486#endif
c3f72b57
BH
487 *nid = 0;
488
489 return end;
490}
491
2898cc4c
BH
492static phys_addr_t __init memblock_alloc_nid_region(struct memblock_region *mp,
493 phys_addr_t size,
494 phys_addr_t align, int nid)
95f72d1e 495{
2898cc4c 496 phys_addr_t start, end;
95f72d1e
YL
497
498 start = mp->base;
499 end = start + mp->size;
500
501 start = memblock_align_up(start, align);
502 while (start < end) {
2898cc4c 503 phys_addr_t this_end;
95f72d1e
YL
504 int this_nid;
505
35a1f0bd 506 this_end = memblock_nid_range(start, end, &this_nid);
95f72d1e 507 if (this_nid == nid) {
3a9c2c81 508 phys_addr_t ret = memblock_find_region(start, this_end, size, align);
4d629f9a 509 if (ret != MEMBLOCK_ERROR &&
3a9c2c81 510 memblock_add_region(&memblock.reserved, ret, size) >= 0)
95f72d1e
YL
511 return ret;
512 }
513 start = this_end;
514 }
515
4d629f9a 516 return MEMBLOCK_ERROR;
95f72d1e
YL
517}
518
2898cc4c 519phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
95f72d1e 520{
e3239ff9 521 struct memblock_type *mem = &memblock.memory;
95f72d1e
YL
522 int i;
523
524 BUG_ON(0 == size);
525
7f219c73
BH
526 /* We align the size to limit fragmentation. Without this, a lot of
527 * small allocs quickly eat up the whole reserve array on sparc
528 */
529 size = memblock_align_up(size, align);
530
c3f72b57
BH
531 /* We do a bottom-up search for a region with the right
532 * nid since that's easier considering how memblock_nid_range()
533 * works
534 */
95f72d1e 535 for (i = 0; i < mem->cnt; i++) {
2898cc4c 536 phys_addr_t ret = memblock_alloc_nid_region(&mem->regions[i],
95f72d1e 537 size, align, nid);
4d629f9a 538 if (ret != MEMBLOCK_ERROR)
95f72d1e
YL
539 return ret;
540 }
541
9d1e2492
BH
542 return 0;
543}
544
545phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
546{
547 phys_addr_t res = memblock_alloc_nid(size, align, nid);
548
549 if (res)
550 return res;
918fe8d6 551 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ANYWHERE);
95f72d1e
YL
552}
553
9d1e2492
BH
554
555/*
556 * Remaining API functions
557 */
558
95f72d1e 559/* You must call memblock_analyze() before this. */
2898cc4c 560phys_addr_t __init memblock_phys_mem_size(void)
95f72d1e 561{
4734b594 562 return memblock.memory_size;
95f72d1e
YL
563}
564
2898cc4c 565phys_addr_t memblock_end_of_DRAM(void)
95f72d1e
YL
566{
567 int idx = memblock.memory.cnt - 1;
568
e3239ff9 569 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
95f72d1e
YL
570}
571
572/* You must call memblock_analyze() after this. */
2898cc4c 573void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
95f72d1e
YL
574{
575 unsigned long i;
2898cc4c 576 phys_addr_t limit;
e3239ff9 577 struct memblock_region *p;
95f72d1e
YL
578
579 if (!memory_limit)
580 return;
581
582 /* Truncate the memblock regions to satisfy the memory limit. */
583 limit = memory_limit;
584 for (i = 0; i < memblock.memory.cnt; i++) {
e3239ff9
BH
585 if (limit > memblock.memory.regions[i].size) {
586 limit -= memblock.memory.regions[i].size;
95f72d1e
YL
587 continue;
588 }
589
e3239ff9 590 memblock.memory.regions[i].size = limit;
95f72d1e
YL
591 memblock.memory.cnt = i + 1;
592 break;
593 }
594
95f72d1e
YL
595 memory_limit = memblock_end_of_DRAM();
596
597 /* And truncate any reserves above the limit also. */
598 for (i = 0; i < memblock.reserved.cnt; i++) {
e3239ff9 599 p = &memblock.reserved.regions[i];
95f72d1e
YL
600
601 if (p->base > memory_limit)
602 p->size = 0;
603 else if ((p->base + p->size) > memory_limit)
604 p->size = memory_limit - p->base;
605
606 if (p->size == 0) {
607 memblock_remove_region(&memblock.reserved, i);
608 i--;
609 }
610 }
611}
612
2898cc4c 613static int memblock_search(struct memblock_type *type, phys_addr_t addr)
72d4b0b4
BH
614{
615 unsigned int left = 0, right = type->cnt;
616
617 do {
618 unsigned int mid = (right + left) / 2;
619
620 if (addr < type->regions[mid].base)
621 right = mid;
622 else if (addr >= (type->regions[mid].base +
623 type->regions[mid].size))
624 left = mid + 1;
625 else
626 return mid;
627 } while (left < right);
628 return -1;
629}
630
2898cc4c 631int __init memblock_is_reserved(phys_addr_t addr)
95f72d1e 632{
72d4b0b4
BH
633 return memblock_search(&memblock.reserved, addr) != -1;
634}
95f72d1e 635
2898cc4c 636int memblock_is_memory(phys_addr_t addr)
72d4b0b4
BH
637{
638 return memblock_search(&memblock.memory, addr) != -1;
639}
640
2898cc4c 641int memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
72d4b0b4
BH
642{
643 int idx = memblock_search(&memblock.reserved, base);
644
645 if (idx == -1)
646 return 0;
647 return memblock.reserved.regions[idx].base <= base &&
648 (memblock.reserved.regions[idx].base +
649 memblock.reserved.regions[idx].size) >= (base + size);
95f72d1e
YL
650}
651
2898cc4c 652int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
95f72d1e 653{
f1c2c19c 654 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
95f72d1e
YL
655}
656
e63075a3 657
2898cc4c 658void __init memblock_set_current_limit(phys_addr_t limit)
e63075a3
BH
659{
660 memblock.current_limit = limit;
661}
662
6ed311b2
BH
663static void memblock_dump(struct memblock_type *region, char *name)
664{
665 unsigned long long base, size;
666 int i;
667
668 pr_info(" %s.cnt = 0x%lx\n", name, region->cnt);
669
670 for (i = 0; i < region->cnt; i++) {
671 base = region->regions[i].base;
672 size = region->regions[i].size;
673
ea9e4376 674 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes\n",
6ed311b2
BH
675 name, i, base, base + size - 1, size);
676 }
677}
678
679void memblock_dump_all(void)
680{
681 if (!memblock_debug)
682 return;
683
684 pr_info("MEMBLOCK configuration:\n");
685 pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
686
687 memblock_dump(&memblock.memory, "memory");
688 memblock_dump(&memblock.reserved, "reserved");
689}
690
691void __init memblock_analyze(void)
692{
693 int i;
694
695 /* Check marker in the unused last array entry */
696 WARN_ON(memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS].base
697 != (phys_addr_t)RED_INACTIVE);
698 WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
699 != (phys_addr_t)RED_INACTIVE);
700
701 memblock.memory_size = 0;
702
703 for (i = 0; i < memblock.memory.cnt; i++)
704 memblock.memory_size += memblock.memory.regions[i].size;
142b45a7
BH
705
706 /* We allow resizing from there */
707 memblock_can_resize = 1;
6ed311b2
BH
708}
709
7590abe8
BH
710void __init memblock_init(void)
711{
712 /* Hookup the initial arrays */
713 memblock.memory.regions = memblock_memory_init_regions;
714 memblock.memory.max = INIT_MEMBLOCK_REGIONS;
715 memblock.reserved.regions = memblock_reserved_init_regions;
716 memblock.reserved.max = INIT_MEMBLOCK_REGIONS;
717
718 /* Write a marker in the unused last array entry */
719 memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
720 memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = (phys_addr_t)RED_INACTIVE;
721
722 /* Create a dummy zero size MEMBLOCK which will get coalesced away later.
723 * This simplifies the memblock_add() code below...
724 */
725 memblock.memory.regions[0].base = 0;
726 memblock.memory.regions[0].size = 0;
727 memblock.memory.cnt = 1;
728
729 /* Ditto. */
730 memblock.reserved.regions[0].base = 0;
731 memblock.reserved.regions[0].size = 0;
732 memblock.reserved.cnt = 1;
733
734 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
735}
736
6ed311b2
BH
737static int __init early_memblock(char *p)
738{
739 if (p && strstr(p, "debug"))
740 memblock_debug = 1;
741 return 0;
742}
743early_param("memblock", early_memblock);
744
6d03b885
BH
745#ifdef CONFIG_DEBUG_FS
746
747static int memblock_debug_show(struct seq_file *m, void *private)
748{
749 struct memblock_type *type = m->private;
750 struct memblock_region *reg;
751 int i;
752
753 for (i = 0; i < type->cnt; i++) {
754 reg = &type->regions[i];
755 seq_printf(m, "%4d: ", i);
756 if (sizeof(phys_addr_t) == 4)
757 seq_printf(m, "0x%08lx..0x%08lx\n",
758 (unsigned long)reg->base,
759 (unsigned long)(reg->base + reg->size - 1));
760 else
761 seq_printf(m, "0x%016llx..0x%016llx\n",
762 (unsigned long long)reg->base,
763 (unsigned long long)(reg->base + reg->size - 1));
764
765 }
766 return 0;
767}
768
769static int memblock_debug_open(struct inode *inode, struct file *file)
770{
771 return single_open(file, memblock_debug_show, inode->i_private);
772}
773
774static const struct file_operations memblock_debug_fops = {
775 .open = memblock_debug_open,
776 .read = seq_read,
777 .llseek = seq_lseek,
778 .release = single_release,
779};
780
781static int __init memblock_init_debugfs(void)
782{
783 struct dentry *root = debugfs_create_dir("memblock", NULL);
784 if (!root)
785 return -ENXIO;
786 debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
787 debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
788
789 return 0;
790}
791__initcall(memblock_init_debugfs);
792
793#endif /* CONFIG_DEBUG_FS */