a95d6dc066d55c866c5ca1ccebf62221a81e85b2
[pandora-kernel.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 #include <asm-generic/sections.h>
24
25 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
26 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
27
28 struct memblock memblock __initdata_memblock = {
29         .memory.regions         = memblock_memory_init_regions,
30         .memory.cnt             = 1,    /* empty dummy entry */
31         .memory.max             = INIT_MEMBLOCK_REGIONS,
32
33         .reserved.regions       = memblock_reserved_init_regions,
34         .reserved.cnt           = 1,    /* empty dummy entry */
35         .reserved.max           = INIT_MEMBLOCK_REGIONS,
36
37         .bottom_up              = false,
38         .current_limit          = MEMBLOCK_ALLOC_ANYWHERE,
39 };
40
41 int memblock_debug __initdata_memblock;
42 #ifdef CONFIG_MOVABLE_NODE
43 bool movable_node_enabled __initdata_memblock = false;
44 #endif
45 static int memblock_can_resize __initdata_memblock;
46 static int memblock_memory_in_slab __initdata_memblock = 0;
47 static int memblock_reserved_in_slab __initdata_memblock = 0;
48
49 /* inline so we don't get a warning when pr_debug is compiled out */
50 static __init_memblock const char *
51 memblock_type_name(struct memblock_type *type)
52 {
53         if (type == &memblock.memory)
54                 return "memory";
55         else if (type == &memblock.reserved)
56                 return "reserved";
57         else
58                 return "unknown";
59 }
60
61 /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
62 static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
63 {
64         return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
65 }
66
67 /*
68  * Address comparison utilities
69  */
70 static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
71                                        phys_addr_t base2, phys_addr_t size2)
72 {
73         return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
74 }
75
76 static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
77                                         phys_addr_t base, phys_addr_t size)
78 {
79         unsigned long i;
80
81         for (i = 0; i < type->cnt; i++) {
82                 phys_addr_t rgnbase = type->regions[i].base;
83                 phys_addr_t rgnsize = type->regions[i].size;
84                 if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
85                         break;
86         }
87
88         return (i < type->cnt) ? i : -1;
89 }
90
91 /*
92  * __memblock_find_range_bottom_up - find free area utility in bottom-up
93  * @start: start of candidate range
94  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
95  * @size: size of free area to find
96  * @align: alignment of free area to find
97  * @nid: nid of the free area to find, %MAX_NUMNODES for any node
98  *
99  * Utility called from memblock_find_in_range_node(), find free area bottom-up.
100  *
101  * RETURNS:
102  * Found address on success, 0 on failure.
103  */
104 static phys_addr_t __init_memblock
105 __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
106                                 phys_addr_t size, phys_addr_t align, int nid)
107 {
108         phys_addr_t this_start, this_end, cand;
109         u64 i;
110
111         for_each_free_mem_range(i, nid, &this_start, &this_end, NULL) {
112                 this_start = clamp(this_start, start, end);
113                 this_end = clamp(this_end, start, end);
114
115                 cand = round_up(this_start, align);
116                 if (cand < this_end && this_end - cand >= size)
117                         return cand;
118         }
119
120         return 0;
121 }
122
123 /**
124  * __memblock_find_range_top_down - find free area utility, in top-down
125  * @start: start of candidate range
126  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
127  * @size: size of free area to find
128  * @align: alignment of free area to find
129  * @nid: nid of the free area to find, %MAX_NUMNODES for any node
130  *
131  * Utility called from memblock_find_in_range_node(), find free area top-down.
132  *
133  * RETURNS:
134  * Found address on success, 0 on failure.
135  */
136 static phys_addr_t __init_memblock
137 __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
138                                phys_addr_t size, phys_addr_t align, int nid)
139 {
140         phys_addr_t this_start, this_end, cand;
141         u64 i;
142
143         for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
144                 this_start = clamp(this_start, start, end);
145                 this_end = clamp(this_end, start, end);
146
147                 if (this_end < size)
148                         continue;
149
150                 cand = round_down(this_end - size, align);
151                 if (cand >= this_start)
152                         return cand;
153         }
154
155         return 0;
156 }
157
158 /**
159  * memblock_find_in_range_node - find free area in given range and node
160  * @size: size of free area to find
161  * @align: alignment of free area to find
162  * @start: start of candidate range
163  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
164  * @nid: nid of the free area to find, %MAX_NUMNODES for any node
165  *
166  * Find @size free area aligned to @align in the specified range and node.
167  *
168  * When allocation direction is bottom-up, the @start should be greater
169  * than the end of the kernel image. Otherwise, it will be trimmed. The
170  * reason is that we want the bottom-up allocation just near the kernel
171  * image so it is highly likely that the allocated memory and the kernel
172  * will reside in the same node.
173  *
174  * If bottom-up allocation failed, will try to allocate memory top-down.
175  *
176  * RETURNS:
177  * Found address on success, 0 on failure.
178  */
179 phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
180                                         phys_addr_t align, phys_addr_t start,
181                                         phys_addr_t end, int nid)
182 {
183         int ret;
184         phys_addr_t kernel_end;
185
186         /* pump up @end */
187         if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
188                 end = memblock.current_limit;
189
190         /* avoid allocating the first page */
191         start = max_t(phys_addr_t, start, PAGE_SIZE);
192         end = max(start, end);
193         kernel_end = __pa_symbol(_end);
194
195         /*
196          * try bottom-up allocation only when bottom-up mode
197          * is set and @end is above the kernel image.
198          */
199         if (memblock_bottom_up() && end > kernel_end) {
200                 phys_addr_t bottom_up_start;
201
202                 /* make sure we will allocate above the kernel */
203                 bottom_up_start = max(start, kernel_end);
204
205                 /* ok, try bottom-up allocation first */
206                 ret = __memblock_find_range_bottom_up(bottom_up_start, end,
207                                                       size, align, nid);
208                 if (ret)
209                         return ret;
210
211                 /*
212                  * we always limit bottom-up allocation above the kernel,
213                  * but top-down allocation doesn't have the limit, so
214                  * retrying top-down allocation may succeed when bottom-up
215                  * allocation failed.
216                  *
217                  * bottom-up allocation is expected to be fail very rarely,
218                  * so we use WARN_ONCE() here to see the stack trace if
219                  * fail happens.
220                  */
221                 WARN_ONCE(1, "memblock: bottom-up allocation failed, "
222                              "memory hotunplug may be affected\n");
223         }
224
225         return __memblock_find_range_top_down(start, end, size, align, nid);
226 }
227
228 /**
229  * memblock_find_in_range - find free area in given range
230  * @start: start of candidate range
231  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
232  * @size: size of free area to find
233  * @align: alignment of free area to find
234  *
235  * Find @size free area aligned to @align in the specified range.
236  *
237  * RETURNS:
238  * Found address on success, 0 on failure.
239  */
240 phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
241                                         phys_addr_t end, phys_addr_t size,
242                                         phys_addr_t align)
243 {
244         return memblock_find_in_range_node(size, align, start, end,
245                                             MAX_NUMNODES);
246 }
247
248 static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
249 {
250         type->total_size -= type->regions[r].size;
251         memmove(&type->regions[r], &type->regions[r + 1],
252                 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
253         type->cnt--;
254
255         /* Special case for empty arrays */
256         if (type->cnt == 0) {
257                 WARN_ON(type->total_size != 0);
258                 type->cnt = 1;
259                 type->regions[0].base = 0;
260                 type->regions[0].size = 0;
261                 type->regions[0].flags = 0;
262                 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
263         }
264 }
265
266 phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info(
267                                         phys_addr_t *addr)
268 {
269         if (memblock.reserved.regions == memblock_reserved_init_regions)
270                 return 0;
271
272         /*
273          * Don't allow nobootmem allocator to free reserved memory regions
274          * array if
275          *  - CONFIG_DEBUG_FS is enabled;
276          *  - CONFIG_ARCH_DISCARD_MEMBLOCK is not enabled;
277          *  - reserved memory regions array have been resized during boot.
278          * Otherwise debug_fs entry "sys/kernel/debug/memblock/reserved"
279          * will show garbage instead of state of memory reservations.
280          */
281         if (IS_ENABLED(CONFIG_DEBUG_FS) &&
282             !IS_ENABLED(CONFIG_ARCH_DISCARD_MEMBLOCK))
283                 return 0;
284
285         *addr = __pa(memblock.reserved.regions);
286
287         return PAGE_ALIGN(sizeof(struct memblock_region) *
288                           memblock.reserved.max);
289 }
290
291 /**
292  * memblock_double_array - double the size of the memblock regions array
293  * @type: memblock type of the regions array being doubled
294  * @new_area_start: starting address of memory range to avoid overlap with
295  * @new_area_size: size of memory range to avoid overlap with
296  *
297  * Double the size of the @type regions array. If memblock is being used to
298  * allocate memory for a new reserved regions array and there is a previously
299  * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
300  * waiting to be reserved, ensure the memory used by the new array does
301  * not overlap.
302  *
303  * RETURNS:
304  * 0 on success, -1 on failure.
305  */
306 static int __init_memblock memblock_double_array(struct memblock_type *type,
307                                                 phys_addr_t new_area_start,
308                                                 phys_addr_t new_area_size)
309 {
310         struct memblock_region *new_array, *old_array;
311         phys_addr_t old_alloc_size, new_alloc_size;
312         phys_addr_t old_size, new_size, addr;
313         int use_slab = slab_is_available();
314         int *in_slab;
315
316         /* We don't allow resizing until we know about the reserved regions
317          * of memory that aren't suitable for allocation
318          */
319         if (!memblock_can_resize)
320                 return -1;
321
322         /* Calculate new doubled size */
323         old_size = type->max * sizeof(struct memblock_region);
324         new_size = old_size << 1;
325         /*
326          * We need to allocated new one align to PAGE_SIZE,
327          *   so we can free them completely later.
328          */
329         old_alloc_size = PAGE_ALIGN(old_size);
330         new_alloc_size = PAGE_ALIGN(new_size);
331
332         /* Retrieve the slab flag */
333         if (type == &memblock.memory)
334                 in_slab = &memblock_memory_in_slab;
335         else
336                 in_slab = &memblock_reserved_in_slab;
337
338         /* Try to find some space for it.
339          *
340          * WARNING: We assume that either slab_is_available() and we use it or
341          * we use MEMBLOCK for allocations. That means that this is unsafe to
342          * use when bootmem is currently active (unless bootmem itself is
343          * implemented on top of MEMBLOCK which isn't the case yet)
344          *
345          * This should however not be an issue for now, as we currently only
346          * call into MEMBLOCK while it's still active, or much later when slab
347          * is active for memory hotplug operations
348          */
349         if (use_slab) {
350                 new_array = kmalloc(new_size, GFP_KERNEL);
351                 addr = new_array ? __pa(new_array) : 0;
352         } else {
353                 /* only exclude range when trying to double reserved.regions */
354                 if (type != &memblock.reserved)
355                         new_area_start = new_area_size = 0;
356
357                 addr = memblock_find_in_range(new_area_start + new_area_size,
358                                                 memblock.current_limit,
359                                                 new_alloc_size, PAGE_SIZE);
360                 if (!addr && new_area_size)
361                         addr = memblock_find_in_range(0,
362                                 min(new_area_start, memblock.current_limit),
363                                 new_alloc_size, PAGE_SIZE);
364
365                 new_array = addr ? __va(addr) : NULL;
366         }
367         if (!addr) {
368                 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
369                        memblock_type_name(type), type->max, type->max * 2);
370                 return -1;
371         }
372
373         memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
374                         memblock_type_name(type), type->max * 2, (u64)addr,
375                         (u64)addr + new_size - 1);
376
377         /*
378          * Found space, we now need to move the array over before we add the
379          * reserved region since it may be our reserved array itself that is
380          * full.
381          */
382         memcpy(new_array, type->regions, old_size);
383         memset(new_array + type->max, 0, old_size);
384         old_array = type->regions;
385         type->regions = new_array;
386         type->max <<= 1;
387
388         /* Free old array. We needn't free it if the array is the static one */
389         if (*in_slab)
390                 kfree(old_array);
391         else if (old_array != memblock_memory_init_regions &&
392                  old_array != memblock_reserved_init_regions)
393                 memblock_free(__pa(old_array), old_alloc_size);
394
395         /*
396          * Reserve the new array if that comes from the memblock.  Otherwise, we
397          * needn't do it
398          */
399         if (!use_slab)
400                 BUG_ON(memblock_reserve(addr, new_alloc_size));
401
402         /* Update slab flag */
403         *in_slab = use_slab;
404
405         return 0;
406 }
407
408 /**
409  * memblock_merge_regions - merge neighboring compatible regions
410  * @type: memblock type to scan
411  *
412  * Scan @type and merge neighboring compatible regions.
413  */
414 static void __init_memblock memblock_merge_regions(struct memblock_type *type)
415 {
416         int i = 0;
417
418         /* cnt never goes below 1 */
419         while (i < type->cnt - 1) {
420                 struct memblock_region *this = &type->regions[i];
421                 struct memblock_region *next = &type->regions[i + 1];
422
423                 if (this->base + this->size != next->base ||
424                     memblock_get_region_node(this) !=
425                     memblock_get_region_node(next) ||
426                     this->flags != next->flags) {
427                         BUG_ON(this->base + this->size > next->base);
428                         i++;
429                         continue;
430                 }
431
432                 this->size += next->size;
433                 /* move forward from next + 1, index of which is i + 2 */
434                 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
435                 type->cnt--;
436         }
437 }
438
439 /**
440  * memblock_insert_region - insert new memblock region
441  * @type:       memblock type to insert into
442  * @idx:        index for the insertion point
443  * @base:       base address of the new region
444  * @size:       size of the new region
445  * @nid:        node id of the new region
446  * @flags:      flags of the new region
447  *
448  * Insert new memblock region [@base,@base+@size) into @type at @idx.
449  * @type must already have extra room to accomodate the new region.
450  */
451 static void __init_memblock memblock_insert_region(struct memblock_type *type,
452                                                    int idx, phys_addr_t base,
453                                                    phys_addr_t size,
454                                                    int nid, unsigned long flags)
455 {
456         struct memblock_region *rgn = &type->regions[idx];
457
458         BUG_ON(type->cnt >= type->max);
459         memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
460         rgn->base = base;
461         rgn->size = size;
462         rgn->flags = flags;
463         memblock_set_region_node(rgn, nid);
464         type->cnt++;
465         type->total_size += size;
466 }
467
468 /**
469  * memblock_add_region - add new memblock region
470  * @type: memblock type to add new region into
471  * @base: base address of the new region
472  * @size: size of the new region
473  * @nid: nid of the new region
474  * @flags: flags of the new region
475  *
476  * Add new memblock region [@base,@base+@size) into @type.  The new region
477  * is allowed to overlap with existing ones - overlaps don't affect already
478  * existing regions.  @type is guaranteed to be minimal (all neighbouring
479  * compatible regions are merged) after the addition.
480  *
481  * RETURNS:
482  * 0 on success, -errno on failure.
483  */
484 static int __init_memblock memblock_add_region(struct memblock_type *type,
485                                 phys_addr_t base, phys_addr_t size,
486                                 int nid, unsigned long flags)
487 {
488         bool insert = false;
489         phys_addr_t obase = base;
490         phys_addr_t end = base + memblock_cap_size(base, &size);
491         int i, nr_new;
492
493         if (!size)
494                 return 0;
495
496         /* special case for empty array */
497         if (type->regions[0].size == 0) {
498                 WARN_ON(type->cnt != 1 || type->total_size);
499                 type->regions[0].base = base;
500                 type->regions[0].size = size;
501                 type->regions[0].flags = flags;
502                 memblock_set_region_node(&type->regions[0], nid);
503                 type->total_size = size;
504                 return 0;
505         }
506 repeat:
507         /*
508          * The following is executed twice.  Once with %false @insert and
509          * then with %true.  The first counts the number of regions needed
510          * to accomodate the new area.  The second actually inserts them.
511          */
512         base = obase;
513         nr_new = 0;
514
515         for (i = 0; i < type->cnt; i++) {
516                 struct memblock_region *rgn = &type->regions[i];
517                 phys_addr_t rbase = rgn->base;
518                 phys_addr_t rend = rbase + rgn->size;
519
520                 if (rbase >= end)
521                         break;
522                 if (rend <= base)
523                         continue;
524                 /*
525                  * @rgn overlaps.  If it separates the lower part of new
526                  * area, insert that portion.
527                  */
528                 if (rbase > base) {
529                         nr_new++;
530                         if (insert)
531                                 memblock_insert_region(type, i++, base,
532                                                        rbase - base, nid,
533                                                        flags);
534                 }
535                 /* area below @rend is dealt with, forget about it */
536                 base = min(rend, end);
537         }
538
539         /* insert the remaining portion */
540         if (base < end) {
541                 nr_new++;
542                 if (insert)
543                         memblock_insert_region(type, i, base, end - base,
544                                                nid, flags);
545         }
546
547         /*
548          * If this was the first round, resize array and repeat for actual
549          * insertions; otherwise, merge and return.
550          */
551         if (!insert) {
552                 while (type->cnt + nr_new > type->max)
553                         if (memblock_double_array(type, obase, size) < 0)
554                                 return -ENOMEM;
555                 insert = true;
556                 goto repeat;
557         } else {
558                 memblock_merge_regions(type);
559                 return 0;
560         }
561 }
562
563 int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
564                                        int nid)
565 {
566         return memblock_add_region(&memblock.memory, base, size, nid, 0);
567 }
568
569 int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
570 {
571         return memblock_add_region(&memblock.memory, base, size,
572                                    MAX_NUMNODES, 0);
573 }
574
575 /**
576  * memblock_isolate_range - isolate given range into disjoint memblocks
577  * @type: memblock type to isolate range for
578  * @base: base of range to isolate
579  * @size: size of range to isolate
580  * @start_rgn: out parameter for the start of isolated region
581  * @end_rgn: out parameter for the end of isolated region
582  *
583  * Walk @type and ensure that regions don't cross the boundaries defined by
584  * [@base,@base+@size).  Crossing regions are split at the boundaries,
585  * which may create at most two more regions.  The index of the first
586  * region inside the range is returned in *@start_rgn and end in *@end_rgn.
587  *
588  * RETURNS:
589  * 0 on success, -errno on failure.
590  */
591 static int __init_memblock memblock_isolate_range(struct memblock_type *type,
592                                         phys_addr_t base, phys_addr_t size,
593                                         int *start_rgn, int *end_rgn)
594 {
595         phys_addr_t end = base + memblock_cap_size(base, &size);
596         int i;
597
598         *start_rgn = *end_rgn = 0;
599
600         if (!size)
601                 return 0;
602
603         /* we'll create at most two more regions */
604         while (type->cnt + 2 > type->max)
605                 if (memblock_double_array(type, base, size) < 0)
606                         return -ENOMEM;
607
608         for (i = 0; i < type->cnt; i++) {
609                 struct memblock_region *rgn = &type->regions[i];
610                 phys_addr_t rbase = rgn->base;
611                 phys_addr_t rend = rbase + rgn->size;
612
613                 if (rbase >= end)
614                         break;
615                 if (rend <= base)
616                         continue;
617
618                 if (rbase < base) {
619                         /*
620                          * @rgn intersects from below.  Split and continue
621                          * to process the next region - the new top half.
622                          */
623                         rgn->base = base;
624                         rgn->size -= base - rbase;
625                         type->total_size -= base - rbase;
626                         memblock_insert_region(type, i, rbase, base - rbase,
627                                                memblock_get_region_node(rgn),
628                                                rgn->flags);
629                 } else if (rend > end) {
630                         /*
631                          * @rgn intersects from above.  Split and redo the
632                          * current region - the new bottom half.
633                          */
634                         rgn->base = end;
635                         rgn->size -= end - rbase;
636                         type->total_size -= end - rbase;
637                         memblock_insert_region(type, i--, rbase, end - rbase,
638                                                memblock_get_region_node(rgn),
639                                                rgn->flags);
640                 } else {
641                         /* @rgn is fully contained, record it */
642                         if (!*end_rgn)
643                                 *start_rgn = i;
644                         *end_rgn = i + 1;
645                 }
646         }
647
648         return 0;
649 }
650
651 static int __init_memblock __memblock_remove(struct memblock_type *type,
652                                              phys_addr_t base, phys_addr_t size)
653 {
654         int start_rgn, end_rgn;
655         int i, ret;
656
657         ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
658         if (ret)
659                 return ret;
660
661         for (i = end_rgn - 1; i >= start_rgn; i--)
662                 memblock_remove_region(type, i);
663         return 0;
664 }
665
666 int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
667 {
668         return __memblock_remove(&memblock.memory, base, size);
669 }
670
671 int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
672 {
673         memblock_dbg("   memblock_free: [%#016llx-%#016llx] %pF\n",
674                      (unsigned long long)base,
675                      (unsigned long long)base + size - 1,
676                      (void *)_RET_IP_);
677
678         return __memblock_remove(&memblock.reserved, base, size);
679 }
680
681 static int __init_memblock memblock_reserve_region(phys_addr_t base,
682                                                    phys_addr_t size,
683                                                    int nid,
684                                                    unsigned long flags)
685 {
686         struct memblock_type *_rgn = &memblock.reserved;
687
688         memblock_dbg("memblock_reserve: [%#016llx-%#016llx] flags %#02lx %pF\n",
689                      (unsigned long long)base,
690                      (unsigned long long)base + size - 1,
691                      flags, (void *)_RET_IP_);
692
693         return memblock_add_region(_rgn, base, size, nid, flags);
694 }
695
696 int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
697 {
698         return memblock_reserve_region(base, size, MAX_NUMNODES, 0);
699 }
700
701 /**
702  * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
703  * @base: the base phys addr of the region
704  * @size: the size of the region
705  *
706  * This function isolates region [@base, @base + @size), and mark it with flag
707  * MEMBLOCK_HOTPLUG.
708  *
709  * Return 0 on succees, -errno on failure.
710  */
711 int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
712 {
713         struct memblock_type *type = &memblock.memory;
714         int i, ret, start_rgn, end_rgn;
715
716         ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
717         if (ret)
718                 return ret;
719
720         for (i = start_rgn; i < end_rgn; i++)
721                 memblock_set_region_flags(&type->regions[i], MEMBLOCK_HOTPLUG);
722
723         memblock_merge_regions(type);
724         return 0;
725 }
726
727 /**
728  * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
729  * @base: the base phys addr of the region
730  * @size: the size of the region
731  *
732  * This function isolates region [@base, @base + @size), and clear flag
733  * MEMBLOCK_HOTPLUG for the isolated regions.
734  *
735  * Return 0 on succees, -errno on failure.
736  */
737 int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
738 {
739         struct memblock_type *type = &memblock.memory;
740         int i, ret, start_rgn, end_rgn;
741
742         ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
743         if (ret)
744                 return ret;
745
746         for (i = start_rgn; i < end_rgn; i++)
747                 memblock_clear_region_flags(&type->regions[i],
748                                             MEMBLOCK_HOTPLUG);
749
750         memblock_merge_regions(type);
751         return 0;
752 }
753
754 /**
755  * __next_free_mem_range - next function for for_each_free_mem_range()
756  * @idx: pointer to u64 loop variable
757  * @nid: node selector, %MAX_NUMNODES for all nodes
758  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
759  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
760  * @out_nid: ptr to int for nid of the range, can be %NULL
761  *
762  * Find the first free area from *@idx which matches @nid, fill the out
763  * parameters, and update *@idx for the next iteration.  The lower 32bit of
764  * *@idx contains index into memory region and the upper 32bit indexes the
765  * areas before each reserved region.  For example, if reserved regions
766  * look like the following,
767  *
768  *      0:[0-16), 1:[32-48), 2:[128-130)
769  *
770  * The upper 32bit indexes the following regions.
771  *
772  *      0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
773  *
774  * As both region arrays are sorted, the function advances the two indices
775  * in lockstep and returns each intersection.
776  */
777 void __init_memblock __next_free_mem_range(u64 *idx, int nid,
778                                            phys_addr_t *out_start,
779                                            phys_addr_t *out_end, int *out_nid)
780 {
781         struct memblock_type *mem = &memblock.memory;
782         struct memblock_type *rsv = &memblock.reserved;
783         int mi = *idx & 0xffffffff;
784         int ri = *idx >> 32;
785
786         for ( ; mi < mem->cnt; mi++) {
787                 struct memblock_region *m = &mem->regions[mi];
788                 phys_addr_t m_start = m->base;
789                 phys_addr_t m_end = m->base + m->size;
790
791                 /* only memory regions are associated with nodes, check it */
792                 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
793                         continue;
794
795                 /* scan areas before each reservation for intersection */
796                 for ( ; ri < rsv->cnt + 1; ri++) {
797                         struct memblock_region *r = &rsv->regions[ri];
798                         phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
799                         phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
800
801                         /* if ri advanced past mi, break out to advance mi */
802                         if (r_start >= m_end)
803                                 break;
804                         /* if the two regions intersect, we're done */
805                         if (m_start < r_end) {
806                                 if (out_start)
807                                         *out_start = max(m_start, r_start);
808                                 if (out_end)
809                                         *out_end = min(m_end, r_end);
810                                 if (out_nid)
811                                         *out_nid = memblock_get_region_node(m);
812                                 /*
813                                  * The region which ends first is advanced
814                                  * for the next iteration.
815                                  */
816                                 if (m_end <= r_end)
817                                         mi++;
818                                 else
819                                         ri++;
820                                 *idx = (u32)mi | (u64)ri << 32;
821                                 return;
822                         }
823                 }
824         }
825
826         /* signal end of iteration */
827         *idx = ULLONG_MAX;
828 }
829
830 /**
831  * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
832  * @idx: pointer to u64 loop variable
833  * @nid: nid: node selector, %MAX_NUMNODES for all nodes
834  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
835  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
836  * @out_nid: ptr to int for nid of the range, can be %NULL
837  *
838  * Reverse of __next_free_mem_range().
839  *
840  * Linux kernel cannot migrate pages used by itself. Memory hotplug users won't
841  * be able to hot-remove hotpluggable memory used by the kernel. So this
842  * function skip hotpluggable regions if needed when allocating memory for the
843  * kernel.
844  */
845 void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
846                                            phys_addr_t *out_start,
847                                            phys_addr_t *out_end, int *out_nid)
848 {
849         struct memblock_type *mem = &memblock.memory;
850         struct memblock_type *rsv = &memblock.reserved;
851         int mi = *idx & 0xffffffff;
852         int ri = *idx >> 32;
853
854         if (*idx == (u64)ULLONG_MAX) {
855                 mi = mem->cnt - 1;
856                 ri = rsv->cnt;
857         }
858
859         for ( ; mi >= 0; mi--) {
860                 struct memblock_region *m = &mem->regions[mi];
861                 phys_addr_t m_start = m->base;
862                 phys_addr_t m_end = m->base + m->size;
863
864                 /* only memory regions are associated with nodes, check it */
865                 if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
866                         continue;
867
868                 /* skip hotpluggable memory regions if needed */
869                 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
870                         continue;
871
872                 /* scan areas before each reservation for intersection */
873                 for ( ; ri >= 0; ri--) {
874                         struct memblock_region *r = &rsv->regions[ri];
875                         phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
876                         phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
877
878                         /* if ri advanced past mi, break out to advance mi */
879                         if (r_end <= m_start)
880                                 break;
881                         /* if the two regions intersect, we're done */
882                         if (m_end > r_start) {
883                                 if (out_start)
884                                         *out_start = max(m_start, r_start);
885                                 if (out_end)
886                                         *out_end = min(m_end, r_end);
887                                 if (out_nid)
888                                         *out_nid = memblock_get_region_node(m);
889
890                                 if (m_start >= r_start)
891                                         mi--;
892                                 else
893                                         ri--;
894                                 *idx = (u32)mi | (u64)ri << 32;
895                                 return;
896                         }
897                 }
898         }
899
900         *idx = ULLONG_MAX;
901 }
902
903 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
904 /*
905  * Common iterator interface used to define for_each_mem_range().
906  */
907 void __init_memblock __next_mem_pfn_range(int *idx, int nid,
908                                 unsigned long *out_start_pfn,
909                                 unsigned long *out_end_pfn, int *out_nid)
910 {
911         struct memblock_type *type = &memblock.memory;
912         struct memblock_region *r;
913
914         while (++*idx < type->cnt) {
915                 r = &type->regions[*idx];
916
917                 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
918                         continue;
919                 if (nid == MAX_NUMNODES || nid == r->nid)
920                         break;
921         }
922         if (*idx >= type->cnt) {
923                 *idx = -1;
924                 return;
925         }
926
927         if (out_start_pfn)
928                 *out_start_pfn = PFN_UP(r->base);
929         if (out_end_pfn)
930                 *out_end_pfn = PFN_DOWN(r->base + r->size);
931         if (out_nid)
932                 *out_nid = r->nid;
933 }
934
935 /**
936  * memblock_set_node - set node ID on memblock regions
937  * @base: base of area to set node ID for
938  * @size: size of area to set node ID for
939  * @type: memblock type to set node ID for
940  * @nid: node ID to set
941  *
942  * Set the nid of memblock @type regions in [@base,@base+@size) to @nid.
943  * Regions which cross the area boundaries are split as necessary.
944  *
945  * RETURNS:
946  * 0 on success, -errno on failure.
947  */
948 int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
949                                       struct memblock_type *type, int nid)
950 {
951         int start_rgn, end_rgn;
952         int i, ret;
953
954         ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
955         if (ret)
956                 return ret;
957
958         for (i = start_rgn; i < end_rgn; i++)
959                 memblock_set_region_node(&type->regions[i], nid);
960
961         memblock_merge_regions(type);
962         return 0;
963 }
964 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
965
966 static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
967                                         phys_addr_t align, phys_addr_t max_addr,
968                                         int nid)
969 {
970         phys_addr_t found;
971
972         if (!align)
973                 align = SMP_CACHE_BYTES;
974
975         /* align @size to avoid excessive fragmentation on reserved array */
976         size = round_up(size, align);
977
978         found = memblock_find_in_range_node(size, align, 0, max_addr, nid);
979         if (found && !memblock_reserve(found, size))
980                 return found;
981
982         return 0;
983 }
984
985 phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
986 {
987         return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
988 }
989
990 phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
991 {
992         return memblock_alloc_base_nid(size, align, max_addr, MAX_NUMNODES);
993 }
994
995 phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
996 {
997         phys_addr_t alloc;
998
999         alloc = __memblock_alloc_base(size, align, max_addr);
1000
1001         if (alloc == 0)
1002                 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
1003                       (unsigned long long) size, (unsigned long long) max_addr);
1004
1005         return alloc;
1006 }
1007
1008 phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
1009 {
1010         return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
1011 }
1012
1013 phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1014 {
1015         phys_addr_t res = memblock_alloc_nid(size, align, nid);
1016
1017         if (res)
1018                 return res;
1019         return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
1020 }
1021
1022
1023 /*
1024  * Remaining API functions
1025  */
1026
1027 phys_addr_t __init memblock_phys_mem_size(void)
1028 {
1029         return memblock.memory.total_size;
1030 }
1031
1032 phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1033 {
1034         unsigned long pages = 0;
1035         struct memblock_region *r;
1036         unsigned long start_pfn, end_pfn;
1037
1038         for_each_memblock(memory, r) {
1039                 start_pfn = memblock_region_memory_base_pfn(r);
1040                 end_pfn = memblock_region_memory_end_pfn(r);
1041                 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1042                 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1043                 pages += end_pfn - start_pfn;
1044         }
1045
1046         return (phys_addr_t)pages << PAGE_SHIFT;
1047 }
1048
1049 /* lowest address */
1050 phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1051 {
1052         return memblock.memory.regions[0].base;
1053 }
1054
1055 phys_addr_t __init_memblock memblock_end_of_DRAM(void)
1056 {
1057         int idx = memblock.memory.cnt - 1;
1058
1059         return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
1060 }
1061
1062 void __init memblock_enforce_memory_limit(phys_addr_t limit)
1063 {
1064         unsigned long i;
1065         phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
1066
1067         if (!limit)
1068                 return;
1069
1070         /* find out max address */
1071         for (i = 0; i < memblock.memory.cnt; i++) {
1072                 struct memblock_region *r = &memblock.memory.regions[i];
1073
1074                 if (limit <= r->size) {
1075                         max_addr = r->base + limit;
1076                         break;
1077                 }
1078                 limit -= r->size;
1079         }
1080
1081         /* truncate both memory and reserved regions */
1082         __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
1083         __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
1084 }
1085
1086 static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
1087 {
1088         unsigned int left = 0, right = type->cnt;
1089
1090         do {
1091                 unsigned int mid = (right + left) / 2;
1092
1093                 if (addr < type->regions[mid].base)
1094                         right = mid;
1095                 else if (addr >= (type->regions[mid].base +
1096                                   type->regions[mid].size))
1097                         left = mid + 1;
1098                 else
1099                         return mid;
1100         } while (left < right);
1101         return -1;
1102 }
1103
1104 int __init memblock_is_reserved(phys_addr_t addr)
1105 {
1106         return memblock_search(&memblock.reserved, addr) != -1;
1107 }
1108
1109 int __init_memblock memblock_is_memory(phys_addr_t addr)
1110 {
1111         return memblock_search(&memblock.memory, addr) != -1;
1112 }
1113
1114 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1115 int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1116                          unsigned long *start_pfn, unsigned long *end_pfn)
1117 {
1118         struct memblock_type *type = &memblock.memory;
1119         int mid = memblock_search(type, (phys_addr_t)pfn << PAGE_SHIFT);
1120
1121         if (mid == -1)
1122                 return -1;
1123
1124         *start_pfn = type->regions[mid].base >> PAGE_SHIFT;
1125         *end_pfn = (type->regions[mid].base + type->regions[mid].size)
1126                         >> PAGE_SHIFT;
1127
1128         return type->regions[mid].nid;
1129 }
1130 #endif
1131
1132 /**
1133  * memblock_is_region_memory - check if a region is a subset of memory
1134  * @base: base of region to check
1135  * @size: size of region to check
1136  *
1137  * Check if the region [@base, @base+@size) is a subset of a memory block.
1138  *
1139  * RETURNS:
1140  * 0 if false, non-zero if true
1141  */
1142 int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
1143 {
1144         int idx = memblock_search(&memblock.memory, base);
1145         phys_addr_t end = base + memblock_cap_size(base, &size);
1146
1147         if (idx == -1)
1148                 return 0;
1149         return memblock.memory.regions[idx].base <= base &&
1150                 (memblock.memory.regions[idx].base +
1151                  memblock.memory.regions[idx].size) >= end;
1152 }
1153
1154 /**
1155  * memblock_is_region_reserved - check if a region intersects reserved memory
1156  * @base: base of region to check
1157  * @size: size of region to check
1158  *
1159  * Check if the region [@base, @base+@size) intersects a reserved memory block.
1160  *
1161  * RETURNS:
1162  * 0 if false, non-zero if true
1163  */
1164 int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
1165 {
1166         memblock_cap_size(base, &size);
1167         return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
1168 }
1169
1170 void __init_memblock memblock_trim_memory(phys_addr_t align)
1171 {
1172         int i;
1173         phys_addr_t start, end, orig_start, orig_end;
1174         struct memblock_type *mem = &memblock.memory;
1175
1176         for (i = 0; i < mem->cnt; i++) {
1177                 orig_start = mem->regions[i].base;
1178                 orig_end = mem->regions[i].base + mem->regions[i].size;
1179                 start = round_up(orig_start, align);
1180                 end = round_down(orig_end, align);
1181
1182                 if (start == orig_start && end == orig_end)
1183                         continue;
1184
1185                 if (start < end) {
1186                         mem->regions[i].base = start;
1187                         mem->regions[i].size = end - start;
1188                 } else {
1189                         memblock_remove_region(mem, i);
1190                         i--;
1191                 }
1192         }
1193 }
1194
1195 void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1196 {
1197         memblock.current_limit = limit;
1198 }
1199
1200 static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
1201 {
1202         unsigned long long base, size;
1203         unsigned long flags;
1204         int i;
1205
1206         pr_info(" %s.cnt  = 0x%lx\n", name, type->cnt);
1207
1208         for (i = 0; i < type->cnt; i++) {
1209                 struct memblock_region *rgn = &type->regions[i];
1210                 char nid_buf[32] = "";
1211
1212                 base = rgn->base;
1213                 size = rgn->size;
1214                 flags = rgn->flags;
1215 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1216                 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1217                         snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1218                                  memblock_get_region_node(rgn));
1219 #endif
1220                 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n",
1221                         name, i, base, base + size - 1, size, nid_buf, flags);
1222         }
1223 }
1224
1225 void __init_memblock __memblock_dump_all(void)
1226 {
1227         pr_info("MEMBLOCK configuration:\n");
1228         pr_info(" memory size = %#llx reserved size = %#llx\n",
1229                 (unsigned long long)memblock.memory.total_size,
1230                 (unsigned long long)memblock.reserved.total_size);
1231
1232         memblock_dump(&memblock.memory, "memory");
1233         memblock_dump(&memblock.reserved, "reserved");
1234 }
1235
1236 void __init memblock_allow_resize(void)
1237 {
1238         memblock_can_resize = 1;
1239 }
1240
1241 static int __init early_memblock(char *p)
1242 {
1243         if (p && strstr(p, "debug"))
1244                 memblock_debug = 1;
1245         return 0;
1246 }
1247 early_param("memblock", early_memblock);
1248
1249 #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
1250
1251 static int memblock_debug_show(struct seq_file *m, void *private)
1252 {
1253         struct memblock_type *type = m->private;
1254         struct memblock_region *reg;
1255         int i;
1256
1257         for (i = 0; i < type->cnt; i++) {
1258                 reg = &type->regions[i];
1259                 seq_printf(m, "%4d: ", i);
1260                 if (sizeof(phys_addr_t) == 4)
1261                         seq_printf(m, "0x%08lx..0x%08lx\n",
1262                                    (unsigned long)reg->base,
1263                                    (unsigned long)(reg->base + reg->size - 1));
1264                 else
1265                         seq_printf(m, "0x%016llx..0x%016llx\n",
1266                                    (unsigned long long)reg->base,
1267                                    (unsigned long long)(reg->base + reg->size - 1));
1268
1269         }
1270         return 0;
1271 }
1272
1273 static int memblock_debug_open(struct inode *inode, struct file *file)
1274 {
1275         return single_open(file, memblock_debug_show, inode->i_private);
1276 }
1277
1278 static const struct file_operations memblock_debug_fops = {
1279         .open = memblock_debug_open,
1280         .read = seq_read,
1281         .llseek = seq_lseek,
1282         .release = single_release,
1283 };
1284
1285 static int __init memblock_init_debugfs(void)
1286 {
1287         struct dentry *root = debugfs_create_dir("memblock", NULL);
1288         if (!root)
1289                 return -ENXIO;
1290         debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1291         debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
1292
1293         return 0;
1294 }
1295 __initcall(memblock_init_debugfs);
1296
1297 #endif /* CONFIG_DEBUG_FS */