blob: d213feded10df30d04fedd2e744fc89ab3f6f934 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
11
12#include <linux/mm.h>
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>
19#include <linux/module.h>
20#include <asm/dma.h>
21#include <asm/io.h>
22#include "internal.h"
23
24/*
25 * Access to this subsystem has to be serialized externally. (this is
26 * true for the boot process anyway)
27 */
28unsigned long max_low_pfn;
29unsigned long min_low_pfn;
30unsigned long max_pfn;
31
32EXPORT_SYMBOL(max_pfn); /* This is exported so
33 * dma_get_required_mask(), which uses
34 * it, can be an inline function */
35
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080036static LIST_HEAD(bdata_list);
Vivek Goyal92aa63a2005-06-25 14:58:18 -070037#ifdef CONFIG_CRASH_DUMP
38/*
39 * If we have booted due to a crash, max_pfn will be a very low value. We need
40 * to know the amount of memory that the previous kernel used.
41 */
42unsigned long saved_max_pfn;
43#endif
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* return the number of _pages_ that will be allocated for the boot bitmap */
46unsigned long __init bootmem_bootmap_pages (unsigned long pages)
47{
48 unsigned long mapsize;
49
50 mapsize = (pages+7)/8;
51 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
52 mapsize >>= PAGE_SHIFT;
53
54 return mapsize;
55}
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080056/*
57 * link bdata in order
58 */
59static void link_bootmem(bootmem_data_t *bdata)
60{
61 bootmem_data_t *ent;
62 if (list_empty(&bdata_list)) {
63 list_add(&bdata->list, &bdata_list);
64 return;
65 }
66 /* insert in order */
67 list_for_each_entry(ent, &bdata_list, list) {
68 if (bdata->node_boot_start < ent->node_boot_start) {
69 list_add_tail(&bdata->list, &ent->list);
70 return;
71 }
72 }
73 list_add_tail(&bdata->list, &bdata_list);
74 return;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Called once to set up the allocator itself.
80 */
81static unsigned long __init init_bootmem_core (pg_data_t *pgdat,
82 unsigned long mapstart, unsigned long start, unsigned long end)
83{
84 bootmem_data_t *bdata = pgdat->bdata;
85 unsigned long mapsize = ((end - start)+7)/8;
86
Nick Wilson8c0e33c2005-06-25 14:59:00 -070087 mapsize = ALIGN(mapsize, sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 bdata->node_bootmem_map = phys_to_virt(mapstart << PAGE_SHIFT);
89 bdata->node_boot_start = (start << PAGE_SHIFT);
90 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080091 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 /*
94 * Initially all pages are reserved - setup_arch() has to
95 * register free RAM areas explicitly.
96 */
97 memset(bdata->node_bootmem_map, 0xff, mapsize);
98
99 return mapsize;
100}
101
102/*
103 * Marks a particular physical memory range as unallocatable. Usable RAM
104 * might be used for boot-time allocations - or it might get added
105 * to the free page pool later on.
106 */
107static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr, unsigned long size)
108{
109 unsigned long i;
110 /*
111 * round up, partially reserved pages are considered
112 * fully reserved.
113 */
114 unsigned long sidx = (addr - bdata->node_boot_start)/PAGE_SIZE;
115 unsigned long eidx = (addr + size - bdata->node_boot_start +
116 PAGE_SIZE-1)/PAGE_SIZE;
117 unsigned long end = (addr + size + PAGE_SIZE-1)/PAGE_SIZE;
118
119 BUG_ON(!size);
120 BUG_ON(sidx >= eidx);
121 BUG_ON((addr >> PAGE_SHIFT) >= bdata->node_low_pfn);
122 BUG_ON(end > bdata->node_low_pfn);
123
124 for (i = sidx; i < eidx; i++)
125 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
126#ifdef CONFIG_DEBUG_BOOTMEM
127 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
128#endif
129 }
130}
131
132static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, unsigned long size)
133{
134 unsigned long i;
135 unsigned long start;
136 /*
137 * round down end of usable mem, partially free pages are
138 * considered reserved.
139 */
140 unsigned long sidx;
141 unsigned long eidx = (addr + size - bdata->node_boot_start)/PAGE_SIZE;
142 unsigned long end = (addr + size)/PAGE_SIZE;
143
144 BUG_ON(!size);
145 BUG_ON(end > bdata->node_low_pfn);
146
147 if (addr < bdata->last_success)
148 bdata->last_success = addr;
149
150 /*
151 * Round up the beginning of the address.
152 */
153 start = (addr + PAGE_SIZE-1) / PAGE_SIZE;
154 sidx = start - (bdata->node_boot_start/PAGE_SIZE);
155
156 for (i = sidx; i < eidx; i++) {
157 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
158 BUG();
159 }
160}
161
162/*
163 * We 'merge' subsequent allocations to save space. We might 'lose'
164 * some fraction of a page if allocations cannot be satisfied due to
165 * size constraints on boxes where there is physical RAM space
166 * fragmentation - in these cases (mostly large memory boxes) this
167 * is not a problem.
168 *
169 * On low memory boxes we get it right in 100% of the cases.
170 *
171 * alignment has to be a power of 2 value.
172 *
173 * NOTE: This function is _not_ reentrant.
174 */
Andi Kleen267b4802006-03-25 16:31:10 +0100175void * __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
Yasunori Goto281dd252005-10-19 15:52:18 -0700177 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 unsigned long offset, remaining_size, areasize, preferred;
Yasunori Goto281dd252005-10-19 15:52:18 -0700180 unsigned long i, start = 0, incr, eidx, end_pfn = bdata->node_low_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 void *ret;
182
183 if(!size) {
184 printk("__alloc_bootmem_core(): zero-sized request\n");
185 BUG();
186 }
187 BUG_ON(align & (align-1));
188
Yasunori Goto281dd252005-10-19 15:52:18 -0700189 if (limit && bdata->node_boot_start >= limit)
190 return NULL;
191
192 limit >>=PAGE_SHIFT;
193 if (limit && end_pfn > limit)
194 end_pfn = limit;
195
196 eidx = end_pfn - (bdata->node_boot_start >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 offset = 0;
198 if (align &&
199 (bdata->node_boot_start & (align - 1UL)) != 0)
200 offset = (align - (bdata->node_boot_start & (align - 1UL)));
201 offset >>= PAGE_SHIFT;
202
203 /*
204 * We try to allocate bootmem pages above 'goal'
205 * first, then we try to allocate lower pages.
206 */
207 if (goal && (goal >= bdata->node_boot_start) &&
Yasunori Goto281dd252005-10-19 15:52:18 -0700208 ((goal >> PAGE_SHIFT) < end_pfn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 preferred = goal - bdata->node_boot_start;
210
211 if (bdata->last_success >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700212 if (!limit || (limit && limit > bdata->last_success))
213 preferred = bdata->last_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 } else
215 preferred = 0;
216
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700217 preferred = ALIGN(preferred, align) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 preferred += offset;
219 areasize = (size+PAGE_SIZE-1)/PAGE_SIZE;
220 incr = align >> PAGE_SHIFT ? : 1;
221
222restart_scan:
223 for (i = preferred; i < eidx; i += incr) {
224 unsigned long j;
225 i = find_next_zero_bit(bdata->node_bootmem_map, eidx, i);
226 i = ALIGN(i, incr);
Haren Myneni66d43e92005-12-12 00:37:39 -0800227 if (i >= eidx)
228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (test_bit(i, bdata->node_bootmem_map))
230 continue;
231 for (j = i + 1; j < i + areasize; ++j) {
232 if (j >= eidx)
233 goto fail_block;
234 if (test_bit (j, bdata->node_bootmem_map))
235 goto fail_block;
236 }
237 start = i;
238 goto found;
239 fail_block:
240 i = ALIGN(j, incr);
241 }
242
243 if (preferred > offset) {
244 preferred = offset;
245 goto restart_scan;
246 }
247 return NULL;
248
249found:
250 bdata->last_success = start << PAGE_SHIFT;
251 BUG_ON(start >= eidx);
252
253 /*
254 * Is the next page of the previous allocation-end the start
255 * of this allocation's buffer? If yes then we can 'merge'
256 * the previous partial page with this allocation.
257 */
258 if (align < PAGE_SIZE &&
259 bdata->last_offset && bdata->last_pos+1 == start) {
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700260 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 BUG_ON(offset > PAGE_SIZE);
262 remaining_size = PAGE_SIZE-offset;
263 if (size < remaining_size) {
264 areasize = 0;
265 /* last_pos unchanged */
266 bdata->last_offset = offset+size;
267 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
268 bdata->node_boot_start);
269 } else {
270 remaining_size = size - remaining_size;
271 areasize = (remaining_size+PAGE_SIZE-1)/PAGE_SIZE;
272 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
273 bdata->node_boot_start);
274 bdata->last_pos = start+areasize-1;
275 bdata->last_offset = remaining_size;
276 }
277 bdata->last_offset &= ~PAGE_MASK;
278 } else {
279 bdata->last_pos = start + areasize - 1;
280 bdata->last_offset = size & ~PAGE_MASK;
281 ret = phys_to_virt(start * PAGE_SIZE + bdata->node_boot_start);
282 }
283
284 /*
285 * Reserve the area now:
286 */
287 for (i = start; i < start+areasize; i++)
288 if (unlikely(test_and_set_bit(i, bdata->node_bootmem_map)))
289 BUG();
290 memset(ret, 0, size);
291 return ret;
292}
293
294static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
295{
296 struct page *page;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700297 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 bootmem_data_t *bdata = pgdat->bdata;
299 unsigned long i, count, total = 0;
300 unsigned long idx;
301 unsigned long *map;
302 int gofast = 0;
303
304 BUG_ON(!bdata->node_bootmem_map);
305
306 count = 0;
307 /* first extant page of the node */
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700308 pfn = bdata->node_boot_start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 idx = bdata->node_low_pfn - (bdata->node_boot_start >> PAGE_SHIFT);
310 map = bdata->node_bootmem_map;
311 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
312 if (bdata->node_boot_start == 0 ||
313 ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG))
314 gofast = 1;
315 for (i = 0; i < idx; ) {
316 unsigned long v = ~map[i / BITS_PER_LONG];
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (gofast && v == ~0UL) {
David Howellsa226f6c2006-01-06 00:11:08 -0800319 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700321 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 count += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 order = ffs(BITS_PER_LONG) - 1;
David Howellsa226f6c2006-01-06 00:11:08 -0800324 __free_pages_bootmem(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 i += BITS_PER_LONG;
326 page += BITS_PER_LONG;
327 } else if (v) {
328 unsigned long m;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700329
330 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 for (m = 1; m && i < idx; m<<=1, page++, i++) {
332 if (v & m) {
333 count++;
David Howellsa226f6c2006-01-06 00:11:08 -0800334 __free_pages_bootmem(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336 }
337 } else {
338 i+=BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700340 pfn += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 total += count;
343
344 /*
345 * Now free the allocator bitmap itself, it's not
346 * needed anymore:
347 */
348 page = virt_to_page(bdata->node_bootmem_map);
349 count = 0;
350 for (i = 0; i < ((bdata->node_low_pfn-(bdata->node_boot_start >> PAGE_SHIFT))/8 + PAGE_SIZE-1)/PAGE_SIZE; i++,page++) {
351 count++;
David Howellsa226f6c2006-01-06 00:11:08 -0800352 __free_pages_bootmem(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 total += count;
355 bdata->node_bootmem_map = NULL;
356
357 return total;
358}
359
360unsigned long __init init_bootmem_node (pg_data_t *pgdat, unsigned long freepfn, unsigned long startpfn, unsigned long endpfn)
361{
362 return(init_bootmem_core(pgdat, freepfn, startpfn, endpfn));
363}
364
365void __init reserve_bootmem_node (pg_data_t *pgdat, unsigned long physaddr, unsigned long size)
366{
367 reserve_bootmem_core(pgdat->bdata, physaddr, size);
368}
369
370void __init free_bootmem_node (pg_data_t *pgdat, unsigned long physaddr, unsigned long size)
371{
372 free_bootmem_core(pgdat->bdata, physaddr, size);
373}
374
375unsigned long __init free_all_bootmem_node (pg_data_t *pgdat)
376{
377 return(free_all_bootmem_core(pgdat));
378}
379
380unsigned long __init init_bootmem (unsigned long start, unsigned long pages)
381{
382 max_low_pfn = pages;
383 min_low_pfn = start;
384 return(init_bootmem_core(NODE_DATA(0), start, 0, pages));
385}
386
387#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
388void __init reserve_bootmem (unsigned long addr, unsigned long size)
389{
390 reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size);
391}
392#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
393
394void __init free_bootmem (unsigned long addr, unsigned long size)
395{
396 free_bootmem_core(NODE_DATA(0)->bdata, addr, size);
397}
398
399unsigned long __init free_all_bootmem (void)
400{
401 return(free_all_bootmem_core(NODE_DATA(0)));
402}
403
Andi Kleena8062232006-04-07 19:49:21 +0200404void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800406 bootmem_data_t *bdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 void *ptr;
408
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800409 list_for_each_entry(bdata, &bdata_list, list)
410 if ((ptr = __alloc_bootmem_core(bdata, size, align, goal, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return(ptr);
Andi Kleena8062232006-04-07 19:49:21 +0200412 return NULL;
413}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Andi Kleena8062232006-04-07 19:49:21 +0200415void * __init __alloc_bootmem(unsigned long size, unsigned long align, unsigned long goal)
416{
417 void *mem = __alloc_bootmem_nopanic(size,align,goal);
418 if (mem)
419 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /*
421 * Whoops, we cannot satisfy the allocation request.
422 */
423 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
424 panic("Out of memory");
425 return NULL;
426}
427
Yasunori Goto281dd252005-10-19 15:52:18 -0700428
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800429void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, unsigned long align,
430 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 void *ptr;
433
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800434 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (ptr)
436 return (ptr);
437
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800438 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800441#define LOW32LIMIT 0xffffffff
442
443void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, unsigned long goal)
444{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800445 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800446 void *ptr;
447
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800448 list_for_each_entry(bdata, &bdata_list, list)
449 if ((ptr = __alloc_bootmem_core(bdata, size,
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800450 align, goal, LOW32LIMIT)))
451 return(ptr);
452
453 /*
454 * Whoops, we cannot satisfy the allocation request.
455 */
456 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
457 panic("Out of low memory");
458 return NULL;
459}
460
461void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
462 unsigned long align, unsigned long goal)
463{
464 return __alloc_bootmem_core(pgdat->bdata, size, align, goal, LOW32LIMIT);
465}