mm/memblock: switch to use NUMA_NO_NODE instead of MAX_NUMNODES
[firefly-linux-kernel-4.4.55.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, %NUMA_NO_NODE 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, %NUMA_NO_NODE 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, %NUMA_NO_NODE 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                                             NUMA_NO_NODE);
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, %NUMA_NO_NODE 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         bool check_node = (nid != NUMA_NO_NODE) && (nid != MAX_NUMNODES);
786
787         if (nid == MAX_NUMNODES)
788                 pr_warn_once("%s: Usage of MAX_NUMNODES is depricated. Use NUMA_NO_NODE instead\n",
789                              __func__);
790
791         for ( ; mi < mem->cnt; mi++) {
792                 struct memblock_region *m = &mem->regions[mi];
793                 phys_addr_t m_start = m->base;
794                 phys_addr_t m_end = m->base + m->size;
795
796                 /* only memory regions are associated with nodes, check it */
797                 if (check_node && nid != memblock_get_region_node(m))
798                         continue;
799
800                 /* scan areas before each reservation for intersection */
801                 for ( ; ri < rsv->cnt + 1; ri++) {
802                         struct memblock_region *r = &rsv->regions[ri];
803                         phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
804                         phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
805
806                         /* if ri advanced past mi, break out to advance mi */
807                         if (r_start >= m_end)
808                                 break;
809                         /* if the two regions intersect, we're done */
810                         if (m_start < r_end) {
811                                 if (out_start)
812                                         *out_start = max(m_start, r_start);
813                                 if (out_end)
814                                         *out_end = min(m_end, r_end);
815                                 if (out_nid)
816                                         *out_nid = memblock_get_region_node(m);
817                                 /*
818                                  * The region which ends first is advanced
819                                  * for the next iteration.
820                                  */
821                                 if (m_end <= r_end)
822                                         mi++;
823                                 else
824                                         ri++;
825                                 *idx = (u32)mi | (u64)ri << 32;
826                                 return;
827                         }
828                 }
829         }
830
831         /* signal end of iteration */
832         *idx = ULLONG_MAX;
833 }
834
835 /**
836  * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
837  * @idx: pointer to u64 loop variable
838  * @nid: nid: node selector, %NUMA_NO_NODE for all nodes
839  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
840  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
841  * @out_nid: ptr to int for nid of the range, can be %NULL
842  *
843  * Reverse of __next_free_mem_range().
844  *
845  * Linux kernel cannot migrate pages used by itself. Memory hotplug users won't
846  * be able to hot-remove hotpluggable memory used by the kernel. So this
847  * function skip hotpluggable regions if needed when allocating memory for the
848  * kernel.
849  */
850 void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
851                                            phys_addr_t *out_start,
852                                            phys_addr_t *out_end, int *out_nid)
853 {
854         struct memblock_type *mem = &memblock.memory;
855         struct memblock_type *rsv = &memblock.reserved;
856         int mi = *idx & 0xffffffff;
857         int ri = *idx >> 32;
858         bool check_node = (nid != NUMA_NO_NODE) && (nid != MAX_NUMNODES);
859
860         if (nid == MAX_NUMNODES)
861                 pr_warn_once("%s: Usage of MAX_NUMNODES is depricated. Use NUMA_NO_NODE instead\n",
862                              __func__);
863
864         if (*idx == (u64)ULLONG_MAX) {
865                 mi = mem->cnt - 1;
866                 ri = rsv->cnt;
867         }
868
869         for ( ; mi >= 0; mi--) {
870                 struct memblock_region *m = &mem->regions[mi];
871                 phys_addr_t m_start = m->base;
872                 phys_addr_t m_end = m->base + m->size;
873
874                 /* only memory regions are associated with nodes, check it */
875                 if (check_node && nid != memblock_get_region_node(m))
876                         continue;
877
878                 /* skip hotpluggable memory regions if needed */
879                 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
880                         continue;
881
882                 /* scan areas before each reservation for intersection */
883                 for ( ; ri >= 0; ri--) {
884                         struct memblock_region *r = &rsv->regions[ri];
885                         phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
886                         phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
887
888                         /* if ri advanced past mi, break out to advance mi */
889                         if (r_end <= m_start)
890                                 break;
891                         /* if the two regions intersect, we're done */
892                         if (m_end > r_start) {
893                                 if (out_start)
894                                         *out_start = max(m_start, r_start);
895                                 if (out_end)
896                                         *out_end = min(m_end, r_end);
897                                 if (out_nid)
898                                         *out_nid = memblock_get_region_node(m);
899
900                                 if (m_start >= r_start)
901                                         mi--;
902                                 else
903                                         ri--;
904                                 *idx = (u32)mi | (u64)ri << 32;
905                                 return;
906                         }
907                 }
908         }
909
910         *idx = ULLONG_MAX;
911 }
912
913 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
914 /*
915  * Common iterator interface used to define for_each_mem_range().
916  */
917 void __init_memblock __next_mem_pfn_range(int *idx, int nid,
918                                 unsigned long *out_start_pfn,
919                                 unsigned long *out_end_pfn, int *out_nid)
920 {
921         struct memblock_type *type = &memblock.memory;
922         struct memblock_region *r;
923
924         while (++*idx < type->cnt) {
925                 r = &type->regions[*idx];
926
927                 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
928                         continue;
929                 if (nid == MAX_NUMNODES || nid == r->nid)
930                         break;
931         }
932         if (*idx >= type->cnt) {
933                 *idx = -1;
934                 return;
935         }
936
937         if (out_start_pfn)
938                 *out_start_pfn = PFN_UP(r->base);
939         if (out_end_pfn)
940                 *out_end_pfn = PFN_DOWN(r->base + r->size);
941         if (out_nid)
942                 *out_nid = r->nid;
943 }
944
945 /**
946  * memblock_set_node - set node ID on memblock regions
947  * @base: base of area to set node ID for
948  * @size: size of area to set node ID for
949  * @type: memblock type to set node ID for
950  * @nid: node ID to set
951  *
952  * Set the nid of memblock @type regions in [@base,@base+@size) to @nid.
953  * Regions which cross the area boundaries are split as necessary.
954  *
955  * RETURNS:
956  * 0 on success, -errno on failure.
957  */
958 int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
959                                       struct memblock_type *type, int nid)
960 {
961         int start_rgn, end_rgn;
962         int i, ret;
963
964         ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
965         if (ret)
966                 return ret;
967
968         for (i = start_rgn; i < end_rgn; i++)
969                 memblock_set_region_node(&type->regions[i], nid);
970
971         memblock_merge_regions(type);
972         return 0;
973 }
974 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
975
976 static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
977                                         phys_addr_t align, phys_addr_t max_addr,
978                                         int nid)
979 {
980         phys_addr_t found;
981
982         if (!align)
983                 align = SMP_CACHE_BYTES;
984
985         /* align @size to avoid excessive fragmentation on reserved array */
986         size = round_up(size, align);
987
988         found = memblock_find_in_range_node(size, align, 0, max_addr, nid);
989         if (found && !memblock_reserve(found, size))
990                 return found;
991
992         return 0;
993 }
994
995 phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
996 {
997         return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
998 }
999
1000 phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1001 {
1002         return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE);
1003 }
1004
1005 phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1006 {
1007         phys_addr_t alloc;
1008
1009         alloc = __memblock_alloc_base(size, align, max_addr);
1010
1011         if (alloc == 0)
1012                 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
1013                       (unsigned long long) size, (unsigned long long) max_addr);
1014
1015         return alloc;
1016 }
1017
1018 phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
1019 {
1020         return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
1021 }
1022
1023 phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1024 {
1025         phys_addr_t res = memblock_alloc_nid(size, align, nid);
1026
1027         if (res)
1028                 return res;
1029         return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
1030 }
1031
1032
1033 /*
1034  * Remaining API functions
1035  */
1036
1037 phys_addr_t __init memblock_phys_mem_size(void)
1038 {
1039         return memblock.memory.total_size;
1040 }
1041
1042 phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1043 {
1044         unsigned long pages = 0;
1045         struct memblock_region *r;
1046         unsigned long start_pfn, end_pfn;
1047
1048         for_each_memblock(memory, r) {
1049                 start_pfn = memblock_region_memory_base_pfn(r);
1050                 end_pfn = memblock_region_memory_end_pfn(r);
1051                 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1052                 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1053                 pages += end_pfn - start_pfn;
1054         }
1055
1056         return (phys_addr_t)pages << PAGE_SHIFT;
1057 }
1058
1059 /* lowest address */
1060 phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1061 {
1062         return memblock.memory.regions[0].base;
1063 }
1064
1065 phys_addr_t __init_memblock memblock_end_of_DRAM(void)
1066 {
1067         int idx = memblock.memory.cnt - 1;
1068
1069         return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
1070 }
1071
1072 void __init memblock_enforce_memory_limit(phys_addr_t limit)
1073 {
1074         unsigned long i;
1075         phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
1076
1077         if (!limit)
1078                 return;
1079
1080         /* find out max address */
1081         for (i = 0; i < memblock.memory.cnt; i++) {
1082                 struct memblock_region *r = &memblock.memory.regions[i];
1083
1084                 if (limit <= r->size) {
1085                         max_addr = r->base + limit;
1086                         break;
1087                 }
1088                 limit -= r->size;
1089         }
1090
1091         /* truncate both memory and reserved regions */
1092         __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
1093         __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
1094 }
1095
1096 static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
1097 {
1098         unsigned int left = 0, right = type->cnt;
1099
1100         do {
1101                 unsigned int mid = (right + left) / 2;
1102
1103                 if (addr < type->regions[mid].base)
1104                         right = mid;
1105                 else if (addr >= (type->regions[mid].base +
1106                                   type->regions[mid].size))
1107                         left = mid + 1;
1108                 else
1109                         return mid;
1110         } while (left < right);
1111         return -1;
1112 }
1113
1114 int __init memblock_is_reserved(phys_addr_t addr)
1115 {
1116         return memblock_search(&memblock.reserved, addr) != -1;
1117 }
1118
1119 int __init_memblock memblock_is_memory(phys_addr_t addr)
1120 {
1121         return memblock_search(&memblock.memory, addr) != -1;
1122 }
1123
1124 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1125 int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1126                          unsigned long *start_pfn, unsigned long *end_pfn)
1127 {
1128         struct memblock_type *type = &memblock.memory;
1129         int mid = memblock_search(type, (phys_addr_t)pfn << PAGE_SHIFT);
1130
1131         if (mid == -1)
1132                 return -1;
1133
1134         *start_pfn = type->regions[mid].base >> PAGE_SHIFT;
1135         *end_pfn = (type->regions[mid].base + type->regions[mid].size)
1136                         >> PAGE_SHIFT;
1137
1138         return type->regions[mid].nid;
1139 }
1140 #endif
1141
1142 /**
1143  * memblock_is_region_memory - check if a region is a subset of memory
1144  * @base: base of region to check
1145  * @size: size of region to check
1146  *
1147  * Check if the region [@base, @base+@size) is a subset of a memory block.
1148  *
1149  * RETURNS:
1150  * 0 if false, non-zero if true
1151  */
1152 int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
1153 {
1154         int idx = memblock_search(&memblock.memory, base);
1155         phys_addr_t end = base + memblock_cap_size(base, &size);
1156
1157         if (idx == -1)
1158                 return 0;
1159         return memblock.memory.regions[idx].base <= base &&
1160                 (memblock.memory.regions[idx].base +
1161                  memblock.memory.regions[idx].size) >= end;
1162 }
1163
1164 /**
1165  * memblock_is_region_reserved - check if a region intersects reserved memory
1166  * @base: base of region to check
1167  * @size: size of region to check
1168  *
1169  * Check if the region [@base, @base+@size) intersects a reserved memory block.
1170  *
1171  * RETURNS:
1172  * 0 if false, non-zero if true
1173  */
1174 int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
1175 {
1176         memblock_cap_size(base, &size);
1177         return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
1178 }
1179
1180 void __init_memblock memblock_trim_memory(phys_addr_t align)
1181 {
1182         int i;
1183         phys_addr_t start, end, orig_start, orig_end;
1184         struct memblock_type *mem = &memblock.memory;
1185
1186         for (i = 0; i < mem->cnt; i++) {
1187                 orig_start = mem->regions[i].base;
1188                 orig_end = mem->regions[i].base + mem->regions[i].size;
1189                 start = round_up(orig_start, align);
1190                 end = round_down(orig_end, align);
1191
1192                 if (start == orig_start && end == orig_end)
1193                         continue;
1194
1195                 if (start < end) {
1196                         mem->regions[i].base = start;
1197                         mem->regions[i].size = end - start;
1198                 } else {
1199                         memblock_remove_region(mem, i);
1200                         i--;
1201                 }
1202         }
1203 }
1204
1205 void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1206 {
1207         memblock.current_limit = limit;
1208 }
1209
1210 static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
1211 {
1212         unsigned long long base, size;
1213         unsigned long flags;
1214         int i;
1215
1216         pr_info(" %s.cnt  = 0x%lx\n", name, type->cnt);
1217
1218         for (i = 0; i < type->cnt; i++) {
1219                 struct memblock_region *rgn = &type->regions[i];
1220                 char nid_buf[32] = "";
1221
1222                 base = rgn->base;
1223                 size = rgn->size;
1224                 flags = rgn->flags;
1225 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1226                 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1227                         snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1228                                  memblock_get_region_node(rgn));
1229 #endif
1230                 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n",
1231                         name, i, base, base + size - 1, size, nid_buf, flags);
1232         }
1233 }
1234
1235 void __init_memblock __memblock_dump_all(void)
1236 {
1237         pr_info("MEMBLOCK configuration:\n");
1238         pr_info(" memory size = %#llx reserved size = %#llx\n",
1239                 (unsigned long long)memblock.memory.total_size,
1240                 (unsigned long long)memblock.reserved.total_size);
1241
1242         memblock_dump(&memblock.memory, "memory");
1243         memblock_dump(&memblock.reserved, "reserved");
1244 }
1245
1246 void __init memblock_allow_resize(void)
1247 {
1248         memblock_can_resize = 1;
1249 }
1250
1251 static int __init early_memblock(char *p)
1252 {
1253         if (p && strstr(p, "debug"))
1254                 memblock_debug = 1;
1255         return 0;
1256 }
1257 early_param("memblock", early_memblock);
1258
1259 #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
1260
1261 static int memblock_debug_show(struct seq_file *m, void *private)
1262 {
1263         struct memblock_type *type = m->private;
1264         struct memblock_region *reg;
1265         int i;
1266
1267         for (i = 0; i < type->cnt; i++) {
1268                 reg = &type->regions[i];
1269                 seq_printf(m, "%4d: ", i);
1270                 if (sizeof(phys_addr_t) == 4)
1271                         seq_printf(m, "0x%08lx..0x%08lx\n",
1272                                    (unsigned long)reg->base,
1273                                    (unsigned long)(reg->base + reg->size - 1));
1274                 else
1275                         seq_printf(m, "0x%016llx..0x%016llx\n",
1276                                    (unsigned long long)reg->base,
1277                                    (unsigned long long)(reg->base + reg->size - 1));
1278
1279         }
1280         return 0;
1281 }
1282
1283 static int memblock_debug_open(struct inode *inode, struct file *file)
1284 {
1285         return single_open(file, memblock_debug_show, inode->i_private);
1286 }
1287
1288 static const struct file_operations memblock_debug_fops = {
1289         .open = memblock_debug_open,
1290         .read = seq_read,
1291         .llseek = seq_lseek,
1292         .release = single_release,
1293 };
1294
1295 static int __init memblock_init_debugfs(void)
1296 {
1297         struct dentry *root = debugfs_create_dir("memblock", NULL);
1298         if (!root)
1299                 return -ENXIO;
1300         debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1301         debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
1302
1303         return 0;
1304 }
1305 __initcall(memblock_init_debugfs);
1306
1307 #endif /* CONFIG_DEBUG_FS */