4 * Copyright (C) 1999 Ingo Molnar
5 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
7 * simple boot-time physical memory area allocator and
8 * free memory collector. It's used to deal with reserved
9 * system memory and memory holes as well.
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>
23 * Access to this subsystem has to be serialized externally. (this is
24 * true for the boot process anyway)
26 unsigned long max_low_pfn
;
27 unsigned long min_low_pfn
;
28 unsigned long max_pfn
;
30 EXPORT_UNUSED_SYMBOL(max_pfn
); /* June 2006 */
32 static LIST_HEAD(bdata_list
);
33 #ifdef CONFIG_CRASH_DUMP
35 * If we have booted due to a crash, max_pfn will be a very low value. We need
36 * to know the amount of memory that the previous kernel used.
38 unsigned long saved_max_pfn
;
41 /* return the number of _pages_ that will be allocated for the boot bitmap */
42 unsigned long __init
bootmem_bootmap_pages(unsigned long pages
)
44 unsigned long mapsize
;
46 mapsize
= (pages
+7)/8;
47 mapsize
= (mapsize
+ ~PAGE_MASK
) & PAGE_MASK
;
48 mapsize
>>= PAGE_SHIFT
;
56 static void __init
link_bootmem(bootmem_data_t
*bdata
)
60 if (list_empty(&bdata_list
)) {
61 list_add(&bdata
->list
, &bdata_list
);
65 list_for_each_entry(ent
, &bdata_list
, list
) {
66 if (bdata
->node_boot_start
< ent
->node_boot_start
) {
67 list_add_tail(&bdata
->list
, &ent
->list
);
71 list_add_tail(&bdata
->list
, &bdata_list
);
75 * Given an initialised bdata, it returns the size of the boot bitmap
77 static unsigned long __init
get_mapsize(bootmem_data_t
*bdata
)
79 unsigned long mapsize
;
80 unsigned long start
= PFN_DOWN(bdata
->node_boot_start
);
81 unsigned long end
= bdata
->node_low_pfn
;
83 mapsize
= ((end
- start
) + 7) / 8;
84 return ALIGN(mapsize
, sizeof(long));
88 * Called once to set up the allocator itself.
90 static unsigned long __init
init_bootmem_core(pg_data_t
*pgdat
,
91 unsigned long mapstart
, unsigned long start
, unsigned long end
)
93 bootmem_data_t
*bdata
= pgdat
->bdata
;
94 unsigned long mapsize
;
96 bdata
->node_bootmem_map
= phys_to_virt(PFN_PHYS(mapstart
));
97 bdata
->node_boot_start
= PFN_PHYS(start
);
98 bdata
->node_low_pfn
= end
;
102 * Initially all pages are reserved - setup_arch() has to
103 * register free RAM areas explicitly.
105 mapsize
= get_mapsize(bdata
);
106 memset(bdata
->node_bootmem_map
, 0xff, mapsize
);
112 * Marks a particular physical memory range as unallocatable. Usable RAM
113 * might be used for boot-time allocations - or it might get added
114 * to the free page pool later on.
116 static void __init
reserve_bootmem_core(bootmem_data_t
*bdata
, unsigned long addr
,
119 unsigned long sidx
, eidx
;
123 * round up, partially reserved pages are considered
127 BUG_ON(PFN_DOWN(addr
) >= bdata
->node_low_pfn
);
128 BUG_ON(PFN_UP(addr
+ size
) > bdata
->node_low_pfn
);
130 sidx
= PFN_DOWN(addr
- bdata
->node_boot_start
);
131 eidx
= PFN_UP(addr
+ size
- bdata
->node_boot_start
);
133 for (i
= sidx
; i
< eidx
; i
++)
134 if (test_and_set_bit(i
, bdata
->node_bootmem_map
)) {
135 #ifdef CONFIG_DEBUG_BOOTMEM
136 printk("hm, page %08lx reserved twice.\n", i
*PAGE_SIZE
);
141 static void __init
free_bootmem_core(bootmem_data_t
*bdata
, unsigned long addr
,
144 unsigned long sidx
, eidx
;
148 * round down end of usable mem, partially free pages are
149 * considered reserved.
152 BUG_ON(PFN_DOWN(addr
+ size
) > bdata
->node_low_pfn
);
154 if (addr
< bdata
->last_success
)
155 bdata
->last_success
= addr
;
158 * Round up the beginning of the address.
160 sidx
= PFN_UP(addr
) - PFN_DOWN(bdata
->node_boot_start
);
161 eidx
= PFN_DOWN(addr
+ size
- bdata
->node_boot_start
);
163 for (i
= sidx
; i
< eidx
; i
++) {
164 if (unlikely(!test_and_clear_bit(i
, bdata
->node_bootmem_map
)))
170 * We 'merge' subsequent allocations to save space. We might 'lose'
171 * some fraction of a page if allocations cannot be satisfied due to
172 * size constraints on boxes where there is physical RAM space
173 * fragmentation - in these cases (mostly large memory boxes) this
176 * On low memory boxes we get it right in 100% of the cases.
178 * alignment has to be a power of 2 value.
180 * NOTE: This function is _not_ reentrant.
183 __alloc_bootmem_core(struct bootmem_data
*bdata
, unsigned long size
,
184 unsigned long align
, unsigned long goal
, unsigned long limit
)
186 unsigned long offset
, remaining_size
, areasize
, preferred
;
187 unsigned long i
, start
= 0, incr
, eidx
, end_pfn
;
191 printk("__alloc_bootmem_core(): zero-sized request\n");
194 BUG_ON(align
& (align
-1));
196 if (limit
&& bdata
->node_boot_start
>= limit
)
199 /* on nodes without memory - bootmem_map is NULL */
200 if (!bdata
->node_bootmem_map
)
203 end_pfn
= bdata
->node_low_pfn
;
204 limit
= PFN_DOWN(limit
);
205 if (limit
&& end_pfn
> limit
)
208 eidx
= end_pfn
- PFN_DOWN(bdata
->node_boot_start
);
210 if (align
&& (bdata
->node_boot_start
& (align
- 1UL)) != 0)
211 offset
= align
- (bdata
->node_boot_start
& (align
- 1UL));
212 offset
= PFN_DOWN(offset
);
215 * We try to allocate bootmem pages above 'goal'
216 * first, then we try to allocate lower pages.
218 if (goal
&& goal
>= bdata
->node_boot_start
&& PFN_DOWN(goal
) < end_pfn
) {
219 preferred
= goal
- bdata
->node_boot_start
;
221 if (bdata
->last_success
>= preferred
)
222 if (!limit
|| (limit
&& limit
> bdata
->last_success
))
223 preferred
= bdata
->last_success
;
227 preferred
= PFN_DOWN(ALIGN(preferred
, align
)) + offset
;
228 areasize
= (size
+ PAGE_SIZE
-1) / PAGE_SIZE
;
229 incr
= align
>> PAGE_SHIFT
? : 1;
232 for (i
= preferred
; i
< eidx
; i
+= incr
) {
234 i
= find_next_zero_bit(bdata
->node_bootmem_map
, eidx
, i
);
238 if (test_bit(i
, bdata
->node_bootmem_map
))
240 for (j
= i
+ 1; j
< i
+ areasize
; ++j
) {
243 if (test_bit(j
, bdata
->node_bootmem_map
))
252 if (preferred
> offset
) {
259 bdata
->last_success
= PFN_PHYS(start
);
260 BUG_ON(start
>= eidx
);
263 * Is the next page of the previous allocation-end the start
264 * of this allocation's buffer? If yes then we can 'merge'
265 * the previous partial page with this allocation.
267 if (align
< PAGE_SIZE
&&
268 bdata
->last_offset
&& bdata
->last_pos
+1 == start
) {
269 offset
= ALIGN(bdata
->last_offset
, align
);
270 BUG_ON(offset
> PAGE_SIZE
);
271 remaining_size
= PAGE_SIZE
- offset
;
272 if (size
< remaining_size
) {
274 /* last_pos unchanged */
275 bdata
->last_offset
= offset
+ size
;
276 ret
= phys_to_virt(bdata
->last_pos
* PAGE_SIZE
+
278 bdata
->node_boot_start
);
280 remaining_size
= size
- remaining_size
;
281 areasize
= (remaining_size
+ PAGE_SIZE
-1) / PAGE_SIZE
;
282 ret
= phys_to_virt(bdata
->last_pos
* PAGE_SIZE
+
284 bdata
->node_boot_start
);
285 bdata
->last_pos
= start
+ areasize
- 1;
286 bdata
->last_offset
= remaining_size
;
288 bdata
->last_offset
&= ~PAGE_MASK
;
290 bdata
->last_pos
= start
+ areasize
- 1;
291 bdata
->last_offset
= size
& ~PAGE_MASK
;
292 ret
= phys_to_virt(start
* PAGE_SIZE
+ bdata
->node_boot_start
);
296 * Reserve the area now:
298 for (i
= start
; i
< start
+ areasize
; i
++)
299 if (unlikely(test_and_set_bit(i
, bdata
->node_bootmem_map
)))
301 memset(ret
, 0, size
);
305 static unsigned long __init
free_all_bootmem_core(pg_data_t
*pgdat
)
309 bootmem_data_t
*bdata
= pgdat
->bdata
;
310 unsigned long i
, count
, total
= 0;
315 BUG_ON(!bdata
->node_bootmem_map
);
318 /* first extant page of the node */
319 pfn
= PFN_DOWN(bdata
->node_boot_start
);
320 idx
= bdata
->node_low_pfn
- pfn
;
321 map
= bdata
->node_bootmem_map
;
322 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
323 if (bdata
->node_boot_start
== 0 ||
324 ffs(bdata
->node_boot_start
) - PAGE_SHIFT
> ffs(BITS_PER_LONG
))
326 for (i
= 0; i
< idx
; ) {
327 unsigned long v
= ~map
[i
/ BITS_PER_LONG
];
329 if (gofast
&& v
== ~0UL) {
332 page
= pfn_to_page(pfn
);
333 count
+= BITS_PER_LONG
;
334 order
= ffs(BITS_PER_LONG
) - 1;
335 __free_pages_bootmem(page
, order
);
337 page
+= BITS_PER_LONG
;
341 page
= pfn_to_page(pfn
);
342 for (m
= 1; m
&& i
< idx
; m
<<=1, page
++, i
++) {
345 __free_pages_bootmem(page
, 0);
351 pfn
+= BITS_PER_LONG
;
356 * Now free the allocator bitmap itself, it's not
359 page
= virt_to_page(bdata
->node_bootmem_map
);
361 idx
= (get_mapsize(bdata
) + PAGE_SIZE
-1) >> PAGE_SHIFT
;
362 for (i
= 0; i
< idx
; i
++, page
++) {
363 __free_pages_bootmem(page
, 0);
367 bdata
->node_bootmem_map
= NULL
;
372 unsigned long __init
init_bootmem_node(pg_data_t
*pgdat
, unsigned long freepfn
,
373 unsigned long startpfn
, unsigned long endpfn
)
375 return init_bootmem_core(pgdat
, freepfn
, startpfn
, endpfn
);
378 void __init
reserve_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
381 reserve_bootmem_core(pgdat
->bdata
, physaddr
, size
);
384 void __init
free_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
387 free_bootmem_core(pgdat
->bdata
, physaddr
, size
);
390 unsigned long __init
free_all_bootmem_node(pg_data_t
*pgdat
)
392 return free_all_bootmem_core(pgdat
);
395 unsigned long __init
init_bootmem(unsigned long start
, unsigned long pages
)
399 return init_bootmem_core(NODE_DATA(0), start
, 0, pages
);
402 #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
403 void __init
reserve_bootmem(unsigned long addr
, unsigned long size
)
405 reserve_bootmem_core(NODE_DATA(0)->bdata
, addr
, size
);
407 #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
409 void __init
free_bootmem(unsigned long addr
, unsigned long size
)
411 free_bootmem_core(NODE_DATA(0)->bdata
, addr
, size
);
414 unsigned long __init
free_all_bootmem(void)
416 return free_all_bootmem_core(NODE_DATA(0));
419 void * __init
__alloc_bootmem_nopanic(unsigned long size
, unsigned long align
,
422 bootmem_data_t
*bdata
;
425 list_for_each_entry(bdata
, &bdata_list
, list
) {
426 ptr
= __alloc_bootmem_core(bdata
, size
, align
, goal
, 0);
433 void * __init
__alloc_bootmem(unsigned long size
, unsigned long align
,
436 void *mem
= __alloc_bootmem_nopanic(size
,align
,goal
);
441 * Whoops, we cannot satisfy the allocation request.
443 printk(KERN_ALERT
"bootmem alloc of %lu bytes failed!\n", size
);
444 panic("Out of memory");
449 void * __init
__alloc_bootmem_node(pg_data_t
*pgdat
, unsigned long size
,
450 unsigned long align
, unsigned long goal
)
454 ptr
= __alloc_bootmem_core(pgdat
->bdata
, size
, align
, goal
, 0);
458 return __alloc_bootmem(size
, align
, goal
);
461 #ifndef ARCH_LOW_ADDRESS_LIMIT
462 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
465 void * __init
__alloc_bootmem_low(unsigned long size
, unsigned long align
,
468 bootmem_data_t
*bdata
;
471 list_for_each_entry(bdata
, &bdata_list
, list
) {
472 ptr
= __alloc_bootmem_core(bdata
, size
, align
, goal
,
473 ARCH_LOW_ADDRESS_LIMIT
);
479 * Whoops, we cannot satisfy the allocation request.
481 printk(KERN_ALERT
"low bootmem alloc of %lu bytes failed!\n", size
);
482 panic("Out of low memory");
486 void * __init
__alloc_bootmem_low_node(pg_data_t
*pgdat
, unsigned long size
,
487 unsigned long align
, unsigned long goal
)
489 return __alloc_bootmem_core(pgdat
->bdata
, size
, align
, goal
,
490 ARCH_LOW_ADDRESS_LIMIT
);