blob: 13b0caa9793c008b7fafa09cbe5c586faa6c7447 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
Catalin Marinasec3a3542009-07-07 10:33:01 +010016#include <linux/kmemleak.h>
Yinghai Lu08677212010-02-10 01:20:20 -080017#include <linux/range.h>
Yinghai Lu72d7c3b2010-08-25 13:39:17 -070018#include <linux/memblock.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070019
20#include <asm/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/io.h>
Heiko Carstensdfd54cb2006-09-25 23:31:33 -070022#include <asm/processor.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "internal.h"
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026unsigned long max_low_pfn;
27unsigned long min_low_pfn;
28unsigned long max_pfn;
29
Vivek Goyal92aa63a2005-06-25 14:58:18 -070030#ifdef CONFIG_CRASH_DUMP
31/*
32 * If we have booted due to a crash, max_pfn will be a very low value. We need
33 * to know the amount of memory that the previous kernel used.
34 */
35unsigned long saved_max_pfn;
36#endif
37
Yinghai Lu08677212010-02-10 01:20:20 -080038#ifndef CONFIG_NO_BOOTMEM
Johannes Weinerb61bfa32008-07-23 21:26:55 -070039bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
40
Johannes Weiner636cc402008-07-23 21:28:03 -070041static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
42
Johannes Weiner2e5237d2008-07-23 21:28:02 -070043static int bootmem_debug;
44
45static int __init bootmem_debug_setup(char *buf)
46{
47 bootmem_debug = 1;
48 return 0;
49}
50early_param("bootmem_debug", bootmem_debug_setup);
51
52#define bdebug(fmt, args...) ({ \
53 if (unlikely(bootmem_debug)) \
54 printk(KERN_INFO \
55 "bootmem::%s " fmt, \
Harvey Harrison80a914d2008-10-15 22:01:25 -070056 __func__, ## args); \
Johannes Weiner2e5237d2008-07-23 21:28:02 -070057})
58
Johannes Weinerdf049a52008-07-23 21:28:02 -070059static unsigned long __init bootmap_bytes(unsigned long pages)
Johannes Weiner223e8dc2008-07-23 21:28:00 -070060{
Johannes Weinerdf049a52008-07-23 21:28:02 -070061 unsigned long bytes = (pages + 7) / 8;
Johannes Weiner223e8dc2008-07-23 21:28:00 -070062
Johannes Weinerdf049a52008-07-23 21:28:02 -070063 return ALIGN(bytes, sizeof(long));
Johannes Weiner223e8dc2008-07-23 21:28:00 -070064}
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{
Johannes Weinerdf049a52008-07-23 21:28:02 -070072 unsigned long bytes = bootmap_bytes(pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Johannes Weinerdf049a52008-07-23 21:28:02 -070074 return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070076
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080077/*
78 * link bdata in order
79 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070080static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080081{
Johannes Weiner636cc402008-07-23 21:28:03 -070082 struct list_head *iter;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070083
Johannes Weiner636cc402008-07-23 21:28:03 -070084 list_for_each(iter, &bdata_list) {
85 bootmem_data_t *ent;
86
87 ent = list_entry(iter, bootmem_data_t, list);
Johannes Weiner3560e242008-07-23 21:28:09 -070088 if (bdata->node_min_pfn < ent->node_min_pfn)
Johannes Weiner636cc402008-07-23 21:28:03 -070089 break;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080090 }
Johannes Weiner636cc402008-07-23 21:28:03 -070091 list_add_tail(&bdata->list, iter);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080092}
93
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070094/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * Called once to set up the allocator itself.
96 */
Johannes Weiner8ae04462008-07-23 21:26:59 -070097static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned long mapstart, unsigned long start, unsigned long end)
99{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700100 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700102 mminit_validate_memmodel_limits(&start, &end);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700103 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
Johannes Weiner3560e242008-07-23 21:28:09 -0700104 bdata->node_min_pfn = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800106 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /*
109 * Initially all pages are reserved - setup_arch() has to
110 * register free RAM areas explicitly.
111 */
Johannes Weinerdf049a52008-07-23 21:28:02 -0700112 mapsize = bootmap_bytes(end - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 memset(bdata->node_bootmem_map, 0xff, mapsize);
114
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700115 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
116 bdata - bootmem_node_data, start, mapstart, end, mapsize);
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return mapsize;
119}
120
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700121/**
122 * init_bootmem_node - register a node as boot memory
123 * @pgdat: node to register
124 * @freepfn: pfn where the bitmap for this node is to be placed
125 * @startpfn: first pfn on the node
126 * @endpfn: first pfn after the node
127 *
128 * Returns the number of bytes needed to hold the bitmap for this node.
129 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700130unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
131 unsigned long startpfn, unsigned long endpfn)
132{
133 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
134}
135
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700136/**
137 * init_bootmem - register boot memory
138 * @start: pfn where the bitmap is to be placed
139 * @pages: number of available physical pages
140 *
141 * Returns the number of bytes needed to hold the bitmap.
142 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700143unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
144{
145 max_low_pfn = pages;
146 min_low_pfn = start;
147 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
148}
Yinghai Lu08677212010-02-10 01:20:20 -0800149#endif
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900150/*
151 * free_bootmem_late - free bootmem pages directly to page allocator
152 * @addr: starting address of the range
153 * @size: size of the range in bytes
154 *
155 * This is only useful when the bootmem allocator has already been torn
156 * down, but we are still initializing the system. Pages are given directly
157 * to the page allocator, no bootmem metadata is updated because it is gone.
158 */
159void __init free_bootmem_late(unsigned long addr, unsigned long size)
160{
161 unsigned long cursor, end;
162
163 kmemleak_free_part(__va(addr), size);
164
165 cursor = PFN_UP(addr);
166 end = PFN_DOWN(addr + size);
167
168 for (; cursor < end; cursor++) {
169 __free_pages_bootmem(pfn_to_page(cursor), 0);
170 totalram_pages++;
171 }
172}
173
Yinghai Lu08677212010-02-10 01:20:20 -0800174#ifdef CONFIG_NO_BOOTMEM
175static void __init __free_pages_memory(unsigned long start, unsigned long end)
176{
177 int i;
178 unsigned long start_aligned, end_aligned;
179 int order = ilog2(BITS_PER_LONG);
180
181 start_aligned = (start + (BITS_PER_LONG - 1)) & ~(BITS_PER_LONG - 1);
182 end_aligned = end & ~(BITS_PER_LONG - 1);
183
184 if (end_aligned <= start_aligned) {
Yinghai Lu08677212010-02-10 01:20:20 -0800185 for (i = start; i < end; i++)
186 __free_pages_bootmem(pfn_to_page(i), 0);
187
188 return;
189 }
190
Yinghai Lu08677212010-02-10 01:20:20 -0800191 for (i = start; i < start_aligned; i++)
192 __free_pages_bootmem(pfn_to_page(i), 0);
193
194 for (i = start_aligned; i < end_aligned; i += BITS_PER_LONG)
195 __free_pages_bootmem(pfn_to_page(i), order);
196
197 for (i = end_aligned; i < end; i++)
198 __free_pages_bootmem(pfn_to_page(i), 0);
199}
200
201unsigned long __init free_all_memory_core_early(int nodeid)
202{
203 int i;
204 u64 start, end;
205 unsigned long count = 0;
206 struct range *range = NULL;
207 int nr_range;
208
209 nr_range = get_free_all_memory_range(&range, nodeid);
210
211 for (i = 0; i < nr_range; i++) {
212 start = range[i].start;
213 end = range[i].end;
214 count += end - start;
215 __free_pages_memory(start, end);
216 }
217
218 return count;
219}
220#else
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700221static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
222{
Johannes Weiner41546c12008-07-23 21:28:03 -0700223 int aligned;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700224 struct page *page;
Johannes Weiner41546c12008-07-23 21:28:03 -0700225 unsigned long start, end, pages, count = 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700226
Johannes Weiner41546c12008-07-23 21:28:03 -0700227 if (!bdata->node_bootmem_map)
228 return 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700229
Johannes Weiner3560e242008-07-23 21:28:09 -0700230 start = bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700231 end = bdata->node_low_pfn;
232
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700233 /*
Johannes Weiner41546c12008-07-23 21:28:03 -0700234 * If the start is aligned to the machines wordsize, we might
235 * be able to free pages in bulks of that order.
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700236 */
Johannes Weiner41546c12008-07-23 21:28:03 -0700237 aligned = !(start & (BITS_PER_LONG - 1));
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700238
Johannes Weiner41546c12008-07-23 21:28:03 -0700239 bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
240 bdata - bootmem_node_data, start, end, aligned);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700241
Johannes Weiner41546c12008-07-23 21:28:03 -0700242 while (start < end) {
243 unsigned long *map, idx, vec;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700244
Johannes Weiner41546c12008-07-23 21:28:03 -0700245 map = bdata->node_bootmem_map;
Johannes Weiner3560e242008-07-23 21:28:09 -0700246 idx = start - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700247 vec = ~map[idx / BITS_PER_LONG];
248
249 if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) {
250 int order = ilog2(BITS_PER_LONG);
251
252 __free_pages_bootmem(pfn_to_page(start), order);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700253 count += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700254 } else {
Johannes Weiner41546c12008-07-23 21:28:03 -0700255 unsigned long off = 0;
256
257 while (vec && off < BITS_PER_LONG) {
258 if (vec & 1) {
259 page = pfn_to_page(start + off);
260 __free_pages_bootmem(page, 0);
261 count++;
262 }
263 vec >>= 1;
264 off++;
265 }
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700266 }
Johannes Weiner41546c12008-07-23 21:28:03 -0700267 start += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700268 }
269
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700270 page = virt_to_page(bdata->node_bootmem_map);
Johannes Weiner3560e242008-07-23 21:28:09 -0700271 pages = bdata->node_low_pfn - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700272 pages = bootmem_bootmap_pages(pages);
273 count += pages;
274 while (pages--)
275 __free_pages_bootmem(page++, 0);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700276
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700277 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
278
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700279 return count;
280}
Yinghai Lu08677212010-02-10 01:20:20 -0800281#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700282
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700283/**
284 * free_all_bootmem_node - release a node's free pages to the buddy allocator
285 * @pgdat: node to be released
286 *
287 * Returns the number of pages actually released.
288 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700289unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
290{
291 register_page_bootmem_info_node(pgdat);
Yinghai Lu08677212010-02-10 01:20:20 -0800292#ifdef CONFIG_NO_BOOTMEM
293 /* free_all_memory_core_early(MAX_NUMNODES) will be called later */
294 return 0;
295#else
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700296 return free_all_bootmem_core(pgdat->bdata);
Yinghai Lu08677212010-02-10 01:20:20 -0800297#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700298}
299
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700300/**
301 * free_all_bootmem - release free pages to the buddy allocator
302 *
303 * Returns the number of pages actually released.
304 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700305unsigned long __init free_all_bootmem(void)
306{
Yinghai Lu08677212010-02-10 01:20:20 -0800307#ifdef CONFIG_NO_BOOTMEM
Yinghai Lu33799852010-03-31 20:44:09 -0700308 /*
309 * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id
310 * because in some case like Node0 doesnt have RAM installed
311 * low ram will be on Node1
312 * Use MAX_NUMNODES will make sure all ranges in early_node_map[]
313 * will be used instead of only Node0 related
314 */
315 return free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800316#else
Yinghai Luaa235fc2010-03-31 20:45:27 -0700317 unsigned long total_pages = 0;
318 bootmem_data_t *bdata;
319
320 list_for_each_entry(bdata, &bdata_list, list)
321 total_pages += free_all_bootmem_core(bdata);
322
323 return total_pages;
Yinghai Lu08677212010-02-10 01:20:20 -0800324#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700325}
326
Yinghai Lu08677212010-02-10 01:20:20 -0800327#ifndef CONFIG_NO_BOOTMEM
Johannes Weinerd747fa42008-07-23 21:28:05 -0700328static void __init __free(bootmem_data_t *bdata,
329 unsigned long sidx, unsigned long eidx)
330{
331 unsigned long idx;
332
333 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700334 sidx + bdata->node_min_pfn,
335 eidx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700336
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700337 if (bdata->hint_idx > sidx)
338 bdata->hint_idx = sidx;
339
Johannes Weinerd747fa42008-07-23 21:28:05 -0700340 for (idx = sidx; idx < eidx; idx++)
341 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
342 BUG();
343}
344
345static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
346 unsigned long eidx, int flags)
347{
348 unsigned long idx;
349 int exclusive = flags & BOOTMEM_EXCLUSIVE;
350
351 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
352 bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700353 sidx + bdata->node_min_pfn,
354 eidx + bdata->node_min_pfn,
Johannes Weinerd747fa42008-07-23 21:28:05 -0700355 flags);
356
357 for (idx = sidx; idx < eidx; idx++)
358 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
359 if (exclusive) {
360 __free(bdata, sidx, idx);
361 return -EBUSY;
362 }
363 bdebug("silent double reserve of PFN %lx\n",
Johannes Weiner3560e242008-07-23 21:28:09 -0700364 idx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700365 }
366 return 0;
367}
368
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700369static int __init mark_bootmem_node(bootmem_data_t *bdata,
370 unsigned long start, unsigned long end,
371 int reserve, int flags)
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700372{
373 unsigned long sidx, eidx;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700374
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700375 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
376 bdata - bootmem_node_data, start, end, reserve, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700377
Johannes Weiner3560e242008-07-23 21:28:09 -0700378 BUG_ON(start < bdata->node_min_pfn);
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700379 BUG_ON(end > bdata->node_low_pfn);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700380
Johannes Weiner3560e242008-07-23 21:28:09 -0700381 sidx = start - bdata->node_min_pfn;
382 eidx = end - bdata->node_min_pfn;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700383
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700384 if (reserve)
385 return __reserve(bdata, sidx, eidx, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700386 else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700387 __free(bdata, sidx, eidx);
388 return 0;
389}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700390
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700391static int __init mark_bootmem(unsigned long start, unsigned long end,
392 int reserve, int flags)
393{
394 unsigned long pos;
395 bootmem_data_t *bdata;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700396
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700397 pos = start;
398 list_for_each_entry(bdata, &bdata_list, list) {
399 int err;
400 unsigned long max;
401
Johannes Weiner3560e242008-07-23 21:28:09 -0700402 if (pos < bdata->node_min_pfn ||
403 pos >= bdata->node_low_pfn) {
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700404 BUG_ON(pos != start);
405 continue;
406 }
407
408 max = min(bdata->node_low_pfn, end);
409
410 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
411 if (reserve && err) {
412 mark_bootmem(start, pos, 0, 0);
413 return err;
414 }
415
416 if (max == end)
417 return 0;
418 pos = bdata->node_low_pfn;
419 }
420 BUG();
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700421}
Yinghai Lu08677212010-02-10 01:20:20 -0800422#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700423
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700424/**
425 * free_bootmem_node - mark a page range as usable
426 * @pgdat: node the range resides on
427 * @physaddr: starting address of the range
428 * @size: size of the range in bytes
429 *
430 * Partial pages will be considered reserved and left as they are.
431 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700432 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700433 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700434void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
435 unsigned long size)
436{
Yinghai Lu08677212010-02-10 01:20:20 -0800437#ifdef CONFIG_NO_BOOTMEM
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700438 kmemleak_free_part(__va(physaddr), size);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700439 memblock_x86_free_range(physaddr, physaddr + size);
Yinghai Lu08677212010-02-10 01:20:20 -0800440#else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700441 unsigned long start, end;
442
Catalin Marinasec3a3542009-07-07 10:33:01 +0100443 kmemleak_free_part(__va(physaddr), size);
444
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700445 start = PFN_UP(physaddr);
446 end = PFN_DOWN(physaddr + size);
447
448 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800449#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700450}
451
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700452/**
453 * free_bootmem - mark a page range as usable
454 * @addr: starting address of the range
455 * @size: size of the range in bytes
456 *
457 * Partial pages will be considered reserved and left as they are.
458 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700459 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700460 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700461void __init free_bootmem(unsigned long addr, unsigned long size)
462{
Yinghai Lu08677212010-02-10 01:20:20 -0800463#ifdef CONFIG_NO_BOOTMEM
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700464 kmemleak_free_part(__va(addr), size);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700465 memblock_x86_free_range(addr, addr + size);
Yinghai Lu08677212010-02-10 01:20:20 -0800466#else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700467 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700468
Catalin Marinasec3a3542009-07-07 10:33:01 +0100469 kmemleak_free_part(__va(addr), size);
470
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700471 start = PFN_UP(addr);
472 end = PFN_DOWN(addr + size);
Yinghai Lua5645a62008-03-18 12:49:12 -0700473
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700474 mark_bootmem(start, end, 0, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700478/**
479 * reserve_bootmem_node - mark a page range as reserved
480 * @pgdat: node the range resides on
481 * @physaddr: starting address of the range
482 * @size: size of the range in bytes
483 * @flags: reservation flags (see linux/bootmem.h)
484 *
485 * Partial pages will be reserved.
486 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700487 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700488 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700489int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
490 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Yinghai Lu08677212010-02-10 01:20:20 -0800492#ifdef CONFIG_NO_BOOTMEM
493 panic("no bootmem");
494 return 0;
495#else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700496 unsigned long start, end;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700497
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700498 start = PFN_DOWN(physaddr);
499 end = PFN_UP(physaddr + size);
500
501 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
Yinghai Lu08677212010-02-10 01:20:20 -0800502#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700505/**
506 * reserve_bootmem - mark a page range as usable
507 * @addr: starting address of the range
508 * @size: size of the range in bytes
509 * @flags: reservation flags (see linux/bootmem.h)
510 *
511 * Partial pages will be reserved.
512 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700513 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700514 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700515int __init reserve_bootmem(unsigned long addr, unsigned long size,
516 int flags)
517{
Yinghai Lu08677212010-02-10 01:20:20 -0800518#ifdef CONFIG_NO_BOOTMEM
519 panic("no bootmem");
520 return 0;
521#else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700522 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700523
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700524 start = PFN_DOWN(addr);
525 end = PFN_UP(addr + size);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700526
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700527 return mark_bootmem(start, end, 1, flags);
Yinghai Lu08677212010-02-10 01:20:20 -0800528#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700529}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700530
Yinghai Lu08677212010-02-10 01:20:20 -0800531#ifndef CONFIG_NO_BOOTMEM
Yinghai Luf88eff72010-08-25 13:39:15 -0700532int __weak __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
533 int flags)
534{
535 return reserve_bootmem(phys, len, flags);
536}
537
Jan Beulich8aa043d2009-12-14 17:59:46 -0800538static unsigned long __init align_idx(struct bootmem_data *bdata,
539 unsigned long idx, unsigned long step)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700540{
541 unsigned long base = bdata->node_min_pfn;
542
543 /*
544 * Align the index with respect to the node start so that the
545 * combination of both satisfies the requested alignment.
546 */
547
548 return ALIGN(base + idx, step) - base;
549}
550
Jan Beulich8aa043d2009-12-14 17:59:46 -0800551static unsigned long __init align_off(struct bootmem_data *bdata,
552 unsigned long off, unsigned long align)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700553{
554 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
555
556 /* Same as align_idx for byte offsets */
557
558 return ALIGN(base + off, align) - base;
559}
560
Tejun Heod0c4f572009-03-01 16:06:56 +0900561static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
562 unsigned long size, unsigned long align,
563 unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700565 unsigned long fallback = 0;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700566 unsigned long min, max, start, sidx, midx, step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Johannes Weiner594fe1a2009-01-06 14:40:32 -0800568 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
569 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
570 align, goal, limit);
571
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700572 BUG_ON(!size);
573 BUG_ON(align & (align - 1));
574 BUG_ON(limit && goal + size > limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Christian Krafft7c309a62006-12-06 20:32:41 -0800576 if (!bdata->node_bootmem_map)
577 return NULL;
578
Johannes Weiner3560e242008-07-23 21:28:09 -0700579 min = bdata->node_min_pfn;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700580 max = bdata->node_low_pfn;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700581
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700582 goal >>= PAGE_SHIFT;
583 limit >>= PAGE_SHIFT;
584
585 if (limit && max > limit)
586 max = limit;
587 if (max <= min)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700588 return NULL;
589
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700590 step = max(align >> PAGE_SHIFT, 1UL);
Yasunori Goto281dd252005-10-19 15:52:18 -0700591
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700592 if (goal && min < goal && goal < max)
593 start = ALIGN(goal, step);
594 else
595 start = ALIGN(min, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Johannes Weiner481ebd02008-08-20 14:09:15 -0700597 sidx = start - bdata->node_min_pfn;
Johannes Weiner3560e242008-07-23 21:28:09 -0700598 midx = max - bdata->node_min_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700600 if (bdata->hint_idx > sidx) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700601 /*
602 * Handle the valid case of sidx being zero and still
603 * catch the fallback below.
604 */
605 fallback = sidx + 1;
Johannes Weiner481ebd02008-08-20 14:09:15 -0700606 sidx = align_idx(bdata, bdata->hint_idx, step);
Yinghai Luad093152008-03-10 23:23:42 -0700607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700609 while (1) {
610 int merge;
611 void *region;
612 unsigned long eidx, i, start_off, end_off;
613find_block:
614 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
Johannes Weiner481ebd02008-08-20 14:09:15 -0700615 sidx = align_idx(bdata, sidx, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700616 eidx = sidx + PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700618 if (sidx >= midx || eidx > midx)
Haren Myneni66d43e92005-12-12 00:37:39 -0800619 break;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700620
621 for (i = sidx; i < eidx; i++)
622 if (test_bit(i, bdata->node_bootmem_map)) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700623 sidx = align_idx(bdata, i, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700624 if (sidx == i)
625 sidx += step;
626 goto find_block;
627 }
628
Mikulas Patocka627240a2008-08-15 00:40:17 -0700629 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700630 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700631 start_off = align_off(bdata, bdata->last_end_off, align);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700632 else
633 start_off = PFN_PHYS(sidx);
634
635 merge = PFN_DOWN(start_off) < sidx;
636 end_off = start_off + size;
637
638 bdata->last_end_off = end_off;
639 bdata->hint_idx = PFN_UP(end_off);
640
641 /*
642 * Reserve the area now:
643 */
Johannes Weinerd747fa42008-07-23 21:28:05 -0700644 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
645 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
646 BUG();
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700647
Johannes Weiner3560e242008-07-23 21:28:09 -0700648 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
649 start_off);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700650 memset(region, 0, size);
Catalin Marinas008139d2009-08-27 14:29:17 +0100651 /*
652 * The min_count is set to 0 so that bootmem allocated blocks
653 * are never reported as leaks.
654 */
655 kmemleak_alloc(region, size, 0, 0);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700656 return region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700659 if (fallback) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700660 sidx = align_idx(bdata, fallback - 1, step);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700661 fallback = 0;
662 goto find_block;
663 }
664
665 return NULL;
666}
667
Tejun Heod0c4f572009-03-01 16:06:56 +0900668static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
669 unsigned long size, unsigned long align,
670 unsigned long goal, unsigned long limit)
671{
Pekka Enberg441c7e02009-06-10 20:05:53 +0300672 if (WARN_ON_ONCE(slab_is_available()))
673 return kzalloc(size, GFP_NOWAIT);
674
Tejun Heod0c4f572009-03-01 16:06:56 +0900675#ifdef CONFIG_HAVE_ARCH_BOOTMEM
Joe Perches433f13a2009-06-18 16:48:58 -0700676 {
677 bootmem_data_t *p_bdata;
Tejun Heod0c4f572009-03-01 16:06:56 +0900678
Joe Perches433f13a2009-06-18 16:48:58 -0700679 p_bdata = bootmem_arch_preferred_node(bdata, size, align,
680 goal, limit);
681 if (p_bdata)
682 return alloc_bootmem_core(p_bdata, size, align,
683 goal, limit);
684 }
Tejun Heod0c4f572009-03-01 16:06:56 +0900685#endif
686 return NULL;
687}
Yinghai Lu08677212010-02-10 01:20:20 -0800688#endif
Tejun Heod0c4f572009-03-01 16:06:56 +0900689
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700690static void * __init ___alloc_bootmem_nopanic(unsigned long size,
691 unsigned long align,
692 unsigned long goal,
693 unsigned long limit)
694{
Yinghai Lu08677212010-02-10 01:20:20 -0800695#ifdef CONFIG_NO_BOOTMEM
696 void *ptr;
697
698 if (WARN_ON_ONCE(slab_is_available()))
699 return kzalloc(size, GFP_NOWAIT);
700
701restart:
702
703 ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);
704
705 if (ptr)
706 return ptr;
707
708 if (goal != 0) {
709 goal = 0;
710 goto restart;
711 }
712
713 return NULL;
714#else
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700715 bootmem_data_t *bdata;
Tejun Heod0c4f572009-03-01 16:06:56 +0900716 void *region;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700717
718restart:
Tejun Heod0c4f572009-03-01 16:06:56 +0900719 region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
720 if (region)
721 return region;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700722
Tejun Heod0c4f572009-03-01 16:06:56 +0900723 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700724 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
725 continue;
Johannes Weiner3560e242008-07-23 21:28:09 -0700726 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700727 break;
728
729 region = alloc_bootmem_core(bdata, size, align, goal, limit);
730 if (region)
731 return region;
732 }
733
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700734 if (goal) {
735 goal = 0;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700736 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return NULL;
Yinghai Lu08677212010-02-10 01:20:20 -0800740#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700743/**
744 * __alloc_bootmem_nopanic - allocate boot memory without panicking
745 * @size: size of the request in bytes
746 * @align: alignment of the region
747 * @goal: preferred starting address of the region
748 *
749 * The goal is dropped if it can not be satisfied and the allocation will
750 * fall back to memory below @goal.
751 *
752 * Allocation may happen on any node in the system.
753 *
754 * Returns NULL on failure.
755 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700756void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700757 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Yinghai Lu08677212010-02-10 01:20:20 -0800759 unsigned long limit = 0;
760
761#ifdef CONFIG_NO_BOOTMEM
762 limit = -1UL;
763#endif
764
765 return ___alloc_bootmem_nopanic(size, align, goal, limit);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700766}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700768static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
769 unsigned long goal, unsigned long limit)
770{
771 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
772
773 if (mem)
774 return mem;
775 /*
776 * Whoops, we cannot satisfy the allocation request.
777 */
778 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
779 panic("Out of memory");
Andi Kleena8062232006-04-07 19:49:21 +0200780 return NULL;
781}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700783/**
784 * __alloc_bootmem - allocate boot memory
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 happen on any node in the system.
793 *
794 * The function panics if the request can not be satisfied.
795 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700796void * __init __alloc_bootmem(unsigned long size, unsigned long align,
797 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200798{
Yinghai Lu08677212010-02-10 01:20:20 -0800799 unsigned long limit = 0;
800
801#ifdef CONFIG_NO_BOOTMEM
802 limit = -1UL;
803#endif
804
805 return ___alloc_bootmem(size, align, goal, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Yinghai Lu08677212010-02-10 01:20:20 -0800808#ifndef CONFIG_NO_BOOTMEM
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700809static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
810 unsigned long size, unsigned long align,
811 unsigned long goal, unsigned long limit)
812{
813 void *ptr;
814
Tejun Heod0c4f572009-03-01 16:06:56 +0900815 ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
816 if (ptr)
817 return ptr;
818
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700819 ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
820 if (ptr)
821 return ptr;
822
823 return ___alloc_bootmem(size, align, goal, limit);
824}
Yinghai Lu08677212010-02-10 01:20:20 -0800825#endif
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700826
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700827/**
828 * __alloc_bootmem_node - allocate boot memory from a specific node
829 * @pgdat: node to allocate from
830 * @size: size of the request in bytes
831 * @align: alignment of the region
832 * @goal: preferred starting address of the region
833 *
834 * The goal is dropped if it can not be satisfied and the allocation will
835 * fall back to memory below @goal.
836 *
837 * Allocation may fall back to any node in the system if the specified node
838 * can not hold the requested memory.
839 *
840 * The function panics if the request can not be satisfied.
841 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700842void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
843 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Yinghai Lub8ab9f82010-07-20 13:24:31 -0700845 void *ptr;
846
Pekka Enbergc91c4772009-06-11 08:10:28 +0300847 if (WARN_ON_ONCE(slab_is_available()))
848 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
849
Yinghai Lu08677212010-02-10 01:20:20 -0800850#ifdef CONFIG_NO_BOOTMEM
Yinghai Lub8ab9f82010-07-20 13:24:31 -0700851 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
852 goal, -1ULL);
853 if (ptr)
854 return ptr;
855
856 ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align,
Yinghai Lu08677212010-02-10 01:20:20 -0800857 goal, -1ULL);
858#else
Yinghai Lub8ab9f82010-07-20 13:24:31 -0700859 ptr = ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800860#endif
Yinghai Lub8ab9f82010-07-20 13:24:31 -0700861
862 return ptr;
Yinghai Lu08677212010-02-10 01:20:20 -0800863}
864
865void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
866 unsigned long align, unsigned long goal)
867{
868#ifdef MAX_DMA32_PFN
869 unsigned long end_pfn;
870
871 if (WARN_ON_ONCE(slab_is_available()))
872 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
873
874 /* update goal according ...MAX_DMA32_PFN */
875 end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
876
877 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
878 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
879 void *ptr;
880 unsigned long new_goal;
881
882 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
883#ifdef CONFIG_NO_BOOTMEM
884 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
885 new_goal, -1ULL);
886#else
887 ptr = alloc_bootmem_core(pgdat->bdata, size, align,
888 new_goal, 0);
889#endif
890 if (ptr)
891 return ptr;
892 }
893#endif
894
895 return __alloc_bootmem_node(pgdat, size, align, goal);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700899#ifdef CONFIG_SPARSEMEM
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700900/**
901 * alloc_bootmem_section - allocate boot memory from a specific section
902 * @size: size of the request in bytes
903 * @section_nr: sparse map section to allocate from
904 *
905 * Return NULL on failure.
906 */
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700907void * __init alloc_bootmem_section(unsigned long size,
908 unsigned long section_nr)
909{
Yinghai Lu08677212010-02-10 01:20:20 -0800910#ifdef CONFIG_NO_BOOTMEM
911 unsigned long pfn, goal, limit;
912
913 pfn = section_nr_to_pfn(section_nr);
914 goal = pfn << PAGE_SHIFT;
915 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
916
917 return __alloc_memory_core_early(early_pfn_to_nid(pfn), size,
918 SMP_CACHE_BYTES, goal, limit);
919#else
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700920 bootmem_data_t *bdata;
921 unsigned long pfn, goal, limit;
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700922
923 pfn = section_nr_to_pfn(section_nr);
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700924 goal = pfn << PAGE_SHIFT;
925 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
926 bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700927
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700928 return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
Yinghai Lu08677212010-02-10 01:20:20 -0800929#endif
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700930}
931#endif
932
Andi Kleenb54bbf72008-07-23 21:27:45 -0700933void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
934 unsigned long align, unsigned long goal)
935{
936 void *ptr;
937
Pekka Enbergc91c4772009-06-11 08:10:28 +0300938 if (WARN_ON_ONCE(slab_is_available()))
939 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
940
Yinghai Lu08677212010-02-10 01:20:20 -0800941#ifdef CONFIG_NO_BOOTMEM
942 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
943 goal, -1ULL);
944#else
Tejun Heod0c4f572009-03-01 16:06:56 +0900945 ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
946 if (ptr)
947 return ptr;
948
Andi Kleenb54bbf72008-07-23 21:27:45 -0700949 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800950#endif
Andi Kleenb54bbf72008-07-23 21:27:45 -0700951 if (ptr)
952 return ptr;
953
954 return __alloc_bootmem_nopanic(size, align, goal);
955}
956
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700957#ifndef ARCH_LOW_ADDRESS_LIMIT
958#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
959#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800960
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700961/**
962 * __alloc_bootmem_low - allocate low boot memory
963 * @size: size of the request in bytes
964 * @align: alignment of the region
965 * @goal: preferred starting address of the region
966 *
967 * The goal is dropped if it can not be satisfied and the allocation will
968 * fall back to memory below @goal.
969 *
970 * Allocation may happen on any node in the system.
971 *
972 * The function panics if the request can not be satisfied.
973 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700974void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
975 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800976{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700977 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800978}
979
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700980/**
981 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
982 * @pgdat: node to allocate from
983 * @size: size of the request in bytes
984 * @align: alignment of the region
985 * @goal: preferred starting address of the region
986 *
987 * The goal is dropped if it can not be satisfied and the allocation will
988 * fall back to memory below @goal.
989 *
990 * Allocation may fall back to any node in the system if the specified node
991 * can not hold the requested memory.
992 *
993 * The function panics if the request can not be satisfied.
994 */
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800995void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
996 unsigned long align, unsigned long goal)
997{
Yinghai Lub8ab9f82010-07-20 13:24:31 -0700998 void *ptr;
999
Pekka Enbergc91c4772009-06-11 08:10:28 +03001000 if (WARN_ON_ONCE(slab_is_available()))
1001 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
1002
Yinghai Lu08677212010-02-10 01:20:20 -08001003#ifdef CONFIG_NO_BOOTMEM
Yinghai Lub8ab9f82010-07-20 13:24:31 -07001004 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
1005 goal, ARCH_LOW_ADDRESS_LIMIT);
1006 if (ptr)
1007 return ptr;
1008 ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align,
Yinghai Lu08677212010-02-10 01:20:20 -08001009 goal, ARCH_LOW_ADDRESS_LIMIT);
1010#else
Yinghai Lub8ab9f82010-07-20 13:24:31 -07001011 ptr = ___alloc_bootmem_node(pgdat->bdata, size, align,
Johannes Weiner4cc278b2008-07-23 21:28:08 -07001012 goal, ARCH_LOW_ADDRESS_LIMIT);
Yinghai Lu08677212010-02-10 01:20:20 -08001013#endif
Yinghai Lub8ab9f82010-07-20 13:24:31 -07001014 return ptr;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -08001015}