bootmem: clean up arch-specific bootmem wrapping
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / bootmem.c
blobd7140c008ba8b6786d7480d3cb4068dcc6a3ce1b
1 /*
2 * bootmem - A boot-time physical memory allocator and configurator
4 * Copyright (C) 1999 Ingo Molnar
5 * 1999 Kanoj Sarcar, SGI
6 * 2008 Johannes Weiner
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>
16 #include <asm/bug.h>
17 #include <asm/io.h>
18 #include <asm/processor.h>
20 #include "internal.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;
32 #endif
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))
48 #endif
50 static int __init bootmem_debug_setup(char *buf)
52 bootmem_debug = 1;
53 return 0;
55 early_param("bootmem_debug", bootmem_debug_setup);
57 #define bdebug(fmt, args...) ({ \
58 if (unlikely(bootmem_debug)) \
59 printk(KERN_INFO \
60 "bootmem::%s " fmt, \
61 __func__, ## args); \
64 static unsigned long __init bootmap_bytes(unsigned long pages)
66 unsigned long bytes = (pages + 7) / 8;
68 return ALIGN(bytes, sizeof(long));
71 /**
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;
83 * link bdata in order
85 static void __init link_bootmem(bootmem_data_t *bdata)
87 struct list_head *iter;
89 list_for_each(iter, &bdata_list) {
90 bootmem_data_t *ent;
92 ent = list_entry(iter, bootmem_data_t, list);
93 if (bdata->node_min_pfn < ent->node_min_pfn)
94 break;
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;
111 link_bootmem(bdata);
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);
123 return 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)
150 max_low_pfn = pages;
151 min_low_pfn = start;
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)
157 int aligned;
158 struct page *page;
159 unsigned long start, end, pages, count = 0;
161 if (!bdata->node_bootmem_map)
162 return 0;
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;
188 } else {
189 unsigned long off = 0;
191 while (vec && off < BITS_PER_LONG) {
192 if (vec & 1) {
193 page = pfn_to_page(start + off);
194 __free_pages_bootmem(page, 0);
195 count++;
197 vec >>= 1;
198 off++;
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);
207 count += pages;
208 while (pages--)
209 __free_pages_bootmem(page++, 0);
211 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
213 return 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)
241 unsigned long idx;
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))
252 BUG();
255 static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
256 unsigned long eidx, int flags)
258 unsigned long idx;
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,
265 flags);
267 for (idx = sidx; idx < eidx; idx++)
268 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
269 if (exclusive) {
270 __free(bdata, sidx, idx);
271 return -EBUSY;
273 bdebug("silent double reserve of PFN %lx\n",
274 idx + bdata->node_min_pfn);
276 return 0;
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;
294 if (reserve)
295 return __reserve(bdata, sidx, eidx, flags);
296 else
297 __free(bdata, sidx, eidx);
298 return 0;
301 static int __init mark_bootmem(unsigned long start, unsigned long end,
302 int reserve, int flags)
304 unsigned long pos;
305 bootmem_data_t *bdata;
307 pos = start;
308 list_for_each_entry(bdata, &bdata_list, list) {
309 int err;
310 unsigned long max;
312 if (pos < bdata->node_min_pfn ||
313 pos >= bdata->node_low_pfn) {
314 BUG_ON(pos != start);
315 continue;
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);
323 return err;
326 if (max == end)
327 return 0;
328 pos = bdata->node_low_pfn;
330 BUG();
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,
344 unsigned long size)
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,
406 int flags)
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,
417 unsigned long step)
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,
430 unsigned long align)
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,
448 align, goal, limit);
450 BUG_ON(!size);
451 BUG_ON(align & (align - 1));
452 BUG_ON(limit && goal + size > limit);
454 if (!bdata->node_bootmem_map)
455 return NULL;
457 min = bdata->node_min_pfn;
458 max = bdata->node_low_pfn;
460 goal >>= PAGE_SHIFT;
461 limit >>= PAGE_SHIFT;
463 if (limit && max > limit)
464 max = limit;
465 if (max <= min)
466 return NULL;
468 step = max(align >> PAGE_SHIFT, 1UL);
470 if (goal && min < goal && goal < max)
471 start = ALIGN(goal, step);
472 else
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.
483 fallback = sidx + 1;
484 sidx = align_idx(bdata, bdata->hint_idx, step);
487 while (1) {
488 int merge;
489 void *region;
490 unsigned long eidx, i, start_off, end_off;
491 find_block:
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)
497 break;
499 for (i = sidx; i < eidx; i++)
500 if (test_bit(i, bdata->node_bootmem_map)) {
501 sidx = align_idx(bdata, i, step);
502 if (sidx == i)
503 sidx += step;
504 goto find_block;
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);
510 else
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))
524 BUG();
526 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
527 start_off);
528 memset(region, 0, size);
529 return region;
532 if (fallback) {
533 sidx = align_idx(bdata, fallback - 1, step);
534 fallback = 0;
535 goto find_block;
538 return NULL;
541 static void * __init ___alloc_bootmem_nopanic(unsigned long size,
542 unsigned long align,
543 unsigned long goal,
544 unsigned long limit)
546 bootmem_data_t *bdata;
548 restart:
549 list_for_each_entry(bdata, &bdata_list, list) {
550 void *region;
552 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
553 continue;
554 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
555 break;
557 region = alloc_bootmem_core(bdata, size, align, goal, limit);
558 if (region)
559 return region;
562 if (goal) {
563 goal = 0;
564 goto restart;
567 return NULL;
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,
584 unsigned long goal)
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);
594 if (mem)
595 return mem;
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");
601 return NULL;
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,
618 unsigned long goal)
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)
627 void *ptr;
629 ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
630 if (ptr)
631 return ptr;
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);
678 #endif
680 void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
681 unsigned long align, unsigned long goal)
683 void *ptr;
685 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
686 if (ptr)
687 return ptr;
689 return __alloc_bootmem_nopanic(size, align, goal);
692 #ifndef ARCH_LOW_ADDRESS_LIMIT
693 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
694 #endif
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,
710 unsigned long goal)
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);