blob: 94253428f0913c016a7dee75d1e68a68fbe87ddc [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
Adrian Bunk6d46cc62006-07-10 04:44:21 -070030EXPORT_UNUSED_SYMBOL(max_pfn); /* June 2006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080032static LIST_HEAD(bdata_list);
Vivek Goyal92aa63a2005-06-25 14:58:18 -070033#ifdef CONFIG_CRASH_DUMP
34/*
35 * If we have booted due to a crash, max_pfn will be a very low value. We need
36 * to know the amount of memory that the previous kernel used.
37 */
38unsigned long saved_max_pfn;
39#endif
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* return the number of _pages_ that will be allocated for the boot bitmap */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070042unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 unsigned long mapsize;
45
46 mapsize = (pages+7)/8;
47 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
48 mapsize >>= PAGE_SHIFT;
49
50 return mapsize;
51}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070052
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080053/*
54 * link bdata in order
55 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070056static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080057{
58 bootmem_data_t *ent;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070059
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080060 if (list_empty(&bdata_list)) {
61 list_add(&bdata->list, &bdata_list);
62 return;
63 }
64 /* insert in order */
65 list_for_each_entry(ent, &bdata_list, list) {
66 if (bdata->node_boot_start < ent->node_boot_start) {
67 list_add_tail(&bdata->list, &ent->list);
68 return;
69 }
70 }
71 list_add_tail(&bdata->list, &bdata_list);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080072}
73
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070074/*
75 * Given an initialised bdata, it returns the size of the boot bitmap
76 */
77static unsigned long __init get_mapsize(bootmem_data_t *bdata)
78{
79 unsigned long mapsize;
80 unsigned long start = PFN_DOWN(bdata->node_boot_start);
81 unsigned long end = bdata->node_low_pfn;
82
83 mapsize = ((end - start) + 7) / 8;
84 return ALIGN(mapsize, sizeof(long));
85}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Called once to set up the allocator itself.
89 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070090static unsigned long __init init_bootmem_core(pg_data_t *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 unsigned long mapstart, unsigned long start, unsigned long end)
92{
93 bootmem_data_t *bdata = pgdat->bdata;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070094 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070096 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
97 bdata->node_boot_start = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080099 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /*
102 * Initially all pages are reserved - setup_arch() has to
103 * register free RAM areas explicitly.
104 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700105 mapsize = get_mapsize(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 memset(bdata->node_bootmem_map, 0xff, mapsize);
107
108 return mapsize;
109}
110
111/*
112 * Marks a particular physical memory range as unallocatable. Usable RAM
113 * might be used for boot-time allocations - or it might get added
114 * to the free page pool later on.
115 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700116static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
117 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700119 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned long i;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /*
123 * round up, partially reserved pages are considered
124 * fully reserved.
125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 BUG_ON(!size);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700127 BUG_ON(PFN_DOWN(addr) >= bdata->node_low_pfn);
128 BUG_ON(PFN_UP(addr + size) > bdata->node_low_pfn);
129
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
138 }
139}
140
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700141static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
142 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700144 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 unsigned long i;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /*
148 * round down end of usable mem, partially free pages are
149 * considered reserved.
150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 BUG_ON(!size);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700152 BUG_ON(PFN_DOWN(addr + size) > bdata->node_low_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 if (addr < bdata->last_success)
155 bdata->last_success = addr;
156
157 /*
158 * Round up the beginning of the address.
159 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700160 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
161 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 for (i = sidx; i < eidx; i++) {
164 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
165 BUG();
166 }
167}
168
169/*
170 * We 'merge' subsequent allocations to save space. We might 'lose'
171 * some fraction of a page if allocations cannot be satisfied due to
172 * size constraints on boxes where there is physical RAM space
173 * fragmentation - in these cases (mostly large memory boxes) this
174 * is not a problem.
175 *
176 * On low memory boxes we get it right in 100% of the cases.
177 *
178 * alignment has to be a power of 2 value.
179 *
180 * NOTE: This function is _not_ reentrant.
181 */
Andi Kleen267b4802006-03-25 16:31:10 +0100182void * __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
Yasunori Goto281dd252005-10-19 15:52:18 -0700184 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 unsigned long offset, remaining_size, areasize, preferred;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700187 unsigned long i, start = 0, incr, eidx, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 void *ret;
189
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700190 if (!size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 printk("__alloc_bootmem_core(): zero-sized request\n");
192 BUG();
193 }
194 BUG_ON(align & (align-1));
195
Yasunori Goto281dd252005-10-19 15:52:18 -0700196 if (limit && bdata->node_boot_start >= limit)
197 return NULL;
198
Christian Krafft7c309a62006-12-06 20:32:41 -0800199 /* on nodes without memory - bootmem_map is NULL */
200 if (!bdata->node_bootmem_map)
201 return NULL;
202
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700203 end_pfn = bdata->node_low_pfn;
204 limit = PFN_DOWN(limit);
Yasunori Goto281dd252005-10-19 15:52:18 -0700205 if (limit && end_pfn > limit)
206 end_pfn = limit;
207
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700208 eidx = end_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 offset = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700210 if (align && (bdata->node_boot_start & (align - 1UL)) != 0)
211 offset = align - (bdata->node_boot_start & (align - 1UL));
212 offset = PFN_DOWN(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /*
215 * We try to allocate bootmem pages above 'goal'
216 * first, then we try to allocate lower pages.
217 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700218 if (goal && goal >= bdata->node_boot_start && PFN_DOWN(goal) < end_pfn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 preferred = goal - bdata->node_boot_start;
220
221 if (bdata->last_success >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700222 if (!limit || (limit && limit > bdata->last_success))
223 preferred = bdata->last_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 } else
225 preferred = 0;
226
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700227 preferred = PFN_DOWN(ALIGN(preferred, align)) + offset;
228 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 incr = align >> PAGE_SHIFT ? : 1;
230
231restart_scan:
232 for (i = preferred; i < eidx; i += incr) {
233 unsigned long j;
234 i = find_next_zero_bit(bdata->node_bootmem_map, eidx, i);
235 i = ALIGN(i, incr);
Haren Myneni66d43e92005-12-12 00:37:39 -0800236 if (i >= eidx)
237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (test_bit(i, bdata->node_bootmem_map))
239 continue;
240 for (j = i + 1; j < i + areasize; ++j) {
241 if (j >= eidx)
242 goto fail_block;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700243 if (test_bit(j, bdata->node_bootmem_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto fail_block;
245 }
246 start = i;
247 goto found;
248 fail_block:
249 i = ALIGN(j, incr);
250 }
251
252 if (preferred > offset) {
253 preferred = offset;
254 goto restart_scan;
255 }
256 return NULL;
257
258found:
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700259 bdata->last_success = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 BUG_ON(start >= eidx);
261
262 /*
263 * Is the next page of the previous allocation-end the start
264 * of this allocation's buffer? If yes then we can 'merge'
265 * the previous partial page with this allocation.
266 */
267 if (align < PAGE_SIZE &&
268 bdata->last_offset && bdata->last_pos+1 == start) {
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700269 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 BUG_ON(offset > PAGE_SIZE);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700271 remaining_size = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (size < remaining_size) {
273 areasize = 0;
274 /* last_pos unchanged */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700275 bdata->last_offset = offset + size;
276 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
277 offset +
278 bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 } else {
280 remaining_size = size - remaining_size;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700281 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
282 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
283 offset +
284 bdata->node_boot_start);
285 bdata->last_pos = start + areasize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 bdata->last_offset = remaining_size;
287 }
288 bdata->last_offset &= ~PAGE_MASK;
289 } else {
290 bdata->last_pos = start + areasize - 1;
291 bdata->last_offset = size & ~PAGE_MASK;
292 ret = phys_to_virt(start * PAGE_SIZE + bdata->node_boot_start);
293 }
294
295 /*
296 * Reserve the area now:
297 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700298 for (i = start; i < start + areasize; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (unlikely(test_and_set_bit(i, bdata->node_bootmem_map)))
300 BUG();
301 memset(ret, 0, size);
302 return ret;
303}
304
305static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
306{
307 struct page *page;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700308 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 bootmem_data_t *bdata = pgdat->bdata;
310 unsigned long i, count, total = 0;
311 unsigned long idx;
312 unsigned long *map;
313 int gofast = 0;
314
315 BUG_ON(!bdata->node_bootmem_map);
316
317 count = 0;
318 /* first extant page of the node */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700319 pfn = PFN_DOWN(bdata->node_boot_start);
320 idx = bdata->node_low_pfn - pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 map = bdata->node_bootmem_map;
322 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
323 if (bdata->node_boot_start == 0 ||
324 ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG))
325 gofast = 1;
326 for (i = 0; i < idx; ) {
327 unsigned long v = ~map[i / BITS_PER_LONG];
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (gofast && v == ~0UL) {
David Howellsa226f6c2006-01-06 00:11:08 -0800330 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700332 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 count += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 order = ffs(BITS_PER_LONG) - 1;
David Howellsa226f6c2006-01-06 00:11:08 -0800335 __free_pages_bootmem(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 i += BITS_PER_LONG;
337 page += BITS_PER_LONG;
338 } else if (v) {
339 unsigned long m;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700340
341 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 for (m = 1; m && i < idx; m<<=1, page++, i++) {
343 if (v & m) {
344 count++;
David Howellsa226f6c2006-01-06 00:11:08 -0800345 __free_pages_bootmem(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 }
348 } else {
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700349 i += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700351 pfn += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 total += count;
354
355 /*
356 * Now free the allocator bitmap itself, it's not
357 * needed anymore:
358 */
359 page = virt_to_page(bdata->node_bootmem_map);
360 count = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700361 idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
362 for (i = 0; i < idx; i++, page++) {
David Howellsa226f6c2006-01-06 00:11:08 -0800363 __free_pages_bootmem(page, 0);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700364 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366 total += count;
367 bdata->node_bootmem_map = NULL;
368
369 return total;
370}
371
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700372unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700373 unsigned long startpfn, unsigned long endpfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700375 return init_bootmem_core(pgdat, freepfn, startpfn, endpfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700378void __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
379 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 reserve_bootmem_core(pgdat->bdata, physaddr, size);
382}
383
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700384void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
385 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 free_bootmem_core(pgdat->bdata, physaddr, size);
388}
389
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700390unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700392 return free_all_bootmem_core(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700395unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 max_low_pfn = pages;
398 min_low_pfn = start;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700399 return init_bootmem_core(NODE_DATA(0), start, 0, pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700403void __init reserve_bootmem(unsigned long addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size);
406}
407#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
408
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700409void __init free_bootmem(unsigned long addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 free_bootmem_core(NODE_DATA(0)->bdata, addr, size);
412}
413
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700414unsigned long __init free_all_bootmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700416 return free_all_bootmem_core(NODE_DATA(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700419void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
420 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800422 bootmem_data_t *bdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 void *ptr;
424
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700425 list_for_each_entry(bdata, &bdata_list, list) {
426 ptr = __alloc_bootmem_core(bdata, size, align, goal, 0);
427 if (ptr)
428 return ptr;
429 }
Andi Kleena8062232006-04-07 19:49:21 +0200430 return NULL;
431}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700433void * __init __alloc_bootmem(unsigned long size, unsigned long align,
434 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200435{
436 void *mem = __alloc_bootmem_nopanic(size,align,goal);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700437
Andi Kleena8062232006-04-07 19:49:21 +0200438 if (mem)
439 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /*
441 * Whoops, we cannot satisfy the allocation request.
442 */
443 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
444 panic("Out of memory");
445 return NULL;
446}
447
Yasunori Goto281dd252005-10-19 15:52:18 -0700448
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700449void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
450 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 void *ptr;
453
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800454 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (ptr)
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700456 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800458 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700461#ifndef ARCH_LOW_ADDRESS_LIMIT
462#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
463#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800464
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700465void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
466 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800467{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800468 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800469 void *ptr;
470
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700471 list_for_each_entry(bdata, &bdata_list, list) {
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700472 ptr = __alloc_bootmem_core(bdata, size, align, goal,
473 ARCH_LOW_ADDRESS_LIMIT);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700474 if (ptr)
475 return ptr;
476 }
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800477
478 /*
479 * Whoops, we cannot satisfy the allocation request.
480 */
481 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
482 panic("Out of low memory");
483 return NULL;
484}
485
486void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
487 unsigned long align, unsigned long goal)
488{
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700489 return __alloc_bootmem_core(pgdat->bdata, size, align, goal,
490 ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800491}