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 static LIST_HEAD(bdata_list
);
31 #ifdef CONFIG_CRASH_DUMP
33 * If we have booted due to a crash, max_pfn will be a very low value. We need
34 * to know the amount of memory that the previous kernel used.
36 unsigned long saved_max_pfn
;
39 /* return the number of _pages_ that will be allocated for the boot bitmap */
40 unsigned long __init
bootmem_bootmap_pages(unsigned long pages
)
42 unsigned long mapsize
;
44 mapsize
= (pages
+7)/8;
45 mapsize
= (mapsize
+ ~PAGE_MASK
) & PAGE_MASK
;
46 mapsize
>>= PAGE_SHIFT
;
54 static void __init
link_bootmem(bootmem_data_t
*bdata
)
58 if (list_empty(&bdata_list
)) {
59 list_add(&bdata
->list
, &bdata_list
);
63 list_for_each_entry(ent
, &bdata_list
, list
) {
64 if (bdata
->node_boot_start
< ent
->node_boot_start
) {
65 list_add_tail(&bdata
->list
, &ent
->list
);
69 list_add_tail(&bdata
->list
, &bdata_list
);
73 * Given an initialised bdata, it returns the size of the boot bitmap
75 static unsigned long __init
get_mapsize(bootmem_data_t
*bdata
)
77 unsigned long mapsize
;
78 unsigned long start
= PFN_DOWN(bdata
->node_boot_start
);
79 unsigned long end
= bdata
->node_low_pfn
;
81 mapsize
= ((end
- start
) + 7) / 8;
82 return ALIGN(mapsize
, sizeof(long));
86 * Called once to set up the allocator itself.
88 static unsigned long __init
init_bootmem_core(pg_data_t
*pgdat
,
89 unsigned long mapstart
, unsigned long start
, unsigned long end
)
91 bootmem_data_t
*bdata
= pgdat
->bdata
;
92 unsigned long mapsize
;
94 bdata
->node_bootmem_map
= phys_to_virt(PFN_PHYS(mapstart
));
95 bdata
->node_boot_start
= PFN_PHYS(start
);
96 bdata
->node_low_pfn
= end
;
100 * Initially all pages are reserved - setup_arch() has to
101 * register free RAM areas explicitly.
103 mapsize
= get_mapsize(bdata
);
104 memset(bdata
->node_bootmem_map
, 0xff, mapsize
);
110 * Marks a particular physical memory range as unallocatable. Usable RAM
111 * might be used for boot-time allocations - or it might get added
112 * to the free page pool later on.
114 static int __init
can_reserve_bootmem_core(bootmem_data_t
*bdata
,
115 unsigned long addr
, unsigned long size
, int flags
)
117 unsigned long sidx
, eidx
;
122 /* out of range, don't hold other */
123 if (addr
+ size
< bdata
->node_boot_start
||
124 PFN_DOWN(addr
) > bdata
->node_low_pfn
)
128 * Round up to index to the range.
130 if (addr
> bdata
->node_boot_start
)
131 sidx
= PFN_DOWN(addr
- bdata
->node_boot_start
);
135 eidx
= PFN_UP(addr
+ size
- bdata
->node_boot_start
);
136 if (eidx
> bdata
->node_low_pfn
- PFN_DOWN(bdata
->node_boot_start
))
137 eidx
= bdata
->node_low_pfn
- PFN_DOWN(bdata
->node_boot_start
);
139 for (i
= sidx
; i
< eidx
; i
++) {
140 if (test_bit(i
, bdata
->node_bootmem_map
)) {
141 if (flags
& BOOTMEM_EXCLUSIVE
)
150 static void __init
reserve_bootmem_core(bootmem_data_t
*bdata
,
151 unsigned long addr
, unsigned long size
, int flags
)
153 unsigned long sidx
, eidx
;
159 if (addr
+ size
< bdata
->node_boot_start
||
160 PFN_DOWN(addr
) > bdata
->node_low_pfn
)
164 * Round up to index to the range.
166 if (addr
> bdata
->node_boot_start
)
167 sidx
= PFN_DOWN(addr
- bdata
->node_boot_start
);
171 eidx
= PFN_UP(addr
+ size
- bdata
->node_boot_start
);
172 if (eidx
> bdata
->node_low_pfn
- PFN_DOWN(bdata
->node_boot_start
))
173 eidx
= bdata
->node_low_pfn
- PFN_DOWN(bdata
->node_boot_start
);
175 for (i
= sidx
; i
< eidx
; i
++) {
176 if (test_and_set_bit(i
, bdata
->node_bootmem_map
)) {
177 #ifdef CONFIG_DEBUG_BOOTMEM
178 printk("hm, page %08lx reserved twice.\n", i
*PAGE_SIZE
);
184 static void __init
free_bootmem_core(bootmem_data_t
*bdata
, unsigned long addr
,
187 unsigned long sidx
, eidx
;
193 if (addr
+ size
< bdata
->node_boot_start
||
194 PFN_DOWN(addr
) > bdata
->node_low_pfn
)
197 * round down end of usable mem, partially free pages are
198 * considered reserved.
201 if (addr
>= bdata
->node_boot_start
&& addr
< bdata
->last_success
)
202 bdata
->last_success
= addr
;
205 * Round up to index to the range.
207 if (PFN_UP(addr
) > PFN_DOWN(bdata
->node_boot_start
))
208 sidx
= PFN_UP(addr
) - PFN_DOWN(bdata
->node_boot_start
);
212 eidx
= PFN_DOWN(addr
+ size
- bdata
->node_boot_start
);
213 if (eidx
> bdata
->node_low_pfn
- PFN_DOWN(bdata
->node_boot_start
))
214 eidx
= bdata
->node_low_pfn
- PFN_DOWN(bdata
->node_boot_start
);
216 for (i
= sidx
; i
< eidx
; i
++) {
217 if (unlikely(!test_and_clear_bit(i
, bdata
->node_bootmem_map
)))
223 * We 'merge' subsequent allocations to save space. We might 'lose'
224 * some fraction of a page if allocations cannot be satisfied due to
225 * size constraints on boxes where there is physical RAM space
226 * fragmentation - in these cases (mostly large memory boxes) this
229 * On low memory boxes we get it right in 100% of the cases.
231 * alignment has to be a power of 2 value.
233 * NOTE: This function is _not_ reentrant.
236 __alloc_bootmem_core(struct bootmem_data
*bdata
, unsigned long size
,
237 unsigned long align
, unsigned long goal
, unsigned long limit
)
239 unsigned long areasize
, preferred
;
240 unsigned long i
, start
= 0, incr
, eidx
, end_pfn
;
242 unsigned long node_boot_start
;
243 void *node_bootmem_map
;
246 printk("__alloc_bootmem_core(): zero-sized request\n");
249 BUG_ON(align
& (align
-1));
251 /* on nodes without memory - bootmem_map is NULL */
252 if (!bdata
->node_bootmem_map
)
255 /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
256 node_boot_start
= bdata
->node_boot_start
;
257 node_bootmem_map
= bdata
->node_bootmem_map
;
259 node_boot_start
= ALIGN(bdata
->node_boot_start
, align
);
260 if (node_boot_start
> bdata
->node_boot_start
)
261 node_bootmem_map
= (unsigned long *)bdata
->node_bootmem_map
+
262 PFN_DOWN(node_boot_start
- bdata
->node_boot_start
)/BITS_PER_LONG
;
265 if (limit
&& node_boot_start
>= limit
)
268 end_pfn
= bdata
->node_low_pfn
;
269 limit
= PFN_DOWN(limit
);
270 if (limit
&& end_pfn
> limit
)
273 eidx
= end_pfn
- PFN_DOWN(node_boot_start
);
276 * We try to allocate bootmem pages above 'goal'
277 * first, then we try to allocate lower pages.
280 if (goal
&& PFN_DOWN(goal
) < end_pfn
) {
281 if (goal
> node_boot_start
)
282 preferred
= goal
- node_boot_start
;
284 if (bdata
->last_success
> node_boot_start
&&
285 bdata
->last_success
- node_boot_start
>= preferred
)
286 if (!limit
|| (limit
&& limit
> bdata
->last_success
))
287 preferred
= bdata
->last_success
- node_boot_start
;
290 preferred
= PFN_DOWN(ALIGN(preferred
, align
));
291 areasize
= (size
+ PAGE_SIZE
-1) / PAGE_SIZE
;
292 incr
= align
>> PAGE_SHIFT
? : 1;
295 for (i
= preferred
; i
< eidx
;) {
298 i
= find_next_zero_bit(node_bootmem_map
, eidx
, i
);
302 if (test_bit(i
, node_bootmem_map
)) {
306 for (j
= i
+ 1; j
< i
+ areasize
; ++j
) {
309 if (test_bit(j
, node_bootmem_map
))
327 bdata
->last_success
= PFN_PHYS(start
) + node_boot_start
;
328 BUG_ON(start
>= eidx
);
331 * Is the next page of the previous allocation-end the start
332 * of this allocation's buffer? If yes then we can 'merge'
333 * the previous partial page with this allocation.
335 if (align
< PAGE_SIZE
&&
336 bdata
->last_offset
&& bdata
->last_pos
+1 == start
) {
337 unsigned long offset
, remaining_size
;
338 offset
= ALIGN(bdata
->last_offset
, align
);
339 BUG_ON(offset
> PAGE_SIZE
);
340 remaining_size
= PAGE_SIZE
- offset
;
341 if (size
< remaining_size
) {
343 /* last_pos unchanged */
344 bdata
->last_offset
= offset
+ size
;
345 ret
= phys_to_virt(bdata
->last_pos
* PAGE_SIZE
+
346 offset
+ node_boot_start
);
348 remaining_size
= size
- remaining_size
;
349 areasize
= (remaining_size
+ PAGE_SIZE
-1) / PAGE_SIZE
;
350 ret
= phys_to_virt(bdata
->last_pos
* PAGE_SIZE
+
351 offset
+ node_boot_start
);
352 bdata
->last_pos
= start
+ areasize
- 1;
353 bdata
->last_offset
= remaining_size
;
355 bdata
->last_offset
&= ~PAGE_MASK
;
357 bdata
->last_pos
= start
+ areasize
- 1;
358 bdata
->last_offset
= size
& ~PAGE_MASK
;
359 ret
= phys_to_virt(start
* PAGE_SIZE
+ node_boot_start
);
363 * Reserve the area now:
365 for (i
= start
; i
< start
+ areasize
; i
++)
366 if (unlikely(test_and_set_bit(i
, node_bootmem_map
)))
368 memset(ret
, 0, size
);
372 static unsigned long __init
free_all_bootmem_core(pg_data_t
*pgdat
)
376 bootmem_data_t
*bdata
= pgdat
->bdata
;
377 unsigned long i
, count
, total
= 0;
382 BUG_ON(!bdata
->node_bootmem_map
);
385 /* first extant page of the node */
386 pfn
= PFN_DOWN(bdata
->node_boot_start
);
387 idx
= bdata
->node_low_pfn
- pfn
;
388 map
= bdata
->node_bootmem_map
;
389 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
390 if (bdata
->node_boot_start
== 0 ||
391 ffs(bdata
->node_boot_start
) - PAGE_SHIFT
> ffs(BITS_PER_LONG
))
393 for (i
= 0; i
< idx
; ) {
394 unsigned long v
= ~map
[i
/ BITS_PER_LONG
];
396 if (gofast
&& v
== ~0UL) {
399 page
= pfn_to_page(pfn
);
400 count
+= BITS_PER_LONG
;
401 order
= ffs(BITS_PER_LONG
) - 1;
402 __free_pages_bootmem(page
, order
);
404 page
+= BITS_PER_LONG
;
408 page
= pfn_to_page(pfn
);
409 for (m
= 1; m
&& i
< idx
; m
<<=1, page
++, i
++) {
412 __free_pages_bootmem(page
, 0);
418 pfn
+= BITS_PER_LONG
;
423 * Now free the allocator bitmap itself, it's not
426 page
= virt_to_page(bdata
->node_bootmem_map
);
428 idx
= (get_mapsize(bdata
) + PAGE_SIZE
-1) >> PAGE_SHIFT
;
429 for (i
= 0; i
< idx
; i
++, page
++) {
430 __free_pages_bootmem(page
, 0);
434 bdata
->node_bootmem_map
= NULL
;
439 unsigned long __init
init_bootmem_node(pg_data_t
*pgdat
, unsigned long freepfn
,
440 unsigned long startpfn
, unsigned long endpfn
)
442 return init_bootmem_core(pgdat
, freepfn
, startpfn
, endpfn
);
445 int __init
reserve_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
446 unsigned long size
, int flags
)
450 ret
= can_reserve_bootmem_core(pgdat
->bdata
, physaddr
, size
, flags
);
453 reserve_bootmem_core(pgdat
->bdata
, physaddr
, size
, flags
);
458 void __init
free_bootmem_node(pg_data_t
*pgdat
, unsigned long physaddr
,
461 free_bootmem_core(pgdat
->bdata
, physaddr
, size
);
464 unsigned long __init
free_all_bootmem_node(pg_data_t
*pgdat
)
466 register_page_bootmem_info_node(pgdat
);
467 return free_all_bootmem_core(pgdat
);
470 unsigned long __init
init_bootmem(unsigned long start
, unsigned long pages
)
474 return init_bootmem_core(NODE_DATA(0), start
, 0, pages
);
477 #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
478 int __init
reserve_bootmem(unsigned long addr
, unsigned long size
,
481 bootmem_data_t
*bdata
;
484 list_for_each_entry(bdata
, &bdata_list
, list
) {
485 ret
= can_reserve_bootmem_core(bdata
, addr
, size
, flags
);
489 list_for_each_entry(bdata
, &bdata_list
, list
)
490 reserve_bootmem_core(bdata
, addr
, size
, flags
);
494 #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
496 void __init
free_bootmem(unsigned long addr
, unsigned long size
)
498 bootmem_data_t
*bdata
;
499 list_for_each_entry(bdata
, &bdata_list
, list
)
500 free_bootmem_core(bdata
, addr
, size
);
503 unsigned long __init
free_all_bootmem(void)
505 return free_all_bootmem_core(NODE_DATA(0));
508 void * __init
__alloc_bootmem_nopanic(unsigned long size
, unsigned long align
,
511 bootmem_data_t
*bdata
;
514 list_for_each_entry(bdata
, &bdata_list
, list
) {
515 ptr
= __alloc_bootmem_core(bdata
, size
, align
, goal
, 0);
522 void * __init
__alloc_bootmem(unsigned long size
, unsigned long align
,
525 void *mem
= __alloc_bootmem_nopanic(size
,align
,goal
);
530 * Whoops, we cannot satisfy the allocation request.
532 printk(KERN_ALERT
"bootmem alloc of %lu bytes failed!\n", size
);
533 panic("Out of memory");
538 void * __init
__alloc_bootmem_node(pg_data_t
*pgdat
, unsigned long size
,
539 unsigned long align
, unsigned long goal
)
543 ptr
= __alloc_bootmem_core(pgdat
->bdata
, size
, align
, goal
, 0);
547 return __alloc_bootmem(size
, align
, goal
);
550 #ifdef CONFIG_SPARSEMEM
551 void * __init
alloc_bootmem_section(unsigned long size
,
552 unsigned long section_nr
)
555 unsigned long limit
, goal
, start_nr
, end_nr
, pfn
;
556 struct pglist_data
*pgdat
;
558 pfn
= section_nr_to_pfn(section_nr
);
559 goal
= PFN_PHYS(pfn
);
560 limit
= PFN_PHYS(section_nr_to_pfn(section_nr
+ 1)) - 1;
561 pgdat
= NODE_DATA(early_pfn_to_nid(pfn
));
562 ptr
= __alloc_bootmem_core(pgdat
->bdata
, size
, SMP_CACHE_BYTES
, goal
,
568 start_nr
= pfn_to_section_nr(PFN_DOWN(__pa(ptr
)));
569 end_nr
= pfn_to_section_nr(PFN_DOWN(__pa(ptr
) + size
));
570 if (start_nr
!= section_nr
|| end_nr
!= section_nr
) {
571 printk(KERN_WARNING
"alloc_bootmem failed on section %ld.\n",
573 free_bootmem_core(pgdat
->bdata
, __pa(ptr
), size
);
581 #ifndef ARCH_LOW_ADDRESS_LIMIT
582 #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
585 void * __init
__alloc_bootmem_low(unsigned long size
, unsigned long align
,
588 bootmem_data_t
*bdata
;
591 list_for_each_entry(bdata
, &bdata_list
, list
) {
592 ptr
= __alloc_bootmem_core(bdata
, size
, align
, goal
,
593 ARCH_LOW_ADDRESS_LIMIT
);
599 * Whoops, we cannot satisfy the allocation request.
601 printk(KERN_ALERT
"low bootmem alloc of %lu bytes failed!\n", size
);
602 panic("Out of low memory");
606 void * __init
__alloc_bootmem_low_node(pg_data_t
*pgdat
, unsigned long size
,
607 unsigned long align
, unsigned long goal
)
609 return __alloc_bootmem_core(pgdat
->bdata
, size
, align
, goal
,
610 ARCH_LOW_ADDRESS_LIMIT
);