]> git.karo-electronics.de Git - karo-tx-linux.git/blob - mm/memblock.c
memblock: let memblock_type_name know about physmem type
[karo-tx-linux.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/sections.h>
24 #include <linux/io.h>
25
26 #include "internal.h"
27
28 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
29 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
30 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
31 static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
32 #endif
33
34 struct memblock memblock __initdata_memblock = {
35         .memory.regions         = memblock_memory_init_regions,
36         .memory.cnt             = 1,    /* empty dummy entry */
37         .memory.max             = INIT_MEMBLOCK_REGIONS,
38
39         .reserved.regions       = memblock_reserved_init_regions,
40         .reserved.cnt           = 1,    /* empty dummy entry */
41         .reserved.max           = INIT_MEMBLOCK_REGIONS,
42
43 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
44         .physmem.regions        = memblock_physmem_init_regions,
45         .physmem.cnt            = 1,    /* empty dummy entry */
46         .physmem.max            = INIT_PHYSMEM_REGIONS,
47 #endif
48
49         .bottom_up              = false,
50         .current_limit          = MEMBLOCK_ALLOC_ANYWHERE,
51 };
52
53 int memblock_debug __initdata_memblock;
54 #ifdef CONFIG_MOVABLE_NODE
55 bool movable_node_enabled __initdata_memblock = false;
56 #endif
57 static bool system_has_some_mirror __initdata_memblock = false;
58 static int memblock_can_resize __initdata_memblock;
59 static int memblock_memory_in_slab __initdata_memblock = 0;
60 static int memblock_reserved_in_slab __initdata_memblock = 0;
61
62 ulong __init_memblock choose_memblock_flags(void)
63 {
64         return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
65 }
66
67 /* inline so we don't get a warning when pr_debug is compiled out */
68 static __init_memblock const char *
69 memblock_type_name(struct memblock_type *type)
70 {
71         if (type == &memblock.memory)
72                 return "memory";
73         else if (type == &memblock.reserved)
74                 return "reserved";
75 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
76         else if (type == &memblock.physmem)
77                 return "physmem";
78 #endif
79         else
80                 return "unknown";
81 }
82
83 /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
84 static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
85 {
86         return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
87 }
88
89 /*
90  * Address comparison utilities
91  */
92 static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
93                                        phys_addr_t base2, phys_addr_t size2)
94 {
95         return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
96 }
97
98 bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
99                                         phys_addr_t base, phys_addr_t size)
100 {
101         unsigned long i;
102
103         for (i = 0; i < type->cnt; i++)
104                 if (memblock_addrs_overlap(base, size, type->regions[i].base,
105                                            type->regions[i].size))
106                         break;
107         return i < type->cnt;
108 }
109
110 /*
111  * __memblock_find_range_bottom_up - find free area utility in bottom-up
112  * @start: start of candidate range
113  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
114  * @size: size of free area to find
115  * @align: alignment of free area to find
116  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
117  * @flags: pick from blocks based on memory attributes
118  *
119  * Utility called from memblock_find_in_range_node(), find free area bottom-up.
120  *
121  * RETURNS:
122  * Found address on success, 0 on failure.
123  */
124 static phys_addr_t __init_memblock
125 __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
126                                 phys_addr_t size, phys_addr_t align, int nid,
127                                 ulong flags)
128 {
129         phys_addr_t this_start, this_end, cand;
130         u64 i;
131
132         for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
133                 this_start = clamp(this_start, start, end);
134                 this_end = clamp(this_end, start, end);
135
136                 cand = round_up(this_start, align);
137                 if (cand < this_end && this_end - cand >= size)
138                         return cand;
139         }
140
141         return 0;
142 }
143
144 /**
145  * __memblock_find_range_top_down - find free area utility, in top-down
146  * @start: start of candidate range
147  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
148  * @size: size of free area to find
149  * @align: alignment of free area to find
150  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
151  * @flags: pick from blocks based on memory attributes
152  *
153  * Utility called from memblock_find_in_range_node(), find free area top-down.
154  *
155  * RETURNS:
156  * Found address on success, 0 on failure.
157  */
158 static phys_addr_t __init_memblock
159 __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
160                                phys_addr_t size, phys_addr_t align, int nid,
161                                ulong flags)
162 {
163         phys_addr_t this_start, this_end, cand;
164         u64 i;
165
166         for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
167                                         NULL) {
168                 this_start = clamp(this_start, start, end);
169                 this_end = clamp(this_end, start, end);
170
171                 if (this_end < size)
172                         continue;
173
174                 cand = round_down(this_end - size, align);
175                 if (cand >= this_start)
176                         return cand;
177         }
178
179         return 0;
180 }
181
182 /**
183  * memblock_find_in_range_node - find free area in given range and node
184  * @size: size of free area to find
185  * @align: alignment of free area to find
186  * @start: start of candidate range
187  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
188  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
189  * @flags: pick from blocks based on memory attributes
190  *
191  * Find @size free area aligned to @align in the specified range and node.
192  *
193  * When allocation direction is bottom-up, the @start should be greater
194  * than the end of the kernel image. Otherwise, it will be trimmed. The
195  * reason is that we want the bottom-up allocation just near the kernel
196  * image so it is highly likely that the allocated memory and the kernel
197  * will reside in the same node.
198  *
199  * If bottom-up allocation failed, will try to allocate memory top-down.
200  *
201  * RETURNS:
202  * Found address on success, 0 on failure.
203  */
204 phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
205                                         phys_addr_t align, phys_addr_t start,
206                                         phys_addr_t end, int nid, ulong flags)
207 {
208         phys_addr_t kernel_end, ret;
209
210         /* pump up @end */
211         if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
212                 end = memblock.current_limit;
213
214         /* avoid allocating the first page */
215         start = max_t(phys_addr_t, start, PAGE_SIZE);
216         end = max(start, end);
217         kernel_end = __pa_symbol(_end);
218
219         /*
220          * try bottom-up allocation only when bottom-up mode
221          * is set and @end is above the kernel image.
222          */
223         if (memblock_bottom_up() && end > kernel_end) {
224                 phys_addr_t bottom_up_start;
225
226                 /* make sure we will allocate above the kernel */
227                 bottom_up_start = max(start, kernel_end);
228
229                 /* ok, try bottom-up allocation first */
230                 ret = __memblock_find_range_bottom_up(bottom_up_start, end,
231                                                       size, align, nid, flags);
232                 if (ret)
233                         return ret;
234
235                 /*
236                  * we always limit bottom-up allocation above the kernel,
237                  * but top-down allocation doesn't have the limit, so
238                  * retrying top-down allocation may succeed when bottom-up
239                  * allocation failed.
240                  *
241                  * bottom-up allocation is expected to be fail very rarely,
242                  * so we use WARN_ONCE() here to see the stack trace if
243                  * fail happens.
244                  */
245                 WARN_ONCE(1, "memblock: bottom-up allocation failed, memory hotunplug may be affected\n");
246         }
247
248         return __memblock_find_range_top_down(start, end, size, align, nid,
249                                               flags);
250 }
251
252 /**
253  * memblock_find_in_range - find free area in given range
254  * @start: start of candidate range
255  * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
256  * @size: size of free area to find
257  * @align: alignment of free area to find
258  *
259  * Find @size free area aligned to @align in the specified range.
260  *
261  * RETURNS:
262  * Found address on success, 0 on failure.
263  */
264 phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
265                                         phys_addr_t end, phys_addr_t size,
266                                         phys_addr_t align)
267 {
268         phys_addr_t ret;
269         ulong flags = choose_memblock_flags();
270
271 again:
272         ret = memblock_find_in_range_node(size, align, start, end,
273                                             NUMA_NO_NODE, flags);
274
275         if (!ret && (flags & MEMBLOCK_MIRROR)) {
276                 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
277                         &size);
278                 flags &= ~MEMBLOCK_MIRROR;
279                 goto again;
280         }
281
282         return ret;
283 }
284
285 static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
286 {
287         type->total_size -= type->regions[r].size;
288         memmove(&type->regions[r], &type->regions[r + 1],
289                 (type->cnt - (r + 1)) * sizeof(type->regions[r]));
290         type->cnt--;
291
292         /* Special case for empty arrays */
293         if (type->cnt == 0) {
294                 WARN_ON(type->total_size != 0);
295                 type->cnt = 1;
296                 type->regions[0].base = 0;
297                 type->regions[0].size = 0;
298                 type->regions[0].flags = 0;
299                 memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
300         }
301 }
302
303 #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
304
305 phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info(
306                                         phys_addr_t *addr)
307 {
308         if (memblock.reserved.regions == memblock_reserved_init_regions)
309                 return 0;
310
311         *addr = __pa(memblock.reserved.regions);
312
313         return PAGE_ALIGN(sizeof(struct memblock_region) *
314                           memblock.reserved.max);
315 }
316
317 phys_addr_t __init_memblock get_allocated_memblock_memory_regions_info(
318                                         phys_addr_t *addr)
319 {
320         if (memblock.memory.regions == memblock_memory_init_regions)
321                 return 0;
322
323         *addr = __pa(memblock.memory.regions);
324
325         return PAGE_ALIGN(sizeof(struct memblock_region) *
326                           memblock.memory.max);
327 }
328
329 #endif
330
331 /**
332  * memblock_double_array - double the size of the memblock regions array
333  * @type: memblock type of the regions array being doubled
334  * @new_area_start: starting address of memory range to avoid overlap with
335  * @new_area_size: size of memory range to avoid overlap with
336  *
337  * Double the size of the @type regions array. If memblock is being used to
338  * allocate memory for a new reserved regions array and there is a previously
339  * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
340  * waiting to be reserved, ensure the memory used by the new array does
341  * not overlap.
342  *
343  * RETURNS:
344  * 0 on success, -1 on failure.
345  */
346 static int __init_memblock memblock_double_array(struct memblock_type *type,
347                                                 phys_addr_t new_area_start,
348                                                 phys_addr_t new_area_size)
349 {
350         struct memblock_region *new_array, *old_array;
351         phys_addr_t old_alloc_size, new_alloc_size;
352         phys_addr_t old_size, new_size, addr;
353         int use_slab = slab_is_available();
354         int *in_slab;
355
356         /* We don't allow resizing until we know about the reserved regions
357          * of memory that aren't suitable for allocation
358          */
359         if (!memblock_can_resize)
360                 return -1;
361
362         /* Calculate new doubled size */
363         old_size = type->max * sizeof(struct memblock_region);
364         new_size = old_size << 1;
365         /*
366          * We need to allocated new one align to PAGE_SIZE,
367          *   so we can free them completely later.
368          */
369         old_alloc_size = PAGE_ALIGN(old_size);
370         new_alloc_size = PAGE_ALIGN(new_size);
371
372         /* Retrieve the slab flag */
373         if (type == &memblock.memory)
374                 in_slab = &memblock_memory_in_slab;
375         else
376                 in_slab = &memblock_reserved_in_slab;
377
378         /* Try to find some space for it.
379          *
380          * WARNING: We assume that either slab_is_available() and we use it or
381          * we use MEMBLOCK for allocations. That means that this is unsafe to
382          * use when bootmem is currently active (unless bootmem itself is
383          * implemented on top of MEMBLOCK which isn't the case yet)
384          *
385          * This should however not be an issue for now, as we currently only
386          * call into MEMBLOCK while it's still active, or much later when slab
387          * is active for memory hotplug operations
388          */
389         if (use_slab) {
390                 new_array = kmalloc(new_size, GFP_KERNEL);
391                 addr = new_array ? __pa(new_array) : 0;
392         } else {
393                 /* only exclude range when trying to double reserved.regions */
394                 if (type != &memblock.reserved)
395                         new_area_start = new_area_size = 0;
396
397                 addr = memblock_find_in_range(new_area_start + new_area_size,
398                                                 memblock.current_limit,
399                                                 new_alloc_size, PAGE_SIZE);
400                 if (!addr && new_area_size)
401                         addr = memblock_find_in_range(0,
402                                 min(new_area_start, memblock.current_limit),
403                                 new_alloc_size, PAGE_SIZE);
404
405                 new_array = addr ? __va(addr) : NULL;
406         }
407         if (!addr) {
408                 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
409                        memblock_type_name(type), type->max, type->max * 2);
410                 return -1;
411         }
412
413         memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
414                         memblock_type_name(type), type->max * 2, (u64)addr,
415                         (u64)addr + new_size - 1);
416
417         /*
418          * Found space, we now need to move the array over before we add the
419          * reserved region since it may be our reserved array itself that is
420          * full.
421          */
422         memcpy(new_array, type->regions, old_size);
423         memset(new_array + type->max, 0, old_size);
424         old_array = type->regions;
425         type->regions = new_array;
426         type->max <<= 1;
427
428         /* Free old array. We needn't free it if the array is the static one */
429         if (*in_slab)
430                 kfree(old_array);
431         else if (old_array != memblock_memory_init_regions &&
432                  old_array != memblock_reserved_init_regions)
433                 memblock_free(__pa(old_array), old_alloc_size);
434
435         /*
436          * Reserve the new array if that comes from the memblock.  Otherwise, we
437          * needn't do it
438          */
439         if (!use_slab)
440                 BUG_ON(memblock_reserve(addr, new_alloc_size));
441
442         /* Update slab flag */
443         *in_slab = use_slab;
444
445         return 0;
446 }
447
448 /**
449  * memblock_merge_regions - merge neighboring compatible regions
450  * @type: memblock type to scan
451  *
452  * Scan @type and merge neighboring compatible regions.
453  */
454 static void __init_memblock memblock_merge_regions(struct memblock_type *type)
455 {
456         int i = 0;
457
458         /* cnt never goes below 1 */
459         while (i < type->cnt - 1) {
460                 struct memblock_region *this = &type->regions[i];
461                 struct memblock_region *next = &type->regions[i + 1];
462
463                 if (this->base + this->size != next->base ||
464                     memblock_get_region_node(this) !=
465                     memblock_get_region_node(next) ||
466                     this->flags != next->flags) {
467                         BUG_ON(this->base + this->size > next->base);
468                         i++;
469                         continue;
470                 }
471
472                 this->size += next->size;
473                 /* move forward from next + 1, index of which is i + 2 */
474                 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
475                 type->cnt--;
476         }
477 }
478
479 /**
480  * memblock_insert_region - insert new memblock region
481  * @type:       memblock type to insert into
482  * @idx:        index for the insertion point
483  * @base:       base address of the new region
484  * @size:       size of the new region
485  * @nid:        node id of the new region
486  * @flags:      flags of the new region
487  *
488  * Insert new memblock region [@base,@base+@size) into @type at @idx.
489  * @type must already have extra room to accommodate the new region.
490  */
491 static void __init_memblock memblock_insert_region(struct memblock_type *type,
492                                                    int idx, phys_addr_t base,
493                                                    phys_addr_t size,
494                                                    int nid, unsigned long flags)
495 {
496         struct memblock_region *rgn = &type->regions[idx];
497
498         BUG_ON(type->cnt >= type->max);
499         memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
500         rgn->base = base;
501         rgn->size = size;
502         rgn->flags = flags;
503         memblock_set_region_node(rgn, nid);
504         type->cnt++;
505         type->total_size += size;
506 }
507
508 /**
509  * memblock_add_range - add new memblock region
510  * @type: memblock type to add new region into
511  * @base: base address of the new region
512  * @size: size of the new region
513  * @nid: nid of the new region
514  * @flags: flags of the new region
515  *
516  * Add new memblock region [@base,@base+@size) into @type.  The new region
517  * is allowed to overlap with existing ones - overlaps don't affect already
518  * existing regions.  @type is guaranteed to be minimal (all neighbouring
519  * compatible regions are merged) after the addition.
520  *
521  * RETURNS:
522  * 0 on success, -errno on failure.
523  */
524 int __init_memblock memblock_add_range(struct memblock_type *type,
525                                 phys_addr_t base, phys_addr_t size,
526                                 int nid, unsigned long flags)
527 {
528         bool insert = false;
529         phys_addr_t obase = base;
530         phys_addr_t end = base + memblock_cap_size(base, &size);
531         int idx, nr_new;
532         struct memblock_region *rgn;
533
534         if (!size)
535                 return 0;
536
537         /* special case for empty array */
538         if (type->regions[0].size == 0) {
539                 WARN_ON(type->cnt != 1 || type->total_size);
540                 type->regions[0].base = base;
541                 type->regions[0].size = size;
542                 type->regions[0].flags = flags;
543                 memblock_set_region_node(&type->regions[0], nid);
544                 type->total_size = size;
545                 return 0;
546         }
547 repeat:
548         /*
549          * The following is executed twice.  Once with %false @insert and
550          * then with %true.  The first counts the number of regions needed
551          * to accommodate the new area.  The second actually inserts them.
552          */
553         base = obase;
554         nr_new = 0;
555
556         for_each_memblock_type(type, rgn) {
557                 phys_addr_t rbase = rgn->base;
558                 phys_addr_t rend = rbase + rgn->size;
559
560                 if (rbase >= end)
561                         break;
562                 if (rend <= base)
563                         continue;
564                 /*
565                  * @rgn overlaps.  If it separates the lower part of new
566                  * area, insert that portion.
567                  */
568                 if (rbase > base) {
569 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
570                         WARN_ON(nid != memblock_get_region_node(rgn));
571 #endif
572                         WARN_ON(flags != rgn->flags);
573                         nr_new++;
574                         if (insert)
575                                 memblock_insert_region(type, idx++, base,
576                                                        rbase - base, nid,
577                                                        flags);
578                 }
579                 /* area below @rend is dealt with, forget about it */
580                 base = min(rend, end);
581         }
582
583         /* insert the remaining portion */
584         if (base < end) {
585                 nr_new++;
586                 if (insert)
587                         memblock_insert_region(type, idx, base, end - base,
588                                                nid, flags);
589         }
590
591         if (!nr_new)
592                 return 0;
593
594         /*
595          * If this was the first round, resize array and repeat for actual
596          * insertions; otherwise, merge and return.
597          */
598         if (!insert) {
599                 while (type->cnt + nr_new > type->max)
600                         if (memblock_double_array(type, obase, size) < 0)
601                                 return -ENOMEM;
602                 insert = true;
603                 goto repeat;
604         } else {
605                 memblock_merge_regions(type);
606                 return 0;
607         }
608 }
609
610 int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
611                                        int nid)
612 {
613         return memblock_add_range(&memblock.memory, base, size, nid, 0);
614 }
615
616 int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
617 {
618         phys_addr_t end = base + size - 1;
619
620         memblock_dbg("memblock_add: [%pa-%pa] %pF\n",
621                      &base, &end, (void *)_RET_IP_);
622
623         return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
624 }
625
626 /**
627  * memblock_isolate_range - isolate given range into disjoint memblocks
628  * @type: memblock type to isolate range for
629  * @base: base of range to isolate
630  * @size: size of range to isolate
631  * @start_rgn: out parameter for the start of isolated region
632  * @end_rgn: out parameter for the end of isolated region
633  *
634  * Walk @type and ensure that regions don't cross the boundaries defined by
635  * [@base,@base+@size).  Crossing regions are split at the boundaries,
636  * which may create at most two more regions.  The index of the first
637  * region inside the range is returned in *@start_rgn and end in *@end_rgn.
638  *
639  * RETURNS:
640  * 0 on success, -errno on failure.
641  */
642 static int __init_memblock memblock_isolate_range(struct memblock_type *type,
643                                         phys_addr_t base, phys_addr_t size,
644                                         int *start_rgn, int *end_rgn)
645 {
646         phys_addr_t end = base + memblock_cap_size(base, &size);
647         int idx;
648         struct memblock_region *rgn;
649
650         *start_rgn = *end_rgn = 0;
651
652         if (!size)
653                 return 0;
654
655         /* we'll create at most two more regions */
656         while (type->cnt + 2 > type->max)
657                 if (memblock_double_array(type, base, size) < 0)
658                         return -ENOMEM;
659
660         for_each_memblock_type(type, rgn) {
661                 phys_addr_t rbase = rgn->base;
662                 phys_addr_t rend = rbase + rgn->size;
663
664                 if (rbase >= end)
665                         break;
666                 if (rend <= base)
667                         continue;
668
669                 if (rbase < base) {
670                         /*
671                          * @rgn intersects from below.  Split and continue
672                          * to process the next region - the new top half.
673                          */
674                         rgn->base = base;
675                         rgn->size -= base - rbase;
676                         type->total_size -= base - rbase;
677                         memblock_insert_region(type, idx, rbase, base - rbase,
678                                                memblock_get_region_node(rgn),
679                                                rgn->flags);
680                 } else if (rend > end) {
681                         /*
682                          * @rgn intersects from above.  Split and redo the
683                          * current region - the new bottom half.
684                          */
685                         rgn->base = end;
686                         rgn->size -= end - rbase;
687                         type->total_size -= end - rbase;
688                         memblock_insert_region(type, idx--, rbase, end - rbase,
689                                                memblock_get_region_node(rgn),
690                                                rgn->flags);
691                 } else {
692                         /* @rgn is fully contained, record it */
693                         if (!*end_rgn)
694                                 *start_rgn = idx;
695                         *end_rgn = idx + 1;
696                 }
697         }
698
699         return 0;
700 }
701
702 static int __init_memblock memblock_remove_range(struct memblock_type *type,
703                                           phys_addr_t base, phys_addr_t size)
704 {
705         int start_rgn, end_rgn;
706         int i, ret;
707
708         ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
709         if (ret)
710                 return ret;
711
712         for (i = end_rgn - 1; i >= start_rgn; i--)
713                 memblock_remove_region(type, i);
714         return 0;
715 }
716
717 int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
718 {
719         return memblock_remove_range(&memblock.memory, base, size);
720 }
721
722
723 int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
724 {
725         phys_addr_t end = base + size - 1;
726
727         memblock_dbg("   memblock_free: [%pa-%pa] %pF\n",
728                      &base, &end, (void *)_RET_IP_);
729
730         kmemleak_free_part_phys(base, size);
731         return memblock_remove_range(&memblock.reserved, base, size);
732 }
733
734 int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
735 {
736         phys_addr_t end = base + size - 1;
737
738         memblock_dbg("memblock_reserve: [%pa-%pa] %pF\n",
739                      &base, &end, (void *)_RET_IP_);
740
741         return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
742 }
743
744 /**
745  *
746  * This function isolates region [@base, @base + @size), and sets/clears flag
747  *
748  * Return 0 on success, -errno on failure.
749  */
750 static int __init_memblock memblock_setclr_flag(phys_addr_t base,
751                                 phys_addr_t size, int set, int flag)
752 {
753         struct memblock_type *type = &memblock.memory;
754         int i, ret, start_rgn, end_rgn;
755
756         ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
757         if (ret)
758                 return ret;
759
760         for (i = start_rgn; i < end_rgn; i++)
761                 if (set)
762                         memblock_set_region_flags(&type->regions[i], flag);
763                 else
764                         memblock_clear_region_flags(&type->regions[i], flag);
765
766         memblock_merge_regions(type);
767         return 0;
768 }
769
770 /**
771  * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
772  * @base: the base phys addr of the region
773  * @size: the size of the region
774  *
775  * Return 0 on success, -errno on failure.
776  */
777 int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
778 {
779         return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
780 }
781
782 /**
783  * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
784  * @base: the base phys addr of the region
785  * @size: the size of the region
786  *
787  * Return 0 on success, -errno on failure.
788  */
789 int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
790 {
791         return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
792 }
793
794 /**
795  * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
796  * @base: the base phys addr of the region
797  * @size: the size of the region
798  *
799  * Return 0 on success, -errno on failure.
800  */
801 int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
802 {
803         system_has_some_mirror = true;
804
805         return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
806 }
807
808 /**
809  * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
810  * @base: the base phys addr of the region
811  * @size: the size of the region
812  *
813  * Return 0 on success, -errno on failure.
814  */
815 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
816 {
817         return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
818 }
819
820 /**
821  * __next_reserved_mem_region - next function for for_each_reserved_region()
822  * @idx: pointer to u64 loop variable
823  * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
824  * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
825  *
826  * Iterate over all reserved memory regions.
827  */
828 void __init_memblock __next_reserved_mem_region(u64 *idx,
829                                            phys_addr_t *out_start,
830                                            phys_addr_t *out_end)
831 {
832         struct memblock_type *type = &memblock.reserved;
833
834         if (*idx < type->cnt) {
835                 struct memblock_region *r = &type->regions[*idx];
836                 phys_addr_t base = r->base;
837                 phys_addr_t size = r->size;
838
839                 if (out_start)
840                         *out_start = base;
841                 if (out_end)
842                         *out_end = base + size - 1;
843
844                 *idx += 1;
845                 return;
846         }
847
848         /* signal end of iteration */
849         *idx = ULLONG_MAX;
850 }
851
852 /**
853  * __next__mem_range - next function for for_each_free_mem_range() etc.
854  * @idx: pointer to u64 loop variable
855  * @nid: node selector, %NUMA_NO_NODE for all nodes
856  * @flags: pick from blocks based on memory attributes
857  * @type_a: pointer to memblock_type from where the range is taken
858  * @type_b: pointer to memblock_type which excludes memory from being taken
859  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
860  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
861  * @out_nid: ptr to int for nid of the range, can be %NULL
862  *
863  * Find the first area from *@idx which matches @nid, fill the out
864  * parameters, and update *@idx for the next iteration.  The lower 32bit of
865  * *@idx contains index into type_a and the upper 32bit indexes the
866  * areas before each region in type_b.  For example, if type_b regions
867  * look like the following,
868  *
869  *      0:[0-16), 1:[32-48), 2:[128-130)
870  *
871  * The upper 32bit indexes the following regions.
872  *
873  *      0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
874  *
875  * As both region arrays are sorted, the function advances the two indices
876  * in lockstep and returns each intersection.
877  */
878 void __init_memblock __next_mem_range(u64 *idx, int nid, ulong flags,
879                                       struct memblock_type *type_a,
880                                       struct memblock_type *type_b,
881                                       phys_addr_t *out_start,
882                                       phys_addr_t *out_end, int *out_nid)
883 {
884         int idx_a = *idx & 0xffffffff;
885         int idx_b = *idx >> 32;
886
887         if (WARN_ONCE(nid == MAX_NUMNODES,
888         "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
889                 nid = NUMA_NO_NODE;
890
891         for (; idx_a < type_a->cnt; idx_a++) {
892                 struct memblock_region *m = &type_a->regions[idx_a];
893
894                 phys_addr_t m_start = m->base;
895                 phys_addr_t m_end = m->base + m->size;
896                 int         m_nid = memblock_get_region_node(m);
897
898                 /* only memory regions are associated with nodes, check it */
899                 if (nid != NUMA_NO_NODE && nid != m_nid)
900                         continue;
901
902                 /* skip hotpluggable memory regions if needed */
903                 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
904                         continue;
905
906                 /* if we want mirror memory skip non-mirror memory regions */
907                 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
908                         continue;
909
910                 /* skip nomap memory unless we were asked for it explicitly */
911                 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
912                         continue;
913
914                 if (!type_b) {
915                         if (out_start)
916                                 *out_start = m_start;
917                         if (out_end)
918                                 *out_end = m_end;
919                         if (out_nid)
920                                 *out_nid = m_nid;
921                         idx_a++;
922                         *idx = (u32)idx_a | (u64)idx_b << 32;
923                         return;
924                 }
925
926                 /* scan areas before each reservation */
927                 for (; idx_b < type_b->cnt + 1; idx_b++) {
928                         struct memblock_region *r;
929                         phys_addr_t r_start;
930                         phys_addr_t r_end;
931
932                         r = &type_b->regions[idx_b];
933                         r_start = idx_b ? r[-1].base + r[-1].size : 0;
934                         r_end = idx_b < type_b->cnt ?
935                                 r->base : ULLONG_MAX;
936
937                         /*
938                          * if idx_b advanced past idx_a,
939                          * break out to advance idx_a
940                          */
941                         if (r_start >= m_end)
942                                 break;
943                         /* if the two regions intersect, we're done */
944                         if (m_start < r_end) {
945                                 if (out_start)
946                                         *out_start =
947                                                 max(m_start, r_start);
948                                 if (out_end)
949                                         *out_end = min(m_end, r_end);
950                                 if (out_nid)
951                                         *out_nid = m_nid;
952                                 /*
953                                  * The region which ends first is
954                                  * advanced for the next iteration.
955                                  */
956                                 if (m_end <= r_end)
957                                         idx_a++;
958                                 else
959                                         idx_b++;
960                                 *idx = (u32)idx_a | (u64)idx_b << 32;
961                                 return;
962                         }
963                 }
964         }
965
966         /* signal end of iteration */
967         *idx = ULLONG_MAX;
968 }
969
970 /**
971  * __next_mem_range_rev - generic next function for for_each_*_range_rev()
972  *
973  * Finds the next range from type_a which is not marked as unsuitable
974  * in type_b.
975  *
976  * @idx: pointer to u64 loop variable
977  * @nid: node selector, %NUMA_NO_NODE for all nodes
978  * @flags: pick from blocks based on memory attributes
979  * @type_a: pointer to memblock_type from where the range is taken
980  * @type_b: pointer to memblock_type which excludes memory from being taken
981  * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
982  * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
983  * @out_nid: ptr to int for nid of the range, can be %NULL
984  *
985  * Reverse of __next_mem_range().
986  */
987 void __init_memblock __next_mem_range_rev(u64 *idx, int nid, ulong flags,
988                                           struct memblock_type *type_a,
989                                           struct memblock_type *type_b,
990                                           phys_addr_t *out_start,
991                                           phys_addr_t *out_end, int *out_nid)
992 {
993         int idx_a = *idx & 0xffffffff;
994         int idx_b = *idx >> 32;
995
996         if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
997                 nid = NUMA_NO_NODE;
998
999         if (*idx == (u64)ULLONG_MAX) {
1000                 idx_a = type_a->cnt - 1;
1001                 if (type_b != NULL)
1002                         idx_b = type_b->cnt;
1003                 else
1004                         idx_b = 0;
1005         }
1006
1007         for (; idx_a >= 0; idx_a--) {
1008                 struct memblock_region *m = &type_a->regions[idx_a];
1009
1010                 phys_addr_t m_start = m->base;
1011                 phys_addr_t m_end = m->base + m->size;
1012                 int m_nid = memblock_get_region_node(m);
1013
1014                 /* only memory regions are associated with nodes, check it */
1015                 if (nid != NUMA_NO_NODE && nid != m_nid)
1016                         continue;
1017
1018                 /* skip hotpluggable memory regions if needed */
1019                 if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
1020                         continue;
1021
1022                 /* if we want mirror memory skip non-mirror memory regions */
1023                 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
1024                         continue;
1025
1026                 /* skip nomap memory unless we were asked for it explicitly */
1027                 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
1028                         continue;
1029
1030                 if (!type_b) {
1031                         if (out_start)
1032                                 *out_start = m_start;
1033                         if (out_end)
1034                                 *out_end = m_end;
1035                         if (out_nid)
1036                                 *out_nid = m_nid;
1037                         idx_a--;
1038                         *idx = (u32)idx_a | (u64)idx_b << 32;
1039                         return;
1040                 }
1041
1042                 /* scan areas before each reservation */
1043                 for (; idx_b >= 0; idx_b--) {
1044                         struct memblock_region *r;
1045                         phys_addr_t r_start;
1046                         phys_addr_t r_end;
1047
1048                         r = &type_b->regions[idx_b];
1049                         r_start = idx_b ? r[-1].base + r[-1].size : 0;
1050                         r_end = idx_b < type_b->cnt ?
1051                                 r->base : ULLONG_MAX;
1052                         /*
1053                          * if idx_b advanced past idx_a,
1054                          * break out to advance idx_a
1055                          */
1056
1057                         if (r_end <= m_start)
1058                                 break;
1059                         /* if the two regions intersect, we're done */
1060                         if (m_end > r_start) {
1061                                 if (out_start)
1062                                         *out_start = max(m_start, r_start);
1063                                 if (out_end)
1064                                         *out_end = min(m_end, r_end);
1065                                 if (out_nid)
1066                                         *out_nid = m_nid;
1067                                 if (m_start >= r_start)
1068                                         idx_a--;
1069                                 else
1070                                         idx_b--;
1071                                 *idx = (u32)idx_a | (u64)idx_b << 32;
1072                                 return;
1073                         }
1074                 }
1075         }
1076         /* signal end of iteration */
1077         *idx = ULLONG_MAX;
1078 }
1079
1080 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1081 /*
1082  * Common iterator interface used to define for_each_mem_range().
1083  */
1084 void __init_memblock __next_mem_pfn_range(int *idx, int nid,
1085                                 unsigned long *out_start_pfn,
1086                                 unsigned long *out_end_pfn, int *out_nid)
1087 {
1088         struct memblock_type *type = &memblock.memory;
1089         struct memblock_region *r;
1090
1091         while (++*idx < type->cnt) {
1092                 r = &type->regions[*idx];
1093
1094                 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
1095                         continue;
1096                 if (nid == MAX_NUMNODES || nid == r->nid)
1097                         break;
1098         }
1099         if (*idx >= type->cnt) {
1100                 *idx = -1;
1101                 return;
1102         }
1103
1104         if (out_start_pfn)
1105                 *out_start_pfn = PFN_UP(r->base);
1106         if (out_end_pfn)
1107                 *out_end_pfn = PFN_DOWN(r->base + r->size);
1108         if (out_nid)
1109                 *out_nid = r->nid;
1110 }
1111
1112 unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn,
1113                                                       unsigned long max_pfn)
1114 {
1115         struct memblock_type *type = &memblock.memory;
1116         unsigned int right = type->cnt;
1117         unsigned int mid, left = 0;
1118         phys_addr_t addr = PFN_PHYS(pfn + 1);
1119
1120         do {
1121                 mid = (right + left) / 2;
1122
1123                 if (addr < type->regions[mid].base)
1124                         right = mid;
1125                 else if (addr >= (type->regions[mid].base +
1126                                   type->regions[mid].size))
1127                         left = mid + 1;
1128                 else {
1129                         /* addr is within the region, so pfn + 1 is valid */
1130                         return min(pfn + 1, max_pfn);
1131                 }
1132         } while (left < right);
1133
1134         return min(PHYS_PFN(type->regions[right].base), max_pfn);
1135 }
1136
1137 /**
1138  * memblock_set_node - set node ID on memblock regions
1139  * @base: base of area to set node ID for
1140  * @size: size of area to set node ID for
1141  * @type: memblock type to set node ID for
1142  * @nid: node ID to set
1143  *
1144  * Set the nid of memblock @type regions in [@base,@base+@size) to @nid.
1145  * Regions which cross the area boundaries are split as necessary.
1146  *
1147  * RETURNS:
1148  * 0 on success, -errno on failure.
1149  */
1150 int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1151                                       struct memblock_type *type, int nid)
1152 {
1153         int start_rgn, end_rgn;
1154         int i, ret;
1155
1156         ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
1157         if (ret)
1158                 return ret;
1159
1160         for (i = start_rgn; i < end_rgn; i++)
1161                 memblock_set_region_node(&type->regions[i], nid);
1162
1163         memblock_merge_regions(type);
1164         return 0;
1165 }
1166 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
1167
1168 static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
1169                                         phys_addr_t align, phys_addr_t start,
1170                                         phys_addr_t end, int nid, ulong flags)
1171 {
1172         phys_addr_t found;
1173
1174         if (!align)
1175                 align = SMP_CACHE_BYTES;
1176
1177         found = memblock_find_in_range_node(size, align, start, end, nid,
1178                                             flags);
1179         if (found && !memblock_reserve(found, size)) {
1180                 /*
1181                  * The min_count is set to 0 so that memblock allocations are
1182                  * never reported as leaks.
1183                  */
1184                 kmemleak_alloc_phys(found, size, 0, 0);
1185                 return found;
1186         }
1187         return 0;
1188 }
1189
1190 phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
1191                                         phys_addr_t start, phys_addr_t end,
1192                                         ulong flags)
1193 {
1194         return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
1195                                         flags);
1196 }
1197
1198 static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
1199                                         phys_addr_t align, phys_addr_t max_addr,
1200                                         int nid, ulong flags)
1201 {
1202         return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags);
1203 }
1204
1205 phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
1206 {
1207         ulong flags = choose_memblock_flags();
1208         phys_addr_t ret;
1209
1210 again:
1211         ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE,
1212                                       nid, flags);
1213
1214         if (!ret && (flags & MEMBLOCK_MIRROR)) {
1215                 flags &= ~MEMBLOCK_MIRROR;
1216                 goto again;
1217         }
1218         return ret;
1219 }
1220
1221 phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1222 {
1223         return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE,
1224                                        MEMBLOCK_NONE);
1225 }
1226
1227 phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
1228 {
1229         phys_addr_t alloc;
1230
1231         alloc = __memblock_alloc_base(size, align, max_addr);
1232
1233         if (alloc == 0)
1234                 panic("ERROR: Failed to allocate %pa bytes below %pa.\n",
1235                       &size, &max_addr);
1236
1237         return alloc;
1238 }
1239
1240 phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
1241 {
1242         return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
1243 }
1244
1245 phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
1246 {
1247         phys_addr_t res = memblock_alloc_nid(size, align, nid);
1248
1249         if (res)
1250                 return res;
1251         return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
1252 }
1253
1254 /**
1255  * memblock_virt_alloc_internal - allocate boot memory block
1256  * @size: size of memory block to be allocated in bytes
1257  * @align: alignment of the region and block's size
1258  * @min_addr: the lower bound of the memory region to allocate (phys address)
1259  * @max_addr: the upper bound of the memory region to allocate (phys address)
1260  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1261  *
1262  * The @min_addr limit is dropped if it can not be satisfied and the allocation
1263  * will fall back to memory below @min_addr. Also, allocation may fall back
1264  * to any node in the system if the specified node can not
1265  * hold the requested memory.
1266  *
1267  * The allocation is performed from memory region limited by
1268  * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE.
1269  *
1270  * The memory block is aligned on SMP_CACHE_BYTES if @align == 0.
1271  *
1272  * The phys address of allocated boot memory block is converted to virtual and
1273  * allocated memory is reset to 0.
1274  *
1275  * In addition, function sets the min_count to 0 using kmemleak_alloc for
1276  * allocated boot memory block, so that it is never reported as leaks.
1277  *
1278  * RETURNS:
1279  * Virtual address of allocated memory block on success, NULL on failure.
1280  */
1281 static void * __init memblock_virt_alloc_internal(
1282                                 phys_addr_t size, phys_addr_t align,
1283                                 phys_addr_t min_addr, phys_addr_t max_addr,
1284                                 int nid)
1285 {
1286         phys_addr_t alloc;
1287         void *ptr;
1288         ulong flags = choose_memblock_flags();
1289
1290         if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
1291                 nid = NUMA_NO_NODE;
1292
1293         /*
1294          * Detect any accidental use of these APIs after slab is ready, as at
1295          * this moment memblock may be deinitialized already and its
1296          * internal data may be destroyed (after execution of free_all_bootmem)
1297          */
1298         if (WARN_ON_ONCE(slab_is_available()))
1299                 return kzalloc_node(size, GFP_NOWAIT, nid);
1300
1301         if (!align)
1302                 align = SMP_CACHE_BYTES;
1303
1304         if (max_addr > memblock.current_limit)
1305                 max_addr = memblock.current_limit;
1306 again:
1307         alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
1308                                             nid, flags);
1309         if (alloc && !memblock_reserve(alloc, size))
1310                 goto done;
1311
1312         if (nid != NUMA_NO_NODE) {
1313                 alloc = memblock_find_in_range_node(size, align, min_addr,
1314                                                     max_addr, NUMA_NO_NODE,
1315                                                     flags);
1316                 if (alloc && !memblock_reserve(alloc, size))
1317                         goto done;
1318         }
1319
1320         if (min_addr) {
1321                 min_addr = 0;
1322                 goto again;
1323         }
1324
1325         if (flags & MEMBLOCK_MIRROR) {
1326                 flags &= ~MEMBLOCK_MIRROR;
1327                 pr_warn("Could not allocate %pap bytes of mirrored memory\n",
1328                         &size);
1329                 goto again;
1330         }
1331
1332         return NULL;
1333 done:
1334         ptr = phys_to_virt(alloc);
1335         memset(ptr, 0, size);
1336
1337         /*
1338          * The min_count is set to 0 so that bootmem allocated blocks
1339          * are never reported as leaks. This is because many of these blocks
1340          * are only referred via the physical address which is not
1341          * looked up by kmemleak.
1342          */
1343         kmemleak_alloc(ptr, size, 0, 0);
1344
1345         return ptr;
1346 }
1347
1348 /**
1349  * memblock_virt_alloc_try_nid_nopanic - allocate boot memory block
1350  * @size: size of memory block to be allocated in bytes
1351  * @align: alignment of the region and block's size
1352  * @min_addr: the lower bound of the memory region from where the allocation
1353  *        is preferred (phys address)
1354  * @max_addr: the upper bound of the memory region from where the allocation
1355  *            is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1356  *            allocate only from memory limited by memblock.current_limit value
1357  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1358  *
1359  * Public version of _memblock_virt_alloc_try_nid_nopanic() which provides
1360  * additional debug information (including caller info), if enabled.
1361  *
1362  * RETURNS:
1363  * Virtual address of allocated memory block on success, NULL on failure.
1364  */
1365 void * __init memblock_virt_alloc_try_nid_nopanic(
1366                                 phys_addr_t size, phys_addr_t align,
1367                                 phys_addr_t min_addr, phys_addr_t max_addr,
1368                                 int nid)
1369 {
1370         memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1371                      __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1372                      (u64)max_addr, (void *)_RET_IP_);
1373         return memblock_virt_alloc_internal(size, align, min_addr,
1374                                              max_addr, nid);
1375 }
1376
1377 /**
1378  * memblock_virt_alloc_try_nid - allocate boot memory block with panicking
1379  * @size: size of memory block to be allocated in bytes
1380  * @align: alignment of the region and block's size
1381  * @min_addr: the lower bound of the memory region from where the allocation
1382  *        is preferred (phys address)
1383  * @max_addr: the upper bound of the memory region from where the allocation
1384  *            is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to
1385  *            allocate only from memory limited by memblock.current_limit value
1386  * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
1387  *
1388  * Public panicking version of _memblock_virt_alloc_try_nid_nopanic()
1389  * which provides debug information (including caller info), if enabled,
1390  * and panics if the request can not be satisfied.
1391  *
1392  * RETURNS:
1393  * Virtual address of allocated memory block on success, NULL on failure.
1394  */
1395 void * __init memblock_virt_alloc_try_nid(
1396                         phys_addr_t size, phys_addr_t align,
1397                         phys_addr_t min_addr, phys_addr_t max_addr,
1398                         int nid)
1399 {
1400         void *ptr;
1401
1402         memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx %pF\n",
1403                      __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1404                      (u64)max_addr, (void *)_RET_IP_);
1405         ptr = memblock_virt_alloc_internal(size, align,
1406                                            min_addr, max_addr, nid);
1407         if (ptr)
1408                 return ptr;
1409
1410         panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=0x%llx max_addr=0x%llx\n",
1411               __func__, (u64)size, (u64)align, nid, (u64)min_addr,
1412               (u64)max_addr);
1413         return NULL;
1414 }
1415
1416 /**
1417  * __memblock_free_early - free boot memory block
1418  * @base: phys starting address of the  boot memory block
1419  * @size: size of the boot memory block in bytes
1420  *
1421  * Free boot memory block previously allocated by memblock_virt_alloc_xx() API.
1422  * The freeing memory will not be released to the buddy allocator.
1423  */
1424 void __init __memblock_free_early(phys_addr_t base, phys_addr_t size)
1425 {
1426         memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1427                      __func__, (u64)base, (u64)base + size - 1,
1428                      (void *)_RET_IP_);
1429         kmemleak_free_part_phys(base, size);
1430         memblock_remove_range(&memblock.reserved, base, size);
1431 }
1432
1433 /*
1434  * __memblock_free_late - free bootmem block pages directly to buddy allocator
1435  * @addr: phys starting address of the  boot memory block
1436  * @size: size of the boot memory block in bytes
1437  *
1438  * This is only useful when the bootmem allocator has already been torn
1439  * down, but we are still initializing the system.  Pages are released directly
1440  * to the buddy allocator, no bootmem metadata is updated because it is gone.
1441  */
1442 void __init __memblock_free_late(phys_addr_t base, phys_addr_t size)
1443 {
1444         u64 cursor, end;
1445
1446         memblock_dbg("%s: [%#016llx-%#016llx] %pF\n",
1447                      __func__, (u64)base, (u64)base + size - 1,
1448                      (void *)_RET_IP_);
1449         kmemleak_free_part_phys(base, size);
1450         cursor = PFN_UP(base);
1451         end = PFN_DOWN(base + size);
1452
1453         for (; cursor < end; cursor++) {
1454                 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
1455                 totalram_pages++;
1456         }
1457 }
1458
1459 /*
1460  * Remaining API functions
1461  */
1462
1463 phys_addr_t __init_memblock memblock_phys_mem_size(void)
1464 {
1465         return memblock.memory.total_size;
1466 }
1467
1468 phys_addr_t __init_memblock memblock_reserved_size(void)
1469 {
1470         return memblock.reserved.total_size;
1471 }
1472
1473 phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
1474 {
1475         unsigned long pages = 0;
1476         struct memblock_region *r;
1477         unsigned long start_pfn, end_pfn;
1478
1479         for_each_memblock(memory, r) {
1480                 start_pfn = memblock_region_memory_base_pfn(r);
1481                 end_pfn = memblock_region_memory_end_pfn(r);
1482                 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
1483                 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
1484                 pages += end_pfn - start_pfn;
1485         }
1486
1487         return PFN_PHYS(pages);
1488 }
1489
1490 /* lowest address */
1491 phys_addr_t __init_memblock memblock_start_of_DRAM(void)
1492 {
1493         return memblock.memory.regions[0].base;
1494 }
1495
1496 phys_addr_t __init_memblock memblock_end_of_DRAM(void)
1497 {
1498         int idx = memblock.memory.cnt - 1;
1499
1500         return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
1501 }
1502
1503 static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
1504 {
1505         phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
1506         struct memblock_region *r;
1507
1508         /*
1509          * translate the memory @limit size into the max address within one of
1510          * the memory memblock regions, if the @limit exceeds the total size
1511          * of those regions, max_addr will keep original value ULLONG_MAX
1512          */
1513         for_each_memblock(memory, r) {
1514                 if (limit <= r->size) {
1515                         max_addr = r->base + limit;
1516                         break;
1517                 }
1518                 limit -= r->size;
1519         }
1520
1521         return max_addr;
1522 }
1523
1524 void __init memblock_enforce_memory_limit(phys_addr_t limit)
1525 {
1526         phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
1527
1528         if (!limit)
1529                 return;
1530
1531         max_addr = __find_max_addr(limit);
1532
1533         /* @limit exceeds the total size of the memory, do nothing */
1534         if (max_addr == (phys_addr_t)ULLONG_MAX)
1535                 return;
1536
1537         /* truncate both memory and reserved regions */
1538         memblock_remove_range(&memblock.memory, max_addr,
1539                               (phys_addr_t)ULLONG_MAX);
1540         memblock_remove_range(&memblock.reserved, max_addr,
1541                               (phys_addr_t)ULLONG_MAX);
1542 }
1543
1544 void __init memblock_mem_limit_remove_map(phys_addr_t limit)
1545 {
1546         struct memblock_type *type = &memblock.memory;
1547         phys_addr_t max_addr;
1548         int i, ret, start_rgn, end_rgn;
1549
1550         if (!limit)
1551                 return;
1552
1553         max_addr = __find_max_addr(limit);
1554
1555         /* @limit exceeds the total size of the memory, do nothing */
1556         if (max_addr == (phys_addr_t)ULLONG_MAX)
1557                 return;
1558
1559         ret = memblock_isolate_range(type, max_addr, (phys_addr_t)ULLONG_MAX,
1560                                 &start_rgn, &end_rgn);
1561         if (ret)
1562                 return;
1563
1564         /* remove all the MAP regions above the limit */
1565         for (i = end_rgn - 1; i >= start_rgn; i--) {
1566                 if (!memblock_is_nomap(&type->regions[i]))
1567                         memblock_remove_region(type, i);
1568         }
1569         /* truncate the reserved regions */
1570         memblock_remove_range(&memblock.reserved, max_addr,
1571                               (phys_addr_t)ULLONG_MAX);
1572 }
1573
1574 static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
1575 {
1576         unsigned int left = 0, right = type->cnt;
1577
1578         do {
1579                 unsigned int mid = (right + left) / 2;
1580
1581                 if (addr < type->regions[mid].base)
1582                         right = mid;
1583                 else if (addr >= (type->regions[mid].base +
1584                                   type->regions[mid].size))
1585                         left = mid + 1;
1586                 else
1587                         return mid;
1588         } while (left < right);
1589         return -1;
1590 }
1591
1592 bool __init memblock_is_reserved(phys_addr_t addr)
1593 {
1594         return memblock_search(&memblock.reserved, addr) != -1;
1595 }
1596
1597 bool __init_memblock memblock_is_memory(phys_addr_t addr)
1598 {
1599         return memblock_search(&memblock.memory, addr) != -1;
1600 }
1601
1602 int __init_memblock memblock_is_map_memory(phys_addr_t addr)
1603 {
1604         int i = memblock_search(&memblock.memory, addr);
1605
1606         if (i == -1)
1607                 return false;
1608         return !memblock_is_nomap(&memblock.memory.regions[i]);
1609 }
1610
1611 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1612 int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
1613                          unsigned long *start_pfn, unsigned long *end_pfn)
1614 {
1615         struct memblock_type *type = &memblock.memory;
1616         int mid = memblock_search(type, PFN_PHYS(pfn));
1617
1618         if (mid == -1)
1619                 return -1;
1620
1621         *start_pfn = PFN_DOWN(type->regions[mid].base);
1622         *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
1623
1624         return type->regions[mid].nid;
1625 }
1626 #endif
1627
1628 /**
1629  * memblock_is_region_memory - check if a region is a subset of memory
1630  * @base: base of region to check
1631  * @size: size of region to check
1632  *
1633  * Check if the region [@base, @base+@size) is a subset of a memory block.
1634  *
1635  * RETURNS:
1636  * 0 if false, non-zero if true
1637  */
1638 int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
1639 {
1640         int idx = memblock_search(&memblock.memory, base);
1641         phys_addr_t end = base + memblock_cap_size(base, &size);
1642
1643         if (idx == -1)
1644                 return 0;
1645         return (memblock.memory.regions[idx].base +
1646                  memblock.memory.regions[idx].size) >= end;
1647 }
1648
1649 /**
1650  * memblock_is_region_reserved - check if a region intersects reserved memory
1651  * @base: base of region to check
1652  * @size: size of region to check
1653  *
1654  * Check if the region [@base, @base+@size) intersects a reserved memory block.
1655  *
1656  * RETURNS:
1657  * True if they intersect, false if not.
1658  */
1659 bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
1660 {
1661         memblock_cap_size(base, &size);
1662         return memblock_overlaps_region(&memblock.reserved, base, size);
1663 }
1664
1665 void __init_memblock memblock_trim_memory(phys_addr_t align)
1666 {
1667         phys_addr_t start, end, orig_start, orig_end;
1668         struct memblock_region *r;
1669
1670         for_each_memblock(memory, r) {
1671                 orig_start = r->base;
1672                 orig_end = r->base + r->size;
1673                 start = round_up(orig_start, align);
1674                 end = round_down(orig_end, align);
1675
1676                 if (start == orig_start && end == orig_end)
1677                         continue;
1678
1679                 if (start < end) {
1680                         r->base = start;
1681                         r->size = end - start;
1682                 } else {
1683                         memblock_remove_region(&memblock.memory,
1684                                                r - memblock.memory.regions);
1685                         r--;
1686                 }
1687         }
1688 }
1689
1690 void __init_memblock memblock_set_current_limit(phys_addr_t limit)
1691 {
1692         memblock.current_limit = limit;
1693 }
1694
1695 phys_addr_t __init_memblock memblock_get_current_limit(void)
1696 {
1697         return memblock.current_limit;
1698 }
1699
1700 static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
1701 {
1702         phys_addr_t base, end, size;
1703         unsigned long flags;
1704         int idx;
1705         struct memblock_region *rgn;
1706
1707         pr_info(" %s.cnt  = 0x%lx\n", name, type->cnt);
1708
1709         for_each_memblock_type(type, rgn) {
1710                 char nid_buf[32] = "";
1711
1712                 base = rgn->base;
1713                 size = rgn->size;
1714                 end = base + size - 1;
1715                 flags = rgn->flags;
1716 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1717                 if (memblock_get_region_node(rgn) != MAX_NUMNODES)
1718                         snprintf(nid_buf, sizeof(nid_buf), " on node %d",
1719                                  memblock_get_region_node(rgn));
1720 #endif
1721                 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#lx\n",
1722                         name, idx, &base, &end, &size, nid_buf, flags);
1723         }
1724 }
1725
1726 void __init_memblock __memblock_dump_all(void)
1727 {
1728         pr_info("MEMBLOCK configuration:\n");
1729         pr_info(" memory size = %pa reserved size = %pa\n",
1730                 &memblock.memory.total_size,
1731                 &memblock.reserved.total_size);
1732
1733         memblock_dump(&memblock.memory, "memory");
1734         memblock_dump(&memblock.reserved, "reserved");
1735 }
1736
1737 void __init memblock_allow_resize(void)
1738 {
1739         memblock_can_resize = 1;
1740 }
1741
1742 static int __init early_memblock(char *p)
1743 {
1744         if (p && strstr(p, "debug"))
1745                 memblock_debug = 1;
1746         return 0;
1747 }
1748 early_param("memblock", early_memblock);
1749
1750 #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
1751
1752 static int memblock_debug_show(struct seq_file *m, void *private)
1753 {
1754         struct memblock_type *type = m->private;
1755         struct memblock_region *reg;
1756         int i;
1757         phys_addr_t end;
1758
1759         for (i = 0; i < type->cnt; i++) {
1760                 reg = &type->regions[i];
1761                 end = reg->base + reg->size - 1;
1762
1763                 seq_printf(m, "%4d: ", i);
1764                 seq_printf(m, "%pa..%pa\n", &reg->base, &end);
1765         }
1766         return 0;
1767 }
1768
1769 static int memblock_debug_open(struct inode *inode, struct file *file)
1770 {
1771         return single_open(file, memblock_debug_show, inode->i_private);
1772 }
1773
1774 static const struct file_operations memblock_debug_fops = {
1775         .open = memblock_debug_open,
1776         .read = seq_read,
1777         .llseek = seq_lseek,
1778         .release = single_release,
1779 };
1780
1781 static int __init memblock_init_debugfs(void)
1782 {
1783         struct dentry *root = debugfs_create_dir("memblock", NULL);
1784         if (!root)
1785                 return -ENXIO;
1786         debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
1787         debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
1788 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
1789         debugfs_create_file("physmem", S_IRUGO, root, &memblock.physmem, &memblock_debug_fops);
1790 #endif
1791
1792         return 0;
1793 }
1794 __initcall(memblock_init_debugfs);
1795
1796 #endif /* CONFIG_DEBUG_FS */