2 * bootmem - A boot-time physical memory allocator and configurator
4 * Copyright (C) 1999 Ingo Molnar
5 * 1999 Kanoj Sarcar, SGI
8 * Access to this subsystem has to be serialized externally (which is true
9 * for the boot process anyway).
11 #include <linux/init.h>
12 #include <linux/pfn.h>
13 #include <linux/bootmem.h>
14 #include <linux/module.h>
18 #include <asm/processor.h>
22 unsigned long max_low_pfn
;
23 unsigned long min_low_pfn
;
24 unsigned long max_pfn
;
26 #ifdef CONFIG_CRASH_DUMP
28 * If we have booted due to a crash, max_pfn will be a very low value. We need
29 * to know the amount of memory that the previous kernel used.
31 unsigned long saved_max_pfn
;
34 bootmem_data_t bootmem_node_data
[MAX_NUMNODES
] __initdata
;
36 static struct list_head bdata_list __initdata
= LIST_HEAD_INIT(bdata_list
);
38 static int bootmem_debug
;
41 * If an arch needs to apply workarounds to bootmem allocation, it can
42 * set CONFIG_HAVE_ARCH_BOOTMEM and define a wrapper around
43 * __alloc_bootmem_core().
45 #ifndef CONFIG_HAVE_ARCH_BOOTMEM
46 #define alloc_bootmem_core(bdata, size, align, goal, limit) \
47 __alloc_bootmem_core((bdata), (size), (align), (goal), (limit))
50 static int __init
bootmem_debug_setup(char *buf
)
55 early_param("bootmem_debug", bootmem_debug_setup
);
57 #define bdebug(fmt, args...) ({ \
58 if (unlikely(bootmem_debug)) \
64 static unsigned long __init
bootmap_bytes(unsigned long pages
)
66 unsigned long bytes
= (pages
+ 7) / 8;
68 return ALIGN(bytes
, sizeof(long));
72 * bootmem_bootmap_pages - calculate bitmap size in pages
73 * @pages: number of pages the bitmap has to represent
75 unsigned long __init
bootmem_bootmap_pages(unsigned long pages
)
77 unsigned long bytes
= bootmap_bytes(pages
);
79 return PAGE_ALIGN(bytes
) >> PAGE_SHIFT
;
85 static void __init
link_bootmem(bootmem_data_t
*bdata
)
87 struct list_head
*iter
;
89 list_for_each(iter
, &bdata_list
) {
92 ent
= list_entry(iter
, bootmem_data_t
, list
);
93 if (bdata
->node_min_pfn
< ent
->node_min_pfn
)
96 list_add_tail(&bdata
->list
, iter
);
100 * Called once to set up the allocator itself.
102 static unsigned long __init
init_bootmem_core(bootmem_data_t
*bdata
,
103 unsigned long mapstart
, unsigned long start
, unsigned long end
)
105 unsigned long mapsize
;
107 mminit_validate_memmodel_limits(&start
, &end
);
108 bdata
->node_bootmem_map
= phys_to_virt(PFN_PHYS(mapstart
));
109 bdata
->node_min_pfn
= start
;
110 bdata
->node_low_pfn
= end
;
114 * Initially all pages are reserved - setup_arch() has to
115 * register free RAM areas explicitly.
117 mapsize
= bootmap_bytes(end
- start
);
118 memset(bdata
->node_bootmem_map
, 0xff, mapsize
);
120 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
121 bdata
- bootmem_node_data
, start
, mapstart
, end
, mapsize
);
127 * init_bootmem_node - register a node as boot memory
128 * @pgdat: node to register
129 * @freepfn: pfn where the bitmap for this node is to be placed
130 * @startpfn: first pfn on the node
131 * @endpfn: first pfn after the node
133 * Returns the number of bytes needed to hold the bitmap for this node.
135 unsigned long __init
init_bootmem_node(pg_data_t
*pgdat
, unsigned long freepfn
,
136 unsigned long startpfn
, unsigned long endpfn
)
138 return init_bootmem_core(pgdat
->bdata
, freepfn
, startpfn
, endpfn
);
142 * init_bootmem - register boot memory
143 * @start: pfn where the bitmap is to be placed
144 * @pages: number of available physical pages
146 * Returns the number of bytes needed to hold the bitmap.
148 unsigned long __init
init_bootmem(unsigned long start
, unsigned long pages
)
152 return init_bootmem_core(NODE_DATA(0)->bdata
, start
, 0, pages
);
155 static unsigned long __init
free_all_bootmem_core(bootmem_data_t
*bdata
)
159 unsigned long start
, end
, pages
, count
= 0;
161 if (!bdata
->node_bootmem_map
)
164 start
= bdata
->node_min_pfn
;
165 end
= bdata
->node_low_pfn
;
168 * If the start is aligned to the machines wordsize, we might
169 * be able to free pages in bulks of that order.
171 aligned
= !(start
& (BITS_PER_LONG
- 1));
173 bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
174 bdata
- bootmem_node_data
, start
, end
, aligned
);
176 while (start
< end
) {
177 unsigned long *map
, idx
, vec
;
179 map
= bdata
->node_bootmem_map
;
180 idx
= start
- bdata
->node_min_pfn
;
181 vec
= ~map
[idx
/ BITS_PER_LONG
];
183 if (aligned
&& vec
== ~0UL && start
+ BITS_PER_LONG
< end
) {
184 int order
= ilog2(BITS_PER_LONG
);
186 __free_pages_bootmem(pfn_to_page(start
), order
);
187 count
+= BITS_PER_LONG
;
189 unsigned long off
= 0;
191 while (vec
&& off
< BITS_PER_LONG
) {
193 page
= pfn_to_page(start
+ off
);
194 __free_pages_bootmem(page
, 0);
201 start
+= BITS_PER_LONG
;
204 page
= virt_to_page(bdata
->node_bootmem_map
);
205 pages
= bdata
->node_low_pfn
- bdata
->node_min_pfn
;
206 pages
= bootmem_bootmap_pages(pages
);
209 __free_pages_bootmem(page
++, 0);
211 bdebug("nid=%td released=%lx\n", bdata
- bootmem_node_data
, count
);
217 * free_all_bootmem_node - release a node's free pages to the buddy allocator
218 * @pgdat: node to be released
220 * Returns the number of pages actually released.
222 unsigned long __init
free_all_bootmem_node(pg_data_t
*pgdat
)
224 register_page_bootmem_info_node(pgdat
);
225 return free_all_bootmem_core(pgdat
->bdata
);
229 * free_all_bootmem - release free pages to the buddy allocator
231 * Returns the number of pages actually released.
233 unsigned long __init
free_all_bootmem(void)
235 return free_all_bootmem_core(NODE_DATA(0)->bdata
);
238 static void __init
__free(bootmem_data_t
*bdata
,
239 unsigned long sidx
, unsigned long eidx
)
243 bdebug("nid=%td start=%lx end=%lx\n", bdata
- bootmem_node_data
,
244 sidx
+ bdata
->node_min_pfn
,
245 eidx
+ bdata
->node_min_pfn
);
247 if (bdata
->hint_idx
> sidx
)
248 bdata
->hint_idx
= sidx
;
250 for (idx
= sidx
; idx
< eidx
; idx
++)
251 if (!test_and_clear_bit(idx
, bdata
->node_bootmem_map
))
255 static int __init
__reserve(bootmem_data_t
*bdata
, unsigned long sidx
,
256 unsigned long eidx
, int flags
)
259 int exclusive
= flags
& BOOTMEM_EXCLUSIVE
;
261 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
262 bdata
- bootmem_node_data
,
263 sidx
+ bdata
->node_min_pfn
,
264 eidx
+ bdata
->node_min_pfn
,
267 for (idx
= sidx
; idx
< eidx
; idx
++)
268 if (test_and_set_bit(idx
, bdata
->node_bootmem_map
)) {
270 __free(bdata
, sidx
, idx
);
273 bdebug("silent double reserve of PFN %lx\n",
274 idx
+ bdata
->node_min_pfn
);
279 static int __init
mark_bootmem_node(bootmem_data_t
*bdata
,
280 unsigned long start
, unsigned long end
,
281 int reserve
, int flags
)
283 unsigned long sidx
, eidx
;
285 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
286 bdata
- bootmem_node_data
, start
, end
, reserve
, flags
);
288 BUG_ON(start
< bdata
->node_min_pfn
);
289 BUG_ON(end
> bdata
->node_low_pfn
);
291 sidx
= start
- bdata
->node_min_pfn
;
292 eidx
= end
- bdata
->node_min_pfn
;
295 return __reserve(bdata
, sidx
, eidx
, flags
);
297 __free(bdata
, sidx
, eidx
);
301 static int __init
mark_bootmem(unsigned long start
, unsigned long end
,
302 int reserve
, int flags
)
305 bootmem_data_t
*bdata
;
308 list_for_each_entry(bdata
, &bdata_list
, list
) {
312 if (pos
< bdata
->node_min_pfn
||
313 pos
>= bdata
->node_low_pfn
) {
314 BUG_ON(pos
!= start
);
318 max
= min(bdata
->node_low_pfn
, end
);
320 err
= mark_bootmem_node(bdata
, pos
, max
, reserve
, flags
);
321 if (reserve
&& err
) {
322 mark_bootmem(start
, pos
, 0, 0);
328 pos
= bdata
->node_low_pfn
;
334 * free_bootmem_node - mark a page range as usable
335 * @pgdat: node the range resides on
336 * @physaddr: starting address of the range
337 * @size: size of the range in bytes
339 * Partial pages will be considered reserved and left as they are.
341 * The range must reside completely on the specified node.
343 void __init
free_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
346 unsigned long start
, end
;
348 start
= PFN_UP(physaddr
);
349 end
= PFN_DOWN(physaddr
+ size
);
351 mark_bootmem_node(pgdat
->bdata
, start
, end
, 0, 0);
355 * free_bootmem - mark a page range as usable
356 * @addr: starting address of the range
357 * @size: size of the range in bytes
359 * Partial pages will be considered reserved and left as they are.
361 * The range must be contiguous but may span node boundaries.
363 void __init
free_bootmem(unsigned long addr
, unsigned long size
)
365 unsigned long start
, end
;
367 start
= PFN_UP(addr
);
368 end
= PFN_DOWN(addr
+ size
);
370 mark_bootmem(start
, end
, 0, 0);
374 * reserve_bootmem_node - mark a page range as reserved
375 * @pgdat: node the range resides on
376 * @physaddr: starting address of the range
377 * @size: size of the range in bytes
378 * @flags: reservation flags (see linux/bootmem.h)
380 * Partial pages will be reserved.
382 * The range must reside completely on the specified node.
384 int __init
reserve_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
385 unsigned long size
, int flags
)
387 unsigned long start
, end
;
389 start
= PFN_DOWN(physaddr
);
390 end
= PFN_UP(physaddr
+ size
);
392 return mark_bootmem_node(pgdat
->bdata
, start
, end
, 1, flags
);
396 * reserve_bootmem - mark a page range as usable
397 * @addr: starting address of the range
398 * @size: size of the range in bytes
399 * @flags: reservation flags (see linux/bootmem.h)
401 * Partial pages will be reserved.
403 * The range must be contiguous but may span node boundaries.
405 int __init
reserve_bootmem(unsigned long addr
, unsigned long size
,
408 unsigned long start
, end
;
410 start
= PFN_DOWN(addr
);
411 end
= PFN_UP(addr
+ size
);
413 return mark_bootmem(start
, end
, 1, flags
);
416 static unsigned long align_idx(struct bootmem_data
*bdata
, unsigned long idx
,
419 unsigned long base
= bdata
->node_min_pfn
;
422 * Align the index with respect to the node start so that the
423 * combination of both satisfies the requested alignment.
426 return ALIGN(base
+ idx
, step
) - base
;
429 static unsigned long align_off(struct bootmem_data
*bdata
, unsigned long off
,
432 unsigned long base
= PFN_PHYS(bdata
->node_min_pfn
);
434 /* Same as align_idx for byte offsets */
436 return ALIGN(base
+ off
, align
) - base
;
439 static void * __init
__alloc_bootmem_core(struct bootmem_data
*bdata
,
440 unsigned long size
, unsigned long align
,
441 unsigned long goal
, unsigned long limit
)
443 unsigned long fallback
= 0;
444 unsigned long min
, max
, start
, sidx
, midx
, step
;
446 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
447 bdata
- bootmem_node_data
, size
, PAGE_ALIGN(size
) >> PAGE_SHIFT
,
451 BUG_ON(align
& (align
- 1));
452 BUG_ON(limit
&& goal
+ size
> limit
);
454 if (!bdata
->node_bootmem_map
)
457 min
= bdata
->node_min_pfn
;
458 max
= bdata
->node_low_pfn
;
461 limit
>>= PAGE_SHIFT
;
463 if (limit
&& max
> limit
)
468 step
= max(align
>> PAGE_SHIFT
, 1UL);
470 if (goal
&& min
< goal
&& goal
< max
)
471 start
= ALIGN(goal
, step
);
473 start
= ALIGN(min
, step
);
475 sidx
= start
- bdata
->node_min_pfn
;
476 midx
= max
- bdata
->node_min_pfn
;
478 if (bdata
->hint_idx
> sidx
) {
480 * Handle the valid case of sidx being zero and still
481 * catch the fallback below.
484 sidx
= align_idx(bdata
, bdata
->hint_idx
, step
);
490 unsigned long eidx
, i
, start_off
, end_off
;
492 sidx
= find_next_zero_bit(bdata
->node_bootmem_map
, midx
, sidx
);
493 sidx
= align_idx(bdata
, sidx
, step
);
494 eidx
= sidx
+ PFN_UP(size
);
496 if (sidx
>= midx
|| eidx
> midx
)
499 for (i
= sidx
; i
< eidx
; i
++)
500 if (test_bit(i
, bdata
->node_bootmem_map
)) {
501 sidx
= align_idx(bdata
, i
, step
);
507 if (bdata
->last_end_off
& (PAGE_SIZE
- 1) &&
508 PFN_DOWN(bdata
->last_end_off
) + 1 == sidx
)
509 start_off
= align_off(bdata
, bdata
->last_end_off
, align
);
511 start_off
= PFN_PHYS(sidx
);
513 merge
= PFN_DOWN(start_off
) < sidx
;
514 end_off
= start_off
+ size
;
516 bdata
->last_end_off
= end_off
;
517 bdata
->hint_idx
= PFN_UP(end_off
);
520 * Reserve the area now:
522 if (__reserve(bdata
, PFN_DOWN(start_off
) + merge
,
523 PFN_UP(end_off
), BOOTMEM_EXCLUSIVE
))
526 region
= phys_to_virt(PFN_PHYS(bdata
->node_min_pfn
) +
528 memset(region
, 0, size
);
533 sidx
= align_idx(bdata
, fallback
- 1, step
);
541 static void * __init
___alloc_bootmem_nopanic(unsigned long size
,
546 bootmem_data_t
*bdata
;
549 list_for_each_entry(bdata
, &bdata_list
, list
) {
552 if (goal
&& bdata
->node_low_pfn
<= PFN_DOWN(goal
))
554 if (limit
&& bdata
->node_min_pfn
>= PFN_DOWN(limit
))
557 region
= alloc_bootmem_core(bdata
, size
, align
, goal
, limit
);
571 * __alloc_bootmem_nopanic - allocate boot memory without panicking
572 * @size: size of the request in bytes
573 * @align: alignment of the region
574 * @goal: preferred starting address of the region
576 * The goal is dropped if it can not be satisfied and the allocation will
577 * fall back to memory below @goal.
579 * Allocation may happen on any node in the system.
581 * Returns NULL on failure.
583 void * __init
__alloc_bootmem_nopanic(unsigned long size
, unsigned long align
,
586 return ___alloc_bootmem_nopanic(size
, align
, goal
, 0);
589 static void * __init
___alloc_bootmem(unsigned long size
, unsigned long align
,
590 unsigned long goal
, unsigned long limit
)
592 void *mem
= ___alloc_bootmem_nopanic(size
, align
, goal
, limit
);
597 * Whoops, we cannot satisfy the allocation request.
599 printk(KERN_ALERT
"bootmem alloc of %lu bytes failed!\n", size
);
600 panic("Out of memory");
605 * __alloc_bootmem - allocate boot memory
606 * @size: size of the request in bytes
607 * @align: alignment of the region
608 * @goal: preferred starting address of the region
610 * The goal is dropped if it can not be satisfied and the allocation will
611 * fall back to memory below @goal.
613 * Allocation may happen on any node in the system.
615 * The function panics if the request can not be satisfied.
617 void * __init
__alloc_bootmem(unsigned long size
, unsigned long align
,
620 return ___alloc_bootmem(size
, align
, goal
, 0);
623 static void * __init
___alloc_bootmem_node(bootmem_data_t
*bdata
,
624 unsigned long size
, unsigned long align
,
625 unsigned long goal
, unsigned long limit
)
629 ptr
= alloc_bootmem_core(bdata
, size
, align
, goal
, limit
);
633 return ___alloc_bootmem(size
, align
, goal
, limit
);
637 * __alloc_bootmem_node - allocate boot memory from a specific node
638 * @pgdat: node to allocate from
639 * @size: size of the request in bytes
640 * @align: alignment of the region
641 * @goal: preferred starting address of the region
643 * The goal is dropped if it can not be satisfied and the allocation will
644 * fall back to memory below @goal.
646 * Allocation may fall back to any node in the system if the specified node
647 * can not hold the requested memory.
649 * The function panics if the request can not be satisfied.
651 void * __init
__alloc_bootmem_node(pg_data_t
*pgdat
, unsigned long size
,
652 unsigned long align
, unsigned long goal
)
654 return ___alloc_bootmem_node(pgdat
->bdata
, size
, align
, goal
, 0);
657 #ifdef CONFIG_SPARSEMEM
659 * alloc_bootmem_section - allocate boot memory from a specific section
660 * @size: size of the request in bytes
661 * @section_nr: sparse map section to allocate from
663 * Return NULL on failure.
665 void * __init
alloc_bootmem_section(unsigned long size
,
666 unsigned long section_nr
)
668 bootmem_data_t
*bdata
;
669 unsigned long pfn
, goal
, limit
;
671 pfn
= section_nr_to_pfn(section_nr
);
672 goal
= pfn
<< PAGE_SHIFT
;
673 limit
= section_nr_to_pfn(section_nr
+ 1) << PAGE_SHIFT
;
674 bdata
= &bootmem_node_data
[early_pfn_to_nid(pfn
)];
676 return alloc_bootmem_core(bdata
, size
, SMP_CACHE_BYTES
, goal
, limit
);
680 void * __init
__alloc_bootmem_node_nopanic(pg_data_t
*pgdat
, unsigned long size
,
681 unsigned long align
, unsigned long goal
)
685 ptr
= alloc_bootmem_core(pgdat
->bdata
, size
, align
, goal
, 0);
689 return __alloc_bootmem_nopanic(size
, align
, goal
);
692 #ifndef ARCH_LOW_ADDRESS_LIMIT
693 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
697 * __alloc_bootmem_low - allocate low boot memory
698 * @size: size of the request in bytes
699 * @align: alignment of the region
700 * @goal: preferred starting address of the region
702 * The goal is dropped if it can not be satisfied and the allocation will
703 * fall back to memory below @goal.
705 * Allocation may happen on any node in the system.
707 * The function panics if the request can not be satisfied.
709 void * __init
__alloc_bootmem_low(unsigned long size
, unsigned long align
,
712 return ___alloc_bootmem(size
, align
, goal
, ARCH_LOW_ADDRESS_LIMIT
);
716 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
717 * @pgdat: node to allocate from
718 * @size: size of the request in bytes
719 * @align: alignment of the region
720 * @goal: preferred starting address of the region
722 * The goal is dropped if it can not be satisfied and the allocation will
723 * fall back to memory below @goal.
725 * Allocation may fall back to any node in the system if the specified node
726 * can not hold the requested memory.
728 * The function panics if the request can not be satisfied.
730 void * __init
__alloc_bootmem_low_node(pg_data_t
*pgdat
, unsigned long size
,
731 unsigned long align
, unsigned long goal
)
733 return ___alloc_bootmem_node(pgdat
->bdata
, size
, align
,
734 goal
, ARCH_LOW_ADDRESS_LIMIT
);