blob: 3c012fb58745cb2caaab61bc09387590479aed49 [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>
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070012#include <linux/pfn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070015
16#include <asm/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/io.h>
Heiko Carstensdfd54cb2006-09-25 23:31:33 -070018#include <asm/processor.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "internal.h"
21
22/*
23 * Access to this subsystem has to be serialized externally. (this is
24 * true for the boot process anyway)
25 */
26unsigned long max_low_pfn;
27unsigned long min_low_pfn;
28unsigned long max_pfn;
29
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 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070040unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
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}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070050
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080051/*
52 * link bdata in order
53 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070054static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080055{
56 bootmem_data_t *ent;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070057
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080058 if (list_empty(&bdata_list)) {
59 list_add(&bdata->list, &bdata_list);
60 return;
61 }
62 /* insert in order */
63 list_for_each_entry(ent, &bdata_list, list) {
64 if (bdata->node_boot_start < ent->node_boot_start) {
65 list_add_tail(&bdata->list, &ent->list);
66 return;
67 }
68 }
69 list_add_tail(&bdata->list, &bdata_list);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080070}
71
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070072/*
73 * Given an initialised bdata, it returns the size of the boot bitmap
74 */
75static unsigned long __init get_mapsize(bootmem_data_t *bdata)
76{
77 unsigned long mapsize;
78 unsigned long start = PFN_DOWN(bdata->node_boot_start);
79 unsigned long end = bdata->node_low_pfn;
80
81 mapsize = ((end - start) + 7) / 8;
82 return ALIGN(mapsize, sizeof(long));
83}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * Called once to set up the allocator itself.
87 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070088static unsigned long __init init_bootmem_core(pg_data_t *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 unsigned long mapstart, unsigned long start, unsigned long end)
90{
91 bootmem_data_t *bdata = pgdat->bdata;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070092 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070094 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
95 bdata->node_boot_start = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080097 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /*
100 * Initially all pages are reserved - setup_arch() has to
101 * register free RAM areas explicitly.
102 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700103 mapsize = get_mapsize(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 memset(bdata->node_bootmem_map, 0xff, mapsize);
105
106 return mapsize;
107}
108
109/*
110 * Marks a particular physical memory range as unallocatable. Usable RAM
111 * might be used for boot-time allocations - or it might get added
112 * to the free page pool later on.
113 */
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800114static int __init reserve_bootmem_core(bootmem_data_t *bdata,
115 unsigned long addr, unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700117 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned long i;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800119 int ret;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 /*
122 * round up, partially reserved pages are considered
123 * fully reserved.
124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 BUG_ON(!size);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700126 BUG_ON(PFN_DOWN(addr) >= bdata->node_low_pfn);
127 BUG_ON(PFN_UP(addr + size) > bdata->node_low_pfn);
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700128 BUG_ON(addr < bdata->node_boot_start);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700129
130 sidx = PFN_DOWN(addr - bdata->node_boot_start);
131 eidx = PFN_UP(addr + size - bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 for (i = sidx; i < eidx; i++)
134 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
135#ifdef CONFIG_DEBUG_BOOTMEM
136 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
137#endif
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800138 if (flags & BOOTMEM_EXCLUSIVE) {
139 ret = -EBUSY;
140 goto err;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800143
144 return 0;
145
146err:
147 /* unreserve memory we accidentally reserved */
148 for (i--; i >= sidx; i--)
149 clear_bit(i, bdata->node_bootmem_map);
150
151 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700154static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
155 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700157 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned long i;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700159
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700160 BUG_ON(!size);
161
162 /* out range */
163 if (addr + size < bdata->node_boot_start ||
164 PFN_DOWN(addr) > bdata->node_low_pfn)
165 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /*
167 * round down end of usable mem, partially free pages are
168 * considered reserved.
169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700171 if (addr >= bdata->node_boot_start && addr < bdata->last_success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 bdata->last_success = addr;
173
174 /*
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700175 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700177 if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
178 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
179 else
180 sidx = 0;
181
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700182 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700183 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
184 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 for (i = sidx; i < eidx; i++) {
187 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
188 BUG();
189 }
190}
191
192/*
193 * We 'merge' subsequent allocations to save space. We might 'lose'
194 * some fraction of a page if allocations cannot be satisfied due to
195 * size constraints on boxes where there is physical RAM space
196 * fragmentation - in these cases (mostly large memory boxes) this
197 * is not a problem.
198 *
199 * On low memory boxes we get it right in 100% of the cases.
200 *
201 * alignment has to be a power of 2 value.
202 *
203 * NOTE: This function is _not_ reentrant.
204 */
Andi Kleen267b4802006-03-25 16:31:10 +0100205void * __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
Yasunori Goto281dd252005-10-19 15:52:18 -0700207 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 unsigned long offset, remaining_size, areasize, preferred;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700210 unsigned long i, start = 0, incr, eidx, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 void *ret;
212
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700213 if (!size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 printk("__alloc_bootmem_core(): zero-sized request\n");
215 BUG();
216 }
217 BUG_ON(align & (align-1));
218
Yasunori Goto281dd252005-10-19 15:52:18 -0700219 if (limit && bdata->node_boot_start >= limit)
220 return NULL;
221
Christian Krafft7c309a62006-12-06 20:32:41 -0800222 /* on nodes without memory - bootmem_map is NULL */
223 if (!bdata->node_bootmem_map)
224 return NULL;
225
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700226 end_pfn = bdata->node_low_pfn;
227 limit = PFN_DOWN(limit);
Yasunori Goto281dd252005-10-19 15:52:18 -0700228 if (limit && end_pfn > limit)
229 end_pfn = limit;
230
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700231 eidx = end_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 offset = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700233 if (align && (bdata->node_boot_start & (align - 1UL)) != 0)
234 offset = align - (bdata->node_boot_start & (align - 1UL));
235 offset = PFN_DOWN(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /*
238 * We try to allocate bootmem pages above 'goal'
239 * first, then we try to allocate lower pages.
240 */
Yinghai Luad093152008-03-10 23:23:42 -0700241 preferred = 0;
242 if (goal && PFN_DOWN(goal) < end_pfn) {
243 if (goal > bdata->node_boot_start)
244 preferred = goal - bdata->node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 if (bdata->last_success >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700247 if (!limit || (limit && limit > bdata->last_success))
248 preferred = bdata->last_success;
Yinghai Luad093152008-03-10 23:23:42 -0700249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700251 preferred = PFN_DOWN(ALIGN(preferred, align)) + offset;
252 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 incr = align >> PAGE_SHIFT ? : 1;
254
255restart_scan:
Yinghai Luad093152008-03-10 23:23:42 -0700256 for (i = preferred; i < eidx;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 unsigned long j;
Yinghai Luad093152008-03-10 23:23:42 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 i = find_next_zero_bit(bdata->node_bootmem_map, eidx, i);
260 i = ALIGN(i, incr);
Haren Myneni66d43e92005-12-12 00:37:39 -0800261 if (i >= eidx)
262 break;
Yinghai Luad093152008-03-10 23:23:42 -0700263 if (test_bit(i, bdata->node_bootmem_map)) {
264 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 continue;
Yinghai Luad093152008-03-10 23:23:42 -0700266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 for (j = i + 1; j < i + areasize; ++j) {
268 if (j >= eidx)
269 goto fail_block;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700270 if (test_bit(j, bdata->node_bootmem_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto fail_block;
272 }
273 start = i;
274 goto found;
275 fail_block:
276 i = ALIGN(j, incr);
Yinghai Luad093152008-03-10 23:23:42 -0700277 if (i == j)
278 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280
281 if (preferred > offset) {
282 preferred = offset;
283 goto restart_scan;
284 }
285 return NULL;
286
287found:
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700288 bdata->last_success = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 BUG_ON(start >= eidx);
290
291 /*
292 * Is the next page of the previous allocation-end the start
293 * of this allocation's buffer? If yes then we can 'merge'
294 * the previous partial page with this allocation.
295 */
296 if (align < PAGE_SIZE &&
297 bdata->last_offset && bdata->last_pos+1 == start) {
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700298 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 BUG_ON(offset > PAGE_SIZE);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700300 remaining_size = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (size < remaining_size) {
302 areasize = 0;
303 /* last_pos unchanged */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700304 bdata->last_offset = offset + size;
305 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
306 offset +
307 bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 } else {
309 remaining_size = size - remaining_size;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700310 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
311 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
312 offset +
313 bdata->node_boot_start);
314 bdata->last_pos = start + areasize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 bdata->last_offset = remaining_size;
316 }
317 bdata->last_offset &= ~PAGE_MASK;
318 } else {
319 bdata->last_pos = start + areasize - 1;
320 bdata->last_offset = size & ~PAGE_MASK;
321 ret = phys_to_virt(start * PAGE_SIZE + bdata->node_boot_start);
322 }
323
324 /*
325 * Reserve the area now:
326 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700327 for (i = start; i < start + areasize; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (unlikely(test_and_set_bit(i, bdata->node_bootmem_map)))
329 BUG();
330 memset(ret, 0, size);
331 return ret;
332}
333
334static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
335{
336 struct page *page;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700337 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 bootmem_data_t *bdata = pgdat->bdata;
339 unsigned long i, count, total = 0;
340 unsigned long idx;
341 unsigned long *map;
342 int gofast = 0;
343
344 BUG_ON(!bdata->node_bootmem_map);
345
346 count = 0;
347 /* first extant page of the node */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700348 pfn = PFN_DOWN(bdata->node_boot_start);
349 idx = bdata->node_low_pfn - pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 map = bdata->node_bootmem_map;
351 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
352 if (bdata->node_boot_start == 0 ||
353 ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG))
354 gofast = 1;
355 for (i = 0; i < idx; ) {
356 unsigned long v = ~map[i / BITS_PER_LONG];
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (gofast && v == ~0UL) {
David Howellsa226f6c2006-01-06 00:11:08 -0800359 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700361 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 count += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 order = ffs(BITS_PER_LONG) - 1;
David Howellsa226f6c2006-01-06 00:11:08 -0800364 __free_pages_bootmem(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 i += BITS_PER_LONG;
366 page += BITS_PER_LONG;
367 } else if (v) {
368 unsigned long m;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700369
370 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 for (m = 1; m && i < idx; m<<=1, page++, i++) {
372 if (v & m) {
373 count++;
David Howellsa226f6c2006-01-06 00:11:08 -0800374 __free_pages_bootmem(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 }
377 } else {
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700378 i += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700380 pfn += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 total += count;
383
384 /*
385 * Now free the allocator bitmap itself, it's not
386 * needed anymore:
387 */
388 page = virt_to_page(bdata->node_bootmem_map);
389 count = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700390 idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
391 for (i = 0; i < idx; i++, page++) {
David Howellsa226f6c2006-01-06 00:11:08 -0800392 __free_pages_bootmem(page, 0);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700393 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 total += count;
396 bdata->node_bootmem_map = NULL;
397
398 return total;
399}
400
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700401unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700402 unsigned long startpfn, unsigned long endpfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700404 return init_bootmem_core(pgdat, freepfn, startpfn, endpfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700407void __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800408 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800410 reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700413void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
414 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 free_bootmem_core(pgdat->bdata, physaddr, size);
417}
418
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700419unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700421 return free_all_bootmem_core(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700424unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 max_low_pfn = pages;
427 min_low_pfn = start;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700428 return init_bootmem_core(NODE_DATA(0), start, 0, pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800432int __init reserve_bootmem(unsigned long addr, unsigned long size,
433 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800435 return reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
438
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700439void __init free_bootmem(unsigned long addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700441 bootmem_data_t *bdata;
442 list_for_each_entry(bdata, &bdata_list, list)
443 free_bootmem_core(bdata, addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700446unsigned long __init free_all_bootmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700448 return free_all_bootmem_core(NODE_DATA(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700451void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
452 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800454 bootmem_data_t *bdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 void *ptr;
456
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700457 list_for_each_entry(bdata, &bdata_list, list) {
458 ptr = __alloc_bootmem_core(bdata, size, align, goal, 0);
459 if (ptr)
460 return ptr;
461 }
Andi Kleena8062232006-04-07 19:49:21 +0200462 return NULL;
463}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700465void * __init __alloc_bootmem(unsigned long size, unsigned long align,
466 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200467{
468 void *mem = __alloc_bootmem_nopanic(size,align,goal);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700469
Andi Kleena8062232006-04-07 19:49:21 +0200470 if (mem)
471 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
473 * Whoops, we cannot satisfy the allocation request.
474 */
475 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
476 panic("Out of memory");
477 return NULL;
478}
479
Yasunori Goto281dd252005-10-19 15:52:18 -0700480
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700481void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
482 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 void *ptr;
485
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800486 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (ptr)
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700488 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800490 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700493#ifndef ARCH_LOW_ADDRESS_LIMIT
494#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
495#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800496
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700497void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
498 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800499{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800500 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800501 void *ptr;
502
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700503 list_for_each_entry(bdata, &bdata_list, list) {
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700504 ptr = __alloc_bootmem_core(bdata, size, align, goal,
505 ARCH_LOW_ADDRESS_LIMIT);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700506 if (ptr)
507 return ptr;
508 }
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800509
510 /*
511 * Whoops, we cannot satisfy the allocation request.
512 */
513 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
514 panic("Out of low memory");
515 return NULL;
516}
517
518void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
519 unsigned long align, unsigned long goal)
520{
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700521 return __alloc_bootmem_core(pgdat->bdata, size, align, goal,
522 ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800523}