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