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