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.
95 * If we have CONFIG_HAVE_MEMBLOCK_NODE_MAP defined, we need to check if the
96 * memory we found if not in hotpluggable ranges.
99 * Found address on success, %0 on failure.
101 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
102 phys_addr_t __init_memblock
memblock_find_in_range_node(phys_addr_t start
,
103 phys_addr_t end
, phys_addr_t size
,
104 phys_addr_t align
, int nid
)
106 phys_addr_t this_start
, this_end
, cand
;
108 int curr
= movablemem_map
.nr_map
- 1;
111 if (end
== MEMBLOCK_ALLOC_ACCESSIBLE
)
112 end
= memblock
.current_limit
;
114 /* avoid allocating the first page */
115 start
= max_t(phys_addr_t
, start
, PAGE_SIZE
);
116 end
= max(start
, end
);
118 for_each_free_mem_range_reverse(i
, nid
, &this_start
, &this_end
, NULL
) {
119 this_start
= clamp(this_start
, start
, end
);
120 this_end
= clamp(this_end
, start
, end
);
123 if (this_end
<= this_start
|| this_end
< size
)
126 for (; curr
>= 0; curr
--) {
127 if ((movablemem_map
.map
[curr
].start_pfn
<< PAGE_SHIFT
)
132 cand
= round_down(this_end
- size
, align
);
134 cand
< movablemem_map
.map
[curr
].end_pfn
<< PAGE_SHIFT
) {
135 this_end
= movablemem_map
.map
[curr
].start_pfn
140 if (cand
>= this_start
)
146 #else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
147 phys_addr_t __init_memblock
memblock_find_in_range_node(phys_addr_t start
,
148 phys_addr_t end
, phys_addr_t size
,
149 phys_addr_t align
, int nid
)
151 phys_addr_t this_start
, this_end
, cand
;
155 if (end
== MEMBLOCK_ALLOC_ACCESSIBLE
)
156 end
= memblock
.current_limit
;
158 /* avoid allocating the first page */
159 start
= max_t(phys_addr_t
, start
, PAGE_SIZE
);
160 end
= max(start
, end
);
162 for_each_free_mem_range_reverse(i
, nid
, &this_start
, &this_end
, NULL
) {
163 this_start
= clamp(this_start
, start
, end
);
164 this_end
= clamp(this_end
, start
, end
);
169 cand
= round_down(this_end
- size
, align
);
170 if (cand
>= this_start
)
175 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
178 * memblock_find_in_range - find free area in given range
179 * @start: start of candidate range
180 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
181 * @size: size of free area to find
182 * @align: alignment of free area to find
184 * Find @size free area aligned to @align in the specified range.
187 * Found address on success, %0 on failure.
189 phys_addr_t __init_memblock
memblock_find_in_range(phys_addr_t start
,
190 phys_addr_t end
, phys_addr_t size
,
193 return memblock_find_in_range_node(start
, end
, size
, align
,
197 static void __init_memblock
memblock_remove_region(struct memblock_type
*type
, unsigned long r
)
199 type
->total_size
-= type
->regions
[r
].size
;
200 memmove(&type
->regions
[r
], &type
->regions
[r
+ 1],
201 (type
->cnt
- (r
+ 1)) * sizeof(type
->regions
[r
]));
204 /* Special case for empty arrays */
205 if (type
->cnt
== 0) {
206 WARN_ON(type
->total_size
!= 0);
208 type
->regions
[0].base
= 0;
209 type
->regions
[0].size
= 0;
210 memblock_set_region_node(&type
->regions
[0], MAX_NUMNODES
);
214 phys_addr_t __init_memblock
get_allocated_memblock_reserved_regions_info(
217 if (memblock
.reserved
.regions
== memblock_reserved_init_regions
)
220 *addr
= __pa(memblock
.reserved
.regions
);
222 return PAGE_ALIGN(sizeof(struct memblock_region
) *
223 memblock
.reserved
.max
);
227 * memblock_double_array - double the size of the memblock regions array
228 * @type: memblock type of the regions array being doubled
229 * @new_area_start: starting address of memory range to avoid overlap with
230 * @new_area_size: size of memory range to avoid overlap with
232 * Double the size of the @type regions array. If memblock is being used to
233 * allocate memory for a new reserved regions array and there is a previously
234 * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
235 * waiting to be reserved, ensure the memory used by the new array does
239 * 0 on success, -1 on failure.
241 static int __init_memblock
memblock_double_array(struct memblock_type
*type
,
242 phys_addr_t new_area_start
,
243 phys_addr_t new_area_size
)
245 struct memblock_region
*new_array
, *old_array
;
246 phys_addr_t old_alloc_size
, new_alloc_size
;
247 phys_addr_t old_size
, new_size
, addr
;
248 int use_slab
= slab_is_available();
251 /* We don't allow resizing until we know about the reserved regions
252 * of memory that aren't suitable for allocation
254 if (!memblock_can_resize
)
257 /* Calculate new doubled size */
258 old_size
= type
->max
* sizeof(struct memblock_region
);
259 new_size
= old_size
<< 1;
261 * We need to allocated new one align to PAGE_SIZE,
262 * so we can free them completely later.
264 old_alloc_size
= PAGE_ALIGN(old_size
);
265 new_alloc_size
= PAGE_ALIGN(new_size
);
267 /* Retrieve the slab flag */
268 if (type
== &memblock
.memory
)
269 in_slab
= &memblock_memory_in_slab
;
271 in_slab
= &memblock_reserved_in_slab
;
273 /* Try to find some space for it.
275 * WARNING: We assume that either slab_is_available() and we use it or
276 * we use MEMBLOCK for allocations. That means that this is unsafe to
277 * use when bootmem is currently active (unless bootmem itself is
278 * implemented on top of MEMBLOCK which isn't the case yet)
280 * This should however not be an issue for now, as we currently only
281 * call into MEMBLOCK while it's still active, or much later when slab
282 * is active for memory hotplug operations
285 new_array
= kmalloc(new_size
, GFP_KERNEL
);
286 addr
= new_array
? __pa(new_array
) : 0;
288 /* only exclude range when trying to double reserved.regions */
289 if (type
!= &memblock
.reserved
)
290 new_area_start
= new_area_size
= 0;
292 addr
= memblock_find_in_range(new_area_start
+ new_area_size
,
293 memblock
.current_limit
,
294 new_alloc_size
, PAGE_SIZE
);
295 if (!addr
&& new_area_size
)
296 addr
= memblock_find_in_range(0,
297 min(new_area_start
, memblock
.current_limit
),
298 new_alloc_size
, PAGE_SIZE
);
300 new_array
= addr
? __va(addr
) : NULL
;
303 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
304 memblock_type_name(type
), type
->max
, type
->max
* 2);
308 memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
309 memblock_type_name(type
), type
->max
* 2, (u64
)addr
,
310 (u64
)addr
+ new_size
- 1);
313 * Found space, we now need to move the array over before we add the
314 * reserved region since it may be our reserved array itself that is
317 memcpy(new_array
, type
->regions
, old_size
);
318 memset(new_array
+ type
->max
, 0, old_size
);
319 old_array
= type
->regions
;
320 type
->regions
= new_array
;
323 /* Free old array. We needn't free it if the array is the static one */
326 else if (old_array
!= memblock_memory_init_regions
&&
327 old_array
!= memblock_reserved_init_regions
)
328 memblock_free(__pa(old_array
), old_alloc_size
);
331 * Reserve the new array if that comes from the memblock. Otherwise, we
335 BUG_ON(memblock_reserve(addr
, new_alloc_size
));
337 /* Update slab flag */
344 * memblock_merge_regions - merge neighboring compatible regions
345 * @type: memblock type to scan
347 * Scan @type and merge neighboring compatible regions.
349 static void __init_memblock
memblock_merge_regions(struct memblock_type
*type
)
353 /* cnt never goes below 1 */
354 while (i
< type
->cnt
- 1) {
355 struct memblock_region
*this = &type
->regions
[i
];
356 struct memblock_region
*next
= &type
->regions
[i
+ 1];
358 if (this->base
+ this->size
!= next
->base
||
359 memblock_get_region_node(this) !=
360 memblock_get_region_node(next
)) {
361 BUG_ON(this->base
+ this->size
> next
->base
);
366 this->size
+= next
->size
;
367 /* move forward from next + 1, index of which is i + 2 */
368 memmove(next
, next
+ 1, (type
->cnt
- (i
+ 2)) * sizeof(*next
));
374 * memblock_insert_region - insert new memblock region
375 * @type: memblock type to insert into
376 * @idx: index for the insertion point
377 * @base: base address of the new region
378 * @size: size of the new region
380 * Insert new memblock region [@base,@base+@size) into @type at @idx.
381 * @type must already have extra room to accomodate the new region.
383 static void __init_memblock
memblock_insert_region(struct memblock_type
*type
,
384 int idx
, phys_addr_t base
,
385 phys_addr_t size
, int nid
)
387 struct memblock_region
*rgn
= &type
->regions
[idx
];
389 BUG_ON(type
->cnt
>= type
->max
);
390 memmove(rgn
+ 1, rgn
, (type
->cnt
- idx
) * sizeof(*rgn
));
393 memblock_set_region_node(rgn
, nid
);
395 type
->total_size
+= size
;
399 * memblock_add_region - add new memblock region
400 * @type: memblock type to add new region into
401 * @base: base address of the new region
402 * @size: size of the new region
403 * @nid: nid of the new region
405 * Add new memblock region [@base,@base+@size) into @type. The new region
406 * is allowed to overlap with existing ones - overlaps don't affect already
407 * existing regions. @type is guaranteed to be minimal (all neighbouring
408 * compatible regions are merged) after the addition.
411 * 0 on success, -errno on failure.
413 static int __init_memblock
memblock_add_region(struct memblock_type
*type
,
414 phys_addr_t base
, phys_addr_t size
, int nid
)
417 phys_addr_t obase
= base
;
418 phys_addr_t end
= base
+ memblock_cap_size(base
, &size
);
424 /* special case for empty array */
425 if (type
->regions
[0].size
== 0) {
426 WARN_ON(type
->cnt
!= 1 || type
->total_size
);
427 type
->regions
[0].base
= base
;
428 type
->regions
[0].size
= size
;
429 memblock_set_region_node(&type
->regions
[0], nid
);
430 type
->total_size
= size
;
435 * The following is executed twice. Once with %false @insert and
436 * then with %true. The first counts the number of regions needed
437 * to accomodate the new area. The second actually inserts them.
442 for (i
= 0; i
< type
->cnt
; i
++) {
443 struct memblock_region
*rgn
= &type
->regions
[i
];
444 phys_addr_t rbase
= rgn
->base
;
445 phys_addr_t rend
= rbase
+ rgn
->size
;
452 * @rgn overlaps. If it separates the lower part of new
453 * area, insert that portion.
458 memblock_insert_region(type
, i
++, base
,
461 /* area below @rend is dealt with, forget about it */
462 base
= min(rend
, end
);
465 /* insert the remaining portion */
469 memblock_insert_region(type
, i
, base
, end
- base
, nid
);
473 * If this was the first round, resize array and repeat for actual
474 * insertions; otherwise, merge and return.
477 while (type
->cnt
+ nr_new
> type
->max
)
478 if (memblock_double_array(type
, obase
, size
) < 0)
483 memblock_merge_regions(type
);
488 int __init_memblock
memblock_add_node(phys_addr_t base
, phys_addr_t size
,
491 return memblock_add_region(&memblock
.memory
, base
, size
, nid
);
494 int __init_memblock
memblock_add(phys_addr_t base
, phys_addr_t size
)
496 return memblock_add_region(&memblock
.memory
, base
, size
, MAX_NUMNODES
);
500 * memblock_isolate_range - isolate given range into disjoint memblocks
501 * @type: memblock type to isolate range for
502 * @base: base of range to isolate
503 * @size: size of range to isolate
504 * @start_rgn: out parameter for the start of isolated region
505 * @end_rgn: out parameter for the end of isolated region
507 * Walk @type and ensure that regions don't cross the boundaries defined by
508 * [@base,@base+@size). Crossing regions are split at the boundaries,
509 * which may create at most two more regions. The index of the first
510 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
513 * 0 on success, -errno on failure.
515 static int __init_memblock
memblock_isolate_range(struct memblock_type
*type
,
516 phys_addr_t base
, phys_addr_t size
,
517 int *start_rgn
, int *end_rgn
)
519 phys_addr_t end
= base
+ memblock_cap_size(base
, &size
);
522 *start_rgn
= *end_rgn
= 0;
527 /* we'll create at most two more regions */
528 while (type
->cnt
+ 2 > type
->max
)
529 if (memblock_double_array(type
, base
, size
) < 0)
532 for (i
= 0; i
< type
->cnt
; i
++) {
533 struct memblock_region
*rgn
= &type
->regions
[i
];
534 phys_addr_t rbase
= rgn
->base
;
535 phys_addr_t rend
= rbase
+ rgn
->size
;
544 * @rgn intersects from below. Split and continue
545 * to process the next region - the new top half.
548 rgn
->size
-= base
- rbase
;
549 type
->total_size
-= base
- rbase
;
550 memblock_insert_region(type
, i
, rbase
, base
- rbase
,
551 memblock_get_region_node(rgn
));
552 } else if (rend
> end
) {
554 * @rgn intersects from above. Split and redo the
555 * current region - the new bottom half.
558 rgn
->size
-= end
- rbase
;
559 type
->total_size
-= end
- rbase
;
560 memblock_insert_region(type
, i
--, rbase
, end
- rbase
,
561 memblock_get_region_node(rgn
));
563 /* @rgn is fully contained, record it */
573 static int __init_memblock
__memblock_remove(struct memblock_type
*type
,
574 phys_addr_t base
, phys_addr_t size
)
576 int start_rgn
, end_rgn
;
579 ret
= memblock_isolate_range(type
, base
, size
, &start_rgn
, &end_rgn
);
583 for (i
= end_rgn
- 1; i
>= start_rgn
; i
--)
584 memblock_remove_region(type
, i
);
588 int __init_memblock
memblock_remove(phys_addr_t base
, phys_addr_t size
)
590 return __memblock_remove(&memblock
.memory
, base
, size
);
593 int __init_memblock
memblock_free(phys_addr_t base
, phys_addr_t size
)
595 memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
596 (unsigned long long)base
,
597 (unsigned long long)base
+ size
,
600 return __memblock_remove(&memblock
.reserved
, base
, size
);
603 int __init_memblock
memblock_reserve(phys_addr_t base
, phys_addr_t size
)
605 struct memblock_type
*_rgn
= &memblock
.reserved
;
607 memblock_dbg("memblock_reserve: [%#016llx-%#016llx] %pF\n",
608 (unsigned long long)base
,
609 (unsigned long long)base
+ size
,
612 return memblock_add_region(_rgn
, base
, size
, MAX_NUMNODES
);
616 * __next_free_mem_range - next function for for_each_free_mem_range()
617 * @idx: pointer to u64 loop variable
618 * @nid: nid: node selector, %MAX_NUMNODES for all nodes
619 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
620 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
621 * @out_nid: ptr to int for nid of the range, can be %NULL
623 * Find the first free area from *@idx which matches @nid, fill the out
624 * parameters, and update *@idx for the next iteration. The lower 32bit of
625 * *@idx contains index into memory region and the upper 32bit indexes the
626 * areas before each reserved region. For example, if reserved regions
627 * look like the following,
629 * 0:[0-16), 1:[32-48), 2:[128-130)
631 * The upper 32bit indexes the following regions.
633 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
635 * As both region arrays are sorted, the function advances the two indices
636 * in lockstep and returns each intersection.
638 void __init_memblock
__next_free_mem_range(u64
*idx
, int nid
,
639 phys_addr_t
*out_start
,
640 phys_addr_t
*out_end
, int *out_nid
)
642 struct memblock_type
*mem
= &memblock
.memory
;
643 struct memblock_type
*rsv
= &memblock
.reserved
;
644 int mi
= *idx
& 0xffffffff;
647 for ( ; mi
< mem
->cnt
; mi
++) {
648 struct memblock_region
*m
= &mem
->regions
[mi
];
649 phys_addr_t m_start
= m
->base
;
650 phys_addr_t m_end
= m
->base
+ m
->size
;
652 /* only memory regions are associated with nodes, check it */
653 if (nid
!= MAX_NUMNODES
&& nid
!= memblock_get_region_node(m
))
656 /* scan areas before each reservation for intersection */
657 for ( ; ri
< rsv
->cnt
+ 1; ri
++) {
658 struct memblock_region
*r
= &rsv
->regions
[ri
];
659 phys_addr_t r_start
= ri
? r
[-1].base
+ r
[-1].size
: 0;
660 phys_addr_t r_end
= ri
< rsv
->cnt
? r
->base
: ULLONG_MAX
;
662 /* if ri advanced past mi, break out to advance mi */
663 if (r_start
>= m_end
)
665 /* if the two regions intersect, we're done */
666 if (m_start
< r_end
) {
668 *out_start
= max(m_start
, r_start
);
670 *out_end
= min(m_end
, r_end
);
672 *out_nid
= memblock_get_region_node(m
);
674 * The region which ends first is advanced
675 * for the next iteration.
681 *idx
= (u32
)mi
| (u64
)ri
<< 32;
687 /* signal end of iteration */
692 * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
693 * @idx: pointer to u64 loop variable
694 * @nid: nid: node selector, %MAX_NUMNODES for all nodes
695 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
696 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
697 * @out_nid: ptr to int for nid of the range, can be %NULL
699 * Reverse of __next_free_mem_range().
701 void __init_memblock
__next_free_mem_range_rev(u64
*idx
, int nid
,
702 phys_addr_t
*out_start
,
703 phys_addr_t
*out_end
, int *out_nid
)
705 struct memblock_type
*mem
= &memblock
.memory
;
706 struct memblock_type
*rsv
= &memblock
.reserved
;
707 int mi
= *idx
& 0xffffffff;
710 if (*idx
== (u64
)ULLONG_MAX
) {
715 for ( ; mi
>= 0; mi
--) {
716 struct memblock_region
*m
= &mem
->regions
[mi
];
717 phys_addr_t m_start
= m
->base
;
718 phys_addr_t m_end
= m
->base
+ m
->size
;
720 /* only memory regions are associated with nodes, check it */
721 if (nid
!= MAX_NUMNODES
&& nid
!= memblock_get_region_node(m
))
724 /* scan areas before each reservation for intersection */
725 for ( ; ri
>= 0; ri
--) {
726 struct memblock_region
*r
= &rsv
->regions
[ri
];
727 phys_addr_t r_start
= ri
? r
[-1].base
+ r
[-1].size
: 0;
728 phys_addr_t r_end
= ri
< rsv
->cnt
? r
->base
: ULLONG_MAX
;
730 /* if ri advanced past mi, break out to advance mi */
731 if (r_end
<= m_start
)
733 /* if the two regions intersect, we're done */
734 if (m_end
> r_start
) {
736 *out_start
= max(m_start
, r_start
);
738 *out_end
= min(m_end
, r_end
);
740 *out_nid
= memblock_get_region_node(m
);
742 if (m_start
>= r_start
)
746 *idx
= (u32
)mi
| (u64
)ri
<< 32;
755 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
757 * Common iterator interface used to define for_each_mem_range().
759 void __init_memblock
__next_mem_pfn_range(int *idx
, int nid
,
760 unsigned long *out_start_pfn
,
761 unsigned long *out_end_pfn
, int *out_nid
)
763 struct memblock_type
*type
= &memblock
.memory
;
764 struct memblock_region
*r
;
766 while (++*idx
< type
->cnt
) {
767 r
= &type
->regions
[*idx
];
769 if (PFN_UP(r
->base
) >= PFN_DOWN(r
->base
+ r
->size
))
771 if (nid
== MAX_NUMNODES
|| nid
== r
->nid
)
774 if (*idx
>= type
->cnt
) {
780 *out_start_pfn
= PFN_UP(r
->base
);
782 *out_end_pfn
= PFN_DOWN(r
->base
+ r
->size
);
788 * memblock_set_node - set node ID on memblock regions
789 * @base: base of area to set node ID for
790 * @size: size of area to set node ID for
791 * @nid: node ID to set
793 * Set the nid of memblock memory regions in [@base,@base+@size) to @nid.
794 * Regions which cross the area boundaries are split as necessary.
797 * 0 on success, -errno on failure.
799 int __init_memblock
memblock_set_node(phys_addr_t base
, phys_addr_t size
,
802 struct memblock_type
*type
= &memblock
.memory
;
803 int start_rgn
, end_rgn
;
806 ret
= memblock_isolate_range(type
, base
, size
, &start_rgn
, &end_rgn
);
810 for (i
= start_rgn
; i
< end_rgn
; i
++)
811 memblock_set_region_node(&type
->regions
[i
], nid
);
813 memblock_merge_regions(type
);
816 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
818 static phys_addr_t __init
memblock_alloc_base_nid(phys_addr_t size
,
819 phys_addr_t align
, phys_addr_t max_addr
,
824 /* align @size to avoid excessive fragmentation on reserved array */
825 size
= round_up(size
, align
);
827 found
= memblock_find_in_range_node(0, max_addr
, size
, align
, nid
);
828 if (found
&& !memblock_reserve(found
, size
))
834 phys_addr_t __init
memblock_alloc_nid(phys_addr_t size
, phys_addr_t align
, int nid
)
836 return memblock_alloc_base_nid(size
, align
, MEMBLOCK_ALLOC_ACCESSIBLE
, nid
);
839 phys_addr_t __init
__memblock_alloc_base(phys_addr_t size
, phys_addr_t align
, phys_addr_t max_addr
)
841 return memblock_alloc_base_nid(size
, align
, max_addr
, MAX_NUMNODES
);
844 phys_addr_t __init
memblock_alloc_base(phys_addr_t size
, phys_addr_t align
, phys_addr_t max_addr
)
848 alloc
= __memblock_alloc_base(size
, align
, max_addr
);
851 panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
852 (unsigned long long) size
, (unsigned long long) max_addr
);
857 phys_addr_t __init
memblock_alloc(phys_addr_t size
, phys_addr_t align
)
859 return memblock_alloc_base(size
, align
, MEMBLOCK_ALLOC_ACCESSIBLE
);
862 phys_addr_t __init
memblock_alloc_try_nid(phys_addr_t size
, phys_addr_t align
, int nid
)
864 phys_addr_t res
= memblock_alloc_nid(size
, align
, nid
);
868 return memblock_alloc_base(size
, align
, MEMBLOCK_ALLOC_ACCESSIBLE
);
873 * Remaining API functions
876 phys_addr_t __init
memblock_phys_mem_size(void)
878 return memblock
.memory
.total_size
;
881 phys_addr_t __init
memblock_mem_size(unsigned long limit_pfn
)
883 unsigned long pages
= 0;
884 struct memblock_region
*r
;
885 unsigned long start_pfn
, end_pfn
;
887 for_each_memblock(memory
, r
) {
888 start_pfn
= memblock_region_memory_base_pfn(r
);
889 end_pfn
= memblock_region_memory_end_pfn(r
);
890 start_pfn
= min_t(unsigned long, start_pfn
, limit_pfn
);
891 end_pfn
= min_t(unsigned long, end_pfn
, limit_pfn
);
892 pages
+= end_pfn
- start_pfn
;
895 return (phys_addr_t
)pages
<< PAGE_SHIFT
;
899 phys_addr_t __init_memblock
memblock_start_of_DRAM(void)
901 return memblock
.memory
.regions
[0].base
;
904 phys_addr_t __init_memblock
memblock_end_of_DRAM(void)
906 int idx
= memblock
.memory
.cnt
- 1;
908 return (memblock
.memory
.regions
[idx
].base
+ memblock
.memory
.regions
[idx
].size
);
911 void __init
memblock_enforce_memory_limit(phys_addr_t limit
)
914 phys_addr_t max_addr
= (phys_addr_t
)ULLONG_MAX
;
919 /* find out max address */
920 for (i
= 0; i
< memblock
.memory
.cnt
; i
++) {
921 struct memblock_region
*r
= &memblock
.memory
.regions
[i
];
923 if (limit
<= r
->size
) {
924 max_addr
= r
->base
+ limit
;
930 /* truncate both memory and reserved regions */
931 __memblock_remove(&memblock
.memory
, max_addr
, (phys_addr_t
)ULLONG_MAX
);
932 __memblock_remove(&memblock
.reserved
, max_addr
, (phys_addr_t
)ULLONG_MAX
);
935 static int __init_memblock
memblock_search(struct memblock_type
*type
, phys_addr_t addr
)
937 unsigned int left
= 0, right
= type
->cnt
;
940 unsigned int mid
= (right
+ left
) / 2;
942 if (addr
< type
->regions
[mid
].base
)
944 else if (addr
>= (type
->regions
[mid
].base
+
945 type
->regions
[mid
].size
))
949 } while (left
< right
);
953 int __init
memblock_is_reserved(phys_addr_t addr
)
955 return memblock_search(&memblock
.reserved
, addr
) != -1;
958 int __init_memblock
memblock_is_memory(phys_addr_t addr
)
960 return memblock_search(&memblock
.memory
, addr
) != -1;
964 * memblock_is_region_memory - check if a region is a subset of memory
965 * @base: base of region to check
966 * @size: size of region to check
968 * Check if the region [@base, @base+@size) is a subset of a memory block.
971 * 0 if false, non-zero if true
973 int __init_memblock
memblock_is_region_memory(phys_addr_t base
, phys_addr_t size
)
975 int idx
= memblock_search(&memblock
.memory
, base
);
976 phys_addr_t end
= base
+ memblock_cap_size(base
, &size
);
980 return memblock
.memory
.regions
[idx
].base
<= base
&&
981 (memblock
.memory
.regions
[idx
].base
+
982 memblock
.memory
.regions
[idx
].size
) >= end
;
986 * memblock_is_region_reserved - check if a region intersects reserved memory
987 * @base: base of region to check
988 * @size: size of region to check
990 * Check if the region [@base, @base+@size) intersects a reserved memory block.
993 * 0 if false, non-zero if true
995 int __init_memblock
memblock_is_region_reserved(phys_addr_t base
, phys_addr_t size
)
997 memblock_cap_size(base
, &size
);
998 return memblock_overlaps_region(&memblock
.reserved
, base
, size
) >= 0;
1001 void __init_memblock
memblock_trim_memory(phys_addr_t align
)
1004 phys_addr_t start
, end
, orig_start
, orig_end
;
1005 struct memblock_type
*mem
= &memblock
.memory
;
1007 for (i
= 0; i
< mem
->cnt
; i
++) {
1008 orig_start
= mem
->regions
[i
].base
;
1009 orig_end
= mem
->regions
[i
].base
+ mem
->regions
[i
].size
;
1010 start
= round_up(orig_start
, align
);
1011 end
= round_down(orig_end
, align
);
1013 if (start
== orig_start
&& end
== orig_end
)
1017 mem
->regions
[i
].base
= start
;
1018 mem
->regions
[i
].size
= end
- start
;
1020 memblock_remove_region(mem
, i
);
1026 void __init_memblock
memblock_set_current_limit(phys_addr_t limit
)
1028 memblock
.current_limit
= limit
;
1031 static void __init_memblock
memblock_dump(struct memblock_type
*type
, char *name
)
1033 unsigned long long base
, size
;
1036 pr_info(" %s.cnt = 0x%lx\n", name
, type
->cnt
);
1038 for (i
= 0; i
< type
->cnt
; i
++) {
1039 struct memblock_region
*rgn
= &type
->regions
[i
];
1040 char nid_buf
[32] = "";
1044 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
1045 if (memblock_get_region_node(rgn
) != MAX_NUMNODES
)
1046 snprintf(nid_buf
, sizeof(nid_buf
), " on node %d",
1047 memblock_get_region_node(rgn
));
1049 pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n",
1050 name
, i
, base
, base
+ size
- 1, size
, nid_buf
);
1054 void __init_memblock
__memblock_dump_all(void)
1056 pr_info("MEMBLOCK configuration:\n");
1057 pr_info(" memory size = %#llx reserved size = %#llx\n",
1058 (unsigned long long)memblock
.memory
.total_size
,
1059 (unsigned long long)memblock
.reserved
.total_size
);
1061 memblock_dump(&memblock
.memory
, "memory");
1062 memblock_dump(&memblock
.reserved
, "reserved");
1065 void __init
memblock_allow_resize(void)
1067 memblock_can_resize
= 1;
1070 static int __init
early_memblock(char *p
)
1072 if (p
&& strstr(p
, "debug"))
1076 early_param("memblock", early_memblock
);
1078 #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
1080 static int memblock_debug_show(struct seq_file
*m
, void *private)
1082 struct memblock_type
*type
= m
->private;
1083 struct memblock_region
*reg
;
1086 for (i
= 0; i
< type
->cnt
; i
++) {
1087 reg
= &type
->regions
[i
];
1088 seq_printf(m
, "%4d: ", i
);
1089 if (sizeof(phys_addr_t
) == 4)
1090 seq_printf(m
, "0x%08lx..0x%08lx\n",
1091 (unsigned long)reg
->base
,
1092 (unsigned long)(reg
->base
+ reg
->size
- 1));
1094 seq_printf(m
, "0x%016llx..0x%016llx\n",
1095 (unsigned long long)reg
->base
,
1096 (unsigned long long)(reg
->base
+ reg
->size
- 1));
1102 static int memblock_debug_open(struct inode
*inode
, struct file
*file
)
1104 return single_open(file
, memblock_debug_show
, inode
->i_private
);
1107 static const struct file_operations memblock_debug_fops
= {
1108 .open
= memblock_debug_open
,
1110 .llseek
= seq_lseek
,
1111 .release
= single_release
,
1114 static int __init
memblock_init_debugfs(void)
1116 struct dentry
*root
= debugfs_create_dir("memblock", NULL
);
1119 debugfs_create_file("memory", S_IRUGO
, root
, &memblock
.memory
, &memblock_debug_fops
);
1120 debugfs_create_file("reserved", S_IRUGO
, root
, &memblock
.reserved
, &memblock_debug_fops
);
1124 __initcall(memblock_init_debugfs
);
1126 #endif /* CONFIG_DEBUG_FS */