blob: 105ad4cff2e1ef01e69cd6d088833c4a4176b3b4 [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 Weiner223e8dc2008-07-23 21:28:00 -070037/*
38 * Given an initialised bdata, it returns the size of the boot bitmap
39 */
40static unsigned long __init get_mapsize(bootmem_data_t *bdata)
41{
42 unsigned long mapsize;
43 unsigned long start = PFN_DOWN(bdata->node_boot_start);
44 unsigned long end = bdata->node_low_pfn;
45
46 mapsize = ((end - start) + 7) / 8;
47 return ALIGN(mapsize, sizeof(long));
48}
49
Johannes Weinera66fd7d2008-07-23 21:28:01 -070050/**
51 * bootmem_bootmap_pages - calculate bitmap size in pages
52 * @pages: number of pages the bitmap has to represent
53 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070054unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 unsigned long mapsize;
57
58 mapsize = (pages+7)/8;
59 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
60 mapsize >>= PAGE_SHIFT;
61
62 return mapsize;
63}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070064
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080065/*
66 * link bdata in order
67 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070068static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080069{
70 bootmem_data_t *ent;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070071
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080072 if (list_empty(&bdata_list)) {
73 list_add(&bdata->list, &bdata_list);
74 return;
75 }
76 /* insert in order */
77 list_for_each_entry(ent, &bdata_list, list) {
78 if (bdata->node_boot_start < ent->node_boot_start) {
79 list_add_tail(&bdata->list, &ent->list);
80 return;
81 }
82 }
83 list_add_tail(&bdata->list, &bdata_list);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080084}
85
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070086/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Called once to set up the allocator itself.
88 */
Johannes Weiner8ae04462008-07-23 21:26:59 -070089static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned long mapstart, unsigned long start, unsigned long end)
91{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070092 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Mel Gorman2dbb51c2008-07-23 21:26:52 -070094 mminit_validate_memmodel_limits(&start, &end);
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
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700110/**
111 * init_bootmem_node - register a node as boot memory
112 * @pgdat: node to register
113 * @freepfn: pfn where the bitmap for this node is to be placed
114 * @startpfn: first pfn on the node
115 * @endpfn: first pfn after the node
116 *
117 * Returns the number of bytes needed to hold the bitmap for this node.
118 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700119unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
120 unsigned long startpfn, unsigned long endpfn)
121{
122 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
123}
124
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700125/**
126 * init_bootmem - register boot memory
127 * @start: pfn where the bitmap is to be placed
128 * @pages: number of available physical pages
129 *
130 * Returns the number of bytes needed to hold the bitmap.
131 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700132unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
133{
134 max_low_pfn = pages;
135 min_low_pfn = start;
136 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
137}
138
139static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
140{
141 struct page *page;
142 unsigned long pfn;
143 unsigned long i, count;
144 unsigned long idx;
145 unsigned long *map;
146 int gofast = 0;
147
148 BUG_ON(!bdata->node_bootmem_map);
149
150 count = 0;
151 /* first extant page of the node */
152 pfn = PFN_DOWN(bdata->node_boot_start);
153 idx = bdata->node_low_pfn - pfn;
154 map = bdata->node_bootmem_map;
155 /*
156 * Check if we are aligned to BITS_PER_LONG pages. If so, we might
157 * be able to free page orders of that size at once.
158 */
159 if (!(pfn & (BITS_PER_LONG-1)))
160 gofast = 1;
161
162 for (i = 0; i < idx; ) {
163 unsigned long v = ~map[i / BITS_PER_LONG];
164
165 if (gofast && v == ~0UL) {
166 int order;
167
168 page = pfn_to_page(pfn);
169 count += BITS_PER_LONG;
170 order = ffs(BITS_PER_LONG) - 1;
171 __free_pages_bootmem(page, order);
172 i += BITS_PER_LONG;
173 page += BITS_PER_LONG;
174 } else if (v) {
175 unsigned long m;
176
177 page = pfn_to_page(pfn);
178 for (m = 1; m && i < idx; m<<=1, page++, i++) {
179 if (v & m) {
180 count++;
181 __free_pages_bootmem(page, 0);
182 }
183 }
184 } else {
185 i += BITS_PER_LONG;
186 }
187 pfn += BITS_PER_LONG;
188 }
189
190 /*
191 * Now free the allocator bitmap itself, it's not
192 * needed anymore:
193 */
194 page = virt_to_page(bdata->node_bootmem_map);
195 idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
196 for (i = 0; i < idx; i++, page++)
197 __free_pages_bootmem(page, 0);
198 count += i;
199 bdata->node_bootmem_map = NULL;
200
201 return count;
202}
203
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700204/**
205 * free_all_bootmem_node - release a node's free pages to the buddy allocator
206 * @pgdat: node to be released
207 *
208 * Returns the number of pages actually released.
209 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700210unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
211{
212 register_page_bootmem_info_node(pgdat);
213 return free_all_bootmem_core(pgdat->bdata);
214}
215
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700216/**
217 * free_all_bootmem - release free pages to the buddy allocator
218 *
219 * Returns the number of pages actually released.
220 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700221unsigned long __init free_all_bootmem(void)
222{
223 return free_all_bootmem_core(NODE_DATA(0)->bdata);
224}
225
226static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
227 unsigned long size)
228{
229 unsigned long sidx, eidx;
230 unsigned long i;
231
232 BUG_ON(!size);
233
234 /* out range */
235 if (addr + size < bdata->node_boot_start ||
236 PFN_DOWN(addr) > bdata->node_low_pfn)
237 return;
238 /*
239 * round down end of usable mem, partially free pages are
240 * considered reserved.
241 */
242
243 if (addr >= bdata->node_boot_start && addr < bdata->last_success)
244 bdata->last_success = addr;
245
246 /*
247 * Round up to index to the range.
248 */
249 if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
250 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
251 else
252 sidx = 0;
253
254 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
255 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
256 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
257
258 for (i = sidx; i < eidx; i++) {
259 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
260 BUG();
261 }
262}
263
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700264/**
265 * free_bootmem_node - mark a page range as usable
266 * @pgdat: node the range resides on
267 * @physaddr: starting address of the range
268 * @size: size of the range in bytes
269 *
270 * Partial pages will be considered reserved and left as they are.
271 *
272 * Only physical pages that actually reside on @pgdat are marked.
273 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700274void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
275 unsigned long size)
276{
277 free_bootmem_core(pgdat->bdata, physaddr, size);
278}
279
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700280/**
281 * free_bootmem - mark a page range as usable
282 * @addr: starting address of the range
283 * @size: size of the range in bytes
284 *
285 * Partial pages will be considered reserved and left as they are.
286 *
287 * All physical pages within the range are marked, no matter what
288 * node they reside on.
289 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700290void __init free_bootmem(unsigned long addr, unsigned long size)
291{
292 bootmem_data_t *bdata;
293 list_for_each_entry(bdata, &bdata_list, list)
294 free_bootmem_core(bdata, addr, size);
295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
298 * Marks a particular physical memory range as unallocatable. Usable RAM
299 * might be used for boot-time allocations - or it might get added
300 * to the free page pool later on.
301 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700302static int __init can_reserve_bootmem_core(bootmem_data_t *bdata,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800303 unsigned long addr, unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700305 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 unsigned long i;
Yinghai Lua5645a62008-03-18 12:49:12 -0700307
308 BUG_ON(!size);
309
310 /* out of range, don't hold other */
311 if (addr + size < bdata->node_boot_start ||
312 PFN_DOWN(addr) > bdata->node_low_pfn)
313 return 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
Yinghai Lua5645a62008-03-18 12:49:12 -0700316 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700318 if (addr > bdata->node_boot_start)
319 sidx= PFN_DOWN(addr - bdata->node_boot_start);
320 else
321 sidx = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700322
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700323 eidx = PFN_UP(addr + size - bdata->node_boot_start);
Yinghai Lua5645a62008-03-18 12:49:12 -0700324 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
325 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Yinghai Lua5645a62008-03-18 12:49:12 -0700327 for (i = sidx; i < eidx; i++) {
328 if (test_bit(i, bdata->node_bootmem_map)) {
329 if (flags & BOOTMEM_EXCLUSIVE)
330 return -EBUSY;
331 }
332 }
333
334 return 0;
335
336}
337
338static void __init reserve_bootmem_core(bootmem_data_t *bdata,
339 unsigned long addr, unsigned long size, int flags)
340{
341 unsigned long sidx, eidx;
342 unsigned long i;
343
344 BUG_ON(!size);
345
346 /* out of range */
347 if (addr + size < bdata->node_boot_start ||
348 PFN_DOWN(addr) > bdata->node_low_pfn)
349 return;
350
351 /*
352 * Round up to index to the range.
353 */
354 if (addr > bdata->node_boot_start)
355 sidx= PFN_DOWN(addr - bdata->node_boot_start);
356 else
357 sidx = 0;
358
359 eidx = PFN_UP(addr + size - bdata->node_boot_start);
360 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
361 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
362
363 for (i = sidx; i < eidx; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
365#ifdef CONFIG_DEBUG_BOOTMEM
366 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
367#endif
368 }
Yinghai Lua5645a62008-03-18 12:49:12 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700372/**
373 * reserve_bootmem_node - mark a page range as reserved
374 * @pgdat: node the range resides on
375 * @physaddr: starting address of the range
376 * @size: size of the range in bytes
377 * @flags: reservation flags (see linux/bootmem.h)
378 *
379 * Partial pages will be reserved.
380 *
381 * Only physical pages that actually reside on @pgdat are marked.
382 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700383int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
384 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700386 int ret;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700387
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700388 ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
389 if (ret < 0)
390 return -ENOMEM;
391 reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700395#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700396/**
397 * reserve_bootmem - mark a page range as usable
398 * @addr: starting address of the range
399 * @size: size of the range in bytes
400 * @flags: reservation flags (see linux/bootmem.h)
401 *
402 * Partial pages will be reserved.
403 *
404 * All physical pages within the range are marked, no matter what
405 * node they reside on.
406 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700407int __init reserve_bootmem(unsigned long addr, unsigned long size,
408 int flags)
409{
410 bootmem_data_t *bdata;
411 int ret;
412
413 list_for_each_entry(bdata, &bdata_list, list) {
414 ret = can_reserve_bootmem_core(bdata, addr, size, flags);
415 if (ret < 0)
416 return ret;
417 }
418 list_for_each_entry(bdata, &bdata_list, list)
419 reserve_bootmem_core(bdata, addr, size, flags);
420
421 return 0;
422}
423#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/*
426 * We 'merge' subsequent allocations to save space. We might 'lose'
427 * some fraction of a page if allocations cannot be satisfied due to
428 * size constraints on boxes where there is physical RAM space
429 * fragmentation - in these cases (mostly large memory boxes) this
430 * is not a problem.
431 *
432 * On low memory boxes we get it right in 100% of the cases.
433 *
434 * alignment has to be a power of 2 value.
435 *
436 * NOTE: This function is _not_ reentrant.
437 */
Johannes Weinerffc64212008-07-23 21:26:59 -0700438static void * __init
439alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
440 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700442 unsigned long areasize, preferred;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700443 unsigned long i, start = 0, incr, eidx, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 void *ret;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700445 unsigned long node_boot_start;
446 void *node_bootmem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700448 if (!size) {
Johannes Weinerffc64212008-07-23 21:26:59 -0700449 printk("alloc_bootmem_core(): zero-sized request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 BUG();
451 }
452 BUG_ON(align & (align-1));
453
Christian Krafft7c309a62006-12-06 20:32:41 -0800454 /* on nodes without memory - bootmem_map is NULL */
455 if (!bdata->node_bootmem_map)
456 return NULL;
457
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700458 /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
459 node_boot_start = bdata->node_boot_start;
460 node_bootmem_map = bdata->node_bootmem_map;
461 if (align) {
462 node_boot_start = ALIGN(bdata->node_boot_start, align);
463 if (node_boot_start > bdata->node_boot_start)
464 node_bootmem_map = (unsigned long *)bdata->node_bootmem_map +
465 PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG;
466 }
467
468 if (limit && node_boot_start >= limit)
469 return NULL;
470
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700471 end_pfn = bdata->node_low_pfn;
472 limit = PFN_DOWN(limit);
Yasunori Goto281dd252005-10-19 15:52:18 -0700473 if (limit && end_pfn > limit)
474 end_pfn = limit;
475
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700476 eidx = end_pfn - PFN_DOWN(node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /*
479 * We try to allocate bootmem pages above 'goal'
480 * first, then we try to allocate lower pages.
481 */
Yinghai Luad093152008-03-10 23:23:42 -0700482 preferred = 0;
483 if (goal && PFN_DOWN(goal) < end_pfn) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700484 if (goal > node_boot_start)
485 preferred = goal - node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700487 if (bdata->last_success > node_boot_start &&
488 bdata->last_success - node_boot_start >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700489 if (!limit || (limit && limit > bdata->last_success))
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700490 preferred = bdata->last_success - node_boot_start;
Yinghai Luad093152008-03-10 23:23:42 -0700491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700493 preferred = PFN_DOWN(ALIGN(preferred, align));
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700494 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 incr = align >> PAGE_SHIFT ? : 1;
496
497restart_scan:
Yinghai Luad093152008-03-10 23:23:42 -0700498 for (i = preferred; i < eidx;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 unsigned long j;
Yinghai Luad093152008-03-10 23:23:42 -0700500
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700501 i = find_next_zero_bit(node_bootmem_map, eidx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 i = ALIGN(i, incr);
Haren Myneni66d43e92005-12-12 00:37:39 -0800503 if (i >= eidx)
504 break;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700505 if (test_bit(i, node_bootmem_map)) {
Yinghai Luad093152008-03-10 23:23:42 -0700506 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 continue;
Yinghai Luad093152008-03-10 23:23:42 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 for (j = i + 1; j < i + areasize; ++j) {
510 if (j >= eidx)
511 goto fail_block;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700512 if (test_bit(j, node_bootmem_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto fail_block;
514 }
515 start = i;
516 goto found;
517 fail_block:
518 i = ALIGN(j, incr);
Yinghai Luad093152008-03-10 23:23:42 -0700519 if (i == j)
520 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700523 if (preferred > 0) {
524 preferred = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto restart_scan;
526 }
527 return NULL;
528
529found:
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700530 bdata->last_success = PFN_PHYS(start) + node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 BUG_ON(start >= eidx);
532
533 /*
534 * Is the next page of the previous allocation-end the start
535 * of this allocation's buffer? If yes then we can 'merge'
536 * the previous partial page with this allocation.
537 */
538 if (align < PAGE_SIZE &&
539 bdata->last_offset && bdata->last_pos+1 == start) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700540 unsigned long offset, remaining_size;
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700541 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 BUG_ON(offset > PAGE_SIZE);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700543 remaining_size = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (size < remaining_size) {
545 areasize = 0;
546 /* last_pos unchanged */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700547 bdata->last_offset = offset + size;
548 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700549 offset + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 } else {
551 remaining_size = size - remaining_size;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700552 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
553 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700554 offset + node_boot_start);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700555 bdata->last_pos = start + areasize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 bdata->last_offset = remaining_size;
557 }
558 bdata->last_offset &= ~PAGE_MASK;
559 } else {
560 bdata->last_pos = start + areasize - 1;
561 bdata->last_offset = size & ~PAGE_MASK;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700562 ret = phys_to_virt(start * PAGE_SIZE + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 /*
566 * Reserve the area now:
567 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700568 for (i = start; i < start + areasize; i++)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700569 if (unlikely(test_and_set_bit(i, node_bootmem_map)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 BUG();
571 memset(ret, 0, size);
572 return ret;
573}
574
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700575/**
576 * __alloc_bootmem_nopanic - allocate boot memory without panicking
577 * @size: size of the request in bytes
578 * @align: alignment of the region
579 * @goal: preferred starting address of the region
580 *
581 * The goal is dropped if it can not be satisfied and the allocation will
582 * fall back to memory below @goal.
583 *
584 * Allocation may happen on any node in the system.
585 *
586 * Returns NULL on failure.
587 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700588void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
589 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800591 bootmem_data_t *bdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 void *ptr;
593
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700594 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weinerffc64212008-07-23 21:26:59 -0700595 ptr = alloc_bootmem_core(bdata, size, align, goal, 0);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700596 if (ptr)
597 return ptr;
598 }
Andi Kleena8062232006-04-07 19:49:21 +0200599 return NULL;
600}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700602/**
603 * __alloc_bootmem - allocate boot memory
604 * @size: size of the request in bytes
605 * @align: alignment of the region
606 * @goal: preferred starting address of the region
607 *
608 * The goal is dropped if it can not be satisfied and the allocation will
609 * fall back to memory below @goal.
610 *
611 * Allocation may happen on any node in the system.
612 *
613 * The function panics if the request can not be satisfied.
614 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700615void * __init __alloc_bootmem(unsigned long size, unsigned long align,
616 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200617{
618 void *mem = __alloc_bootmem_nopanic(size,align,goal);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700619
Andi Kleena8062232006-04-07 19:49:21 +0200620 if (mem)
621 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
623 * Whoops, we cannot satisfy the allocation request.
624 */
625 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
626 panic("Out of memory");
627 return NULL;
628}
629
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700630/**
631 * __alloc_bootmem_node - allocate boot memory from a specific node
632 * @pgdat: node to allocate from
633 * @size: size of the request in bytes
634 * @align: alignment of the region
635 * @goal: preferred starting address of the region
636 *
637 * The goal is dropped if it can not be satisfied and the allocation will
638 * fall back to memory below @goal.
639 *
640 * Allocation may fall back to any node in the system if the specified node
641 * can not hold the requested memory.
642 *
643 * The function panics if the request can not be satisfied.
644 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700645void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
646 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 void *ptr;
649
Johannes Weinerffc64212008-07-23 21:26:59 -0700650 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (ptr)
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700652 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800654 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700657#ifdef CONFIG_SPARSEMEM
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700658/**
659 * alloc_bootmem_section - allocate boot memory from a specific section
660 * @size: size of the request in bytes
661 * @section_nr: sparse map section to allocate from
662 *
663 * Return NULL on failure.
664 */
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700665void * __init alloc_bootmem_section(unsigned long size,
666 unsigned long section_nr)
667{
668 void *ptr;
669 unsigned long limit, goal, start_nr, end_nr, pfn;
670 struct pglist_data *pgdat;
671
672 pfn = section_nr_to_pfn(section_nr);
673 goal = PFN_PHYS(pfn);
674 limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1;
675 pgdat = NODE_DATA(early_pfn_to_nid(pfn));
Johannes Weinerffc64212008-07-23 21:26:59 -0700676 ptr = alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal,
677 limit);
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700678
679 if (!ptr)
680 return NULL;
681
682 start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr)));
683 end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size));
684 if (start_nr != section_nr || end_nr != section_nr) {
685 printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n",
686 section_nr);
687 free_bootmem_core(pgdat->bdata, __pa(ptr), size);
688 ptr = NULL;
689 }
690
691 return ptr;
692}
693#endif
694
Andi Kleenb54bbf72008-07-23 21:27:45 -0700695void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
696 unsigned long align, unsigned long goal)
697{
698 void *ptr;
699
700 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
701 if (ptr)
702 return ptr;
703
704 return __alloc_bootmem_nopanic(size, align, goal);
705}
706
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700707#ifndef ARCH_LOW_ADDRESS_LIMIT
708#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
709#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800710
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700711/**
712 * __alloc_bootmem_low - allocate low boot memory
713 * @size: size of the request in bytes
714 * @align: alignment of the region
715 * @goal: preferred starting address of the region
716 *
717 * The goal is dropped if it can not be satisfied and the allocation will
718 * fall back to memory below @goal.
719 *
720 * Allocation may happen on any node in the system.
721 *
722 * The function panics if the request can not be satisfied.
723 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700724void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
725 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800726{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800727 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800728 void *ptr;
729
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700730 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weinerffc64212008-07-23 21:26:59 -0700731 ptr = alloc_bootmem_core(bdata, size, align, goal,
732 ARCH_LOW_ADDRESS_LIMIT);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700733 if (ptr)
734 return ptr;
735 }
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800736
737 /*
738 * Whoops, we cannot satisfy the allocation request.
739 */
740 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
741 panic("Out of low memory");
742 return NULL;
743}
744
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700745/**
746 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
747 * @pgdat: node to allocate from
748 * @size: size of the request in bytes
749 * @align: alignment of the region
750 * @goal: preferred starting address of the region
751 *
752 * The goal is dropped if it can not be satisfied and the allocation will
753 * fall back to memory below @goal.
754 *
755 * Allocation may fall back to any node in the system if the specified node
756 * can not hold the requested memory.
757 *
758 * The function panics if the request can not be satisfied.
759 */
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800760void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
761 unsigned long align, unsigned long goal)
762{
Johannes Weinerffc64212008-07-23 21:26:59 -0700763 return alloc_bootmem_core(pgdat->bdata, size, align, goal,
764 ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800765}