blob: d7140c008ba8b6786d7480d3cb4068dcc6a3ce1b [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
Vivek Goyal92aa63a2005-06-25 14:58:18 -070026#ifdef CONFIG_CRASH_DUMP
27/*
28 * If we have booted due to a crash, max_pfn will be a very low value. We need
29 * to know the amount of memory that the previous kernel used.
30 */
31unsigned long saved_max_pfn;
32#endif
33
Johannes Weinerb61bfa32008-07-23 21:26:55 -070034bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
35
Johannes Weiner636cc402008-07-23 21:28:03 -070036static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
37
Johannes Weiner2e5237d2008-07-23 21:28:02 -070038static int bootmem_debug;
39
Tejun Heoc1329372009-02-24 11:57:20 +090040/*
41 * If an arch needs to apply workarounds to bootmem allocation, it can
42 * set CONFIG_HAVE_ARCH_BOOTMEM and define a wrapper around
43 * __alloc_bootmem_core().
44 */
45#ifndef CONFIG_HAVE_ARCH_BOOTMEM
46#define alloc_bootmem_core(bdata, size, align, goal, limit) \
47 __alloc_bootmem_core((bdata), (size), (align), (goal), (limit))
48#endif
49
Johannes Weiner2e5237d2008-07-23 21:28:02 -070050static int __init bootmem_debug_setup(char *buf)
51{
52 bootmem_debug = 1;
53 return 0;
54}
55early_param("bootmem_debug", bootmem_debug_setup);
56
57#define bdebug(fmt, args...) ({ \
58 if (unlikely(bootmem_debug)) \
59 printk(KERN_INFO \
60 "bootmem::%s " fmt, \
Harvey Harrison80a914d2008-10-15 22:01:25 -070061 __func__, ## args); \
Johannes Weiner2e5237d2008-07-23 21:28:02 -070062})
63
Johannes Weinerdf049a52008-07-23 21:28:02 -070064static unsigned long __init bootmap_bytes(unsigned long pages)
Johannes Weiner223e8dc2008-07-23 21:28:00 -070065{
Johannes Weinerdf049a52008-07-23 21:28:02 -070066 unsigned long bytes = (pages + 7) / 8;
Johannes Weiner223e8dc2008-07-23 21:28:00 -070067
Johannes Weinerdf049a52008-07-23 21:28:02 -070068 return ALIGN(bytes, sizeof(long));
Johannes Weiner223e8dc2008-07-23 21:28:00 -070069}
70
Johannes Weinera66fd7d2008-07-23 21:28:01 -070071/**
72 * bootmem_bootmap_pages - calculate bitmap size in pages
73 * @pages: number of pages the bitmap has to represent
74 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070075unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Johannes Weinerdf049a52008-07-23 21:28:02 -070077 unsigned long bytes = bootmap_bytes(pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Johannes Weinerdf049a52008-07-23 21:28:02 -070079 return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070081
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080082/*
83 * link bdata in order
84 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070085static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080086{
Johannes Weiner636cc402008-07-23 21:28:03 -070087 struct list_head *iter;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070088
Johannes Weiner636cc402008-07-23 21:28:03 -070089 list_for_each(iter, &bdata_list) {
90 bootmem_data_t *ent;
91
92 ent = list_entry(iter, bootmem_data_t, list);
Johannes Weiner3560e242008-07-23 21:28:09 -070093 if (bdata->node_min_pfn < ent->node_min_pfn)
Johannes Weiner636cc402008-07-23 21:28:03 -070094 break;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080095 }
Johannes Weiner636cc402008-07-23 21:28:03 -070096 list_add_tail(&bdata->list, iter);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080097}
98
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070099/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * Called once to set up the allocator itself.
101 */
Johannes Weiner8ae04462008-07-23 21:26:59 -0700102static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long mapstart, unsigned long start, unsigned long end)
104{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700105 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700107 mminit_validate_memmodel_limits(&start, &end);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700108 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
Johannes Weiner3560e242008-07-23 21:28:09 -0700109 bdata->node_min_pfn = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800111 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /*
114 * Initially all pages are reserved - setup_arch() has to
115 * register free RAM areas explicitly.
116 */
Johannes Weinerdf049a52008-07-23 21:28:02 -0700117 mapsize = bootmap_bytes(end - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 memset(bdata->node_bootmem_map, 0xff, mapsize);
119
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700120 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
121 bdata - bootmem_node_data, start, mapstart, end, mapsize);
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return mapsize;
124}
125
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700126/**
127 * init_bootmem_node - register a node as boot memory
128 * @pgdat: node to register
129 * @freepfn: pfn where the bitmap for this node is to be placed
130 * @startpfn: first pfn on the node
131 * @endpfn: first pfn after the node
132 *
133 * Returns the number of bytes needed to hold the bitmap for this node.
134 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700135unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
136 unsigned long startpfn, unsigned long endpfn)
137{
138 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
139}
140
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700141/**
142 * init_bootmem - register boot memory
143 * @start: pfn where the bitmap is to be placed
144 * @pages: number of available physical pages
145 *
146 * Returns the number of bytes needed to hold the bitmap.
147 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700148unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
149{
150 max_low_pfn = pages;
151 min_low_pfn = start;
152 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
153}
154
155static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
156{
Johannes Weiner41546c12008-07-23 21:28:03 -0700157 int aligned;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700158 struct page *page;
Johannes Weiner41546c12008-07-23 21:28:03 -0700159 unsigned long start, end, pages, count = 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700160
Johannes Weiner41546c12008-07-23 21:28:03 -0700161 if (!bdata->node_bootmem_map)
162 return 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700163
Johannes Weiner3560e242008-07-23 21:28:09 -0700164 start = bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700165 end = bdata->node_low_pfn;
166
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700167 /*
Johannes Weiner41546c12008-07-23 21:28:03 -0700168 * If the start is aligned to the machines wordsize, we might
169 * be able to free pages in bulks of that order.
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700170 */
Johannes Weiner41546c12008-07-23 21:28:03 -0700171 aligned = !(start & (BITS_PER_LONG - 1));
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700172
Johannes Weiner41546c12008-07-23 21:28:03 -0700173 bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
174 bdata - bootmem_node_data, start, end, aligned);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700175
Johannes Weiner41546c12008-07-23 21:28:03 -0700176 while (start < end) {
177 unsigned long *map, idx, vec;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700178
Johannes Weiner41546c12008-07-23 21:28:03 -0700179 map = bdata->node_bootmem_map;
Johannes Weiner3560e242008-07-23 21:28:09 -0700180 idx = start - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700181 vec = ~map[idx / BITS_PER_LONG];
182
183 if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) {
184 int order = ilog2(BITS_PER_LONG);
185
186 __free_pages_bootmem(pfn_to_page(start), order);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700187 count += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700188 } else {
Johannes Weiner41546c12008-07-23 21:28:03 -0700189 unsigned long off = 0;
190
191 while (vec && off < BITS_PER_LONG) {
192 if (vec & 1) {
193 page = pfn_to_page(start + off);
194 __free_pages_bootmem(page, 0);
195 count++;
196 }
197 vec >>= 1;
198 off++;
199 }
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700200 }
Johannes Weiner41546c12008-07-23 21:28:03 -0700201 start += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700202 }
203
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700204 page = virt_to_page(bdata->node_bootmem_map);
Johannes Weiner3560e242008-07-23 21:28:09 -0700205 pages = bdata->node_low_pfn - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700206 pages = bootmem_bootmap_pages(pages);
207 count += pages;
208 while (pages--)
209 __free_pages_bootmem(page++, 0);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700210
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
Johannes Weinerd747fa42008-07-23 21:28:05 -0700238static void __init __free(bootmem_data_t *bdata,
239 unsigned long sidx, unsigned long eidx)
240{
241 unsigned long idx;
242
243 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700244 sidx + bdata->node_min_pfn,
245 eidx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700246
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700247 if (bdata->hint_idx > sidx)
248 bdata->hint_idx = sidx;
249
Johannes Weinerd747fa42008-07-23 21:28:05 -0700250 for (idx = sidx; idx < eidx; idx++)
251 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
252 BUG();
253}
254
255static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
256 unsigned long eidx, int flags)
257{
258 unsigned long idx;
259 int exclusive = flags & BOOTMEM_EXCLUSIVE;
260
261 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
262 bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700263 sidx + bdata->node_min_pfn,
264 eidx + bdata->node_min_pfn,
Johannes Weinerd747fa42008-07-23 21:28:05 -0700265 flags);
266
267 for (idx = sidx; idx < eidx; idx++)
268 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
269 if (exclusive) {
270 __free(bdata, sidx, idx);
271 return -EBUSY;
272 }
273 bdebug("silent double reserve of PFN %lx\n",
Johannes Weiner3560e242008-07-23 21:28:09 -0700274 idx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700275 }
276 return 0;
277}
278
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700279static int __init mark_bootmem_node(bootmem_data_t *bdata,
280 unsigned long start, unsigned long end,
281 int reserve, int flags)
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700282{
283 unsigned long sidx, eidx;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700284
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700285 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
286 bdata - bootmem_node_data, start, end, reserve, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700287
Johannes Weiner3560e242008-07-23 21:28:09 -0700288 BUG_ON(start < bdata->node_min_pfn);
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700289 BUG_ON(end > bdata->node_low_pfn);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700290
Johannes Weiner3560e242008-07-23 21:28:09 -0700291 sidx = start - bdata->node_min_pfn;
292 eidx = end - bdata->node_min_pfn;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700293
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700294 if (reserve)
295 return __reserve(bdata, sidx, eidx, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700296 else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700297 __free(bdata, sidx, eidx);
298 return 0;
299}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700300
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700301static int __init mark_bootmem(unsigned long start, unsigned long end,
302 int reserve, int flags)
303{
304 unsigned long pos;
305 bootmem_data_t *bdata;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700306
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700307 pos = start;
308 list_for_each_entry(bdata, &bdata_list, list) {
309 int err;
310 unsigned long max;
311
Johannes Weiner3560e242008-07-23 21:28:09 -0700312 if (pos < bdata->node_min_pfn ||
313 pos >= bdata->node_low_pfn) {
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700314 BUG_ON(pos != start);
315 continue;
316 }
317
318 max = min(bdata->node_low_pfn, end);
319
320 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
321 if (reserve && err) {
322 mark_bootmem(start, pos, 0, 0);
323 return err;
324 }
325
326 if (max == end)
327 return 0;
328 pos = bdata->node_low_pfn;
329 }
330 BUG();
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700331}
332
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700333/**
334 * free_bootmem_node - mark a page range as usable
335 * @pgdat: node the range resides on
336 * @physaddr: starting address of the range
337 * @size: size of the range in bytes
338 *
339 * Partial pages will be considered reserved and left as they are.
340 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700341 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700342 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700343void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
344 unsigned long size)
345{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700346 unsigned long start, end;
347
348 start = PFN_UP(physaddr);
349 end = PFN_DOWN(physaddr + size);
350
351 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700352}
353
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700354/**
355 * free_bootmem - mark a page range as usable
356 * @addr: starting address of the range
357 * @size: size of the range in bytes
358 *
359 * Partial pages will be considered reserved and left as they are.
360 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700361 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700362 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700363void __init free_bootmem(unsigned long addr, unsigned long size)
364{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700365 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700366
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700367 start = PFN_UP(addr);
368 end = PFN_DOWN(addr + size);
Yinghai Lua5645a62008-03-18 12:49:12 -0700369
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700370 mark_bootmem(start, end, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700373/**
374 * reserve_bootmem_node - mark a page range as reserved
375 * @pgdat: node the range resides on
376 * @physaddr: starting address of the range
377 * @size: size of the range in bytes
378 * @flags: reservation flags (see linux/bootmem.h)
379 *
380 * Partial pages will be reserved.
381 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700382 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700383 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700384int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
385 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700387 unsigned long start, end;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700388
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700389 start = PFN_DOWN(physaddr);
390 end = PFN_UP(physaddr + size);
391
392 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700395/**
396 * reserve_bootmem - mark a page range as usable
397 * @addr: starting address of the range
398 * @size: size of the range in bytes
399 * @flags: reservation flags (see linux/bootmem.h)
400 *
401 * Partial pages will be reserved.
402 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700403 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700404 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700405int __init reserve_bootmem(unsigned long addr, unsigned long size,
406 int flags)
407{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700408 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700409
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700410 start = PFN_DOWN(addr);
411 end = PFN_UP(addr + size);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700412
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700413 return mark_bootmem(start, end, 1, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700414}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700415
Johannes Weiner481ebd02008-08-20 14:09:15 -0700416static unsigned long align_idx(struct bootmem_data *bdata, unsigned long idx,
417 unsigned long step)
418{
419 unsigned long base = bdata->node_min_pfn;
420
421 /*
422 * Align the index with respect to the node start so that the
423 * combination of both satisfies the requested alignment.
424 */
425
426 return ALIGN(base + idx, step) - base;
427}
428
429static unsigned long align_off(struct bootmem_data *bdata, unsigned long off,
430 unsigned long align)
431{
432 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
433
434 /* Same as align_idx for byte offsets */
435
436 return ALIGN(base + off, align) - base;
437}
438
Tejun Heoc1329372009-02-24 11:57:20 +0900439static void * __init __alloc_bootmem_core(struct bootmem_data *bdata,
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700440 unsigned long size, unsigned long align,
441 unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700443 unsigned long fallback = 0;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700444 unsigned long min, max, start, sidx, midx, step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Johannes Weiner594fe1a2009-01-06 14:40:32 -0800446 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
447 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
448 align, goal, limit);
449
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700450 BUG_ON(!size);
451 BUG_ON(align & (align - 1));
452 BUG_ON(limit && goal + size > limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Christian Krafft7c309a62006-12-06 20:32:41 -0800454 if (!bdata->node_bootmem_map)
455 return NULL;
456
Johannes Weiner3560e242008-07-23 21:28:09 -0700457 min = bdata->node_min_pfn;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700458 max = bdata->node_low_pfn;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700459
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700460 goal >>= PAGE_SHIFT;
461 limit >>= PAGE_SHIFT;
462
463 if (limit && max > limit)
464 max = limit;
465 if (max <= min)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700466 return NULL;
467
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700468 step = max(align >> PAGE_SHIFT, 1UL);
Yasunori Goto281dd252005-10-19 15:52:18 -0700469
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700470 if (goal && min < goal && goal < max)
471 start = ALIGN(goal, step);
472 else
473 start = ALIGN(min, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Johannes Weiner481ebd02008-08-20 14:09:15 -0700475 sidx = start - bdata->node_min_pfn;
Johannes Weiner3560e242008-07-23 21:28:09 -0700476 midx = max - bdata->node_min_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700478 if (bdata->hint_idx > sidx) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700479 /*
480 * Handle the valid case of sidx being zero and still
481 * catch the fallback below.
482 */
483 fallback = sidx + 1;
Johannes Weiner481ebd02008-08-20 14:09:15 -0700484 sidx = align_idx(bdata, bdata->hint_idx, step);
Yinghai Luad093152008-03-10 23:23:42 -0700485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700487 while (1) {
488 int merge;
489 void *region;
490 unsigned long eidx, i, start_off, end_off;
491find_block:
492 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
Johannes Weiner481ebd02008-08-20 14:09:15 -0700493 sidx = align_idx(bdata, sidx, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700494 eidx = sidx + PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700496 if (sidx >= midx || eidx > midx)
Haren Myneni66d43e92005-12-12 00:37:39 -0800497 break;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700498
499 for (i = sidx; i < eidx; i++)
500 if (test_bit(i, bdata->node_bootmem_map)) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700501 sidx = align_idx(bdata, i, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700502 if (sidx == i)
503 sidx += step;
504 goto find_block;
505 }
506
Mikulas Patocka627240a2008-08-15 00:40:17 -0700507 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700508 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700509 start_off = align_off(bdata, bdata->last_end_off, align);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700510 else
511 start_off = PFN_PHYS(sidx);
512
513 merge = PFN_DOWN(start_off) < sidx;
514 end_off = start_off + size;
515
516 bdata->last_end_off = end_off;
517 bdata->hint_idx = PFN_UP(end_off);
518
519 /*
520 * Reserve the area now:
521 */
Johannes Weinerd747fa42008-07-23 21:28:05 -0700522 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
523 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
524 BUG();
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700525
Johannes Weiner3560e242008-07-23 21:28:09 -0700526 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
527 start_off);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700528 memset(region, 0, size);
529 return region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700532 if (fallback) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700533 sidx = align_idx(bdata, fallback - 1, step);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700534 fallback = 0;
535 goto find_block;
536 }
537
538 return NULL;
539}
540
541static void * __init ___alloc_bootmem_nopanic(unsigned long size,
542 unsigned long align,
543 unsigned long goal,
544 unsigned long limit)
545{
546 bootmem_data_t *bdata;
547
548restart:
549 list_for_each_entry(bdata, &bdata_list, list) {
550 void *region;
551
552 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
553 continue;
Johannes Weiner3560e242008-07-23 21:28:09 -0700554 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700555 break;
556
557 region = alloc_bootmem_core(bdata, size, align, goal, limit);
558 if (region)
559 return region;
560 }
561
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700562 if (goal) {
563 goal = 0;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700564 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700570/**
571 * __alloc_bootmem_nopanic - allocate boot memory without panicking
572 * @size: size of the request in bytes
573 * @align: alignment of the region
574 * @goal: preferred starting address of the region
575 *
576 * The goal is dropped if it can not be satisfied and the allocation will
577 * fall back to memory below @goal.
578 *
579 * Allocation may happen on any node in the system.
580 *
581 * Returns NULL on failure.
582 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700583void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700584 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700586 return ___alloc_bootmem_nopanic(size, align, goal, 0);
587}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700589static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
590 unsigned long goal, unsigned long limit)
591{
592 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
593
594 if (mem)
595 return mem;
596 /*
597 * Whoops, we cannot satisfy the allocation request.
598 */
599 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
600 panic("Out of memory");
Andi Kleena8062232006-04-07 19:49:21 +0200601 return NULL;
602}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700604/**
605 * __alloc_bootmem - allocate boot memory
606 * @size: size of the request in bytes
607 * @align: alignment of the region
608 * @goal: preferred starting address of the region
609 *
610 * The goal is dropped if it can not be satisfied and the allocation will
611 * fall back to memory below @goal.
612 *
613 * Allocation may happen on any node in the system.
614 *
615 * The function panics if the request can not be satisfied.
616 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700617void * __init __alloc_bootmem(unsigned long size, unsigned long align,
618 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200619{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700620 return ___alloc_bootmem(size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700623static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
624 unsigned long size, unsigned long align,
625 unsigned long goal, unsigned long limit)
626{
627 void *ptr;
628
629 ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
630 if (ptr)
631 return ptr;
632
633 return ___alloc_bootmem(size, align, goal, limit);
634}
635
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700636/**
637 * __alloc_bootmem_node - allocate boot memory from a specific node
638 * @pgdat: node to allocate from
639 * @size: size of the request in bytes
640 * @align: alignment of the region
641 * @goal: preferred starting address of the region
642 *
643 * The goal is dropped if it can not be satisfied and the allocation will
644 * fall back to memory below @goal.
645 *
646 * Allocation may fall back to any node in the system if the specified node
647 * can not hold the requested memory.
648 *
649 * The function panics if the request can not be satisfied.
650 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700651void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
652 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700654 return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
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{
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700668 bootmem_data_t *bdata;
669 unsigned long pfn, goal, limit;
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700670
671 pfn = section_nr_to_pfn(section_nr);
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700672 goal = pfn << PAGE_SHIFT;
673 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
674 bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700675
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700676 return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700677}
678#endif
679
Andi Kleenb54bbf72008-07-23 21:27:45 -0700680void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
681 unsigned long align, unsigned long goal)
682{
683 void *ptr;
684
685 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
686 if (ptr)
687 return ptr;
688
689 return __alloc_bootmem_nopanic(size, align, goal);
690}
691
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700692#ifndef ARCH_LOW_ADDRESS_LIMIT
693#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
694#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800695
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700696/**
697 * __alloc_bootmem_low - allocate low boot memory
698 * @size: size of the request in bytes
699 * @align: alignment of the region
700 * @goal: preferred starting address of the region
701 *
702 * The goal is dropped if it can not be satisfied and the allocation will
703 * fall back to memory below @goal.
704 *
705 * Allocation may happen on any node in the system.
706 *
707 * The function panics if the request can not be satisfied.
708 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700709void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
710 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800711{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700712 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800713}
714
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700715/**
716 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
717 * @pgdat: node to allocate from
718 * @size: size of the request in bytes
719 * @align: alignment of the region
720 * @goal: preferred starting address of the region
721 *
722 * The goal is dropped if it can not be satisfied and the allocation will
723 * fall back to memory below @goal.
724 *
725 * Allocation may fall back to any node in the system if the specified node
726 * can not hold the requested memory.
727 *
728 * The function panics if the request can not be satisfied.
729 */
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800730void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
731 unsigned long align, unsigned long goal)
732{
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700733 return ___alloc_bootmem_node(pgdat->bdata, size, align,
734 goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800735}