blob: 4e085ee1d98e5721bb8e120d291aef55891eff2f [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 Weiner223e8dc2008-07-23 21:28:00 -070053/*
54 * Given an initialised bdata, it returns the size of the boot bitmap
55 */
56static unsigned long __init get_mapsize(bootmem_data_t *bdata)
57{
58 unsigned long mapsize;
59 unsigned long start = PFN_DOWN(bdata->node_boot_start);
60 unsigned long end = bdata->node_low_pfn;
61
62 mapsize = ((end - start) + 7) / 8;
63 return ALIGN(mapsize, sizeof(long));
64}
65
Johannes Weinera66fd7d2008-07-23 21:28:01 -070066/**
67 * bootmem_bootmap_pages - calculate bitmap size in pages
68 * @pages: number of pages the bitmap has to represent
69 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070070unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 unsigned long mapsize;
73
74 mapsize = (pages+7)/8;
75 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
76 mapsize >>= PAGE_SHIFT;
77
78 return mapsize;
79}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070080
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080081/*
82 * link bdata in order
83 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070084static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080085{
86 bootmem_data_t *ent;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070087
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080088 if (list_empty(&bdata_list)) {
89 list_add(&bdata->list, &bdata_list);
90 return;
91 }
92 /* insert in order */
93 list_for_each_entry(ent, &bdata_list, list) {
94 if (bdata->node_boot_start < ent->node_boot_start) {
95 list_add_tail(&bdata->list, &ent->list);
96 return;
97 }
98 }
99 list_add_tail(&bdata->list, &bdata_list);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800100}
101
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700102/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * Called once to set up the allocator itself.
104 */
Johannes Weiner8ae04462008-07-23 21:26:59 -0700105static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned long mapstart, unsigned long start, unsigned long end)
107{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700108 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700110 mminit_validate_memmodel_limits(&start, &end);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700111 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
112 bdata->node_boot_start = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800114 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 /*
117 * Initially all pages are reserved - setup_arch() has to
118 * register free RAM areas explicitly.
119 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700120 mapsize = get_mapsize(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 memset(bdata->node_bootmem_map, 0xff, mapsize);
122
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700123 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
124 bdata - bootmem_node_data, start, mapstart, end, mapsize);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return mapsize;
127}
128
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700129/**
130 * init_bootmem_node - register a node as boot memory
131 * @pgdat: node to register
132 * @freepfn: pfn where the bitmap for this node is to be placed
133 * @startpfn: first pfn on the node
134 * @endpfn: first pfn after the node
135 *
136 * Returns the number of bytes needed to hold the bitmap for this node.
137 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700138unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
139 unsigned long startpfn, unsigned long endpfn)
140{
141 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
142}
143
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700144/**
145 * init_bootmem - register boot memory
146 * @start: pfn where the bitmap is to be placed
147 * @pages: number of available physical pages
148 *
149 * Returns the number of bytes needed to hold the bitmap.
150 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700151unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
152{
153 max_low_pfn = pages;
154 min_low_pfn = start;
155 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
156}
157
158static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
159{
160 struct page *page;
161 unsigned long pfn;
162 unsigned long i, count;
163 unsigned long idx;
164 unsigned long *map;
165 int gofast = 0;
166
167 BUG_ON(!bdata->node_bootmem_map);
168
169 count = 0;
170 /* first extant page of the node */
171 pfn = PFN_DOWN(bdata->node_boot_start);
172 idx = bdata->node_low_pfn - pfn;
173 map = bdata->node_bootmem_map;
174 /*
175 * Check if we are aligned to BITS_PER_LONG pages. If so, we might
176 * be able to free page orders of that size at once.
177 */
178 if (!(pfn & (BITS_PER_LONG-1)))
179 gofast = 1;
180
181 for (i = 0; i < idx; ) {
182 unsigned long v = ~map[i / BITS_PER_LONG];
183
184 if (gofast && v == ~0UL) {
185 int order;
186
187 page = pfn_to_page(pfn);
188 count += BITS_PER_LONG;
189 order = ffs(BITS_PER_LONG) - 1;
190 __free_pages_bootmem(page, order);
191 i += BITS_PER_LONG;
192 page += BITS_PER_LONG;
193 } else if (v) {
194 unsigned long m;
195
196 page = pfn_to_page(pfn);
197 for (m = 1; m && i < idx; m<<=1, page++, i++) {
198 if (v & m) {
199 count++;
200 __free_pages_bootmem(page, 0);
201 }
202 }
203 } else {
204 i += BITS_PER_LONG;
205 }
206 pfn += BITS_PER_LONG;
207 }
208
209 /*
210 * Now free the allocator bitmap itself, it's not
211 * needed anymore:
212 */
213 page = virt_to_page(bdata->node_bootmem_map);
214 idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
215 for (i = 0; i < idx; i++, page++)
216 __free_pages_bootmem(page, 0);
217 count += i;
218 bdata->node_bootmem_map = NULL;
219
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700220 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
221
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700222 return count;
223}
224
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700225/**
226 * free_all_bootmem_node - release a node's free pages to the buddy allocator
227 * @pgdat: node to be released
228 *
229 * Returns the number of pages actually released.
230 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700231unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
232{
233 register_page_bootmem_info_node(pgdat);
234 return free_all_bootmem_core(pgdat->bdata);
235}
236
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700237/**
238 * free_all_bootmem - release free pages to the buddy allocator
239 *
240 * Returns the number of pages actually released.
241 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700242unsigned long __init free_all_bootmem(void)
243{
244 return free_all_bootmem_core(NODE_DATA(0)->bdata);
245}
246
247static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
248 unsigned long size)
249{
250 unsigned long sidx, eidx;
251 unsigned long i;
252
253 BUG_ON(!size);
254
255 /* out range */
256 if (addr + size < bdata->node_boot_start ||
257 PFN_DOWN(addr) > bdata->node_low_pfn)
258 return;
259 /*
260 * round down end of usable mem, partially free pages are
261 * considered reserved.
262 */
263
264 if (addr >= bdata->node_boot_start && addr < bdata->last_success)
265 bdata->last_success = addr;
266
267 /*
268 * Round up to index to the range.
269 */
270 if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
271 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
272 else
273 sidx = 0;
274
275 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
276 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
277 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
278
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700279 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
280 sidx + PFN_DOWN(bdata->node_boot_start),
281 eidx + PFN_DOWN(bdata->node_boot_start));
282
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700283 for (i = sidx; i < eidx; i++) {
284 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
285 BUG();
286 }
287}
288
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700289/**
290 * free_bootmem_node - mark a page range as usable
291 * @pgdat: node the range resides on
292 * @physaddr: starting address of the range
293 * @size: size of the range in bytes
294 *
295 * Partial pages will be considered reserved and left as they are.
296 *
297 * Only physical pages that actually reside on @pgdat are marked.
298 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700299void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
300 unsigned long size)
301{
302 free_bootmem_core(pgdat->bdata, physaddr, size);
303}
304
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700305/**
306 * free_bootmem - mark a page range as usable
307 * @addr: starting address of the range
308 * @size: size of the range in bytes
309 *
310 * Partial pages will be considered reserved and left as they are.
311 *
312 * All physical pages within the range are marked, no matter what
313 * node they reside on.
314 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700315void __init free_bootmem(unsigned long addr, unsigned long size)
316{
317 bootmem_data_t *bdata;
318 list_for_each_entry(bdata, &bdata_list, list)
319 free_bootmem_core(bdata, addr, size);
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/*
323 * Marks a particular physical memory range as unallocatable. Usable RAM
324 * might be used for boot-time allocations - or it might get added
325 * to the free page pool later on.
326 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700327static int __init can_reserve_bootmem_core(bootmem_data_t *bdata,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800328 unsigned long addr, unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700330 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 unsigned long i;
Yinghai Lua5645a62008-03-18 12:49:12 -0700332
333 BUG_ON(!size);
334
335 /* out of range, don't hold other */
336 if (addr + size < bdata->node_boot_start ||
337 PFN_DOWN(addr) > bdata->node_low_pfn)
338 return 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /*
Yinghai Lua5645a62008-03-18 12:49:12 -0700341 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700343 if (addr > bdata->node_boot_start)
344 sidx= PFN_DOWN(addr - bdata->node_boot_start);
345 else
346 sidx = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700347
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700348 eidx = PFN_UP(addr + size - bdata->node_boot_start);
Yinghai Lua5645a62008-03-18 12:49:12 -0700349 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
350 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Yinghai Lua5645a62008-03-18 12:49:12 -0700352 for (i = sidx; i < eidx; i++) {
353 if (test_bit(i, bdata->node_bootmem_map)) {
354 if (flags & BOOTMEM_EXCLUSIVE)
355 return -EBUSY;
356 }
357 }
358
359 return 0;
360
361}
362
363static void __init reserve_bootmem_core(bootmem_data_t *bdata,
364 unsigned long addr, unsigned long size, int flags)
365{
366 unsigned long sidx, eidx;
367 unsigned long i;
368
369 BUG_ON(!size);
370
371 /* out of range */
372 if (addr + size < bdata->node_boot_start ||
373 PFN_DOWN(addr) > bdata->node_low_pfn)
374 return;
375
376 /*
377 * Round up to index to the range.
378 */
379 if (addr > bdata->node_boot_start)
380 sidx= PFN_DOWN(addr - bdata->node_boot_start);
381 else
382 sidx = 0;
383
384 eidx = PFN_UP(addr + size - bdata->node_boot_start);
385 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
386 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
387
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700388 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
389 bdata - bootmem_node_data,
390 sidx + PFN_DOWN(bdata->node_boot_start),
391 eidx + PFN_DOWN(bdata->node_boot_start),
392 flags);
393
394 for (i = sidx; i < eidx; i++)
395 if (test_and_set_bit(i, bdata->node_bootmem_map))
396 bdebug("hm, page %lx reserved twice.\n",
397 PFN_DOWN(bdata->node_boot_start) + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700400/**
401 * reserve_bootmem_node - mark a page range as reserved
402 * @pgdat: node the range resides on
403 * @physaddr: starting address of the range
404 * @size: size of the range in bytes
405 * @flags: reservation flags (see linux/bootmem.h)
406 *
407 * Partial pages will be reserved.
408 *
409 * Only physical pages that actually reside on @pgdat are marked.
410 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700411int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
412 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700414 int ret;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700415
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700416 ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
417 if (ret < 0)
418 return -ENOMEM;
419 reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700423#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700424/**
425 * reserve_bootmem - mark a page range as usable
426 * @addr: starting address of the range
427 * @size: size of the range in bytes
428 * @flags: reservation flags (see linux/bootmem.h)
429 *
430 * Partial pages will be reserved.
431 *
432 * All physical pages within the range are marked, no matter what
433 * node they reside on.
434 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700435int __init reserve_bootmem(unsigned long addr, unsigned long size,
436 int flags)
437{
438 bootmem_data_t *bdata;
439 int ret;
440
441 list_for_each_entry(bdata, &bdata_list, list) {
442 ret = can_reserve_bootmem_core(bdata, addr, size, flags);
443 if (ret < 0)
444 return ret;
445 }
446 list_for_each_entry(bdata, &bdata_list, list)
447 reserve_bootmem_core(bdata, addr, size, flags);
448
449 return 0;
450}
451#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/*
454 * We 'merge' subsequent allocations to save space. We might 'lose'
455 * some fraction of a page if allocations cannot be satisfied due to
456 * size constraints on boxes where there is physical RAM space
457 * fragmentation - in these cases (mostly large memory boxes) this
458 * is not a problem.
459 *
460 * On low memory boxes we get it right in 100% of the cases.
461 *
462 * alignment has to be a power of 2 value.
463 *
464 * NOTE: This function is _not_ reentrant.
465 */
Johannes Weinerffc64212008-07-23 21:26:59 -0700466static void * __init
467alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
468 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700470 unsigned long areasize, preferred;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700471 unsigned long i, start = 0, incr, eidx, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 void *ret;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700473 unsigned long node_boot_start;
474 void *node_bootmem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700476 if (!size) {
Johannes Weinerffc64212008-07-23 21:26:59 -0700477 printk("alloc_bootmem_core(): zero-sized request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 BUG();
479 }
480 BUG_ON(align & (align-1));
481
Christian Krafft7c309a62006-12-06 20:32:41 -0800482 /* on nodes without memory - bootmem_map is NULL */
483 if (!bdata->node_bootmem_map)
484 return NULL;
485
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700486 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
487 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
488 align, goal, limit);
489
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700490 /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
491 node_boot_start = bdata->node_boot_start;
492 node_bootmem_map = bdata->node_bootmem_map;
493 if (align) {
494 node_boot_start = ALIGN(bdata->node_boot_start, align);
495 if (node_boot_start > bdata->node_boot_start)
496 node_bootmem_map = (unsigned long *)bdata->node_bootmem_map +
497 PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG;
498 }
499
500 if (limit && node_boot_start >= limit)
501 return NULL;
502
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700503 end_pfn = bdata->node_low_pfn;
504 limit = PFN_DOWN(limit);
Yasunori Goto281dd252005-10-19 15:52:18 -0700505 if (limit && end_pfn > limit)
506 end_pfn = limit;
507
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700508 eidx = end_pfn - PFN_DOWN(node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /*
511 * We try to allocate bootmem pages above 'goal'
512 * first, then we try to allocate lower pages.
513 */
Yinghai Luad093152008-03-10 23:23:42 -0700514 preferred = 0;
515 if (goal && PFN_DOWN(goal) < end_pfn) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700516 if (goal > node_boot_start)
517 preferred = goal - node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700519 if (bdata->last_success > node_boot_start &&
520 bdata->last_success - node_boot_start >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700521 if (!limit || (limit && limit > bdata->last_success))
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700522 preferred = bdata->last_success - node_boot_start;
Yinghai Luad093152008-03-10 23:23:42 -0700523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700525 preferred = PFN_DOWN(ALIGN(preferred, align));
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700526 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 incr = align >> PAGE_SHIFT ? : 1;
528
529restart_scan:
Yinghai Luad093152008-03-10 23:23:42 -0700530 for (i = preferred; i < eidx;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 unsigned long j;
Yinghai Luad093152008-03-10 23:23:42 -0700532
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700533 i = find_next_zero_bit(node_bootmem_map, eidx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 i = ALIGN(i, incr);
Haren Myneni66d43e92005-12-12 00:37:39 -0800535 if (i >= eidx)
536 break;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700537 if (test_bit(i, node_bootmem_map)) {
Yinghai Luad093152008-03-10 23:23:42 -0700538 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 continue;
Yinghai Luad093152008-03-10 23:23:42 -0700540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 for (j = i + 1; j < i + areasize; ++j) {
542 if (j >= eidx)
543 goto fail_block;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700544 if (test_bit(j, node_bootmem_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto fail_block;
546 }
547 start = i;
548 goto found;
549 fail_block:
550 i = ALIGN(j, incr);
Yinghai Luad093152008-03-10 23:23:42 -0700551 if (i == j)
552 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700555 if (preferred > 0) {
556 preferred = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto restart_scan;
558 }
559 return NULL;
560
561found:
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700562 bdata->last_success = PFN_PHYS(start) + node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 BUG_ON(start >= eidx);
564
565 /*
566 * Is the next page of the previous allocation-end the start
567 * of this allocation's buffer? If yes then we can 'merge'
568 * the previous partial page with this allocation.
569 */
570 if (align < PAGE_SIZE &&
571 bdata->last_offset && bdata->last_pos+1 == start) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700572 unsigned long offset, remaining_size;
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700573 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 BUG_ON(offset > PAGE_SIZE);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700575 remaining_size = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (size < remaining_size) {
577 areasize = 0;
578 /* last_pos unchanged */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700579 bdata->last_offset = offset + size;
580 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700581 offset + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 } else {
583 remaining_size = size - remaining_size;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700584 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
585 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700586 offset + node_boot_start);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700587 bdata->last_pos = start + areasize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 bdata->last_offset = remaining_size;
589 }
590 bdata->last_offset &= ~PAGE_MASK;
591 } else {
592 bdata->last_pos = start + areasize - 1;
593 bdata->last_offset = size & ~PAGE_MASK;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700594 ret = phys_to_virt(start * PAGE_SIZE + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700597 bdebug("nid=%td start=%lx end=%lx\n",
598 bdata - bootmem_node_data,
599 start + PFN_DOWN(bdata->node_boot_start),
600 start + areasize + PFN_DOWN(bdata->node_boot_start));
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /*
603 * Reserve the area now:
604 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700605 for (i = start; i < start + areasize; i++)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700606 if (unlikely(test_and_set_bit(i, node_bootmem_map)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 BUG();
608 memset(ret, 0, size);
609 return ret;
610}
611
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700612/**
613 * __alloc_bootmem_nopanic - allocate boot memory without panicking
614 * @size: size of the request in bytes
615 * @align: alignment of the region
616 * @goal: preferred starting address of the region
617 *
618 * The goal is dropped if it can not be satisfied and the allocation will
619 * fall back to memory below @goal.
620 *
621 * Allocation may happen on any node in the system.
622 *
623 * Returns NULL on failure.
624 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700625void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
626 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800628 bootmem_data_t *bdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 void *ptr;
630
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700631 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weinerffc64212008-07-23 21:26:59 -0700632 ptr = alloc_bootmem_core(bdata, size, align, goal, 0);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700633 if (ptr)
634 return ptr;
635 }
Andi Kleena8062232006-04-07 19:49:21 +0200636 return NULL;
637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700639/**
640 * __alloc_bootmem - allocate boot memory
641 * @size: size of the request in bytes
642 * @align: alignment of the region
643 * @goal: preferred starting address of the region
644 *
645 * The goal is dropped if it can not be satisfied and the allocation will
646 * fall back to memory below @goal.
647 *
648 * Allocation may happen on any node in the system.
649 *
650 * The function panics if the request can not be satisfied.
651 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700652void * __init __alloc_bootmem(unsigned long size, unsigned long align,
653 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200654{
655 void *mem = __alloc_bootmem_nopanic(size,align,goal);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700656
Andi Kleena8062232006-04-07 19:49:21 +0200657 if (mem)
658 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /*
660 * Whoops, we cannot satisfy the allocation request.
661 */
662 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
663 panic("Out of memory");
664 return NULL;
665}
666
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700667/**
668 * __alloc_bootmem_node - allocate boot memory from a specific node
669 * @pgdat: node to allocate from
670 * @size: size of the request in bytes
671 * @align: alignment of the region
672 * @goal: preferred starting address of the region
673 *
674 * The goal is dropped if it can not be satisfied and the allocation will
675 * fall back to memory below @goal.
676 *
677 * Allocation may fall back to any node in the system if the specified node
678 * can not hold the requested memory.
679 *
680 * The function panics if the request can not be satisfied.
681 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700682void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
683 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 void *ptr;
686
Johannes Weinerffc64212008-07-23 21:26:59 -0700687 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (ptr)
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700689 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800691 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700694#ifdef CONFIG_SPARSEMEM
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700695/**
696 * alloc_bootmem_section - allocate boot memory from a specific section
697 * @size: size of the request in bytes
698 * @section_nr: sparse map section to allocate from
699 *
700 * Return NULL on failure.
701 */
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700702void * __init alloc_bootmem_section(unsigned long size,
703 unsigned long section_nr)
704{
705 void *ptr;
706 unsigned long limit, goal, start_nr, end_nr, pfn;
707 struct pglist_data *pgdat;
708
709 pfn = section_nr_to_pfn(section_nr);
710 goal = PFN_PHYS(pfn);
711 limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1;
712 pgdat = NODE_DATA(early_pfn_to_nid(pfn));
Johannes Weinerffc64212008-07-23 21:26:59 -0700713 ptr = alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal,
714 limit);
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700715
716 if (!ptr)
717 return NULL;
718
719 start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr)));
720 end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size));
721 if (start_nr != section_nr || end_nr != section_nr) {
722 printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n",
723 section_nr);
724 free_bootmem_core(pgdat->bdata, __pa(ptr), size);
725 ptr = NULL;
726 }
727
728 return ptr;
729}
730#endif
731
Andi Kleenb54bbf72008-07-23 21:27:45 -0700732void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
733 unsigned long align, unsigned long goal)
734{
735 void *ptr;
736
737 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
738 if (ptr)
739 return ptr;
740
741 return __alloc_bootmem_nopanic(size, align, goal);
742}
743
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700744#ifndef ARCH_LOW_ADDRESS_LIMIT
745#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
746#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800747
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700748/**
749 * __alloc_bootmem_low - allocate low boot memory
750 * @size: size of the request in bytes
751 * @align: alignment of the region
752 * @goal: preferred starting address of the region
753 *
754 * The goal is dropped if it can not be satisfied and the allocation will
755 * fall back to memory below @goal.
756 *
757 * Allocation may happen on any node in the system.
758 *
759 * The function panics if the request can not be satisfied.
760 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700761void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
762 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800763{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800764 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800765 void *ptr;
766
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700767 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weinerffc64212008-07-23 21:26:59 -0700768 ptr = alloc_bootmem_core(bdata, size, align, goal,
769 ARCH_LOW_ADDRESS_LIMIT);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700770 if (ptr)
771 return ptr;
772 }
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800773
774 /*
775 * Whoops, we cannot satisfy the allocation request.
776 */
777 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
778 panic("Out of low memory");
779 return NULL;
780}
781
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700782/**
783 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
784 * @pgdat: node to allocate from
785 * @size: size of the request in bytes
786 * @align: alignment of the region
787 * @goal: preferred starting address of the region
788 *
789 * The goal is dropped if it can not be satisfied and the allocation will
790 * fall back to memory below @goal.
791 *
792 * Allocation may fall back to any node in the system if the specified node
793 * can not hold the requested memory.
794 *
795 * The function panics if the request can not be satisfied.
796 */
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800797void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
798 unsigned long align, unsigned long goal)
799{
Johannes Weinerffc64212008-07-23 21:26:59 -0700800 return alloc_bootmem_core(pgdat->bdata, size, align, goal,
801 ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800802}