memblock: Make memblock_{add|remove|free|reserve}() return int and update prototypes
[GitHub/moto-9609/android_kernel_motorola_exynos9610.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
10d06439 23struct memblock memblock __initdata_memblock;
95f72d1e 24
10d06439
YL
25int 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;
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 */
10d06439 44static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
2898cc4c 45 phys_addr_t base2, phys_addr_t size2)
95f72d1e
YL
46{
47 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
48}
49
2d7d3eb2
HS
50static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
51 phys_addr_t base, phys_addr_t size)
6ed311b2
BH
52{
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
cd79481d 70static phys_addr_t __init_memblock memblock_find_region(phys_addr_t start, phys_addr_t end,
6ed311b2
BH
71 phys_addr_t size, phys_addr_t align)
72{
73 phys_addr_t base, res_base;
74 long j;
75
f1af98c7
YL
76 /* In case, huge size is requested */
77 if (end < size)
1f5026a7 78 return 0;
f1af98c7 79
348968eb 80 base = round_down(end - size, align);
f1af98c7 81
25818f0f
BH
82 /* 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
6ed311b2
BH
88 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;
348968eb 95 base = round_down(res_base - size, align);
6ed311b2
BH
96 }
97
1f5026a7 98 return 0;
6ed311b2
BH
99}
100
fc769a8e
TH
101/*
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)
6ed311b2
BH
106{
107 long i;
6ed311b2
BH
108
109 BUG_ON(0 == size);
110
6ed311b2 111 /* Pump up max_addr */
fef501d4
BH
112 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
113 end = memblock.current_limit;
6ed311b2
BH
114
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;
fef501d4 122 phys_addr_t bottom, top, found;
6ed311b2
BH
123
124 if (memblocksize < size)
125 continue;
fef501d4
BH
126 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);
1f5026a7 133 if (found)
fef501d4 134 return found;
6ed311b2 135 }
1f5026a7 136 return 0;
6ed311b2
BH
137}
138
7950c407
YL
139/*
140 * 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
10d06439 163static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
95f72d1e 164{
7c0caeb8
TH
165 memmove(&type->regions[r], &type->regions[r + 1],
166 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
e3239ff9 167 type->cnt--;
95f72d1e 168
8f7a6605
BH
169 /* 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;
7c0caeb8 174 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
8f7a6605 175 }
95f72d1e
YL
176}
177
142b45a7 178/* Defined below but needed now */
581adcbe 179static int memblock_add_region(struct memblock_type *type, phys_addr_t base, phys_addr_t size);
142b45a7 180
10d06439 181static int __init_memblock memblock_double_array(struct memblock_type *type)
142b45a7
BH
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);
1f5026a7 210 addr = new_array ? __pa(new_array) : 0;
142b45a7 211 } else
fc769a8e 212 addr = memblock_find_in_range(0, MEMBLOCK_ALLOC_ACCESSIBLE, new_size, sizeof(phys_addr_t));
1f5026a7 213 if (!addr) {
142b45a7
BH
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 ! */
8f7a6605 238 BUG_ON(memblock_add_region(&memblock.reserved, addr, new_size));
142b45a7
BH
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
784656f9
TH
252/**
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)
95f72d1e 259{
784656f9 260 int i = 0;
95f72d1e 261
784656f9
TH
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];
95f72d1e 266
7c0caeb8
TH
267 if (this->base + this->size != next->base ||
268 memblock_get_region_node(this) !=
269 memblock_get_region_node(next)) {
784656f9
TH
270 BUG_ON(this->base + this->size > next->base);
271 i++;
272 continue;
8f7a6605
BH
273 }
274
784656f9
TH
275 this->size += next->size;
276 memmove(next, next + 1, (type->cnt - (i + 1)) * sizeof(*next));
277 type->cnt--;
95f72d1e 278 }
784656f9 279}
95f72d1e 280
784656f9
TH
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,
7c0caeb8 293 phys_addr_t size, int nid)
784656f9
TH
294{
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;
7c0caeb8 301 memblock_set_region_node(rgn, nid);
784656f9
TH
302 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 */
581adcbe
TH
319static int __init_memblock memblock_add_region(struct memblock_type *type,
320 phys_addr_t base, phys_addr_t size)
784656f9
TH
321{
322 bool insert = false;
323 phys_addr_t obase = base, end = base + size;
324 int i, nr_new;
325
326 /* special case for empty array */
327 if (type->regions[0].size == 0) {
328 WARN_ON(type->cnt != 1);
8f7a6605
BH
329 type->regions[0].base = base;
330 type->regions[0].size = size;
7c0caeb8 331 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
8f7a6605 332 return 0;
95f72d1e 333 }
784656f9
TH
334repeat:
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.
142b45a7 339 */
784656f9
TH
340 base = obase;
341 nr_new = 0;
95f72d1e 342
784656f9
TH
343 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)
95f72d1e 349 break;
784656f9
TH
350 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,
7c0caeb8 360 rbase - base, MAX_NUMNODES);
95f72d1e 361 }
784656f9
TH
362 /* area below @rend is dealt with, forget about it */
363 base = min(rend, end);
95f72d1e 364 }
784656f9
TH
365
366 /* insert the remaining portion */
367 if (base < end) {
368 nr_new++;
369 if (insert)
7c0caeb8
TH
370 memblock_insert_region(type, i, base, end - base,
371 MAX_NUMNODES);
95f72d1e 372 }
95f72d1e 373
784656f9
TH
374 /*
375 * If this was the first round, resize array and repeat for actual
376 * insertions; otherwise, merge and return.
142b45a7 377 */
784656f9
TH
378 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;
142b45a7 387 }
95f72d1e
YL
388}
389
581adcbe 390int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
95f72d1e 391{
e3239ff9 392 return memblock_add_region(&memblock.memory, base, size);
95f72d1e
YL
393}
394
581adcbe
TH
395static int __init_memblock __memblock_remove(struct memblock_type *type,
396 phys_addr_t base, phys_addr_t size)
95f72d1e 397{
2898cc4c 398 phys_addr_t end = base + size;
95f72d1e
YL
399 int i;
400
8f7a6605
BH
401 /* 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;
95f72d1e 405
8f7a6605
BH
406 /* Nothing more to do, exit */
407 if (rgn->base > end || rgn->size == 0)
95f72d1e 408 break;
95f72d1e 409
8f7a6605
BH
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 }
95f72d1e 415
8f7a6605
BH
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 }
95f72d1e 430
8f7a6605
BH
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 }
95f72d1e 437
8f7a6605
BH
438 /* And check if we need to trim the top of a block */
439 if (base < rend)
440 rgn->size -= rend - base;
95f72d1e 441
8f7a6605
BH
442 }
443 return 0;
95f72d1e
YL
444}
445
581adcbe 446int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
95f72d1e
YL
447{
448 return __memblock_remove(&memblock.memory, base, size);
449}
450
581adcbe 451int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
95f72d1e 452{
24aa0788 453 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
a150439c
PA
454 (unsigned long long)base,
455 (unsigned long long)base + size,
456 (void *)_RET_IP_);
24aa0788 457
95f72d1e
YL
458 return __memblock_remove(&memblock.reserved, base, size);
459}
460
581adcbe 461int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
95f72d1e 462{
e3239ff9 463 struct memblock_type *_rgn = &memblock.reserved;
95f72d1e 464
24aa0788 465 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
a150439c
PA
466 (unsigned long long)base,
467 (unsigned long long)base + size,
468 (void *)_RET_IP_);
95f72d1e
YL
469 BUG_ON(0 == size);
470
471 return memblock_add_region(_rgn, base, size);
472}
473
35fd0808
TH
474/**
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
7c0caeb8
TH
550#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
6ed311b2 645phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
95f72d1e 646{
6ed311b2 647 phys_addr_t found;
95f72d1e 648
6ed311b2
BH
649 /* 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 */
348968eb 652 size = round_up(size, align);
95f72d1e 653
fc769a8e 654 found = memblock_find_in_range(0, max_addr, size, align);
1f5026a7 655 if (found && !memblock_add_region(&memblock.reserved, found, size))
6ed311b2 656 return found;
95f72d1e 657
6ed311b2 658 return 0;
95f72d1e
YL
659}
660
6ed311b2 661phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
95f72d1e 662{
6ed311b2
BH
663 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;
95f72d1e
YL
672}
673
6ed311b2 674phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
95f72d1e 675{
6ed311b2
BH
676 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
677}
95f72d1e 678
95f72d1e 679
6ed311b2 680/*
34e18455 681 * Additional node-local top-down allocators.
c196f76f
BH
682 *
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.
6ed311b2 686 */
95f72d1e 687
34e18455
TH
688static phys_addr_t __init memblock_nid_range_rev(phys_addr_t start,
689 phys_addr_t end, int *nid)
c3f72b57 690{
c196f76f 691#ifdef CONFIG_ARCH_POPULATES_NODE_MAP
c196f76f
BH
692 unsigned long start_pfn, end_pfn;
693 int i;
694
b2fea988 695 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, nid)
34e18455
TH
696 if (end > PFN_PHYS(start_pfn) && end <= PFN_PHYS(end_pfn))
697 return max(start, PFN_PHYS(start_pfn));
c196f76f 698#endif
c3f72b57 699 *nid = 0;
34e18455 700 return start;
c3f72b57
BH
701}
702
e6498040
TH
703phys_addr_t __init memblock_find_in_range_node(phys_addr_t start,
704 phys_addr_t end,
2898cc4c
BH
705 phys_addr_t size,
706 phys_addr_t align, int nid)
95f72d1e 707{
e6498040
TH
708 struct memblock_type *mem = &memblock.memory;
709 int i;
95f72d1e 710
e6498040 711 BUG_ON(0 == size);
95f72d1e 712
e6498040
TH
713 /* Pump up max_addr */
714 if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
715 end = memblock.current_limit;
95f72d1e 716
e6498040
TH
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;
95f72d1e 733 }
95f72d1e 734 }
e6498040 735
1f5026a7 736 return 0;
95f72d1e
YL
737}
738
2898cc4c 739phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
95f72d1e 740{
e6498040 741 phys_addr_t found;
95f72d1e 742
e6498040
TH
743 /*
744 * We align the size to limit fragmentation. Without this, a lot of
7f219c73
BH
745 * small allocs quickly eat up the whole reserve array on sparc
746 */
348968eb 747 size = round_up(size, align);
7f219c73 748
e6498040
TH
749 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;
95f72d1e 753
9d1e2492
BH
754 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;
15fb0972 763 return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
95f72d1e
YL
764}
765
9d1e2492
BH
766
767/*
768 * Remaining API functions
769 */
770
95f72d1e 771/* You must call memblock_analyze() before this. */
2898cc4c 772phys_addr_t __init memblock_phys_mem_size(void)
95f72d1e 773{
4734b594 774 return memblock.memory_size;
95f72d1e
YL
775}
776
0a93ebef
SR
777/* lowest address */
778phys_addr_t __init_memblock memblock_start_of_DRAM(void)
779{
780 return memblock.memory.regions[0].base;
781}
782
10d06439 783phys_addr_t __init_memblock memblock_end_of_DRAM(void)
95f72d1e
YL
784{
785 int idx = memblock.memory.cnt - 1;
786
e3239ff9 787 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
95f72d1e
YL
788}
789
790/* You must call memblock_analyze() after this. */
2898cc4c 791void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
95f72d1e
YL
792{
793 unsigned long i;
2898cc4c 794 phys_addr_t limit;
e3239ff9 795 struct memblock_region *p;
95f72d1e
YL
796
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++) {
e3239ff9
BH
803 if (limit > memblock.memory.regions[i].size) {
804 limit -= memblock.memory.regions[i].size;
95f72d1e
YL
805 continue;
806 }
807
e3239ff9 808 memblock.memory.regions[i].size = limit;
95f72d1e
YL
809 memblock.memory.cnt = i + 1;
810 break;
811 }
812
95f72d1e
YL
813 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++) {
e3239ff9 817 p = &memblock.reserved.regions[i];
95f72d1e
YL
818
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
cd79481d 831static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
72d4b0b4
BH
832{
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
2898cc4c 849int __init memblock_is_reserved(phys_addr_t addr)
95f72d1e 850{
72d4b0b4
BH
851 return memblock_search(&memblock.reserved, addr) != -1;
852}
95f72d1e 853
3661ca66 854int __init_memblock memblock_is_memory(phys_addr_t addr)
72d4b0b4
BH
855{
856 return memblock_search(&memblock.memory, addr) != -1;
857}
858
3661ca66 859int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
72d4b0b4 860{
abb65272 861 int idx = memblock_search(&memblock.memory, base);
72d4b0b4
BH
862
863 if (idx == -1)
864 return 0;
abb65272
TV
865 return memblock.memory.regions[idx].base <= base &&
866 (memblock.memory.regions[idx].base +
867 memblock.memory.regions[idx].size) >= (base + size);
95f72d1e
YL
868}
869
10d06439 870int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
95f72d1e 871{
f1c2c19c 872 return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
95f72d1e
YL
873}
874
e63075a3 875
3661ca66 876void __init_memblock memblock_set_current_limit(phys_addr_t limit)
e63075a3
BH
877{
878 memblock.current_limit = limit;
879}
880
7c0caeb8 881static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
6ed311b2
BH
882{
883 unsigned long long base, size;
884 int i;
885
7c0caeb8 886 pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
6ed311b2 887
7c0caeb8
TH
888 for (i = 0; i < type->cnt; i++) {
889 struct memblock_region *rgn = &type->regions[i];
890 char nid_buf[32] = "";
891
892 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);
6ed311b2
BH
901 }
902}
903
10d06439 904void __init_memblock memblock_dump_all(void)
6ed311b2
BH
905{
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
c9d8c3d0 922 != MEMBLOCK_INACTIVE);
6ed311b2 923 WARN_ON(memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS].base
c9d8c3d0 924 != MEMBLOCK_INACTIVE);
6ed311b2
BH
925
926 memblock.memory_size = 0;
927
928 for (i = 0; i < memblock.memory.cnt; i++)
929 memblock.memory_size += memblock.memory.regions[i].size;
142b45a7
BH
930
931 /* We allow resizing from there */
932 memblock_can_resize = 1;
6ed311b2
BH
933}
934
7590abe8
BH
935void __init memblock_init(void)
936{
236260b9
JF
937 static int init_done __initdata = 0;
938
939 if (init_done)
940 return;
941 init_done = 1;
942
7590abe8
BH
943 /* 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 */
c9d8c3d0
AM
950 memblock.memory.regions[INIT_MEMBLOCK_REGIONS].base = MEMBLOCK_INACTIVE;
951 memblock.reserved.regions[INIT_MEMBLOCK_REGIONS].base = MEMBLOCK_INACTIVE;
7590abe8
BH
952
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;
7c0caeb8 958 memblock_set_region_node(&memblock.memory.regions[0], MAX_NUMNODES);
7590abe8
BH
959 memblock.memory.cnt = 1;
960
961 /* Ditto. */
962 memblock.reserved.regions[0].base = 0;
963 memblock.reserved.regions[0].size = 0;
7c0caeb8 964 memblock_set_region_node(&memblock.reserved.regions[0], MAX_NUMNODES);
7590abe8
BH
965 memblock.reserved.cnt = 1;
966
967 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
968}
969
6ed311b2
BH
970static 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
c378ddd5 978#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
6d03b885
BH
979
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 */