blob: f0f85fa713ca3f3e596c8bbd7ecba09b6baed188 [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>
Franck Bui-Huue786e862006-09-25 23:31:06 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "internal.h"
20
21/*
22 * Access to this subsystem has to be serialized externally. (this is
23 * true for the boot process anyway)
24 */
25unsigned long max_low_pfn;
26unsigned long min_low_pfn;
27unsigned long max_pfn;
28
Adrian Bunk6d46cc62006-07-10 04:44:21 -070029EXPORT_UNUSED_SYMBOL(max_pfn); /* June 2006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080031static LIST_HEAD(bdata_list);
Vivek Goyal92aa63a2005-06-25 14:58:18 -070032#ifdef CONFIG_CRASH_DUMP
33/*
34 * If we have booted due to a crash, max_pfn will be a very low value. We need
35 * to know the amount of memory that the previous kernel used.
36 */
37unsigned long saved_max_pfn;
38#endif
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* return the number of _pages_ that will be allocated for the boot bitmap */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070041unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 unsigned long mapsize;
44
45 mapsize = (pages+7)/8;
46 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
47 mapsize >>= PAGE_SHIFT;
48
49 return mapsize;
50}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070051
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080052/*
53 * link bdata in order
54 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070055static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080056{
57 bootmem_data_t *ent;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070058
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080059 if (list_empty(&bdata_list)) {
60 list_add(&bdata->list, &bdata_list);
61 return;
62 }
63 /* insert in order */
64 list_for_each_entry(ent, &bdata_list, list) {
65 if (bdata->node_boot_start < ent->node_boot_start) {
66 list_add_tail(&bdata->list, &ent->list);
67 return;
68 }
69 }
70 list_add_tail(&bdata->list, &bdata_list);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080071}
72
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070073/*
74 * Given an initialised bdata, it returns the size of the boot bitmap
75 */
76static unsigned long __init get_mapsize(bootmem_data_t *bdata)
77{
78 unsigned long mapsize;
79 unsigned long start = PFN_DOWN(bdata->node_boot_start);
80 unsigned long end = bdata->node_low_pfn;
81
82 mapsize = ((end - start) + 7) / 8;
83 return ALIGN(mapsize, sizeof(long));
84}
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * Called once to set up the allocator itself.
88 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070089static unsigned long __init init_bootmem_core(pg_data_t *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned long mapstart, unsigned long start, unsigned long end)
91{
92 bootmem_data_t *bdata = pgdat->bdata;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070093 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070095 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
96 bdata->node_boot_start = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080098 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /*
101 * Initially all pages are reserved - setup_arch() has to
102 * register free RAM areas explicitly.
103 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700104 mapsize = get_mapsize(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 memset(bdata->node_bootmem_map, 0xff, mapsize);
106
107 return mapsize;
108}
109
110/*
111 * Marks a particular physical memory range as unallocatable. Usable RAM
112 * might be used for boot-time allocations - or it might get added
113 * to the free page pool later on.
114 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700115static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
116 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700118 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned long i;
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);
128
129 sidx = PFN_DOWN(addr - bdata->node_boot_start);
130 eidx = PFN_UP(addr + size - bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 for (i = sidx; i < eidx; i++)
133 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
134#ifdef CONFIG_DEBUG_BOOTMEM
135 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
136#endif
137 }
138}
139
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700140static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
141 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700143 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned long i;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /*
147 * round down end of usable mem, partially free pages are
148 * considered reserved.
149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 BUG_ON(!size);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700151 BUG_ON(PFN_DOWN(addr + size) > bdata->node_low_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (addr < bdata->last_success)
154 bdata->last_success = addr;
155
156 /*
157 * Round up the beginning of the address.
158 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700159 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
160 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 for (i = sidx; i < eidx; i++) {
163 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
164 BUG();
165 }
166}
167
168/*
169 * We 'merge' subsequent allocations to save space. We might 'lose'
170 * some fraction of a page if allocations cannot be satisfied due to
171 * size constraints on boxes where there is physical RAM space
172 * fragmentation - in these cases (mostly large memory boxes) this
173 * is not a problem.
174 *
175 * On low memory boxes we get it right in 100% of the cases.
176 *
177 * alignment has to be a power of 2 value.
178 *
179 * NOTE: This function is _not_ reentrant.
180 */
Andi Kleen267b4802006-03-25 16:31:10 +0100181void * __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
Yasunori Goto281dd252005-10-19 15:52:18 -0700183 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 unsigned long offset, remaining_size, areasize, preferred;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700186 unsigned long i, start = 0, incr, eidx, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 void *ret;
188
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700189 if (!size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 printk("__alloc_bootmem_core(): zero-sized request\n");
191 BUG();
192 }
193 BUG_ON(align & (align-1));
194
Yasunori Goto281dd252005-10-19 15:52:18 -0700195 if (limit && bdata->node_boot_start >= limit)
196 return NULL;
197
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700198 end_pfn = bdata->node_low_pfn;
199 limit = PFN_DOWN(limit);
Yasunori Goto281dd252005-10-19 15:52:18 -0700200 if (limit && end_pfn > limit)
201 end_pfn = limit;
202
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700203 eidx = end_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 offset = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700205 if (align && (bdata->node_boot_start & (align - 1UL)) != 0)
206 offset = align - (bdata->node_boot_start & (align - 1UL));
207 offset = PFN_DOWN(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /*
210 * We try to allocate bootmem pages above 'goal'
211 * first, then we try to allocate lower pages.
212 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700213 if (goal && goal >= bdata->node_boot_start && PFN_DOWN(goal) < end_pfn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 preferred = goal - bdata->node_boot_start;
215
216 if (bdata->last_success >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700217 if (!limit || (limit && limit > bdata->last_success))
218 preferred = bdata->last_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 } else
220 preferred = 0;
221
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700222 preferred = PFN_DOWN(ALIGN(preferred, align)) + offset;
223 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 incr = align >> PAGE_SHIFT ? : 1;
225
226restart_scan:
227 for (i = preferred; i < eidx; i += incr) {
228 unsigned long j;
229 i = find_next_zero_bit(bdata->node_bootmem_map, eidx, i);
230 i = ALIGN(i, incr);
Haren Myneni66d43e92005-12-12 00:37:39 -0800231 if (i >= eidx)
232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (test_bit(i, bdata->node_bootmem_map))
234 continue;
235 for (j = i + 1; j < i + areasize; ++j) {
236 if (j >= eidx)
237 goto fail_block;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700238 if (test_bit(j, bdata->node_bootmem_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto fail_block;
240 }
241 start = i;
242 goto found;
243 fail_block:
244 i = ALIGN(j, incr);
245 }
246
247 if (preferred > offset) {
248 preferred = offset;
249 goto restart_scan;
250 }
251 return NULL;
252
253found:
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700254 bdata->last_success = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 BUG_ON(start >= eidx);
256
257 /*
258 * Is the next page of the previous allocation-end the start
259 * of this allocation's buffer? If yes then we can 'merge'
260 * the previous partial page with this allocation.
261 */
262 if (align < PAGE_SIZE &&
263 bdata->last_offset && bdata->last_pos+1 == start) {
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700264 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 BUG_ON(offset > PAGE_SIZE);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700266 remaining_size = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (size < remaining_size) {
268 areasize = 0;
269 /* last_pos unchanged */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700270 bdata->last_offset = offset + size;
271 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
272 offset +
273 bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 } else {
275 remaining_size = size - remaining_size;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700276 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
277 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
278 offset +
279 bdata->node_boot_start);
280 bdata->last_pos = start + areasize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 bdata->last_offset = remaining_size;
282 }
283 bdata->last_offset &= ~PAGE_MASK;
284 } else {
285 bdata->last_pos = start + areasize - 1;
286 bdata->last_offset = size & ~PAGE_MASK;
287 ret = phys_to_virt(start * PAGE_SIZE + bdata->node_boot_start);
288 }
289
290 /*
291 * Reserve the area now:
292 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700293 for (i = start; i < start + areasize; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (unlikely(test_and_set_bit(i, bdata->node_bootmem_map)))
295 BUG();
296 memset(ret, 0, size);
297 return ret;
298}
299
300static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
301{
302 struct page *page;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700303 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 bootmem_data_t *bdata = pgdat->bdata;
305 unsigned long i, count, total = 0;
306 unsigned long idx;
307 unsigned long *map;
308 int gofast = 0;
309
310 BUG_ON(!bdata->node_bootmem_map);
311
312 count = 0;
313 /* first extant page of the node */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700314 pfn = PFN_DOWN(bdata->node_boot_start);
315 idx = bdata->node_low_pfn - pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 map = bdata->node_bootmem_map;
317 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
318 if (bdata->node_boot_start == 0 ||
319 ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG))
320 gofast = 1;
321 for (i = 0; i < idx; ) {
322 unsigned long v = ~map[i / BITS_PER_LONG];
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (gofast && v == ~0UL) {
David Howellsa226f6c2006-01-06 00:11:08 -0800325 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700327 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 count += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 order = ffs(BITS_PER_LONG) - 1;
David Howellsa226f6c2006-01-06 00:11:08 -0800330 __free_pages_bootmem(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 i += BITS_PER_LONG;
332 page += BITS_PER_LONG;
333 } else if (v) {
334 unsigned long m;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700335
336 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 for (m = 1; m && i < idx; m<<=1, page++, i++) {
338 if (v & m) {
339 count++;
David Howellsa226f6c2006-01-06 00:11:08 -0800340 __free_pages_bootmem(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 }
343 } else {
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700344 i += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700346 pfn += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 total += count;
349
350 /*
351 * Now free the allocator bitmap itself, it's not
352 * needed anymore:
353 */
354 page = virt_to_page(bdata->node_bootmem_map);
355 count = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700356 idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
357 for (i = 0; i < idx; i++, page++) {
David Howellsa226f6c2006-01-06 00:11:08 -0800358 __free_pages_bootmem(page, 0);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700359 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 total += count;
362 bdata->node_bootmem_map = NULL;
363
364 return total;
365}
366
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700367unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700368 unsigned long startpfn, unsigned long endpfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700370 return init_bootmem_core(pgdat, freepfn, startpfn, endpfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700373void __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
374 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 reserve_bootmem_core(pgdat->bdata, physaddr, size);
377}
378
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700379void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
380 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 free_bootmem_core(pgdat->bdata, physaddr, size);
383}
384
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700385unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700387 return free_all_bootmem_core(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700390unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 max_low_pfn = pages;
393 min_low_pfn = start;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700394 return init_bootmem_core(NODE_DATA(0), start, 0, pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700398void __init reserve_bootmem(unsigned long addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size);
401}
402#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
403
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700404void __init free_bootmem(unsigned long addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 free_bootmem_core(NODE_DATA(0)->bdata, addr, size);
407}
408
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700409unsigned long __init free_all_bootmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700411 return free_all_bootmem_core(NODE_DATA(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700414void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
415 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800417 bootmem_data_t *bdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 void *ptr;
419
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700420 list_for_each_entry(bdata, &bdata_list, list) {
421 ptr = __alloc_bootmem_core(bdata, size, align, goal, 0);
422 if (ptr)
423 return ptr;
424 }
Andi Kleena8062232006-04-07 19:49:21 +0200425 return NULL;
426}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700428void * __init __alloc_bootmem(unsigned long size, unsigned long align,
429 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200430{
431 void *mem = __alloc_bootmem_nopanic(size,align,goal);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700432
Andi Kleena8062232006-04-07 19:49:21 +0200433 if (mem)
434 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /*
436 * Whoops, we cannot satisfy the allocation request.
437 */
438 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
439 panic("Out of memory");
440 return NULL;
441}
442
Yasunori Goto281dd252005-10-19 15:52:18 -0700443
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700444void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
445 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 void *ptr;
448
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800449 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (ptr)
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700451 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800453 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800456#define LOW32LIMIT 0xffffffff
457
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700458void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
459 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800460{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800461 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800462 void *ptr;
463
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700464 list_for_each_entry(bdata, &bdata_list, list) {
465 ptr = __alloc_bootmem_core(bdata, size, align, goal, LOW32LIMIT);
466 if (ptr)
467 return ptr;
468 }
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800469
470 /*
471 * Whoops, we cannot satisfy the allocation request.
472 */
473 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
474 panic("Out of low memory");
475 return NULL;
476}
477
478void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
479 unsigned long align, unsigned long goal)
480{
481 return __alloc_bootmem_core(pgdat->bdata, size, align, goal, LOW32LIMIT);
482}