[PATCH] bootmem: remove useless headers inclusions
[linux-2.6-block.git] / mm / bootmem.c
CommitLineData
1da177e4
LT
1/*
2 * linux/mm/bootmem.c
3 *
4 * Copyright (C) 1999 Ingo Molnar
5 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
6 *
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.
10 */
1da177e4
LT
11#include <linux/init.h>
12#include <linux/bootmem.h>
1da177e4 13#include <linux/module.h>
e786e86a
FBH
14
15#include <asm/bug.h>
1da177e4 16#include <asm/io.h>
e786e86a 17
1da177e4
LT
18#include "internal.h"
19
20/*
21 * Access to this subsystem has to be serialized externally. (this is
22 * true for the boot process anyway)
23 */
24unsigned long max_low_pfn;
25unsigned long min_low_pfn;
26unsigned long max_pfn;
27
6d46cc6b 28EXPORT_UNUSED_SYMBOL(max_pfn); /* June 2006 */
1da177e4 29
679bc9fb 30static LIST_HEAD(bdata_list);
92aa63a5
VG
31#ifdef CONFIG_CRASH_DUMP
32/*
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.
35 */
36unsigned long saved_max_pfn;
37#endif
38
1da177e4
LT
39/* return the number of _pages_ that will be allocated for the boot bitmap */
40unsigned long __init bootmem_bootmap_pages (unsigned long pages)
41{
42 unsigned long mapsize;
43
44 mapsize = (pages+7)/8;
45 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
46 mapsize >>= PAGE_SHIFT;
47
48 return mapsize;
49}
679bc9fb
KH
50/*
51 * link bdata in order
52 */
69d49e68 53static void __init link_bootmem(bootmem_data_t *bdata)
679bc9fb
KH
54{
55 bootmem_data_t *ent;
56 if (list_empty(&bdata_list)) {
57 list_add(&bdata->list, &bdata_list);
58 return;
59 }
60 /* insert in order */
61 list_for_each_entry(ent, &bdata_list, list) {
62 if (bdata->node_boot_start < ent->node_boot_start) {
63 list_add_tail(&bdata->list, &ent->list);
64 return;
65 }
66 }
67 list_add_tail(&bdata->list, &bdata_list);
68 return;
69}
70
1da177e4
LT
71
72/*
73 * Called once to set up the allocator itself.
74 */
75static unsigned long __init init_bootmem_core (pg_data_t *pgdat,
76 unsigned long mapstart, unsigned long start, unsigned long end)
77{
78 bootmem_data_t *bdata = pgdat->bdata;
79 unsigned long mapsize = ((end - start)+7)/8;
80
8c0e33c1 81 mapsize = ALIGN(mapsize, sizeof(long));
1da177e4
LT
82 bdata->node_bootmem_map = phys_to_virt(mapstart << PAGE_SHIFT);
83 bdata->node_boot_start = (start << PAGE_SHIFT);
84 bdata->node_low_pfn = end;
679bc9fb 85 link_bootmem(bdata);
1da177e4
LT
86
87 /*
88 * Initially all pages are reserved - setup_arch() has to
89 * register free RAM areas explicitly.
90 */
91 memset(bdata->node_bootmem_map, 0xff, mapsize);
92
93 return mapsize;
94}
95
96/*
97 * Marks a particular physical memory range as unallocatable. Usable RAM
98 * might be used for boot-time allocations - or it might get added
99 * to the free page pool later on.
100 */
bb0923a6
FBH
101static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
102 unsigned long size)
1da177e4
LT
103{
104 unsigned long i;
105 /*
106 * round up, partially reserved pages are considered
107 * fully reserved.
108 */
109 unsigned long sidx = (addr - bdata->node_boot_start)/PAGE_SIZE;
110 unsigned long eidx = (addr + size - bdata->node_boot_start +
111 PAGE_SIZE-1)/PAGE_SIZE;
112 unsigned long end = (addr + size + PAGE_SIZE-1)/PAGE_SIZE;
113
114 BUG_ON(!size);
115 BUG_ON(sidx >= eidx);
116 BUG_ON((addr >> PAGE_SHIFT) >= bdata->node_low_pfn);
117 BUG_ON(end > bdata->node_low_pfn);
118
119 for (i = sidx; i < eidx; i++)
120 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
121#ifdef CONFIG_DEBUG_BOOTMEM
122 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
123#endif
124 }
125}
126
bb0923a6
FBH
127static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
128 unsigned long size)
1da177e4
LT
129{
130 unsigned long i;
131 unsigned long start;
132 /*
133 * round down end of usable mem, partially free pages are
134 * considered reserved.
135 */
136 unsigned long sidx;
137 unsigned long eidx = (addr + size - bdata->node_boot_start)/PAGE_SIZE;
138 unsigned long end = (addr + size)/PAGE_SIZE;
139
140 BUG_ON(!size);
141 BUG_ON(end > bdata->node_low_pfn);
142
143 if (addr < bdata->last_success)
144 bdata->last_success = addr;
145
146 /*
147 * Round up the beginning of the address.
148 */
149 start = (addr + PAGE_SIZE-1) / PAGE_SIZE;
150 sidx = start - (bdata->node_boot_start/PAGE_SIZE);
151
152 for (i = sidx; i < eidx; i++) {
153 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
154 BUG();
155 }
156}
157
158/*
159 * We 'merge' subsequent allocations to save space. We might 'lose'
160 * some fraction of a page if allocations cannot be satisfied due to
161 * size constraints on boxes where there is physical RAM space
162 * fragmentation - in these cases (mostly large memory boxes) this
163 * is not a problem.
164 *
165 * On low memory boxes we get it right in 100% of the cases.
166 *
167 * alignment has to be a power of 2 value.
168 *
169 * NOTE: This function is _not_ reentrant.
170 */
267b4801 171void * __init
1da177e4 172__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
281dd25c 173 unsigned long align, unsigned long goal, unsigned long limit)
1da177e4
LT
174{
175 unsigned long offset, remaining_size, areasize, preferred;
281dd25c 176 unsigned long i, start = 0, incr, eidx, end_pfn = bdata->node_low_pfn;
1da177e4
LT
177 void *ret;
178
179 if(!size) {
180 printk("__alloc_bootmem_core(): zero-sized request\n");
181 BUG();
182 }
183 BUG_ON(align & (align-1));
184
281dd25c
YG
185 if (limit && bdata->node_boot_start >= limit)
186 return NULL;
187
188 limit >>=PAGE_SHIFT;
189 if (limit && end_pfn > limit)
190 end_pfn = limit;
191
192 eidx = end_pfn - (bdata->node_boot_start >> PAGE_SHIFT);
1da177e4
LT
193 offset = 0;
194 if (align &&
195 (bdata->node_boot_start & (align - 1UL)) != 0)
196 offset = (align - (bdata->node_boot_start & (align - 1UL)));
197 offset >>= PAGE_SHIFT;
198
199 /*
200 * We try to allocate bootmem pages above 'goal'
201 * first, then we try to allocate lower pages.
202 */
203 if (goal && (goal >= bdata->node_boot_start) &&
281dd25c 204 ((goal >> PAGE_SHIFT) < end_pfn)) {
1da177e4
LT
205 preferred = goal - bdata->node_boot_start;
206
207 if (bdata->last_success >= preferred)
281dd25c
YG
208 if (!limit || (limit && limit > bdata->last_success))
209 preferred = bdata->last_success;
1da177e4
LT
210 } else
211 preferred = 0;
212
8c0e33c1 213 preferred = ALIGN(preferred, align) >> PAGE_SHIFT;
1da177e4
LT
214 preferred += offset;
215 areasize = (size+PAGE_SIZE-1)/PAGE_SIZE;
216 incr = align >> PAGE_SHIFT ? : 1;
217
218restart_scan:
219 for (i = preferred; i < eidx; i += incr) {
220 unsigned long j;
221 i = find_next_zero_bit(bdata->node_bootmem_map, eidx, i);
222 i = ALIGN(i, incr);
66d43e98
HM
223 if (i >= eidx)
224 break;
1da177e4
LT
225 if (test_bit(i, bdata->node_bootmem_map))
226 continue;
227 for (j = i + 1; j < i + areasize; ++j) {
228 if (j >= eidx)
229 goto fail_block;
230 if (test_bit (j, bdata->node_bootmem_map))
231 goto fail_block;
232 }
233 start = i;
234 goto found;
235 fail_block:
236 i = ALIGN(j, incr);
237 }
238
239 if (preferred > offset) {
240 preferred = offset;
241 goto restart_scan;
242 }
243 return NULL;
244
245found:
246 bdata->last_success = start << PAGE_SHIFT;
247 BUG_ON(start >= eidx);
248
249 /*
250 * Is the next page of the previous allocation-end the start
251 * of this allocation's buffer? If yes then we can 'merge'
252 * the previous partial page with this allocation.
253 */
254 if (align < PAGE_SIZE &&
255 bdata->last_offset && bdata->last_pos+1 == start) {
8c0e33c1 256 offset = ALIGN(bdata->last_offset, align);
1da177e4
LT
257 BUG_ON(offset > PAGE_SIZE);
258 remaining_size = PAGE_SIZE-offset;
259 if (size < remaining_size) {
260 areasize = 0;
261 /* last_pos unchanged */
262 bdata->last_offset = offset+size;
263 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
264 bdata->node_boot_start);
265 } else {
266 remaining_size = size - remaining_size;
267 areasize = (remaining_size+PAGE_SIZE-1)/PAGE_SIZE;
268 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
269 bdata->node_boot_start);
270 bdata->last_pos = start+areasize-1;
271 bdata->last_offset = remaining_size;
272 }
273 bdata->last_offset &= ~PAGE_MASK;
274 } else {
275 bdata->last_pos = start + areasize - 1;
276 bdata->last_offset = size & ~PAGE_MASK;
277 ret = phys_to_virt(start * PAGE_SIZE + bdata->node_boot_start);
278 }
279
280 /*
281 * Reserve the area now:
282 */
283 for (i = start; i < start+areasize; i++)
284 if (unlikely(test_and_set_bit(i, bdata->node_bootmem_map)))
285 BUG();
286 memset(ret, 0, size);
287 return ret;
288}
289
290static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
291{
292 struct page *page;
d41dee36 293 unsigned long pfn;
1da177e4
LT
294 bootmem_data_t *bdata = pgdat->bdata;
295 unsigned long i, count, total = 0;
296 unsigned long idx;
297 unsigned long *map;
298 int gofast = 0;
299
300 BUG_ON(!bdata->node_bootmem_map);
301
302 count = 0;
303 /* first extant page of the node */
d41dee36 304 pfn = bdata->node_boot_start >> PAGE_SHIFT;
1da177e4
LT
305 idx = bdata->node_low_pfn - (bdata->node_boot_start >> PAGE_SHIFT);
306 map = bdata->node_bootmem_map;
307 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
308 if (bdata->node_boot_start == 0 ||
309 ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG))
310 gofast = 1;
311 for (i = 0; i < idx; ) {
312 unsigned long v = ~map[i / BITS_PER_LONG];
d41dee36 313
1da177e4 314 if (gofast && v == ~0UL) {
a226f6c8 315 int order;
1da177e4 316
d41dee36 317 page = pfn_to_page(pfn);
1da177e4 318 count += BITS_PER_LONG;
1da177e4 319 order = ffs(BITS_PER_LONG) - 1;
a226f6c8 320 __free_pages_bootmem(page, order);
1da177e4
LT
321 i += BITS_PER_LONG;
322 page += BITS_PER_LONG;
323 } else if (v) {
324 unsigned long m;
d41dee36
AW
325
326 page = pfn_to_page(pfn);
1da177e4
LT
327 for (m = 1; m && i < idx; m<<=1, page++, i++) {
328 if (v & m) {
329 count++;
a226f6c8 330 __free_pages_bootmem(page, 0);
1da177e4
LT
331 }
332 }
333 } else {
334 i+=BITS_PER_LONG;
1da177e4 335 }
d41dee36 336 pfn += BITS_PER_LONG;
1da177e4
LT
337 }
338 total += count;
339
340 /*
341 * Now free the allocator bitmap itself, it's not
342 * needed anymore:
343 */
344 page = virt_to_page(bdata->node_bootmem_map);
345 count = 0;
346 for (i = 0; i < ((bdata->node_low_pfn-(bdata->node_boot_start >> PAGE_SHIFT))/8 + PAGE_SIZE-1)/PAGE_SIZE; i++,page++) {
347 count++;
a226f6c8 348 __free_pages_bootmem(page, 0);
1da177e4
LT
349 }
350 total += count;
351 bdata->node_bootmem_map = NULL;
352
353 return total;
354}
355
bb0923a6
FBH
356unsigned long __init init_bootmem_node (pg_data_t *pgdat, unsigned long freepfn,
357 unsigned long startpfn, unsigned long endpfn)
1da177e4
LT
358{
359 return(init_bootmem_core(pgdat, freepfn, startpfn, endpfn));
360}
361
bb0923a6
FBH
362void __init reserve_bootmem_node (pg_data_t *pgdat, unsigned long physaddr,
363 unsigned long size)
1da177e4
LT
364{
365 reserve_bootmem_core(pgdat->bdata, physaddr, size);
366}
367
bb0923a6
FBH
368void __init free_bootmem_node (pg_data_t *pgdat, unsigned long physaddr,
369 unsigned long size)
1da177e4
LT
370{
371 free_bootmem_core(pgdat->bdata, physaddr, size);
372}
373
374unsigned long __init free_all_bootmem_node (pg_data_t *pgdat)
375{
376 return(free_all_bootmem_core(pgdat));
377}
378
379unsigned long __init init_bootmem (unsigned long start, unsigned long pages)
380{
381 max_low_pfn = pages;
382 min_low_pfn = start;
383 return(init_bootmem_core(NODE_DATA(0), start, 0, pages));
384}
385
386#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
387void __init reserve_bootmem (unsigned long addr, unsigned long size)
388{
389 reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size);
390}
391#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
392
393void __init free_bootmem (unsigned long addr, unsigned long size)
394{
395 free_bootmem_core(NODE_DATA(0)->bdata, addr, size);
396}
397
398unsigned long __init free_all_bootmem (void)
399{
400 return(free_all_bootmem_core(NODE_DATA(0)));
401}
402
bb0923a6
FBH
403void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
404 unsigned long goal)
1da177e4 405{
679bc9fb 406 bootmem_data_t *bdata;
1da177e4
LT
407 void *ptr;
408
679bc9fb
KH
409 list_for_each_entry(bdata, &bdata_list, list)
410 if ((ptr = __alloc_bootmem_core(bdata, size, align, goal, 0)))
1da177e4 411 return(ptr);
a8062231
AK
412 return NULL;
413}
1da177e4 414
bb0923a6
FBH
415void * __init __alloc_bootmem(unsigned long size, unsigned long align,
416 unsigned long goal)
a8062231
AK
417{
418 void *mem = __alloc_bootmem_nopanic(size,align,goal);
419 if (mem)
420 return mem;
1da177e4
LT
421 /*
422 * Whoops, we cannot satisfy the allocation request.
423 */
424 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
425 panic("Out of memory");
426 return NULL;
427}
428
281dd25c 429
bb0923a6
FBH
430void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
431 unsigned long align, unsigned long goal)
1da177e4
LT
432{
433 void *ptr;
434
008857c1 435 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
1da177e4
LT
436 if (ptr)
437 return (ptr);
438
008857c1 439 return __alloc_bootmem(size, align, goal);
1da177e4
LT
440}
441
008857c1
RT
442#define LOW32LIMIT 0xffffffff
443
bb0923a6
FBH
444void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
445 unsigned long goal)
008857c1 446{
679bc9fb 447 bootmem_data_t *bdata;
008857c1
RT
448 void *ptr;
449
679bc9fb
KH
450 list_for_each_entry(bdata, &bdata_list, list)
451 if ((ptr = __alloc_bootmem_core(bdata, size,
008857c1
RT
452 align, goal, LOW32LIMIT)))
453 return(ptr);
454
455 /*
456 * Whoops, we cannot satisfy the allocation request.
457 */
458 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
459 panic("Out of low memory");
460 return NULL;
461}
462
463void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
464 unsigned long align, unsigned long goal)
465{
466 return __alloc_bootmem_core(pgdat->bdata, size, align, goal, LOW32LIMIT);
467}