2 * Procedures for maintaining information about logical memory blocks.
4 * Peter Bergner, IBM Corp. June 2001.
5 * Copyright (C) 2001 Peter Bergner.
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.
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>
23 static struct memblock_region memblock_memory_init_regions
[INIT_MEMBLOCK_REGIONS
] __initdata_memblock
;
24 static struct memblock_region memblock_reserved_init_regions
[INIT_MEMBLOCK_REGIONS
] __initdata_memblock
;
26 struct memblock memblock __initdata_memblock
= {
27 .memory
.regions
= memblock_memory_init_regions
,
28 .memory
.cnt
= 1, /* empty dummy entry */
29 .memory
.max
= INIT_MEMBLOCK_REGIONS
,
31 .reserved
.regions
= memblock_reserved_init_regions
,
32 .reserved
.cnt
= 1, /* empty dummy entry */
33 .reserved
.max
= INIT_MEMBLOCK_REGIONS
,
35 .current_limit
= MEMBLOCK_ALLOC_ANYWHERE
,
38 int memblock_debug __initdata_memblock
;
39 static int memblock_can_resize __initdata_memblock
;
40 static int memblock_memory_in_slab __initdata_memblock
= 0;
41 static int memblock_reserved_in_slab __initdata_memblock
= 0;
43 /* inline so we don't get a warning when pr_debug is compiled out */
44 static __init_memblock
const char *
45 memblock_type_name(struct memblock_type
*type
)
47 if (type
== &memblock
.memory
)
49 else if (type
== &memblock
.reserved
)
55 /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
56 static inline phys_addr_t
memblock_cap_size(phys_addr_t base
, phys_addr_t
*size
)
58 return *size
= min(*size
, (phys_addr_t
)ULLONG_MAX
- base
);
62 * Address comparison utilities
64 static unsigned long __init_memblock
memblock_addrs_overlap(phys_addr_t base1
, phys_addr_t size1
,
65 phys_addr_t base2
, phys_addr_t size2
)
67 return ((base1
< (base2
+ size2
)) && (base2
< (base1
+ size1
)));
70 static long __init_memblock
memblock_overlaps_region(struct memblock_type
*type
,
71 phys_addr_t base
, phys_addr_t size
)
75 for (i
= 0; i
< type
->cnt
; i
++) {
76 phys_addr_t rgnbase
= type
->regions
[i
].base
;
77 phys_addr_t rgnsize
= type
->regions
[i
].size
;
78 if (memblock_addrs_overlap(base
, size
, rgnbase
, rgnsize
))
82 return (i
< type
->cnt
) ? i
: -1;
86 * memblock_find_in_range_node - find free area in given range and node
87 * @start: start of candidate range
88 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
89 * @size: size of free area to find
90 * @align: alignment of free area to find
91 * @nid: nid of the free area to find, %MAX_NUMNODES for any node
93 * Find @size free area aligned to @align in the specified range and node.
96 * Found address on success, %0 on failure.
98 phys_addr_t __init_memblock
memblock_find_in_range_node(phys_addr_t start
,
99 phys_addr_t end
, phys_addr_t size
,
100 phys_addr_t align
, int nid
)
102 phys_addr_t this_start
, this_end
, cand
;
104 int curr
= movablemem_map
.nr_map
- 1;
107 if (end
== MEMBLOCK_ALLOC_ACCESSIBLE
)
108 end
= memblock
.current_limit
;
110 /* avoid allocating the first page */
111 start
= max_t(phys_addr_t
, start
, PAGE_SIZE
);
112 end
= max(start
, end
);
114 for_each_free_mem_range_reverse(i
, nid
, &this_start
, &this_end
, NULL
) {
115 this_start
= clamp(this_start
, start
, end
);
116 this_end
= clamp(this_end
, start
, end
);
119 if (this_end
<= this_start
|| this_end
< size
)
122 for (; curr
>= 0; curr
--) {
123 if ((movablemem_map
.map
[curr
].start_pfn
<< PAGE_SHIFT
)
128 cand
= round_down(this_end
- size
, align
);
130 cand
< movablemem_map
.map
[curr
].end_pfn
<< PAGE_SHIFT
) {
131 this_end
= movablemem_map
.map
[curr
].start_pfn
136 if (cand
>= this_start
)
144 * memblock_find_in_range - find free area in given range
145 * @start: start of candidate range
146 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
147 * @size: size of free area to find
148 * @align: alignment of free area to find
150 * Find @size free area aligned to @align in the specified range.
153 * Found address on success, %0 on failure.
155 phys_addr_t __init_memblock
memblock_find_in_range(phys_addr_t start
,
156 phys_addr_t end
, phys_addr_t size
,
159 return memblock_find_in_range_node(start
, end
, size
, align
,
163 static void __init_memblock
memblock_remove_region(struct memblock_type
*type
, unsigned long r
)
165 type
->total_size
-= type
->regions
[r
].size
;
166 memmove(&type
->regions
[r
], &type
->regions
[r
+ 1],
167 (type
->cnt
- (r
+ 1)) * sizeof(type
->regions
[r
]));
170 /* Special case for empty arrays */
171 if (type
->cnt
== 0) {
172 WARN_ON(type
->total_size
!= 0);
174 type
->regions
[0].base
= 0;
175 type
->regions
[0].size
= 0;
176 memblock_set_region_node(&type
->regions
[0], MAX_NUMNODES
);
180 phys_addr_t __init_memblock
get_allocated_memblock_reserved_regions_info(
183 if (memblock
.reserved
.regions
== memblock_reserved_init_regions
)
186 *addr
= __pa(memblock
.reserved
.regions
);
188 return PAGE_ALIGN(sizeof(struct memblock_region
) *
189 memblock
.reserved
.max
);
193 * memblock_double_array - double the size of the memblock regions array
194 * @type: memblock type of the regions array being doubled
195 * @new_area_start: starting address of memory range to avoid overlap with
196 * @new_area_size: size of memory range to avoid overlap with
198 * Double the size of the @type regions array. If memblock is being used to
199 * allocate memory for a new reserved regions array and there is a previously
200 * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
201 * waiting to be reserved, ensure the memory used by the new array does
205 * 0 on success, -1 on failure.
207 static int __init_memblock
memblock_double_array(struct memblock_type
*type
,
208 phys_addr_t new_area_start
,
209 phys_addr_t new_area_size
)
211 struct memblock_region
*new_array
, *old_array
;
212 phys_addr_t old_alloc_size
, new_alloc_size
;
213 phys_addr_t old_size
, new_size
, addr
;
214 int use_slab
= slab_is_available();
217 /* We don't allow resizing until we know about the reserved regions
218 * of memory that aren't suitable for allocation
220 if (!memblock_can_resize
)
223 /* Calculate new doubled size */
224 old_size
= type
->max
* sizeof(struct memblock_region
);
225 new_size
= old_size
<< 1;
227 * We need to allocated new one align to PAGE_SIZE,
228 * so we can free them completely later.
230 old_alloc_size
= PAGE_ALIGN(old_size
);
231 new_alloc_size
= PAGE_ALIGN(new_size
);
233 /* Retrieve the slab flag */
234 if (type
== &memblock
.memory
)
235 in_slab
= &memblock_memory_in_slab
;
237 in_slab
= &memblock_reserved_in_slab
;
239 /* Try to find some space for it.
241 * WARNING: We assume that either slab_is_available() and we use it or
242 * we use MEMBLOCK for allocations. That means that this is unsafe to
243 * use when bootmem is currently active (unless bootmem itself is
244 * implemented on top of MEMBLOCK which isn't the case yet)
246 * This should however not be an issue for now, as we currently only
247 * call into MEMBLOCK while it's still active, or much later when slab
248 * is active for memory hotplug operations
251 new_array
= kmalloc(new_size
, GFP_KERNEL
);
252 addr
= new_array
? __pa(new_array
) : 0;
254 /* only exclude range when trying to double reserved.regions */
255 if (type
!= &memblock
.reserved
)
256 new_area_start
= new_area_size
= 0;
258 addr
= memblock_find_in_range(new_area_start
+ new_area_size
,
259 memblock
.current_limit
,
260 new_alloc_size
, PAGE_SIZE
);
261 if (!addr
&& new_area_size
)
262 addr
= memblock_find_in_range(0,
263 min(new_area_start
, memblock
.current_limit
),
264 new_alloc_size
, PAGE_SIZE
);
266 new_array
= addr
? __va(addr
) : NULL
;
269 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
270 memblock_type_name(type
), type
->max
, type
->max
* 2);
274 memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
275 memblock_type_name(type
), type
->max
* 2, (u64
)addr
,
276 (u64
)addr
+ new_size
- 1);
279 * Found space, we now need to move the array over before we add the
280 * reserved region since it may be our reserved array itself that is
283 memcpy(new_array
, type
->regions
, old_size
);
284 memset(new_array
+ type
->max
, 0, old_size
);
285 old_array
= type
->regions
;
286 type
->regions
= new_array
;
289 /* Free old array. We needn't free it if the array is the static one */
292 else if (old_array
!= memblock_memory_init_regions
&&
293 old_array
!= memblock_reserved_init_regions
)
294 memblock_free(__pa(old_array
), old_alloc_size
);
297 * Reserve the new array if that comes from the memblock. Otherwise, we
301 BUG_ON(memblock_reserve(addr
, new_alloc_size
));
303 /* Update slab flag */
310 * memblock_merge_regions - merge neighboring compatible regions
311 * @type: memblock type to scan
313 * Scan @type and merge neighboring compatible regions.
315 static void __init_memblock
memblock_merge_regions(struct memblock_type
*type
)
319 /* cnt never goes below 1 */
320 while (i
< type
->cnt
- 1) {
321 struct memblock_region
*this = &type
->regions
[i
];
322 struct memblock_region
*next
= &type
->regions
[i
+ 1];
324 if (this->base
+ this->size
!= next
->base
||
325 memblock_get_region_node(this) !=
326 memblock_get_region_node(next
)) {
327 BUG_ON(this->base
+ this->size
> next
->base
);
332 this->size
+= next
->size
;
333 /* move forward from next + 1, index of which is i + 2 */
334 memmove(next
, next
+ 1, (type
->cnt
- (i
+ 2)) * sizeof(*next
));
340 * memblock_insert_region - insert new memblock region
341 * @type: memblock type to insert into
342 * @idx: index for the insertion point
343 * @base: base address of the new region
344 * @size: size of the new region
346 * Insert new memblock region [@base,@base+@size) into @type at @idx.
347 * @type must already have extra room to accomodate the new region.
349 static void __init_memblock
memblock_insert_region(struct memblock_type
*type
,
350 int idx
, phys_addr_t base
,
351 phys_addr_t size
, int nid
)
353 struct memblock_region
*rgn
= &type
->regions
[idx
];
355 BUG_ON(type
->cnt
>= type
->max
);
356 memmove(rgn
+ 1, rgn
, (type
->cnt
- idx
) * sizeof(*rgn
));
359 memblock_set_region_node(rgn
, nid
);
361 type
->total_size
+= size
;
365 * memblock_add_region - add new memblock region
366 * @type: memblock type to add new region into
367 * @base: base address of the new region
368 * @size: size of the new region
369 * @nid: nid of the new region
371 * Add new memblock region [@base,@base+@size) into @type. The new region
372 * is allowed to overlap with existing ones - overlaps don't affect already
373 * existing regions. @type is guaranteed to be minimal (all neighbouring
374 * compatible regions are merged) after the addition.
377 * 0 on success, -errno on failure.
379 static int __init_memblock
memblock_add_region(struct memblock_type
*type
,
380 phys_addr_t base
, phys_addr_t size
, int nid
)
383 phys_addr_t obase
= base
;
384 phys_addr_t end
= base
+ memblock_cap_size(base
, &size
);
390 /* special case for empty array */
391 if (type
->regions
[0].size
== 0) {
392 WARN_ON(type
->cnt
!= 1 || type
->total_size
);
393 type
->regions
[0].base
= base
;
394 type
->regions
[0].size
= size
;
395 memblock_set_region_node(&type
->regions
[0], nid
);
396 type
->total_size
= size
;
401 * The following is executed twice. Once with %false @insert and
402 * then with %true. The first counts the number of regions needed
403 * to accomodate the new area. The second actually inserts them.
408 for (i
= 0; i
< type
->cnt
; i
++) {
409 struct memblock_region
*rgn
= &type
->regions
[i
];
410 phys_addr_t rbase
= rgn
->base
;
411 phys_addr_t rend
= rbase
+ rgn
->size
;
418 * @rgn overlaps. If it separates the lower part of new
419 * area, insert that portion.
424 memblock_insert_region(type
, i
++, base
,
427 /* area below @rend is dealt with, forget about it */
428 base
= min(rend
, end
);
431 /* insert the remaining portion */
435 memblock_insert_region(type
, i
, base
, end
- base
, nid
);
439 * If this was the first round, resize array and repeat for actual
440 * insertions; otherwise, merge and return.
443 while (type
->cnt
+ nr_new
> type
->max
)
444 if (memblock_double_array(type
, obase
, size
) < 0)
449 memblock_merge_regions(type
);
454 int __init_memblock
memblock_add_node(phys_addr_t base
, phys_addr_t size
,
457 return memblock_add_region(&memblock
.memory
, base
, size
, nid
);
460 int __init_memblock
memblock_add(phys_addr_t base
, phys_addr_t size
)
462 return memblock_add_region(&memblock
.memory
, base
, size
, MAX_NUMNODES
);
466 * memblock_isolate_range - isolate given range into disjoint memblocks
467 * @type: memblock type to isolate range for
468 * @base: base of range to isolate
469 * @size: size of range to isolate
470 * @start_rgn: out parameter for the start of isolated region
471 * @end_rgn: out parameter for the end of isolated region
473 * Walk @type and ensure that regions don't cross the boundaries defined by
474 * [@base,@base+@size). Crossing regions are split at the boundaries,
475 * which may create at most two more regions. The index of the first
476 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
479 * 0 on success, -errno on failure.
481 static int __init_memblock
memblock_isolate_range(struct memblock_type
*type
,
482 phys_addr_t base
, phys_addr_t size
,
483 int *start_rgn
, int *end_rgn
)
485 phys_addr_t end
= base
+ memblock_cap_size(base
, &size
);
488 *start_rgn
= *end_rgn
= 0;
493 /* we'll create at most two more regions */
494 while (type
->cnt
+ 2 > type
->max
)
495 if (memblock_double_array(type
, base
, size
) < 0)
498 for (i
= 0; i
< type
->cnt
; i
++) {
499 struct memblock_region
*rgn
= &type
->regions
[i
];
500 phys_addr_t rbase
= rgn
->base
;
501 phys_addr_t rend
= rbase
+ rgn
->size
;
510 * @rgn intersects from below. Split and continue
511 * to process the next region - the new top half.
514 rgn
->size
-= base
- rbase
;
515 type
->total_size
-= base
- rbase
;
516 memblock_insert_region(type
, i
, rbase
, base
- rbase
,
517 memblock_get_region_node(rgn
));
518 } else if (rend
> end
) {
520 * @rgn intersects from above. Split and redo the
521 * current region - the new bottom half.
524 rgn
->size
-= end
- rbase
;
525 type
->total_size
-= end
- rbase
;
526 memblock_insert_region(type
, i
--, rbase
, end
- rbase
,
527 memblock_get_region_node(rgn
));
529 /* @rgn is fully contained, record it */
539 static int __init_memblock
__memblock_remove(struct memblock_type
*type
,
540 phys_addr_t base
, phys_addr_t size
)
542 int start_rgn
, end_rgn
;
545 ret
= memblock_isolate_range(type
, base
, size
, &start_rgn
, &end_rgn
);
549 for (i
= end_rgn
- 1; i
>= start_rgn
; i
--)
550 memblock_remove_region(type
, i
);
554 int __init_memblock
memblock_remove(phys_addr_t base
, phys_addr_t size
)
556 return __memblock_remove(&memblock
.memory
, base
, size
);
559 int __init_memblock
memblock_free(phys_addr_t base
, phys_addr_t size
)
561 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
562 (unsigned long long)base
,
563 (unsigned long long)base
+ size
,
566 return __memblock_remove(&memblock
.reserved
, base
, size
);
569 int __init_memblock
memblock_reserve(phys_addr_t base
, phys_addr_t size
)
571 struct memblock_type
*_rgn
= &memblock
.reserved
;
573 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
574 (unsigned long long)base
,
575 (unsigned long long)base
+ size
,
578 return memblock_add_region(_rgn
, base
, size
, MAX_NUMNODES
);
582 * __next_free_mem_range - next function for for_each_free_mem_range()
583 * @idx: pointer to u64 loop variable
584 * @nid: nid: node selector, %MAX_NUMNODES for all nodes
585 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
586 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
587 * @out_nid: ptr to int for nid of the range, can be %NULL
589 * Find the first free area from *@idx which matches @nid, fill the out
590 * parameters, and update *@idx for the next iteration. The lower 32bit of
591 * *@idx contains index into memory region and the upper 32bit indexes the
592 * areas before each reserved region. For example, if reserved regions
593 * look like the following,
595 * 0:[0-16), 1:[32-48), 2:[128-130)
597 * The upper 32bit indexes the following regions.
599 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
601 * As both region arrays are sorted, the function advances the two indices
602 * in lockstep and returns each intersection.
604 void __init_memblock
__next_free_mem_range(u64
*idx
, int nid
,
605 phys_addr_t
*out_start
,
606 phys_addr_t
*out_end
, int *out_nid
)
608 struct memblock_type
*mem
= &memblock
.memory
;
609 struct memblock_type
*rsv
= &memblock
.reserved
;
610 int mi
= *idx
& 0xffffffff;
613 for ( ; mi
< mem
->cnt
; mi
++) {
614 struct memblock_region
*m
= &mem
->regions
[mi
];
615 phys_addr_t m_start
= m
->base
;
616 phys_addr_t m_end
= m
->base
+ m
->size
;
618 /* only memory regions are associated with nodes, check it */
619 if (nid
!= MAX_NUMNODES
&& nid
!= memblock_get_region_node(m
))
622 /* scan areas before each reservation for intersection */
623 for ( ; ri
< rsv
->cnt
+ 1; ri
++) {
624 struct memblock_region
*r
= &rsv
->regions
[ri
];
625 phys_addr_t r_start
= ri
? r
[-1].base
+ r
[-1].size
: 0;
626 phys_addr_t r_end
= ri
< rsv
->cnt
? r
->base
: ULLONG_MAX
;
628 /* if ri advanced past mi, break out to advance mi */
629 if (r_start
>= m_end
)
631 /* if the two regions intersect, we're done */
632 if (m_start
< r_end
) {
634 *out_start
= max(m_start
, r_start
);
636 *out_end
= min(m_end
, r_end
);
638 *out_nid
= memblock_get_region_node(m
);
640 * The region which ends first is advanced
641 * for the next iteration.
647 *idx
= (u32
)mi
| (u64
)ri
<< 32;
653 /* signal end of iteration */
658 * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
659 * @idx: pointer to u64 loop variable
660 * @nid: nid: node selector, %MAX_NUMNODES for all nodes
661 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
662 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
663 * @out_nid: ptr to int for nid of the range, can be %NULL
665 * Reverse of __next_free_mem_range().
667 void __init_memblock
__next_free_mem_range_rev(u64
*idx
, int nid
,
668 phys_addr_t
*out_start
,
669 phys_addr_t
*out_end
, int *out_nid
)
671 struct memblock_type
*mem
= &memblock
.memory
;
672 struct memblock_type
*rsv
= &memblock
.reserved
;
673 int mi
= *idx
& 0xffffffff;
676 if (*idx
== (u64
)ULLONG_MAX
) {
681 for ( ; mi
>= 0; mi
--) {
682 struct memblock_region
*m
= &mem
->regions
[mi
];
683 phys_addr_t m_start
= m
->base
;
684 phys_addr_t m_end
= m
->base
+ m
->size
;
686 /* only memory regions are associated with nodes, check it */
687 if (nid
!= MAX_NUMNODES
&& nid
!= memblock_get_region_node(m
))
690 /* scan areas before each reservation for intersection */
691 for ( ; ri
>= 0; ri
--) {
692 struct memblock_region
*r
= &rsv
->regions
[ri
];
693 phys_addr_t r_start
= ri
? r
[-1].base
+ r
[-1].size
: 0;
694 phys_addr_t r_end
= ri
< rsv
->cnt
? r
->base
: ULLONG_MAX
;
696 /* if ri advanced past mi, break out to advance mi */
697 if (r_end
<= m_start
)
699 /* if the two regions intersect, we're done */
700 if (m_end
> r_start
) {
702 *out_start
= max(m_start
, r_start
);
704 *out_end
= min(m_end
, r_end
);
706 *out_nid
= memblock_get_region_node(m
);
708 if (m_start
>= r_start
)
712 *idx
= (u32
)mi
| (u64
)ri
<< 32;
721 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
723 * Common iterator interface used to define for_each_mem_range().
725 void __init_memblock
__next_mem_pfn_range(int *idx
, int nid
,
726 unsigned long *out_start_pfn
,
727 unsigned long *out_end_pfn
, int *out_nid
)
729 struct memblock_type
*type
= &memblock
.memory
;
730 struct memblock_region
*r
;
732 while (++*idx
< type
->cnt
) {
733 r
= &type
->regions
[*idx
];
735 if (PFN_UP(r
->base
) >= PFN_DOWN(r
->base
+ r
->size
))
737 if (nid
== MAX_NUMNODES
|| nid
== r
->nid
)
740 if (*idx
>= type
->cnt
) {
746 *out_start_pfn
= PFN_UP(r
->base
);
748 *out_end_pfn
= PFN_DOWN(r
->base
+ r
->size
);
754 * memblock_set_node - set node ID on memblock regions
755 * @base: base of area to set node ID for
756 * @size: size of area to set node ID for
757 * @nid: node ID to set
759 * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
760 * Regions which cross the area boundaries are split as necessary.
763 * 0 on success, -errno on failure.
765 int __init_memblock
memblock_set_node(phys_addr_t base
, phys_addr_t size
,
768 struct memblock_type
*type
= &memblock
.memory
;
769 int start_rgn
, end_rgn
;
772 ret
= memblock_isolate_range(type
, base
, size
, &start_rgn
, &end_rgn
);
776 for (i
= start_rgn
; i
< end_rgn
; i
++)
777 memblock_set_region_node(&type
->regions
[i
], nid
);
779 memblock_merge_regions(type
);
782 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
784 static phys_addr_t __init
memblock_alloc_base_nid(phys_addr_t size
,
785 phys_addr_t align
, phys_addr_t max_addr
,
790 /* align @size to avoid excessive fragmentation on reserved array */
791 size
= round_up(size
, align
);
793 found
= memblock_find_in_range_node(0, max_addr
, size
, align
, nid
);
794 if (found
&& !memblock_reserve(found
, size
))
800 phys_addr_t __init
memblock_alloc_nid(phys_addr_t size
, phys_addr_t align
, int nid
)
802 return memblock_alloc_base_nid(size
, align
, MEMBLOCK_ALLOC_ACCESSIBLE
, nid
);
805 phys_addr_t __init
__memblock_alloc_base(phys_addr_t size
, phys_addr_t align
, phys_addr_t max_addr
)
807 return memblock_alloc_base_nid(size
, align
, max_addr
, MAX_NUMNODES
);
810 phys_addr_t __init
memblock_alloc_base(phys_addr_t size
, phys_addr_t align
, phys_addr_t max_addr
)
814 alloc
= __memblock_alloc_base(size
, align
, max_addr
);
817 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
818 (unsigned long long) size
, (unsigned long long) max_addr
);
823 phys_addr_t __init
memblock_alloc(phys_addr_t size
, phys_addr_t align
)
825 return memblock_alloc_base(size
, align
, MEMBLOCK_ALLOC_ACCESSIBLE
);
828 phys_addr_t __init
memblock_alloc_try_nid(phys_addr_t size
, phys_addr_t align
, int nid
)
830 phys_addr_t res
= memblock_alloc_nid(size
, align
, nid
);
834 return memblock_alloc_base(size
, align
, MEMBLOCK_ALLOC_ACCESSIBLE
);
839 * Remaining API functions
842 phys_addr_t __init
memblock_phys_mem_size(void)
844 return memblock
.memory
.total_size
;
847 phys_addr_t __init
memblock_mem_size(unsigned long limit_pfn
)
849 unsigned long pages
= 0;
850 struct memblock_region
*r
;
851 unsigned long start_pfn
, end_pfn
;
853 for_each_memblock(memory
, r
) {
854 start_pfn
= memblock_region_memory_base_pfn(r
);
855 end_pfn
= memblock_region_memory_end_pfn(r
);
856 start_pfn
= min_t(unsigned long, start_pfn
, limit_pfn
);
857 end_pfn
= min_t(unsigned long, end_pfn
, limit_pfn
);
858 pages
+= end_pfn
- start_pfn
;
861 return (phys_addr_t
)pages
<< PAGE_SHIFT
;
865 phys_addr_t __init_memblock
memblock_start_of_DRAM(void)
867 return memblock
.memory
.regions
[0].base
;
870 phys_addr_t __init_memblock
memblock_end_of_DRAM(void)
872 int idx
= memblock
.memory
.cnt
- 1;
874 return (memblock
.memory
.regions
[idx
].base
+ memblock
.memory
.regions
[idx
].size
);
877 void __init
memblock_enforce_memory_limit(phys_addr_t limit
)
880 phys_addr_t max_addr
= (phys_addr_t
)ULLONG_MAX
;
885 /* find out max address */
886 for (i
= 0; i
< memblock
.memory
.cnt
; i
++) {
887 struct memblock_region
*r
= &memblock
.memory
.regions
[i
];
889 if (limit
<= r
->size
) {
890 max_addr
= r
->base
+ limit
;
896 /* truncate both memory and reserved regions */
897 __memblock_remove(&memblock
.memory
, max_addr
, (phys_addr_t
)ULLONG_MAX
);
898 __memblock_remove(&memblock
.reserved
, max_addr
, (phys_addr_t
)ULLONG_MAX
);
901 static int __init_memblock
memblock_search(struct memblock_type
*type
, phys_addr_t addr
)
903 unsigned int left
= 0, right
= type
->cnt
;
906 unsigned int mid
= (right
+ left
) / 2;
908 if (addr
< type
->regions
[mid
].base
)
910 else if (addr
>= (type
->regions
[mid
].base
+
911 type
->regions
[mid
].size
))
915 } while (left
< right
);
919 int __init
memblock_is_reserved(phys_addr_t addr
)
921 return memblock_search(&memblock
.reserved
, addr
) != -1;
924 int __init_memblock
memblock_is_memory(phys_addr_t addr
)
926 return memblock_search(&memblock
.memory
, addr
) != -1;
930 * memblock_is_region_memory - check if a region is a subset of memory
931 * @base: base of region to check
932 * @size: size of region to check
934 * Check if the region [@base, @base+@size) is a subset of a memory block.
937 * 0 if false, non-zero if true
939 int __init_memblock
memblock_is_region_memory(phys_addr_t base
, phys_addr_t size
)
941 int idx
= memblock_search(&memblock
.memory
, base
);
942 phys_addr_t end
= base
+ memblock_cap_size(base
, &size
);
946 return memblock
.memory
.regions
[idx
].base
<= base
&&
947 (memblock
.memory
.regions
[idx
].base
+
948 memblock
.memory
.regions
[idx
].size
) >= end
;
952 * memblock_is_region_reserved - check if a region intersects reserved memory
953 * @base: base of region to check
954 * @size: size of region to check
956 * Check if the region [@base, @base+@size) intersects a reserved memory block.
959 * 0 if false, non-zero if true
961 int __init_memblock
memblock_is_region_reserved(phys_addr_t base
, phys_addr_t size
)
963 memblock_cap_size(base
, &size
);
964 return memblock_overlaps_region(&memblock
.reserved
, base
, size
) >= 0;
967 void __init_memblock
memblock_trim_memory(phys_addr_t align
)
970 phys_addr_t start
, end
, orig_start
, orig_end
;
971 struct memblock_type
*mem
= &memblock
.memory
;
973 for (i
= 0; i
< mem
->cnt
; i
++) {
974 orig_start
= mem
->regions
[i
].base
;
975 orig_end
= mem
->regions
[i
].base
+ mem
->regions
[i
].size
;
976 start
= round_up(orig_start
, align
);
977 end
= round_down(orig_end
, align
);
979 if (start
== orig_start
&& end
== orig_end
)
983 mem
->regions
[i
].base
= start
;
984 mem
->regions
[i
].size
= end
- start
;
986 memblock_remove_region(mem
, i
);
992 void __init_memblock
memblock_set_current_limit(phys_addr_t limit
)
994 memblock
.current_limit
= limit
;
997 static void __init_memblock
memblock_dump(struct memblock_type
*type
, char *name
)
999 unsigned long long base
, size
;
1002 pr_info(" %s.cnt = 0x%lx\n", name
, type
->cnt
);
1004 for (i
= 0; i
< type
->cnt
; i
++) {
1005 struct memblock_region
*rgn
= &type
->regions
[i
];
1006 char nid_buf
[32] = "";
1010 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1011 if (memblock_get_region_node(rgn
) != MAX_NUMNODES
)
1012 snprintf(nid_buf
, sizeof(nid_buf
), " on node %d",
1013 memblock_get_region_node(rgn
));
1015 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
1016 name
, i
, base
, base
+ size
- 1, size
, nid_buf
);
1020 void __init_memblock
__memblock_dump_all(void)
1022 pr_info("MEMBLOCK configuration:\n");
1023 pr_info(" memory size = %#llx reserved size = %#llx\n",
1024 (unsigned long long)memblock
.memory
.total_size
,
1025 (unsigned long long)memblock
.reserved
.total_size
);
1027 memblock_dump(&memblock
.memory
, "memory");
1028 memblock_dump(&memblock
.reserved
, "reserved");
1031 void __init
memblock_allow_resize(void)
1033 memblock_can_resize
= 1;
1036 static int __init
early_memblock(char *p
)
1038 if (p
&& strstr(p
, "debug"))
1042 early_param("memblock", early_memblock
);
1044 #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
1046 static int memblock_debug_show(struct seq_file
*m
, void *private)
1048 struct memblock_type
*type
= m
->private;
1049 struct memblock_region
*reg
;
1052 for (i
= 0; i
< type
->cnt
; i
++) {
1053 reg
= &type
->regions
[i
];
1054 seq_printf(m
, "%4d: ", i
);
1055 if (sizeof(phys_addr_t
) == 4)
1056 seq_printf(m
, "0x%08lx..0x%08lx\n",
1057 (unsigned long)reg
->base
,
1058 (unsigned long)(reg
->base
+ reg
->size
- 1));
1060 seq_printf(m
, "0x%016llx..0x%016llx\n",
1061 (unsigned long long)reg
->base
,
1062 (unsigned long long)(reg
->base
+ reg
->size
- 1));
1068 static int memblock_debug_open(struct inode
*inode
, struct file
*file
)
1070 return single_open(file
, memblock_debug_show
, inode
->i_private
);
1073 static const struct file_operations memblock_debug_fops
= {
1074 .open
= memblock_debug_open
,
1076 .llseek
= seq_lseek
,
1077 .release
= single_release
,
1080 static int __init
memblock_init_debugfs(void)
1082 struct dentry
*root
= debugfs_create_dir("memblock", NULL
);
1085 debugfs_create_file("memory", S_IRUGO
, root
, &memblock
.memory
, &memblock_debug_fops
);
1086 debugfs_create_file("reserved", S_IRUGO
, root
, &memblock
.reserved
, &memblock_debug_fops
);
1090 __initcall(memblock_init_debugfs
);
1092 #endif /* CONFIG_DEBUG_FS */