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 static LIST_HEAD(bdata_list);
27 #ifdef CONFIG_CRASH_DUMP
29 * If we have booted due to a crash, max_pfn will be a very low value. We need
30 * to know the amount of memory that the previous kernel used.
32 unsigned long saved_max_pfn;
35 bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
38 * Given an initialised bdata, it returns the size of the boot bitmap
40 static unsigned long __init get_mapsize(bootmem_data_t *bdata)
42 unsigned long mapsize;
43 unsigned long start = PFN_DOWN(bdata->node_boot_start);
44 unsigned long end = bdata->node_low_pfn;
46 mapsize = ((end - start) + 7) / 8;
47 return ALIGN(mapsize, sizeof(long));
51 * bootmem_bootmap_pages - calculate bitmap size in pages
52 * @pages: number of pages the bitmap has to represent
54 unsigned long __init bootmem_bootmap_pages(unsigned long pages)
56 unsigned long mapsize;
58 mapsize = (pages+7)/8;
59 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
60 mapsize >>= PAGE_SHIFT;
68 static void __init link_bootmem(bootmem_data_t *bdata)
72 if (list_empty(&bdata_list)) {
73 list_add(&bdata->list, &bdata_list);
77 list_for_each_entry(ent, &bdata_list, list) {
78 if (bdata->node_boot_start < ent->node_boot_start) {
79 list_add_tail(&bdata->list, &ent->list);
83 list_add_tail(&bdata->list, &bdata_list);
87 * Called once to set up the allocator itself.
89 static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
90 unsigned long mapstart, unsigned long start, unsigned long end)
92 unsigned long mapsize;
94 mminit_validate_memmodel_limits(&start, &end);
95 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
96 bdata->node_boot_start = PFN_PHYS(start);
97 bdata->node_low_pfn = end;
101 * Initially all pages are reserved - setup_arch() has to
102 * register free RAM areas explicitly.
104 mapsize = get_mapsize(bdata);
105 memset(bdata->node_bootmem_map, 0xff, mapsize);
111 * init_bootmem_node - register a node as boot memory
112 * @pgdat: node to register
113 * @freepfn: pfn where the bitmap for this node is to be placed
114 * @startpfn: first pfn on the node
115 * @endpfn: first pfn after the node
117 * Returns the number of bytes needed to hold the bitmap for this node.
119 unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
120 unsigned long startpfn, unsigned long endpfn)
122 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
126 * init_bootmem - register boot memory
127 * @start: pfn where the bitmap is to be placed
128 * @pages: number of available physical pages
130 * Returns the number of bytes needed to hold the bitmap.
132 unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
136 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
139 static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
143 unsigned long i, count;
148 BUG_ON(!bdata->node_bootmem_map);
151 /* first extant page of the node */
152 pfn = PFN_DOWN(bdata->node_boot_start);
153 idx = bdata->node_low_pfn - pfn;
154 map = bdata->node_bootmem_map;
156 * Check if we are aligned to BITS_PER_LONG pages. If so, we might
157 * be able to free page orders of that size at once.
159 if (!(pfn & (BITS_PER_LONG-1)))
162 for (i = 0; i < idx; ) {
163 unsigned long v = ~map[i / BITS_PER_LONG];
165 if (gofast && v == ~0UL) {
168 page = pfn_to_page(pfn);
169 count += BITS_PER_LONG;
170 order = ffs(BITS_PER_LONG) - 1;
171 __free_pages_bootmem(page, order);
173 page += BITS_PER_LONG;
177 page = pfn_to_page(pfn);
178 for (m = 1; m && i < idx; m<<=1, page++, i++) {
181 __free_pages_bootmem(page, 0);
187 pfn += BITS_PER_LONG;
191 * Now free the allocator bitmap itself, it's not
194 page = virt_to_page(bdata->node_bootmem_map);
195 idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
196 for (i = 0; i < idx; i++, page++)
197 __free_pages_bootmem(page, 0);
199 bdata->node_bootmem_map = NULL;
205 * free_all_bootmem_node - release a node's free pages to the buddy allocator
206 * @pgdat: node to be released
208 * Returns the number of pages actually released.
210 unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
212 register_page_bootmem_info_node(pgdat);
213 return free_all_bootmem_core(pgdat->bdata);
217 * free_all_bootmem - release free pages to the buddy allocator
219 * Returns the number of pages actually released.
221 unsigned long __init free_all_bootmem(void)
223 return free_all_bootmem_core(NODE_DATA(0)->bdata);
226 static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
229 unsigned long sidx, eidx;
235 if (addr + size < bdata->node_boot_start ||
236 PFN_DOWN(addr) > bdata->node_low_pfn)
239 * round down end of usable mem, partially free pages are
240 * considered reserved.
243 if (addr >= bdata->node_boot_start && addr < bdata->last_success)
244 bdata->last_success = addr;
247 * Round up to index to the range.
249 if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
250 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
254 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
255 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
256 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
258 for (i = sidx; i < eidx; i++) {
259 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
265 * free_bootmem_node - mark a page range as usable
266 * @pgdat: node the range resides on
267 * @physaddr: starting address of the range
268 * @size: size of the range in bytes
270 * Partial pages will be considered reserved and left as they are.
272 * Only physical pages that actually reside on @pgdat are marked.
274 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
277 free_bootmem_core(pgdat->bdata, physaddr, size);
281 * free_bootmem - mark a page range as usable
282 * @addr: starting address of the range
283 * @size: size of the range in bytes
285 * Partial pages will be considered reserved and left as they are.
287 * All physical pages within the range are marked, no matter what
288 * node they reside on.
290 void __init free_bootmem(unsigned long addr, unsigned long size)
292 bootmem_data_t *bdata;
293 list_for_each_entry(bdata, &bdata_list, list)
294 free_bootmem_core(bdata, addr, size);
298 * Marks a particular physical memory range as unallocatable. Usable RAM
299 * might be used for boot-time allocations - or it might get added
300 * to the free page pool later on.
302 static int __init can_reserve_bootmem_core(bootmem_data_t *bdata,
303 unsigned long addr, unsigned long size, int flags)
305 unsigned long sidx, eidx;
310 /* out of range, don't hold other */
311 if (addr + size < bdata->node_boot_start ||
312 PFN_DOWN(addr) > bdata->node_low_pfn)
316 * Round up to index to the range.
318 if (addr > bdata->node_boot_start)
319 sidx= PFN_DOWN(addr - bdata->node_boot_start);
323 eidx = PFN_UP(addr + size - bdata->node_boot_start);
324 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
325 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
327 for (i = sidx; i < eidx; i++) {
328 if (test_bit(i, bdata->node_bootmem_map)) {
329 if (flags & BOOTMEM_EXCLUSIVE)
338 static void __init reserve_bootmem_core(bootmem_data_t *bdata,
339 unsigned long addr, unsigned long size, int flags)
341 unsigned long sidx, eidx;
347 if (addr + size < bdata->node_boot_start ||
348 PFN_DOWN(addr) > bdata->node_low_pfn)
352 * Round up to index to the range.
354 if (addr > bdata->node_boot_start)
355 sidx= PFN_DOWN(addr - bdata->node_boot_start);
359 eidx = PFN_UP(addr + size - bdata->node_boot_start);
360 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
361 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
363 for (i = sidx; i < eidx; i++) {
364 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
365 #ifdef CONFIG_DEBUG_BOOTMEM
366 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
373 * reserve_bootmem_node - mark a page range as reserved
374 * @pgdat: node the range resides on
375 * @physaddr: starting address of the range
376 * @size: size of the range in bytes
377 * @flags: reservation flags (see linux/bootmem.h)
379 * Partial pages will be reserved.
381 * Only physical pages that actually reside on @pgdat are marked.
383 int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
384 unsigned long size, int flags)
388 ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
391 reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
395 #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
397 * reserve_bootmem - mark a page range as usable
398 * @addr: starting address of the range
399 * @size: size of the range in bytes
400 * @flags: reservation flags (see linux/bootmem.h)
402 * Partial pages will be reserved.
404 * All physical pages within the range are marked, no matter what
405 * node they reside on.
407 int __init reserve_bootmem(unsigned long addr, unsigned long size,
410 bootmem_data_t *bdata;
413 list_for_each_entry(bdata, &bdata_list, list) {
414 ret = can_reserve_bootmem_core(bdata, addr, size, flags);
418 list_for_each_entry(bdata, &bdata_list, list)
419 reserve_bootmem_core(bdata, addr, size, flags);
423 #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
426 * We 'merge' subsequent allocations to save space. We might 'lose'
427 * some fraction of a page if allocations cannot be satisfied due to
428 * size constraints on boxes where there is physical RAM space
429 * fragmentation - in these cases (mostly large memory boxes) this
432 * On low memory boxes we get it right in 100% of the cases.
434 * alignment has to be a power of 2 value.
436 * NOTE: This function is _not_ reentrant.
439 alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
440 unsigned long align, unsigned long goal, unsigned long limit)
442 unsigned long areasize, preferred;
443 unsigned long i, start = 0, incr, eidx, end_pfn;
445 unsigned long node_boot_start;
446 void *node_bootmem_map;
449 printk("alloc_bootmem_core(): zero-sized request\n");
452 BUG_ON(align & (align-1));
454 /* on nodes without memory - bootmem_map is NULL */
455 if (!bdata->node_bootmem_map)
458 /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
459 node_boot_start = bdata->node_boot_start;
460 node_bootmem_map = bdata->node_bootmem_map;
462 node_boot_start = ALIGN(bdata->node_boot_start, align);
463 if (node_boot_start > bdata->node_boot_start)
464 node_bootmem_map = (unsigned long *)bdata->node_bootmem_map +
465 PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG;
468 if (limit && node_boot_start >= limit)
471 end_pfn = bdata->node_low_pfn;
472 limit = PFN_DOWN(limit);
473 if (limit && end_pfn > limit)
476 eidx = end_pfn - PFN_DOWN(node_boot_start);
479 * We try to allocate bootmem pages above 'goal'
480 * first, then we try to allocate lower pages.
483 if (goal && PFN_DOWN(goal) < end_pfn) {
484 if (goal > node_boot_start)
485 preferred = goal - node_boot_start;
487 if (bdata->last_success > node_boot_start &&
488 bdata->last_success - node_boot_start >= preferred)
489 if (!limit || (limit && limit > bdata->last_success))
490 preferred = bdata->last_success - node_boot_start;
493 preferred = PFN_DOWN(ALIGN(preferred, align));
494 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
495 incr = align >> PAGE_SHIFT ? : 1;
498 for (i = preferred; i < eidx;) {
501 i = find_next_zero_bit(node_bootmem_map, eidx, i);
505 if (test_bit(i, node_bootmem_map)) {
509 for (j = i + 1; j < i + areasize; ++j) {
512 if (test_bit(j, node_bootmem_map))
530 bdata->last_success = PFN_PHYS(start) + node_boot_start;
531 BUG_ON(start >= eidx);
534 * Is the next page of the previous allocation-end the start
535 * of this allocation's buffer? If yes then we can 'merge'
536 * the previous partial page with this allocation.
538 if (align < PAGE_SIZE &&
539 bdata->last_offset && bdata->last_pos+1 == start) {
540 unsigned long offset, remaining_size;
541 offset = ALIGN(bdata->last_offset, align);
542 BUG_ON(offset > PAGE_SIZE);
543 remaining_size = PAGE_SIZE - offset;
544 if (size < remaining_size) {
546 /* last_pos unchanged */
547 bdata->last_offset = offset + size;
548 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
549 offset + node_boot_start);
551 remaining_size = size - remaining_size;
552 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
553 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
554 offset + node_boot_start);
555 bdata->last_pos = start + areasize - 1;
556 bdata->last_offset = remaining_size;
558 bdata->last_offset &= ~PAGE_MASK;
560 bdata->last_pos = start + areasize - 1;
561 bdata->last_offset = size & ~PAGE_MASK;
562 ret = phys_to_virt(start * PAGE_SIZE + node_boot_start);
566 * Reserve the area now:
568 for (i = start; i < start + areasize; i++)
569 if (unlikely(test_and_set_bit(i, node_bootmem_map)))
571 memset(ret, 0, size);
576 * __alloc_bootmem_nopanic - allocate boot memory without panicking
577 * @size: size of the request in bytes
578 * @align: alignment of the region
579 * @goal: preferred starting address of the region
581 * The goal is dropped if it can not be satisfied and the allocation will
582 * fall back to memory below @goal.
584 * Allocation may happen on any node in the system.
586 * Returns NULL on failure.
588 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
591 bootmem_data_t *bdata;
594 list_for_each_entry(bdata, &bdata_list, list) {
595 ptr = alloc_bootmem_core(bdata, size, align, goal, 0);
603 * __alloc_bootmem - allocate boot memory
604 * @size: size of the request in bytes
605 * @align: alignment of the region
606 * @goal: preferred starting address of the region
608 * The goal is dropped if it can not be satisfied and the allocation will
609 * fall back to memory below @goal.
611 * Allocation may happen on any node in the system.
613 * The function panics if the request can not be satisfied.
615 void * __init __alloc_bootmem(unsigned long size, unsigned long align,
618 void *mem = __alloc_bootmem_nopanic(size,align,goal);
623 * Whoops, we cannot satisfy the allocation request.
625 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
626 panic("Out of memory");
631 * __alloc_bootmem_node - allocate boot memory from a specific node
632 * @pgdat: node to allocate from
633 * @size: size of the request in bytes
634 * @align: alignment of the region
635 * @goal: preferred starting address of the region
637 * The goal is dropped if it can not be satisfied and the allocation will
638 * fall back to memory below @goal.
640 * Allocation may fall back to any node in the system if the specified node
641 * can not hold the requested memory.
643 * The function panics if the request can not be satisfied.
645 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
646 unsigned long align, unsigned long goal)
650 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
654 return __alloc_bootmem(size, align, goal);
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)
669 unsigned long limit, goal, start_nr, end_nr, pfn;
670 struct pglist_data *pgdat;
672 pfn = section_nr_to_pfn(section_nr);
673 goal = PFN_PHYS(pfn);
674 limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1;
675 pgdat = NODE_DATA(early_pfn_to_nid(pfn));
676 ptr = alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal,
682 start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr)));
683 end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size));
684 if (start_nr != section_nr || end_nr != section_nr) {
685 printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n",
687 free_bootmem_core(pgdat->bdata, __pa(ptr), size);
695 void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
696 unsigned long align, unsigned long goal)
700 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
704 return __alloc_bootmem_nopanic(size, align, goal);
707 #ifndef ARCH_LOW_ADDRESS_LIMIT
708 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
712 * __alloc_bootmem_low - allocate low boot memory
713 * @size: size of the request in bytes
714 * @align: alignment of the region
715 * @goal: preferred starting address of the region
717 * The goal is dropped if it can not be satisfied and the allocation will
718 * fall back to memory below @goal.
720 * Allocation may happen on any node in the system.
722 * The function panics if the request can not be satisfied.
724 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
727 bootmem_data_t *bdata;
730 list_for_each_entry(bdata, &bdata_list, list) {
731 ptr = alloc_bootmem_core(bdata, size, align, goal,
732 ARCH_LOW_ADDRESS_LIMIT);
738 * Whoops, we cannot satisfy the allocation request.
740 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
741 panic("Out of low memory");
746 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
747 * @pgdat: node to allocate from
748 * @size: size of the request in bytes
749 * @align: alignment of the region
750 * @goal: preferred starting address of the region
752 * The goal is dropped if it can not be satisfied and the allocation will
753 * fall back to memory below @goal.
755 * Allocation may fall back to any node in the system if the specified node
756 * can not hold the requested memory.
758 * The function panics if the request can not be satisfied.
760 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
761 unsigned long align, unsigned long goal)
763 return alloc_bootmem_core(pgdat->bdata, size, align, goal,
764 ARCH_LOW_ADDRESS_LIMIT);