blob: 484849bfc8c43032cbd8641519f3e6d10dae6876 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Johannes Weiner57cfc292008-07-23 21:28:00 -07002 * bootmem - A boot-time physical memory allocator and configurator
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 1999 Ingo Molnar
Johannes Weiner57cfc292008-07-23 21:28:00 -07005 * 1999 Kanoj Sarcar, SGI
6 * 2008 Johannes Weiner
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Johannes Weiner57cfc292008-07-23 21:28:00 -07008 * Access to this subsystem has to be serialized externally (which is true
9 * for the boot process anyway).
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022unsigned long max_low_pfn;
23unsigned long min_low_pfn;
24unsigned long max_pfn;
25
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080026static LIST_HEAD(bdata_list);
Vivek Goyal92aa63a2005-06-25 14:58:18 -070027#ifdef CONFIG_CRASH_DUMP
28/*
29 * If we have booted due to a crash, max_pfn will be a very low value. We need
30 * to know the amount of memory that the previous kernel used.
31 */
32unsigned long saved_max_pfn;
33#endif
34
Johannes Weinerb61bfa32008-07-23 21:26:55 -070035bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
36
Johannes Weiner2e5237d2008-07-23 21:28:02 -070037static int bootmem_debug;
38
39static int __init bootmem_debug_setup(char *buf)
40{
41 bootmem_debug = 1;
42 return 0;
43}
44early_param("bootmem_debug", bootmem_debug_setup);
45
46#define bdebug(fmt, args...) ({ \
47 if (unlikely(bootmem_debug)) \
48 printk(KERN_INFO \
49 "bootmem::%s " fmt, \
50 __FUNCTION__, ## args); \
51})
52
Johannes Weinerdf049a52008-07-23 21:28:02 -070053static unsigned long __init bootmap_bytes(unsigned long pages)
Johannes Weiner223e8dc2008-07-23 21:28:00 -070054{
Johannes Weinerdf049a52008-07-23 21:28:02 -070055 unsigned long bytes = (pages + 7) / 8;
Johannes Weiner223e8dc2008-07-23 21:28:00 -070056
Johannes Weinerdf049a52008-07-23 21:28:02 -070057 return ALIGN(bytes, sizeof(long));
Johannes Weiner223e8dc2008-07-23 21:28:00 -070058}
59
Johannes Weinera66fd7d2008-07-23 21:28:01 -070060/**
61 * bootmem_bootmap_pages - calculate bitmap size in pages
62 * @pages: number of pages the bitmap has to represent
63 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070064unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Johannes Weinerdf049a52008-07-23 21:28:02 -070066 unsigned long bytes = bootmap_bytes(pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Johannes Weinerdf049a52008-07-23 21:28:02 -070068 return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070070
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080071/*
72 * link bdata in order
73 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070074static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080075{
76 bootmem_data_t *ent;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070077
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080078 if (list_empty(&bdata_list)) {
79 list_add(&bdata->list, &bdata_list);
80 return;
81 }
82 /* insert in order */
83 list_for_each_entry(ent, &bdata_list, list) {
84 if (bdata->node_boot_start < ent->node_boot_start) {
85 list_add_tail(&bdata->list, &ent->list);
86 return;
87 }
88 }
89 list_add_tail(&bdata->list, &bdata_list);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080090}
91
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070092/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Called once to set up the allocator itself.
94 */
Johannes Weiner8ae04462008-07-23 21:26:59 -070095static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 unsigned long mapstart, unsigned long start, unsigned long end)
97{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070098 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700100 mminit_validate_memmodel_limits(&start, &end);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700101 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
102 bdata->node_boot_start = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800104 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /*
107 * Initially all pages are reserved - setup_arch() has to
108 * register free RAM areas explicitly.
109 */
Johannes Weinerdf049a52008-07-23 21:28:02 -0700110 mapsize = bootmap_bytes(end - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 memset(bdata->node_bootmem_map, 0xff, mapsize);
112
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700113 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
114 bdata - bootmem_node_data, start, mapstart, end, mapsize);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return mapsize;
117}
118
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700119/**
120 * init_bootmem_node - register a node as boot memory
121 * @pgdat: node to register
122 * @freepfn: pfn where the bitmap for this node is to be placed
123 * @startpfn: first pfn on the node
124 * @endpfn: first pfn after the node
125 *
126 * Returns the number of bytes needed to hold the bitmap for this node.
127 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700128unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
129 unsigned long startpfn, unsigned long endpfn)
130{
131 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
132}
133
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700134/**
135 * init_bootmem - register boot memory
136 * @start: pfn where the bitmap is to be placed
137 * @pages: number of available physical pages
138 *
139 * Returns the number of bytes needed to hold the bitmap.
140 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700141unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
142{
143 max_low_pfn = pages;
144 min_low_pfn = start;
145 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
146}
147
148static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
149{
150 struct page *page;
151 unsigned long pfn;
152 unsigned long i, count;
Johannes Weinerdf049a52008-07-23 21:28:02 -0700153 unsigned long idx, pages;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700154 unsigned long *map;
155 int gofast = 0;
156
157 BUG_ON(!bdata->node_bootmem_map);
158
159 count = 0;
160 /* first extant page of the node */
161 pfn = PFN_DOWN(bdata->node_boot_start);
162 idx = bdata->node_low_pfn - pfn;
163 map = bdata->node_bootmem_map;
164 /*
165 * Check if we are aligned to BITS_PER_LONG pages. If so, we might
166 * be able to free page orders of that size at once.
167 */
168 if (!(pfn & (BITS_PER_LONG-1)))
169 gofast = 1;
170
171 for (i = 0; i < idx; ) {
172 unsigned long v = ~map[i / BITS_PER_LONG];
173
174 if (gofast && v == ~0UL) {
175 int order;
176
177 page = pfn_to_page(pfn);
178 count += BITS_PER_LONG;
179 order = ffs(BITS_PER_LONG) - 1;
180 __free_pages_bootmem(page, order);
181 i += BITS_PER_LONG;
182 page += BITS_PER_LONG;
183 } else if (v) {
184 unsigned long m;
185
186 page = pfn_to_page(pfn);
187 for (m = 1; m && i < idx; m<<=1, page++, i++) {
188 if (v & m) {
189 count++;
190 __free_pages_bootmem(page, 0);
191 }
192 }
193 } else {
194 i += BITS_PER_LONG;
195 }
196 pfn += BITS_PER_LONG;
197 }
198
199 /*
200 * Now free the allocator bitmap itself, it's not
201 * needed anymore:
202 */
203 page = virt_to_page(bdata->node_bootmem_map);
Johannes Weinerdf049a52008-07-23 21:28:02 -0700204 pages = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
205 idx = bootmem_bootmap_pages(pages);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700206 for (i = 0; i < idx; i++, page++)
207 __free_pages_bootmem(page, 0);
208 count += i;
209 bdata->node_bootmem_map = NULL;
210
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700211 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
212
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700213 return count;
214}
215
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700216/**
217 * free_all_bootmem_node - release a node's free pages to the buddy allocator
218 * @pgdat: node to be released
219 *
220 * Returns the number of pages actually released.
221 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700222unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
223{
224 register_page_bootmem_info_node(pgdat);
225 return free_all_bootmem_core(pgdat->bdata);
226}
227
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700228/**
229 * free_all_bootmem - release free pages to the buddy allocator
230 *
231 * Returns the number of pages actually released.
232 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700233unsigned long __init free_all_bootmem(void)
234{
235 return free_all_bootmem_core(NODE_DATA(0)->bdata);
236}
237
238static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
239 unsigned long size)
240{
241 unsigned long sidx, eidx;
242 unsigned long i;
243
244 BUG_ON(!size);
245
246 /* out range */
247 if (addr + size < bdata->node_boot_start ||
248 PFN_DOWN(addr) > bdata->node_low_pfn)
249 return;
250 /*
251 * round down end of usable mem, partially free pages are
252 * considered reserved.
253 */
254
255 if (addr >= bdata->node_boot_start && addr < bdata->last_success)
256 bdata->last_success = addr;
257
258 /*
259 * Round up to index to the range.
260 */
261 if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
262 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
263 else
264 sidx = 0;
265
266 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
267 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
268 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
269
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700270 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
271 sidx + PFN_DOWN(bdata->node_boot_start),
272 eidx + PFN_DOWN(bdata->node_boot_start));
273
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700274 for (i = sidx; i < eidx; i++) {
275 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
276 BUG();
277 }
278}
279
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700280/**
281 * free_bootmem_node - mark a page range as usable
282 * @pgdat: node the range resides on
283 * @physaddr: starting address of the range
284 * @size: size of the range in bytes
285 *
286 * Partial pages will be considered reserved and left as they are.
287 *
288 * Only physical pages that actually reside on @pgdat are marked.
289 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700290void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
291 unsigned long size)
292{
293 free_bootmem_core(pgdat->bdata, physaddr, size);
294}
295
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700296/**
297 * free_bootmem - mark a page range as usable
298 * @addr: starting address of the range
299 * @size: size of the range in bytes
300 *
301 * Partial pages will be considered reserved and left as they are.
302 *
303 * All physical pages within the range are marked, no matter what
304 * node they reside on.
305 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700306void __init free_bootmem(unsigned long addr, unsigned long size)
307{
308 bootmem_data_t *bdata;
309 list_for_each_entry(bdata, &bdata_list, list)
310 free_bootmem_core(bdata, addr, size);
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
314 * Marks a particular physical memory range as unallocatable. Usable RAM
315 * might be used for boot-time allocations - or it might get added
316 * to the free page pool later on.
317 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700318static int __init can_reserve_bootmem_core(bootmem_data_t *bdata,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800319 unsigned long addr, unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700321 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 unsigned long i;
Yinghai Lua5645a62008-03-18 12:49:12 -0700323
324 BUG_ON(!size);
325
326 /* out of range, don't hold other */
327 if (addr + size < bdata->node_boot_start ||
328 PFN_DOWN(addr) > bdata->node_low_pfn)
329 return 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
Yinghai Lua5645a62008-03-18 12:49:12 -0700332 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700334 if (addr > bdata->node_boot_start)
335 sidx= PFN_DOWN(addr - bdata->node_boot_start);
336 else
337 sidx = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700338
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700339 eidx = PFN_UP(addr + size - bdata->node_boot_start);
Yinghai Lua5645a62008-03-18 12:49:12 -0700340 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
341 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Yinghai Lua5645a62008-03-18 12:49:12 -0700343 for (i = sidx; i < eidx; i++) {
344 if (test_bit(i, bdata->node_bootmem_map)) {
345 if (flags & BOOTMEM_EXCLUSIVE)
346 return -EBUSY;
347 }
348 }
349
350 return 0;
351
352}
353
354static void __init reserve_bootmem_core(bootmem_data_t *bdata,
355 unsigned long addr, unsigned long size, int flags)
356{
357 unsigned long sidx, eidx;
358 unsigned long i;
359
360 BUG_ON(!size);
361
362 /* out of range */
363 if (addr + size < bdata->node_boot_start ||
364 PFN_DOWN(addr) > bdata->node_low_pfn)
365 return;
366
367 /*
368 * Round up to index to the range.
369 */
370 if (addr > bdata->node_boot_start)
371 sidx= PFN_DOWN(addr - bdata->node_boot_start);
372 else
373 sidx = 0;
374
375 eidx = PFN_UP(addr + size - bdata->node_boot_start);
376 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
377 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
378
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700379 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
380 bdata - bootmem_node_data,
381 sidx + PFN_DOWN(bdata->node_boot_start),
382 eidx + PFN_DOWN(bdata->node_boot_start),
383 flags);
384
385 for (i = sidx; i < eidx; i++)
386 if (test_and_set_bit(i, bdata->node_bootmem_map))
387 bdebug("hm, page %lx reserved twice.\n",
388 PFN_DOWN(bdata->node_boot_start) + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700391/**
392 * reserve_bootmem_node - mark a page range as reserved
393 * @pgdat: node the range resides on
394 * @physaddr: starting address of the range
395 * @size: size of the range in bytes
396 * @flags: reservation flags (see linux/bootmem.h)
397 *
398 * Partial pages will be reserved.
399 *
400 * Only physical pages that actually reside on @pgdat are marked.
401 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700402int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
403 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700405 int ret;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700406
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700407 ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
408 if (ret < 0)
409 return -ENOMEM;
410 reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700414#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700415/**
416 * reserve_bootmem - mark a page range as usable
417 * @addr: starting address of the range
418 * @size: size of the range in bytes
419 * @flags: reservation flags (see linux/bootmem.h)
420 *
421 * Partial pages will be reserved.
422 *
423 * All physical pages within the range are marked, no matter what
424 * node they reside on.
425 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700426int __init reserve_bootmem(unsigned long addr, unsigned long size,
427 int flags)
428{
429 bootmem_data_t *bdata;
430 int ret;
431
432 list_for_each_entry(bdata, &bdata_list, list) {
433 ret = can_reserve_bootmem_core(bdata, addr, size, flags);
434 if (ret < 0)
435 return ret;
436 }
437 list_for_each_entry(bdata, &bdata_list, list)
438 reserve_bootmem_core(bdata, addr, size, flags);
439
440 return 0;
441}
442#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
445 * We 'merge' subsequent allocations to save space. We might 'lose'
446 * some fraction of a page if allocations cannot be satisfied due to
447 * size constraints on boxes where there is physical RAM space
448 * fragmentation - in these cases (mostly large memory boxes) this
449 * is not a problem.
450 *
451 * On low memory boxes we get it right in 100% of the cases.
452 *
453 * alignment has to be a power of 2 value.
454 *
455 * NOTE: This function is _not_ reentrant.
456 */
Johannes Weinerffc64212008-07-23 21:26:59 -0700457static void * __init
458alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
459 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700461 unsigned long areasize, preferred;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700462 unsigned long i, start = 0, incr, eidx, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 void *ret;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700464 unsigned long node_boot_start;
465 void *node_bootmem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700467 if (!size) {
Johannes Weinerffc64212008-07-23 21:26:59 -0700468 printk("alloc_bootmem_core(): zero-sized request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 BUG();
470 }
471 BUG_ON(align & (align-1));
472
Christian Krafft7c309a62006-12-06 20:32:41 -0800473 /* on nodes without memory - bootmem_map is NULL */
474 if (!bdata->node_bootmem_map)
475 return NULL;
476
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700477 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
478 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
479 align, goal, limit);
480
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700481 /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
482 node_boot_start = bdata->node_boot_start;
483 node_bootmem_map = bdata->node_bootmem_map;
484 if (align) {
485 node_boot_start = ALIGN(bdata->node_boot_start, align);
486 if (node_boot_start > bdata->node_boot_start)
487 node_bootmem_map = (unsigned long *)bdata->node_bootmem_map +
488 PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG;
489 }
490
491 if (limit && node_boot_start >= limit)
492 return NULL;
493
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700494 end_pfn = bdata->node_low_pfn;
495 limit = PFN_DOWN(limit);
Yasunori Goto281dd252005-10-19 15:52:18 -0700496 if (limit && end_pfn > limit)
497 end_pfn = limit;
498
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700499 eidx = end_pfn - PFN_DOWN(node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /*
502 * We try to allocate bootmem pages above 'goal'
503 * first, then we try to allocate lower pages.
504 */
Yinghai Luad093152008-03-10 23:23:42 -0700505 preferred = 0;
506 if (goal && PFN_DOWN(goal) < end_pfn) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700507 if (goal > node_boot_start)
508 preferred = goal - node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700510 if (bdata->last_success > node_boot_start &&
511 bdata->last_success - node_boot_start >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700512 if (!limit || (limit && limit > bdata->last_success))
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700513 preferred = bdata->last_success - node_boot_start;
Yinghai Luad093152008-03-10 23:23:42 -0700514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700516 preferred = PFN_DOWN(ALIGN(preferred, align));
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700517 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 incr = align >> PAGE_SHIFT ? : 1;
519
520restart_scan:
Yinghai Luad093152008-03-10 23:23:42 -0700521 for (i = preferred; i < eidx;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 unsigned long j;
Yinghai Luad093152008-03-10 23:23:42 -0700523
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700524 i = find_next_zero_bit(node_bootmem_map, eidx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 i = ALIGN(i, incr);
Haren Myneni66d43e92005-12-12 00:37:39 -0800526 if (i >= eidx)
527 break;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700528 if (test_bit(i, node_bootmem_map)) {
Yinghai Luad093152008-03-10 23:23:42 -0700529 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 continue;
Yinghai Luad093152008-03-10 23:23:42 -0700531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 for (j = i + 1; j < i + areasize; ++j) {
533 if (j >= eidx)
534 goto fail_block;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700535 if (test_bit(j, node_bootmem_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto fail_block;
537 }
538 start = i;
539 goto found;
540 fail_block:
541 i = ALIGN(j, incr);
Yinghai Luad093152008-03-10 23:23:42 -0700542 if (i == j)
543 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700546 if (preferred > 0) {
547 preferred = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto restart_scan;
549 }
550 return NULL;
551
552found:
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700553 bdata->last_success = PFN_PHYS(start) + node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 BUG_ON(start >= eidx);
555
556 /*
557 * Is the next page of the previous allocation-end the start
558 * of this allocation's buffer? If yes then we can 'merge'
559 * the previous partial page with this allocation.
560 */
561 if (align < PAGE_SIZE &&
562 bdata->last_offset && bdata->last_pos+1 == start) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700563 unsigned long offset, remaining_size;
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700564 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 BUG_ON(offset > PAGE_SIZE);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700566 remaining_size = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (size < remaining_size) {
568 areasize = 0;
569 /* last_pos unchanged */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700570 bdata->last_offset = offset + size;
571 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700572 offset + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 } else {
574 remaining_size = size - remaining_size;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700575 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
576 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700577 offset + node_boot_start);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700578 bdata->last_pos = start + areasize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 bdata->last_offset = remaining_size;
580 }
581 bdata->last_offset &= ~PAGE_MASK;
582 } else {
583 bdata->last_pos = start + areasize - 1;
584 bdata->last_offset = size & ~PAGE_MASK;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700585 ret = phys_to_virt(start * PAGE_SIZE + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700588 bdebug("nid=%td start=%lx end=%lx\n",
589 bdata - bootmem_node_data,
590 start + PFN_DOWN(bdata->node_boot_start),
591 start + areasize + PFN_DOWN(bdata->node_boot_start));
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /*
594 * Reserve the area now:
595 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700596 for (i = start; i < start + areasize; i++)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700597 if (unlikely(test_and_set_bit(i, node_bootmem_map)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 BUG();
599 memset(ret, 0, size);
600 return ret;
601}
602
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700603/**
604 * __alloc_bootmem_nopanic - allocate boot memory without panicking
605 * @size: size of the request in bytes
606 * @align: alignment of the region
607 * @goal: preferred starting address of the region
608 *
609 * The goal is dropped if it can not be satisfied and the allocation will
610 * fall back to memory below @goal.
611 *
612 * Allocation may happen on any node in the system.
613 *
614 * Returns NULL on failure.
615 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700616void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
617 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800619 bootmem_data_t *bdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 void *ptr;
621
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700622 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weinerffc64212008-07-23 21:26:59 -0700623 ptr = alloc_bootmem_core(bdata, size, align, goal, 0);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700624 if (ptr)
625 return ptr;
626 }
Andi Kleena8062232006-04-07 19:49:21 +0200627 return NULL;
628}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700630/**
631 * __alloc_bootmem - allocate boot memory
632 * @size: size of the request in bytes
633 * @align: alignment of the region
634 * @goal: preferred starting address of the region
635 *
636 * The goal is dropped if it can not be satisfied and the allocation will
637 * fall back to memory below @goal.
638 *
639 * Allocation may happen on any node in the system.
640 *
641 * The function panics if the request can not be satisfied.
642 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700643void * __init __alloc_bootmem(unsigned long size, unsigned long align,
644 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200645{
646 void *mem = __alloc_bootmem_nopanic(size,align,goal);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700647
Andi Kleena8062232006-04-07 19:49:21 +0200648 if (mem)
649 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /*
651 * Whoops, we cannot satisfy the allocation request.
652 */
653 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
654 panic("Out of memory");
655 return NULL;
656}
657
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700658/**
659 * __alloc_bootmem_node - allocate boot memory from a specific node
660 * @pgdat: node to allocate from
661 * @size: size of the request in bytes
662 * @align: alignment of the region
663 * @goal: preferred starting address of the region
664 *
665 * The goal is dropped if it can not be satisfied and the allocation will
666 * fall back to memory below @goal.
667 *
668 * Allocation may fall back to any node in the system if the specified node
669 * can not hold the requested memory.
670 *
671 * The function panics if the request can not be satisfied.
672 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700673void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
674 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 void *ptr;
677
Johannes Weinerffc64212008-07-23 21:26:59 -0700678 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (ptr)
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700680 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800682 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700685#ifdef CONFIG_SPARSEMEM
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700686/**
687 * alloc_bootmem_section - allocate boot memory from a specific section
688 * @size: size of the request in bytes
689 * @section_nr: sparse map section to allocate from
690 *
691 * Return NULL on failure.
692 */
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700693void * __init alloc_bootmem_section(unsigned long size,
694 unsigned long section_nr)
695{
696 void *ptr;
697 unsigned long limit, goal, start_nr, end_nr, pfn;
698 struct pglist_data *pgdat;
699
700 pfn = section_nr_to_pfn(section_nr);
701 goal = PFN_PHYS(pfn);
702 limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1;
703 pgdat = NODE_DATA(early_pfn_to_nid(pfn));
Johannes Weinerffc64212008-07-23 21:26:59 -0700704 ptr = alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal,
705 limit);
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700706
707 if (!ptr)
708 return NULL;
709
710 start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr)));
711 end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size));
712 if (start_nr != section_nr || end_nr != section_nr) {
713 printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n",
714 section_nr);
715 free_bootmem_core(pgdat->bdata, __pa(ptr), size);
716 ptr = NULL;
717 }
718
719 return ptr;
720}
721#endif
722
Andi Kleenb54bbf72008-07-23 21:27:45 -0700723void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
724 unsigned long align, unsigned long goal)
725{
726 void *ptr;
727
728 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
729 if (ptr)
730 return ptr;
731
732 return __alloc_bootmem_nopanic(size, align, goal);
733}
734
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700735#ifndef ARCH_LOW_ADDRESS_LIMIT
736#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
737#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800738
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700739/**
740 * __alloc_bootmem_low - allocate low boot memory
741 * @size: size of the request in bytes
742 * @align: alignment of the region
743 * @goal: preferred starting address of the region
744 *
745 * The goal is dropped if it can not be satisfied and the allocation will
746 * fall back to memory below @goal.
747 *
748 * Allocation may happen on any node in the system.
749 *
750 * The function panics if the request can not be satisfied.
751 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700752void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
753 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800754{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800755 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800756 void *ptr;
757
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700758 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weinerffc64212008-07-23 21:26:59 -0700759 ptr = alloc_bootmem_core(bdata, size, align, goal,
760 ARCH_LOW_ADDRESS_LIMIT);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700761 if (ptr)
762 return ptr;
763 }
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800764
765 /*
766 * Whoops, we cannot satisfy the allocation request.
767 */
768 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
769 panic("Out of low memory");
770 return NULL;
771}
772
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700773/**
774 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
775 * @pgdat: node to allocate from
776 * @size: size of the request in bytes
777 * @align: alignment of the region
778 * @goal: preferred starting address of the region
779 *
780 * The goal is dropped if it can not be satisfied and the allocation will
781 * fall back to memory below @goal.
782 *
783 * Allocation may fall back to any node in the system if the specified node
784 * can not hold the requested memory.
785 *
786 * The function panics if the request can not be satisfied.
787 */
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800788void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
789 unsigned long align, unsigned long goal)
790{
Johannes Weinerffc64212008-07-23 21:26:59 -0700791 return alloc_bootmem_core(pgdat->bdata, size, align, goal,
792 ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800793}