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/swapctl.h>
16 #include <linux/interrupt.h>
17 #include <linux/init.h>
18 #include <linux/bootmem.h>
19 #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
;
29 /* return the number of _pages_ that will be allocated for the boot bitmap */
30 unsigned long __init
bootmem_bootmap_pages (unsigned long pages
)
32 unsigned long mapsize
;
34 mapsize
= (pages
+7)/8;
35 mapsize
= (mapsize
+ ~PAGE_MASK
) & PAGE_MASK
;
36 mapsize
>>= PAGE_SHIFT
;
42 * Called once to set up the allocator itself.
44 static unsigned long __init
init_bootmem_core (pg_data_t
*pgdat
,
45 unsigned long mapstart
, unsigned long start
, unsigned long end
)
47 bootmem_data_t
*bdata
= pgdat
->bdata
;
48 unsigned long mapsize
= ((end
- start
)+7)/8;
50 pgdat
->node_next
= pgdat_list
;
53 mapsize
= (mapsize
+ (sizeof(long) - 1UL)) & ~(sizeof(long) - 1UL);
54 bdata
->node_bootmem_map
= phys_to_virt(mapstart
<< PAGE_SHIFT
);
55 bdata
->node_boot_start
= (start
<< PAGE_SHIFT
);
56 bdata
->node_low_pfn
= end
;
59 * Initially all pages are reserved - setup_arch() has to
60 * register free RAM areas explicitly.
62 memset(bdata
->node_bootmem_map
, 0xff, mapsize
);
68 * Marks a particular physical memory range as unallocatable. Usable RAM
69 * might be used for boot-time allocations - or it might get added
70 * to the free page pool later on.
72 static void __init
reserve_bootmem_core(bootmem_data_t
*bdata
, unsigned long addr
, unsigned long size
)
76 * round up, partially reserved pages are considered
79 unsigned long sidx
= (addr
- bdata
->node_boot_start
)/PAGE_SIZE
;
80 unsigned long eidx
= (addr
+ size
- bdata
->node_boot_start
+
81 PAGE_SIZE
-1)/PAGE_SIZE
;
82 unsigned long end
= (addr
+ size
+ PAGE_SIZE
-1)/PAGE_SIZE
;
86 if (end
> bdata
->node_low_pfn
)
88 for (i
= sidx
; i
< eidx
; i
++)
89 if (test_and_set_bit(i
, bdata
->node_bootmem_map
))
90 printk("hm, page %08lx reserved twice.\n", i
*PAGE_SIZE
);
93 static void __init
free_bootmem_core(bootmem_data_t
*bdata
, unsigned long addr
, unsigned long size
)
98 * round down end of usable mem, partially free pages are
99 * considered reserved.
102 unsigned long eidx
= (addr
+ size
- bdata
->node_boot_start
)/PAGE_SIZE
;
103 unsigned long end
= (addr
+ size
)/PAGE_SIZE
;
106 if (end
> bdata
->node_low_pfn
)
110 * Round up the beginning of the address.
112 start
= (addr
+ PAGE_SIZE
-1) / PAGE_SIZE
;
113 sidx
= start
- (bdata
->node_boot_start
/PAGE_SIZE
);
115 for (i
= sidx
; i
< eidx
; i
++) {
116 if (!test_and_clear_bit(i
, bdata
->node_bootmem_map
))
122 * We 'merge' subsequent allocations to save space. We might 'lose'
123 * some fraction of a page if allocations cannot be satisfied due to
124 * size constraints on boxes where there is physical RAM space
125 * fragmentation - in these cases * (mostly large memory boxes) this
128 * On low memory boxes we get it right in 100% of the cases.
132 * alignment has to be a power of 2 value.
134 static void * __init
__alloc_bootmem_core (bootmem_data_t
*bdata
,
135 unsigned long size
, unsigned long align
, unsigned long goal
)
137 unsigned long i
, start
= 0;
139 unsigned long offset
, remaining_size
;
140 unsigned long areasize
, preferred
, incr
;
141 unsigned long eidx
= bdata
->node_low_pfn
- (bdata
->node_boot_start
>>
147 * We try to allocate bootmem pages above 'goal'
148 * first, then we try to allocate lower pages.
150 if (goal
&& (goal
>= bdata
->node_boot_start
) &&
151 ((goal
>> PAGE_SHIFT
) < bdata
->node_low_pfn
)) {
152 preferred
= goal
- bdata
->node_boot_start
;
156 preferred
= ((preferred
+ align
- 1) & ~(align
- 1)) >> PAGE_SHIFT
;
157 areasize
= (size
+PAGE_SIZE
-1)/PAGE_SIZE
;
158 incr
= align
>> PAGE_SHIFT
? : 1;
161 for (i
= preferred
; i
< eidx
; i
+= incr
) {
163 if (test_bit(i
, bdata
->node_bootmem_map
))
165 for (j
= i
+ 1; j
< i
+ areasize
; ++j
) {
168 if (test_bit (j
, bdata
->node_bootmem_map
))
184 * Is the next page of the previous allocation-end the start
185 * of this allocation's buffer? If yes then we can 'merge'
186 * the previous partial page with this allocation.
188 if (align
<= PAGE_SIZE
189 && bdata
->last_offset
&& bdata
->last_pos
+1 == start
) {
190 offset
= (bdata
->last_offset
+align
-1) & ~(align
-1);
191 if (offset
> PAGE_SIZE
)
193 remaining_size
= PAGE_SIZE
-offset
;
194 if (size
< remaining_size
) {
196 // last_pos unchanged
197 bdata
->last_offset
= offset
+size
;
198 ret
= phys_to_virt(bdata
->last_pos
*PAGE_SIZE
+ offset
+
199 bdata
->node_boot_start
);
201 remaining_size
= size
- remaining_size
;
202 areasize
= (remaining_size
+PAGE_SIZE
-1)/PAGE_SIZE
;
203 ret
= phys_to_virt(bdata
->last_pos
*PAGE_SIZE
+ offset
+
204 bdata
->node_boot_start
);
205 bdata
->last_pos
= start
+areasize
-1;
206 bdata
->last_offset
= remaining_size
;
208 bdata
->last_offset
&= ~PAGE_MASK
;
210 bdata
->last_pos
= start
+ areasize
- 1;
211 bdata
->last_offset
= size
& ~PAGE_MASK
;
212 ret
= phys_to_virt(start
* PAGE_SIZE
+ bdata
->node_boot_start
);
215 * Reserve the area now:
217 for (i
= start
; i
< start
+areasize
; i
++)
218 if (test_and_set_bit(i
, bdata
->node_bootmem_map
))
220 memset(ret
, 0, size
);
224 static unsigned long __init
free_all_bootmem_core(pg_data_t
*pgdat
)
226 struct page
*page
= pgdat
->node_mem_map
;
227 bootmem_data_t
*bdata
= pgdat
->bdata
;
228 unsigned long i
, count
, total
= 0;
231 if (!bdata
->node_bootmem_map
) BUG();
234 idx
= bdata
->node_low_pfn
- (bdata
->node_boot_start
>> PAGE_SHIFT
);
235 for (i
= 0; i
< idx
; i
++, page
++) {
236 if (!test_bit(i
, bdata
->node_bootmem_map
)) {
238 ClearPageReserved(page
);
239 set_page_count(page
, 1);
246 * Now free the allocator bitmap itself, it's not
249 page
= virt_to_page(bdata
->node_bootmem_map
);
251 for (i
= 0; i
< ((bdata
->node_low_pfn
-(bdata
->node_boot_start
>> PAGE_SHIFT
))/8 + PAGE_SIZE
-1)/PAGE_SIZE
; i
++,page
++) {
253 ClearPageReserved(page
);
254 set_page_count(page
, 1);
258 bdata
->node_bootmem_map
= NULL
;
263 unsigned long __init
init_bootmem_node (pg_data_t
*pgdat
, unsigned long freepfn
, unsigned long startpfn
, unsigned long endpfn
)
265 return(init_bootmem_core(pgdat
, freepfn
, startpfn
, endpfn
));
268 void __init
reserve_bootmem_node (pg_data_t
*pgdat
, unsigned long physaddr
, unsigned long size
)
270 reserve_bootmem_core(pgdat
->bdata
, physaddr
, size
);
273 void __init
free_bootmem_node (pg_data_t
*pgdat
, unsigned long physaddr
, unsigned long size
)
275 return(free_bootmem_core(pgdat
->bdata
, physaddr
, size
));
278 unsigned long __init
free_all_bootmem_node (pg_data_t
*pgdat
)
280 return(free_all_bootmem_core(pgdat
));
283 unsigned long __init
init_bootmem (unsigned long start
, unsigned long pages
)
287 return(init_bootmem_core(&contig_page_data
, start
, 0, pages
));
290 void __init
reserve_bootmem (unsigned long addr
, unsigned long size
)
292 reserve_bootmem_core(contig_page_data
.bdata
, addr
, size
);
295 void __init
free_bootmem (unsigned long addr
, unsigned long size
)
297 return(free_bootmem_core(contig_page_data
.bdata
, addr
, size
));
300 unsigned long __init
free_all_bootmem (void)
302 return(free_all_bootmem_core(&contig_page_data
));
305 void * __init
__alloc_bootmem (unsigned long size
, unsigned long align
, unsigned long goal
)
307 pg_data_t
*pgdat
= pgdat_list
;
311 if ((ptr
= __alloc_bootmem_core(pgdat
->bdata
, size
,
314 pgdat
= pgdat
->node_next
;
317 * Whoops, we cannot satisfy the allocation request.
323 void * __init
__alloc_bootmem_node (pg_data_t
*pgdat
, unsigned long size
, unsigned long align
, unsigned long goal
)
327 ptr
= __alloc_bootmem_core(pgdat
->bdata
, size
, align
, goal
);
332 * Whoops, we cannot satisfy the allocation request.