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.
13 #include <linux/kernel_stat.h>
14 #include <linux/swap.h>
15 #include <linux/interrupt.h>
16 #include <linux/init.h>
17 #include <linux/bootmem.h>
18 #include <linux/mmzone.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 /* return the number of _pages_ that will be allocated for the boot bitmap */
31 unsigned long __init
bootmem_bootmap_pages (unsigned long pages
)
33 unsigned long mapsize
;
35 mapsize
= (pages
+7)/8;
36 mapsize
= (mapsize
+ ~PAGE_MASK
) & PAGE_MASK
;
37 mapsize
>>= PAGE_SHIFT
;
43 * Called once to set up the allocator itself.
45 static unsigned long __init
init_bootmem_core (pg_data_t
*pgdat
,
46 unsigned long mapstart
, unsigned long start
, unsigned long end
)
48 bootmem_data_t
*bdata
= pgdat
->bdata
;
49 unsigned long mapsize
= ((end
- start
)+7)/8;
51 pgdat
->pgdat_next
= pgdat_list
;
54 mapsize
= (mapsize
+ (sizeof(long) - 1UL)) & ~(sizeof(long) - 1UL);
55 bdata
->node_bootmem_map
= phys_to_virt(mapstart
<< PAGE_SHIFT
);
56 bdata
->node_boot_start
= (start
<< PAGE_SHIFT
);
57 bdata
->node_low_pfn
= end
;
60 * Initially all pages are reserved - setup_arch() has to
61 * register free RAM areas explicitly.
63 memset(bdata
->node_bootmem_map
, 0xff, mapsize
);
69 * Marks a particular physical memory range as unallocatable. Usable RAM
70 * might be used for boot-time allocations - or it might get added
71 * to the free page pool later on.
73 static void __init
reserve_bootmem_core(bootmem_data_t
*bdata
, unsigned long addr
, unsigned long size
)
77 * round up, partially reserved pages are considered
80 unsigned long sidx
= (addr
- bdata
->node_boot_start
)/PAGE_SIZE
;
81 unsigned long eidx
= (addr
+ size
- bdata
->node_boot_start
+
82 PAGE_SIZE
-1)/PAGE_SIZE
;
83 unsigned long end
= (addr
+ size
+ PAGE_SIZE
-1)/PAGE_SIZE
;
93 if ((addr
>> PAGE_SHIFT
) >= bdata
->node_low_pfn
)
95 if (end
> bdata
->node_low_pfn
)
97 for (i
= sidx
; i
< eidx
; i
++)
98 if (test_and_set_bit(i
, bdata
->node_bootmem_map
))
99 printk("hm, page %08lx reserved twice.\n", i
*PAGE_SIZE
);
102 static void __init
free_bootmem_core(bootmem_data_t
*bdata
, unsigned long addr
, unsigned long size
)
107 * round down end of usable mem, partially free pages are
108 * considered reserved.
111 unsigned long eidx
= (addr
+ size
- bdata
->node_boot_start
)/PAGE_SIZE
;
112 unsigned long end
= (addr
+ size
)/PAGE_SIZE
;
115 if (end
> bdata
->node_low_pfn
)
119 * Round up the beginning of the address.
121 start
= (addr
+ PAGE_SIZE
-1) / PAGE_SIZE
;
122 sidx
= start
- (bdata
->node_boot_start
/PAGE_SIZE
);
124 for (i
= sidx
; i
< eidx
; i
++) {
125 if (!test_and_clear_bit(i
, bdata
->node_bootmem_map
))
131 * We 'merge' subsequent allocations to save space. We might 'lose'
132 * some fraction of a page if allocations cannot be satisfied due to
133 * size constraints on boxes where there is physical RAM space
134 * fragmentation - in these cases * (mostly large memory boxes) this
137 * On low memory boxes we get it right in 100% of the cases.
141 * alignment has to be a power of 2 value.
143 static void * __init
__alloc_bootmem_core (bootmem_data_t
*bdata
,
144 unsigned long size
, unsigned long align
, unsigned long goal
)
146 unsigned long i
, start
= 0;
148 unsigned long offset
, remaining_size
;
149 unsigned long areasize
, preferred
, incr
;
150 unsigned long eidx
= bdata
->node_low_pfn
- (bdata
->node_boot_start
>>
155 if (align
& (align
-1))
160 (bdata
->node_boot_start
& (align
- 1UL)) != 0)
161 offset
= (align
- (bdata
->node_boot_start
& (align
- 1UL)));
162 offset
>>= PAGE_SHIFT
;
165 * We try to allocate bootmem pages above 'goal'
166 * first, then we try to allocate lower pages.
168 if (goal
&& (goal
>= bdata
->node_boot_start
) &&
169 ((goal
>> PAGE_SHIFT
) < bdata
->node_low_pfn
)) {
170 preferred
= goal
- bdata
->node_boot_start
;
174 preferred
= ((preferred
+ align
- 1) & ~(align
- 1)) >> PAGE_SHIFT
;
176 areasize
= (size
+PAGE_SIZE
-1)/PAGE_SIZE
;
177 incr
= align
>> PAGE_SHIFT
? : 1;
180 for (i
= preferred
; i
< eidx
; i
+= incr
) {
182 if (test_bit(i
, bdata
->node_bootmem_map
))
184 for (j
= i
+ 1; j
< i
+ areasize
; ++j
) {
187 if (test_bit (j
, bdata
->node_bootmem_map
))
204 * Is the next page of the previous allocation-end the start
205 * of this allocation's buffer? If yes then we can 'merge'
206 * the previous partial page with this allocation.
208 if (align
< PAGE_SIZE
209 && bdata
->last_offset
&& bdata
->last_pos
+1 == start
) {
210 offset
= (bdata
->last_offset
+align
-1) & ~(align
-1);
211 if (offset
> PAGE_SIZE
)
213 remaining_size
= PAGE_SIZE
-offset
;
214 if (size
< remaining_size
) {
216 // last_pos unchanged
217 bdata
->last_offset
= offset
+size
;
218 ret
= phys_to_virt(bdata
->last_pos
*PAGE_SIZE
+ offset
+
219 bdata
->node_boot_start
);
221 remaining_size
= size
- remaining_size
;
222 areasize
= (remaining_size
+PAGE_SIZE
-1)/PAGE_SIZE
;
223 ret
= phys_to_virt(bdata
->last_pos
*PAGE_SIZE
+ offset
+
224 bdata
->node_boot_start
);
225 bdata
->last_pos
= start
+areasize
-1;
226 bdata
->last_offset
= remaining_size
;
228 bdata
->last_offset
&= ~PAGE_MASK
;
230 bdata
->last_pos
= start
+ areasize
- 1;
231 bdata
->last_offset
= size
& ~PAGE_MASK
;
232 ret
= phys_to_virt(start
* PAGE_SIZE
+ bdata
->node_boot_start
);
235 * Reserve the area now:
237 for (i
= start
; i
< start
+areasize
; i
++)
238 if (test_and_set_bit(i
, bdata
->node_bootmem_map
))
240 memset(ret
, 0, size
);
244 static unsigned long __init
free_all_bootmem_core(pg_data_t
*pgdat
)
246 struct page
*page
= pgdat
->node_mem_map
;
247 bootmem_data_t
*bdata
= pgdat
->bdata
;
248 unsigned long i
, count
, total
= 0;
252 if (!bdata
->node_bootmem_map
) BUG();
255 idx
= bdata
->node_low_pfn
- (bdata
->node_boot_start
>> PAGE_SHIFT
);
256 map
= bdata
->node_bootmem_map
;
257 for (i
= 0; i
< idx
; ) {
258 unsigned long v
= ~map
[i
/ BITS_PER_LONG
];
261 for (m
= 1; m
&& i
< idx
; m
<<=1, page
++, i
++) {
264 ClearPageReserved(page
);
265 set_page_count(page
, 1);
277 * Now free the allocator bitmap itself, it's not
280 page
= virt_to_page(bdata
->node_bootmem_map
);
282 for (i
= 0; i
< ((bdata
->node_low_pfn
-(bdata
->node_boot_start
>> PAGE_SHIFT
))/8 + PAGE_SIZE
-1)/PAGE_SIZE
; i
++,page
++) {
284 ClearPageReserved(page
);
285 set_page_count(page
, 1);
289 bdata
->node_bootmem_map
= NULL
;
294 unsigned long __init
init_bootmem_node (pg_data_t
*pgdat
, unsigned long freepfn
, unsigned long startpfn
, unsigned long endpfn
)
296 return(init_bootmem_core(pgdat
, freepfn
, startpfn
, endpfn
));
299 void __init
reserve_bootmem_node (pg_data_t
*pgdat
, unsigned long physaddr
, unsigned long size
)
301 reserve_bootmem_core(pgdat
->bdata
, physaddr
, size
);
304 void __init
free_bootmem_node (pg_data_t
*pgdat
, unsigned long physaddr
, unsigned long size
)
306 return(free_bootmem_core(pgdat
->bdata
, physaddr
, size
));
309 unsigned long __init
free_all_bootmem_node (pg_data_t
*pgdat
)
311 return(free_all_bootmem_core(pgdat
));
314 #ifndef CONFIG_DISCONTIGMEM
315 unsigned long __init
init_bootmem (unsigned long start
, unsigned long pages
)
319 return(init_bootmem_core(&contig_page_data
, start
, 0, pages
));
322 #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
323 void __init
reserve_bootmem (unsigned long addr
, unsigned long size
)
325 reserve_bootmem_core(contig_page_data
.bdata
, addr
, size
);
327 #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
329 void __init
free_bootmem (unsigned long addr
, unsigned long size
)
331 return(free_bootmem_core(contig_page_data
.bdata
, addr
, size
));
334 unsigned long __init
free_all_bootmem (void)
336 return(free_all_bootmem_core(&contig_page_data
));
338 #endif /* !CONFIG_DISCONTIGMEM */
340 void * __init
__alloc_bootmem (unsigned long size
, unsigned long align
, unsigned long goal
)
342 pg_data_t
*pgdat
= pgdat_list
;
345 for_each_pgdat(pgdat
)
346 if ((ptr
= __alloc_bootmem_core(pgdat
->bdata
, size
,
351 * Whoops, we cannot satisfy the allocation request.
353 printk(KERN_ALERT
"bootmem alloc of %lu bytes failed!\n", size
);
354 panic("Out of memory");
358 void * __init
__alloc_bootmem_node (pg_data_t
*pgdat
, unsigned long size
, unsigned long align
, unsigned long goal
)
362 ptr
= __alloc_bootmem_core(pgdat
->bdata
, size
, align
, goal
);
367 * Whoops, we cannot satisfy the allocation request.
369 printk(KERN_ALERT
"bootmem alloc of %lu bytes failed!\n", size
);
370 panic("Out of memory");