memblock: Reimplement __memblock_remove() using memblock_isolate_range()
[linux-2.6-block.git] / mm / memblock.c
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>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/bitops.h>
17 #include <linux/poison.h>
18 #include <linux/pfn.h>
19 #include <linux/debugfs.h>
20 #include <linux/seq_file.h>
21 #include <linux/memblock.h>
22
23 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
24 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
25
26 struct memblock memblock __initdata_memblock = {
27         .memory.regions         = memblock_memory_init_regions,
28         .memory.cnt             = 1,    /* empty dummy entry */
29         .memory.max             = INIT_MEMBLOCK_REGIONS,
30
31         .reserved.regions       = memblock_reserved_init_regions,
32         .reserved.cnt           = 1,    /* empty dummy entry */
33         .reserved.max           = INIT_MEMBLOCK_REGIONS,
34
35         .current_limit          = MEMBLOCK_ALLOC_ANYWHERE,
36 };
37
38 int memblock_debug __initdata_memblock;
39 int memblock_can_resize __initdata_memblock;
40
41 /* inline so we don't get a warning when pr_debug is compiled out */
42 static inline const char *memblock_type_name(struct memblock_type *type)
43 {
44         if (type == &memblock.memory)
45                 return "memory";
46         else if (type == &memblock.reserved)
47                 return "reserved";
48         else
49                 return "unknown";
50 }
51
52 /*
53  * Address comparison utilities
54  */
55 static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
56                                        phys_addr_t base2, phys_addr_t size2)
57 {
58         return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
59 }
60
61 static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
62                                         phys_addr_t base, phys_addr_t size)
63 {
64         unsigned long i;
65
66         for (i = 0; i < type->cnt; i++) {
67                 phys_addr_t rgnbase = type->regions[i].base;
68                 phys_addr_t rgnsize = type->regions[i].size;
69                 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
70                         break;
71         }
72
73         return (i < type->cnt) ? i : -1;
74 }
75
76 /*
77  * Find, allocate, deallocate or reserve unreserved regions. All allocations
78  * are top-down.
79  */
80
81 static phys_addr_t __init_memblock memblock_find_region(phys_addr_t start, phys_addr_t end,
82                                           phys_addr_t size, phys_addr_t align)
83 {
84         phys_addr_t base, res_base;
85         long j;
86
87         /* In case, huge size is requested */
88         if (end < size)
89                 return 0;
90
91         base = round_down(end - size, align);
92
93         /* Prevent allocations returning 0 as it's also used to
94          * indicate an allocation failure
95          */
96         if (start == 0)
97                 start = PAGE_SIZE;
98
99         while (start <= base) {
100                 j = memblock_overlaps_region(&memblock.reserved, base, size);
101                 if (j < 0)
102                         return base;
103                 res_base = memblock.reserved.regions[j].base;
104                 if (res_base < size)
105                         break;
106                 base = round_down(res_base - size, align);
107         }
108
109         return 0;
110 }
111
112 /*
113  * Find a free area with specified alignment in a specific range.
114  */
115 phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, phys_addr_t end,
116                                         phys_addr_t size, phys_addr_t align)
117 {
118         long i;
119
120         BUG_ON(0 == size);
121
122         /* Pump up max_addr */
123         if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
124                 end = memblock.current_limit;
125
126         /* We do a top-down search, this tends to limit memory
127          * fragmentation by keeping early boot allocs near the
128          * top of memory
129          */
130         for (i = memblock.memory.cnt - 1; i >= 0; i--) {
131                 phys_addr_t memblockbase = memblock.memory.regions[i].base;
132                 phys_addr_t memblocksize = memblock.memory.regions[i].size;
133                 phys_addr_t bottom, top, found;
134
135                 if (memblocksize < size)
136                         continue;
137                 if ((memblockbase + memblocksize) <= start)
138                         break;
139                 bottom = max(memblockbase, start);
140                 top = min(memblockbase + memblocksize, end);
141                 if (bottom >= top)
142                         continue;
143                 found = memblock_find_region(bottom, top, size, align);
144                 if (found)
145                         return found;
146         }
147         return 0;
148 }
149
150 /*
151  * Free memblock.reserved.regions
152  */
153 int __init_memblock memblock_free_reserved_regions(void)
154 {
155         if (memblock.reserved.regions == memblock_reserved_init_regions)
156                 return 0;
157
158         return memblock_free(__pa(memblock.reserved.regions),
159                  sizeof(struct memblock_region) * memblock.reserved.max);
160 }
161
162 /*
163  * Reserve memblock.reserved.regions
164  */
165 int __init_memblock memblock_reserve_reserved_regions(void)
166 {
167         if (memblock.reserved.regions == memblock_reserved_init_regions)
168                 return 0;
169
170         return memblock_reserve(__pa(memblock.reserved.regions),
171                  sizeof(struct memblock_region) * memblock.reserved.max);
172 }
173
174 static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
175 {
176         memmove(&type->regions[r], &type->regions[r + 1],
177                 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
178         type->cnt--;
179
180         /* Special case for empty arrays */
181         if (type->cnt == 0) {
182                 type->cnt = 1;
183                 type->regions[0].base = 0;
184                 type->regions[0].size = 0;
185                 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
186         }
187 }
188
189 static int __init_memblock memblock_double_array(struct memblock_type *type)
190 {
191         struct memblock_region *new_array, *old_array;
192         phys_addr_t old_size, new_size, addr;
193         int use_slab = slab_is_available();
194
195         /* We don't allow resizing until we know about the reserved regions
196          * of memory that aren't suitable for allocation
197          */
198         if (!memblock_can_resize)
199                 return -1;
200
201         /* Calculate new doubled size */
202         old_size = type->max * sizeof(struct memblock_region);
203         new_size = old_size << 1;
204
205         /* Try to find some space for it.
206          *
207          * WARNING: We assume that either slab_is_available() and we use it or
208          * we use MEMBLOCK for allocations. That means that this is unsafe to use
209          * when bootmem is currently active (unless bootmem itself is implemented
210          * on top of MEMBLOCK which isn't the case yet)
211          *
212          * This should however not be an issue for now, as we currently only
213          * call into MEMBLOCK while it's still active, or much later when slab is
214          * active for memory hotplug operations
215          */
216         if (use_slab) {
217                 new_array = kmalloc(new_size, GFP_KERNEL);
218                 addr = new_array ? __pa(new_array) : 0;
219         } else
220                 addr = memblock_find_in_range(0, MEMBLOCK_ALLOC_ACCESSIBLE, new_size, sizeof(phys_addr_t));
221         if (!addr) {
222                 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
223                        memblock_type_name(type), type->max, type->max * 2);
224                 return -1;
225         }
226         new_array = __va(addr);
227
228         memblock_dbg("memblock: %s array is doubled to %ld at [%#010llx-%#010llx]",
229                  memblock_type_name(type), type->max * 2, (u64)addr, (u64)addr + new_size - 1);
230
231         /* Found space, we now need to move the array over before
232          * we add the reserved region since it may be our reserved
233          * array itself that is full.
234          */
235         memcpy(new_array, type->regions, old_size);
236         memset(new_array + type->max, 0, old_size);
237         old_array = type->regions;
238         type->regions = new_array;
239         type->max <<= 1;
240
241         /* If we use SLAB that's it, we are done */
242         if (use_slab)
243                 return 0;
244
245         /* Add the new reserved region now. Should not fail ! */
246         BUG_ON(memblock_reserve(addr, new_size));
247
248         /* If the array wasn't our static init one, then free it. We only do
249          * that before SLAB is available as later on, we don't know whether
250          * to use kfree or free_bootmem_pages(). Shouldn't be a big deal
251          * anyways
252          */
253         if (old_array != memblock_memory_init_regions &&
254             old_array != memblock_reserved_init_regions)
255                 memblock_free(__pa(old_array), old_size);
256
257         return 0;
258 }
259
260 /**
261  * memblock_merge_regions - merge neighboring compatible regions
262  * @type: memblock type to scan
263  *
264  * Scan @type and merge neighboring compatible regions.
265  */
266 static void __init_memblock memblock_merge_regions(struct memblock_type *type)
267 {
268         int i = 0;
269
270         /* cnt never goes below 1 */
271         while (i < type->cnt - 1) {
272                 struct memblock_region *this = &type->regions[i];
273                 struct memblock_region *next = &type->regions[i + 1];
274
275                 if (this->base + this->size != next->base ||
276                     memblock_get_region_node(this) !=
277                     memblock_get_region_node(next)) {
278                         BUG_ON(this->base + this->size > next->base);
279                         i++;
280                         continue;
281                 }
282
283                 this->size += next->size;
284                 memmove(next, next + 1, (type->cnt - (i + 1)) * sizeof(*next));
285                 type->cnt--;
286         }
287 }
288
289 /**
290  * memblock_insert_region - insert new memblock region
291  * @type: memblock type to insert into
292  * @idx: index for the insertion point
293  * @base: base address of the new region
294  * @size: size of the new region
295  *
296  * Insert new memblock region [@base,@base+@size) into @type at @idx.
297  * @type must already have extra room to accomodate the new region.
298  */
299 static void __init_memblock memblock_insert_region(struct memblock_type *type,
300                                                    int idx, phys_addr_t base,
301                                                    phys_addr_t size, int nid)
302 {
303         struct memblock_region *rgn = &type->regions[idx];
304
305         BUG_ON(type->cnt >= type->max);
306         memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
307         rgn->base = base;
308         rgn->size = size;
309         memblock_set_region_node(rgn, nid);
310         type->cnt++;
311 }
312
313 /**
314  * memblock_add_region - add new memblock region
315  * @type: memblock type to add new region into
316  * @base: base address of the new region
317  * @size: size of the new region
318  *
319  * Add new memblock region [@base,@base+@size) into @type.  The new region
320  * is allowed to overlap with existing ones - overlaps don't affect already
321  * existing regions.  @type is guaranteed to be minimal (all neighbouring
322  * compatible regions are merged) after the addition.
323  *
324  * RETURNS:
325  * 0 on success, -errno on failure.
326  */
327 static int __init_memblock memblock_add_region(struct memblock_type *type,
328                                                phys_addr_t base, phys_addr_t size)
329 {
330         bool insert = false;
331         phys_addr_t obase = base, end = base + size;
332         int i, nr_new;
333
334         /* special case for empty array */
335         if (type->regions[0].size == 0) {
336                 WARN_ON(type->cnt != 1);
337                 type->regions[0].base = base;
338                 type->regions[0].size = size;
339                 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
340                 return 0;
341         }
342 repeat:
343         /*
344          * The following is executed twice.  Once with %false @insert and
345          * then with %true.  The first counts the number of regions needed
346          * to accomodate the new area.  The second actually inserts them.
347          */
348         base = obase;
349         nr_new = 0;
350
351         for (i = 0; i < type->cnt; i++) {
352                 struct memblock_region *rgn = &type->regions[i];
353                 phys_addr_t rbase = rgn->base;
354                 phys_addr_t rend = rbase + rgn->size;
355
356                 if (rbase >= end)
357                         break;
358                 if (rend <= base)
359                         continue;
360                 /*
361                  * @rgn overlaps.  If it separates the lower part of new
362                  * area, insert that portion.
363                  */
364                 if (rbase > base) {
365                         nr_new++;
366                         if (insert)
367                                 memblock_insert_region(type, i++, base,
368                                                 rbase - base, MAX_NUMNODES);
369                 }
370                 /* area below @rend is dealt with, forget about it */
371                 base = min(rend, end);
372         }
373
374         /* insert the remaining portion */
375         if (base < end) {
376                 nr_new++;
377                 if (insert)
378                         memblock_insert_region(type, i, base, end - base,
379                                                MAX_NUMNODES);
380         }
381
382         /*
383          * If this was the first round, resize array and repeat for actual
384          * insertions; otherwise, merge and return.
385          */
386         if (!insert) {
387                 while (type->cnt + nr_new > type->max)
388                         if (memblock_double_array(type) < 0)
389                                 return -ENOMEM;
390                 insert = true;
391                 goto repeat;
392         } else {
393                 memblock_merge_regions(type);
394                 return 0;
395         }
396 }
397
398 int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
399 {
400         return memblock_add_region(&memblock.memory, base, size);
401 }
402
403 /**
404  * memblock_isolate_range - isolate given range into disjoint memblocks
405  * @type: memblock type to isolate range for
406  * @base: base of range to isolate
407  * @size: size of range to isolate
408  * @start_rgn: out parameter for the start of isolated region
409  * @end_rgn: out parameter for the end of isolated region
410  *
411  * Walk @type and ensure that regions don't cross the boundaries defined by
412  * [@base,@base+@size).  Crossing regions are split at the boundaries,
413  * which may create at most two more regions.  The index of the first
414  * region inside the range is returned in *@start_rgn and end in *@end_rgn.
415  *
416  * RETURNS:
417  * 0 on success, -errno on failure.
418  */
419 static int __init_memblock memblock_isolate_range(struct memblock_type *type,
420                                         phys_addr_t base, phys_addr_t size,
421                                         int *start_rgn, int *end_rgn)
422 {
423         phys_addr_t end = base + size;
424         int i;
425
426         *start_rgn = *end_rgn = 0;
427
428         /* we'll create at most two more regions */
429         while (type->cnt + 2 > type->max)
430                 if (memblock_double_array(type) < 0)
431                         return -ENOMEM;
432
433         for (i = 0; i < type->cnt; i++) {
434                 struct memblock_region *rgn = &type->regions[i];
435                 phys_addr_t rbase = rgn->base;
436                 phys_addr_t rend = rbase + rgn->size;
437
438                 if (rbase >= end)
439                         break;
440                 if (rend <= base)
441                         continue;
442
443                 if (rbase < base) {
444                         /*
445                          * @rgn intersects from below.  Split and continue
446                          * to process the next region - the new top half.
447                          */
448                         rgn->base = base;
449                         rgn->size = rend - rgn->base;
450                         memblock_insert_region(type, i, rbase, base - rbase,
451                                                memblock_get_region_node(rgn));
452                 } else if (rend > end) {
453                         /*
454                          * @rgn intersects from above.  Split and redo the
455                          * current region - the new bottom half.
456                          */
457                         rgn->base = end;
458                         rgn->size = rend - rgn->base;
459                         memblock_insert_region(type, i--, rbase, end - rbase,
460                                                memblock_get_region_node(rgn));
461                 } else {
462                         /* @rgn is fully contained, record it */
463                         if (!*end_rgn)
464                                 *start_rgn = i;
465                         *end_rgn = i + 1;
466                 }
467         }
468
469         return 0;
470 }
471
472 static int __init_memblock __memblock_remove(struct memblock_type *type,
473                                              phys_addr_t base, phys_addr_t size)
474 {
475         int start_rgn, end_rgn;
476         int i, ret;
477
478         ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
479         if (ret)
480                 return ret;
481
482         for (i = end_rgn - 1; i >= start_rgn; i--)
483                 memblock_remove_region(type, i);
484         return 0;
485 }
486
487 int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
488 {
489         return __memblock_remove(&memblock.memory, base, size);
490 }
491
492 int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
493 {
494         memblock_dbg("   memblock_free: [%#016llx-%#016llx] %pF\n",
495                      (unsigned long long)base,
496                      (unsigned long long)base + size,
497                      (void *)_RET_IP_);
498
499         return __memblock_remove(&memblock.reserved, base, size);
500 }
501
502 int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
503 {
504         struct memblock_type *_rgn = &memblock.reserved;
505
506         memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
507                      (unsigned long long)base,
508                      (unsigned long long)base + size,
509                      (void *)_RET_IP_);
510         BUG_ON(0 == size);
511
512         return memblock_add_region(_rgn, base, size);
513 }
514
515 /**
516  * __next_free_mem_range - next function for for_each_free_mem_range()
517  * @idx: pointer to u64 loop variable
518  * @nid: nid: node selector, %MAX_NUMNODES for all nodes
519  * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
520  * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
521  * @p_nid: ptr to int for nid of the range, can be %NULL
522  *
523  * Find the first free area from *@idx which matches @nid, fill the out
524  * parameters, and update *@idx for the next iteration.  The lower 32bit of
525  * *@idx contains index into memory region and the upper 32bit indexes the
526  * areas before each reserved region.  For example, if reserved regions
527  * look like the following,
528  *
529  *      0:[0-16), 1:[32-48), 2:[128-130)
530  *
531  * The upper 32bit indexes the following regions.
532  *
533  *      0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
534  *
535  * As both region arrays are sorted, the function advances the two indices
536  * in lockstep and returns each intersection.
537  */
538 void __init_memblock __next_free_mem_range(u64 *idx, int nid,
539                                            phys_addr_t *out_start,
540                                            phys_addr_t *out_end, int *out_nid)
541 {
542         struct memblock_type *mem = &memblock.memory;
543         struct memblock_type *rsv = &memblock.reserved;
544         int mi = *idx & 0xffffffff;
545         int ri = *idx >> 32;
546
547         for ( ; mi < mem->cnt; mi++) {
548                 struct memblock_region *m = &mem->regions[mi];
549                 phys_addr_t m_start = m->base;
550                 phys_addr_t m_end = m->base + m->size;
551
552                 /* only memory regions are associated with nodes, check it */
553                 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
554                         continue;
555
556                 /* scan areas before each reservation for intersection */
557                 for ( ; ri < rsv->cnt + 1; ri++) {
558                         struct memblock_region *r = &rsv->regions[ri];
559                         phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
560                         phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
561
562                         /* if ri advanced past mi, break out to advance mi */
563                         if (r_start >= m_end)
564                                 break;
565                         /* if the two regions intersect, we're done */
566                         if (m_start < r_end) {
567                                 if (out_start)
568                                         *out_start = max(m_start, r_start);
569                                 if (out_end)
570                                         *out_end = min(m_end, r_end);
571                                 if (out_nid)
572                                         *out_nid = memblock_get_region_node(m);
573                                 /*
574                                  * The region which ends first is advanced
575                                  * for the next iteration.
576                                  */
577                                 if (m_end <= r_end)
578                                         mi++;
579                                 else
580                                         ri++;
581                                 *idx = (u32)mi | (u64)ri << 32;
582                                 return;
583                         }
584                 }
585         }
586
587         /* signal end of iteration */
588         *idx = ULLONG_MAX;
589 }
590
591 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
592 /*
593  * Common iterator interface used to define for_each_mem_range().
594  */
595 void __init_memblock __next_mem_pfn_range(int *idx, int nid,
596                                 unsigned long *out_start_pfn,
597                                 unsigned long *out_end_pfn, int *out_nid)
598 {
599         struct memblock_type *type = &memblock.memory;
600         struct memblock_region *r;
601
602         while (++*idx < type->cnt) {
603                 r = &type->regions[*idx];
604
605                 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
606                         continue;
607                 if (nid == MAX_NUMNODES || nid == r->nid)
608                         break;
609         }
610         if (*idx >= type->cnt) {
611                 *idx = -1;
612                 return;
613         }
614
615         if (out_start_pfn)
616                 *out_start_pfn = PFN_UP(r->base);
617         if (out_end_pfn)
618                 *out_end_pfn = PFN_DOWN(r->base + r->size);
619         if (out_nid)
620                 *out_nid = r->nid;
621 }
622
623 /**
624  * memblock_set_node - set node ID on memblock regions
625  * @base: base of area to set node ID for
626  * @size: size of area to set node ID for
627  * @nid: node ID to set
628  *
629  * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
630  * Regions which cross the area boundaries are split as necessary.
631  *
632  * RETURNS:
633  * 0 on success, -errno on failure.
634  */
635 int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
636                                       int nid)
637 {
638         struct memblock_type *type = &memblock.memory;
639         int start_rgn, end_rgn;
640         int i, ret;
641
642         ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
643         if (ret)
644                 return ret;
645
646         for (i = start_rgn; i < end_rgn; i++)
647                 type->regions[i].nid = nid;
648
649         memblock_merge_regions(type);
650         return 0;
651 }
652 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
653
654 phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
655 {
656         phys_addr_t found;
657
658         /* We align the size to limit fragmentation. Without this, a lot of
659          * small allocs quickly eat up the whole reserve array on sparc
660          */
661         size = round_up(size, align);
662
663         found = memblock_find_in_range(0, max_addr, size, align);
664         if (found && !memblock_reserve(found, size))
665                 return found;
666
667         return 0;
668 }
669
670 phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
671 {
672         phys_addr_t alloc;
673
674         alloc = __memblock_alloc_base(size, align, max_addr);
675
676         if (alloc == 0)
677                 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
678                       (unsigned long long) size, (unsigned long long) max_addr);
679
680         return alloc;
681 }
682
683 phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
684 {
685         return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
686 }
687
688
689 /*
690  * Additional node-local top-down allocators.
691  *
692  * WARNING: Only available after early_node_map[] has been populated,
693  * on some architectures, that is after all the calls to add_active_range()
694  * have been done to populate it.
695  */
696
697 static phys_addr_t __init memblock_nid_range_rev(phys_addr_t start,
698                                                  phys_addr_t end, int *nid)
699 {
700 #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
701         unsigned long start_pfn, end_pfn;
702         int i;
703
704         for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, nid)
705                 if (end > PFN_PHYS(start_pfn) && end <= PFN_PHYS(end_pfn))
706                         return max(start, PFN_PHYS(start_pfn));
707 #endif
708         *nid = 0;
709         return start;
710 }
711
712 phys_addr_t __init memblock_find_in_range_node(phys_addr_t start,
713                                                phys_addr_t end,
714                                                phys_addr_t size,
715                                                phys_addr_t align, int nid)
716 {
717         struct memblock_type *mem = &memblock.memory;
718         int i;
719
720         BUG_ON(0 == size);
721
722         /* Pump up max_addr */
723         if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
724                 end = memblock.current_limit;
725
726         for (i = mem->cnt - 1; i >= 0; i--) {
727                 struct memblock_region *r = &mem->regions[i];
728                 phys_addr_t base = max(start, r->base);
729                 phys_addr_t top = min(end, r->base + r->size);
730
731                 while (base < top) {
732                         phys_addr_t tbase, ret;
733                         int tnid;
734
735                         tbase = memblock_nid_range_rev(base, top, &tnid);
736                         if (nid == MAX_NUMNODES || tnid == nid) {
737                                 ret = memblock_find_region(tbase, top, size, align);
738                                 if (ret)
739                                         return ret;
740                         }
741                         top = tbase;
742                 }
743         }
744
745         return 0;
746 }
747
748 phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
749 {
750         phys_addr_t found;
751
752         /*
753          * We align the size to limit fragmentation. Without this, a lot of
754          * small allocs quickly eat up the whole reserve array on sparc
755          */
756         size = round_up(size, align);
757
758         found = memblock_find_in_range_node(0, MEMBLOCK_ALLOC_ACCESSIBLE,
759                                             size, align, nid);
760         if (found && !memblock_reserve(found, size))
761                 return found;
762
763         return 0;
764 }
765
766 phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
767 {
768         phys_addr_t res = memblock_alloc_nid(size, align, nid);
769
770         if (res)
771                 return res;
772         return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
773 }
774
775
776 /*
777  * Remaining API functions
778  */
779
780 /* You must call memblock_analyze() before this. */
781 phys_addr_t __init memblock_phys_mem_size(void)
782 {
783         return memblock.memory_size;
784 }
785
786 /* lowest address */
787 phys_addr_t __init_memblock memblock_start_of_DRAM(void)
788 {
789         return memblock.memory.regions[0].base;
790 }
791
792 phys_addr_t __init_memblock memblock_end_of_DRAM(void)
793 {
794         int idx = memblock.memory.cnt - 1;
795
796         return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
797 }
798
799 /* You must call memblock_analyze() after this. */
800 void __init memblock_enforce_memory_limit(phys_addr_t memory_limit)
801 {
802         unsigned long i;
803         phys_addr_t limit;
804         struct memblock_region *p;
805
806         if (!memory_limit)
807                 return;
808
809         /* Truncate the memblock regions to satisfy the memory limit. */
810         limit = memory_limit;
811         for (i = 0; i < memblock.memory.cnt; i++) {
812                 if (limit > memblock.memory.regions[i].size) {
813                         limit -= memblock.memory.regions[i].size;
814                         continue;
815                 }
816
817                 memblock.memory.regions[i].size = limit;
818                 memblock.memory.cnt = i + 1;
819                 break;
820         }
821
822         memory_limit = memblock_end_of_DRAM();
823
824         /* And truncate any reserves above the limit also. */
825         for (i = 0; i < memblock.reserved.cnt; i++) {
826                 p = &memblock.reserved.regions[i];
827
828                 if (p->base > memory_limit)
829                         p->size = 0;
830                 else if ((p->base + p->size) > memory_limit)
831                         p->size = memory_limit - p->base;
832
833                 if (p->size == 0) {
834                         memblock_remove_region(&memblock.reserved, i);
835                         i--;
836                 }
837         }
838 }
839
840 static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
841 {
842         unsigned int left = 0, right = type->cnt;
843
844         do {
845                 unsigned int mid = (right + left) / 2;
846
847                 if (addr < type->regions[mid].base)
848                         right = mid;
849                 else if (addr >= (type->regions[mid].base +
850                                   type->regions[mid].size))
851                         left = mid + 1;
852                 else
853                         return mid;
854         } while (left < right);
855         return -1;
856 }
857
858 int __init memblock_is_reserved(phys_addr_t addr)
859 {
860         return memblock_search(&memblock.reserved, addr) != -1;
861 }
862
863 int __init_memblock memblock_is_memory(phys_addr_t addr)
864 {
865         return memblock_search(&memblock.memory, addr) != -1;
866 }
867
868 int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
869 {
870         int idx = memblock_search(&memblock.memory, base);
871
872         if (idx == -1)
873                 return 0;
874         return memblock.memory.regions[idx].base <= base &&
875                 (memblock.memory.regions[idx].base +
876                  memblock.memory.regions[idx].size) >= (base + size);
877 }
878
879 int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
880 {
881         return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
882 }
883
884
885 void __init_memblock memblock_set_current_limit(phys_addr_t limit)
886 {
887         memblock.current_limit = limit;
888 }
889
890 static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
891 {
892         unsigned long long base, size;
893         int i;
894
895         pr_info(" %s.cnt  = 0x%lx\n", name, type->cnt);
896
897         for (i = 0; i < type->cnt; i++) {
898                 struct memblock_region *rgn = &type->regions[i];
899                 char nid_buf[32] = "";
900
901                 base = rgn->base;
902                 size = rgn->size;
903 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
904                 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
905                         snprintf(nid_buf, sizeof(nid_buf), " on node %d",
906                                  memblock_get_region_node(rgn));
907 #endif
908                 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
909                         name, i, base, base + size - 1, size, nid_buf);
910         }
911 }
912
913 void __init_memblock __memblock_dump_all(void)
914 {
915         pr_info("MEMBLOCK configuration:\n");
916         pr_info(" memory size = 0x%llx\n", (unsigned long long)memblock.memory_size);
917
918         memblock_dump(&memblock.memory, "memory");
919         memblock_dump(&memblock.reserved, "reserved");
920 }
921
922 void __init memblock_analyze(void)
923 {
924         int i;
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;
930
931         /* We allow resizing from there */
932         memblock_can_resize = 1;
933 }
934
935 static int __init early_memblock(char *p)
936 {
937         if (p && strstr(p, "debug"))
938                 memblock_debug = 1;
939         return 0;
940 }
941 early_param("memblock", early_memblock);
942
943 #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
944
945 static int memblock_debug_show(struct seq_file *m, void *private)
946 {
947         struct memblock_type *type = m->private;
948         struct memblock_region *reg;
949         int i;
950
951         for (i = 0; i < type->cnt; i++) {
952                 reg = &type->regions[i];
953                 seq_printf(m, "%4d: ", i);
954                 if (sizeof(phys_addr_t) == 4)
955                         seq_printf(m, "0x%08lx..0x%08lx\n",
956                                    (unsigned long)reg->base,
957                                    (unsigned long)(reg->base + reg->size - 1));
958                 else
959                         seq_printf(m, "0x%016llx..0x%016llx\n",
960                                    (unsigned long long)reg->base,
961                                    (unsigned long long)(reg->base + reg->size - 1));
962
963         }
964         return 0;
965 }
966
967 static int memblock_debug_open(struct inode *inode, struct file *file)
968 {
969         return single_open(file, memblock_debug_show, inode->i_private);
970 }
971
972 static const struct file_operations memblock_debug_fops = {
973         .open = memblock_debug_open,
974         .read = seq_read,
975         .llseek = seq_lseek,
976         .release = single_release,
977 };
978
979 static int __init memblock_init_debugfs(void)
980 {
981         struct dentry *root = debugfs_create_dir("memblock", NULL);
982         if (!root)
983                 return -ENXIO;
984         debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
985         debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
986
987         return 0;
988 }
989 __initcall(memblock_init_debugfs);
990
991 #endif /* CONFIG_DEBUG_FS */