blob: e136c086fd981c8e61611c88546f2175d8204a86 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
12#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070014
15#include <asm/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/io.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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
Adrian Bunk6d46cc62006-07-10 04:44:21 -070028EXPORT_UNUSED_SYMBOL(max_pfn); /* June 2006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080030static LIST_HEAD(bdata_list);
Vivek Goyal92aa63a2005-06-25 14:58:18 -070031#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* 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}
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080050/*
51 * link bdata in order
52 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070053static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080054{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
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
Nick Wilson8c0e33c2005-06-25 14:59:00 -070081 mapsize = ALIGN(mapsize, sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 bdata->node_bootmem_map = phys_to_virt(mapstart << PAGE_SHIFT);
83 bdata->node_boot_start = (start << PAGE_SHIFT);
84 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080085 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
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 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700101static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
102 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
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
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700127static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
128 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
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 */
Andi Kleen267b4802006-03-25 16:31:10 +0100171void * __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
Yasunori Goto281dd252005-10-19 15:52:18 -0700173 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 unsigned long offset, remaining_size, areasize, preferred;
Yasunori Goto281dd252005-10-19 15:52:18 -0700176 unsigned long i, start = 0, incr, eidx, end_pfn = bdata->node_low_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 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
Yasunori Goto281dd252005-10-19 15:52:18 -0700185 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 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) &&
Yasunori Goto281dd252005-10-19 15:52:18 -0700204 ((goal >> PAGE_SHIFT) < end_pfn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 preferred = goal - bdata->node_boot_start;
206
207 if (bdata->last_success >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700208 if (!limit || (limit && limit > bdata->last_success))
209 preferred = bdata->last_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 } else
211 preferred = 0;
212
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700213 preferred = ALIGN(preferred, align) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 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);
Haren Myneni66d43e92005-12-12 00:37:39 -0800223 if (i >= eidx)
224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 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) {
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700256 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 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;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700293 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 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 */
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700304 pfn = bdata->node_boot_start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 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];
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (gofast && v == ~0UL) {
David Howellsa226f6c2006-01-06 00:11:08 -0800315 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700317 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 count += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 order = ffs(BITS_PER_LONG) - 1;
David Howellsa226f6c2006-01-06 00:11:08 -0800320 __free_pages_bootmem(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 i += BITS_PER_LONG;
322 page += BITS_PER_LONG;
323 } else if (v) {
324 unsigned long m;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700325
326 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 for (m = 1; m && i < idx; m<<=1, page++, i++) {
328 if (v & m) {
329 count++;
David Howellsa226f6c2006-01-06 00:11:08 -0800330 __free_pages_bootmem(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332 }
333 } else {
334 i+=BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700336 pfn += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
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++;
David Howellsa226f6c2006-01-06 00:11:08 -0800348 __free_pages_bootmem(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 total += count;
351 bdata->node_bootmem_map = NULL;
352
353 return total;
354}
355
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700356unsigned long __init init_bootmem_node (pg_data_t *pgdat, unsigned long freepfn,
357 unsigned long startpfn, unsigned long endpfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 return(init_bootmem_core(pgdat, freepfn, startpfn, endpfn));
360}
361
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700362void __init reserve_bootmem_node (pg_data_t *pgdat, unsigned long physaddr,
363 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 reserve_bootmem_core(pgdat->bdata, physaddr, size);
366}
367
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700368void __init free_bootmem_node (pg_data_t *pgdat, unsigned long physaddr,
369 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
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
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700403void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
404 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
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700415void * __init __alloc_bootmem(unsigned long size, unsigned long align,
416 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200417{
418 void *mem = __alloc_bootmem_nopanic(size,align,goal);
419 if (mem)
420 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /*
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
Yasunori Goto281dd252005-10-19 15:52:18 -0700429
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700430void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
431 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 void *ptr;
434
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800435 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (ptr)
437 return (ptr);
438
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800439 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800442#define LOW32LIMIT 0xffffffff
443
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700444void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
445 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800446{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800447 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800448 void *ptr;
449
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800450 list_for_each_entry(bdata, &bdata_list, list)
451 if ((ptr = __alloc_bootmem_core(bdata, size,
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800452 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}