blob: 319a79bce7cd5e23314a91aca0fa87701934f7fe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/bootmem.c
3 *
4 * Copyright (C) 1999 Ingo Molnar
5 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
6 *
7 * simple boot-time physical memory area allocator and
8 * free memory collector. It's used to deal with reserved
9 * system memory and memory holes as well.
10 */
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
22/*
23 * Access to this subsystem has to be serialized externally. (this is
24 * true for the boot process anyway)
25 */
26unsigned long max_low_pfn;
27unsigned long min_low_pfn;
28unsigned long max_pfn;
29
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080030static LIST_HEAD(bdata_list);
Vivek Goyal92aa63a2005-06-25 14:58:18 -070031#ifdef CONFIG_CRASH_DUMP
32/*
33 * If we have booted due to a crash, max_pfn will be a very low value. We need
34 * to know the amount of memory that the previous kernel used.
35 */
36unsigned long saved_max_pfn;
37#endif
38
Johannes Weinerb61bfa32008-07-23 21:26:55 -070039bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* return the number of _pages_ that will be allocated for the boot bitmap */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070042unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 unsigned long mapsize;
45
46 mapsize = (pages+7)/8;
47 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
48 mapsize >>= PAGE_SHIFT;
49
50 return mapsize;
51}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070052
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080053/*
54 * link bdata in order
55 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070056static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080057{
58 bootmem_data_t *ent;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070059
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080060 if (list_empty(&bdata_list)) {
61 list_add(&bdata->list, &bdata_list);
62 return;
63 }
64 /* insert in order */
65 list_for_each_entry(ent, &bdata_list, list) {
66 if (bdata->node_boot_start < ent->node_boot_start) {
67 list_add_tail(&bdata->list, &ent->list);
68 return;
69 }
70 }
71 list_add_tail(&bdata->list, &bdata_list);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080072}
73
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070074/*
75 * Given an initialised bdata, it returns the size of the boot bitmap
76 */
77static unsigned long __init get_mapsize(bootmem_data_t *bdata)
78{
79 unsigned long mapsize;
80 unsigned long start = PFN_DOWN(bdata->node_boot_start);
81 unsigned long end = bdata->node_low_pfn;
82
83 mapsize = ((end - start) + 7) / 8;
84 return ALIGN(mapsize, sizeof(long));
85}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Called once to set up the allocator itself.
89 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070090static unsigned long __init init_bootmem_core(pg_data_t *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 unsigned long mapstart, unsigned long start, unsigned long end)
92{
93 bootmem_data_t *bdata = pgdat->bdata;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070094 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Mel Gorman2dbb51c2008-07-23 21:26:52 -070096 mminit_validate_memmodel_limits(&start, &end);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070097 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
98 bdata->node_boot_start = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800100 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /*
103 * Initially all pages are reserved - setup_arch() has to
104 * register free RAM areas explicitly.
105 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700106 mapsize = get_mapsize(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 memset(bdata->node_bootmem_map, 0xff, mapsize);
108
109 return mapsize;
110}
111
112/*
113 * Marks a particular physical memory range as unallocatable. Usable RAM
114 * might be used for boot-time allocations - or it might get added
115 * to the free page pool later on.
116 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700117static int __init can_reserve_bootmem_core(bootmem_data_t *bdata,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800118 unsigned long addr, unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700120 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned long i;
Yinghai Lua5645a62008-03-18 12:49:12 -0700122
123 BUG_ON(!size);
124
125 /* out of range, don't hold other */
126 if (addr + size < bdata->node_boot_start ||
127 PFN_DOWN(addr) > bdata->node_low_pfn)
128 return 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /*
Yinghai Lua5645a62008-03-18 12:49:12 -0700131 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700133 if (addr > bdata->node_boot_start)
134 sidx= PFN_DOWN(addr - bdata->node_boot_start);
135 else
136 sidx = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700137
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700138 eidx = PFN_UP(addr + size - bdata->node_boot_start);
Yinghai Lua5645a62008-03-18 12:49:12 -0700139 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
140 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Yinghai Lua5645a62008-03-18 12:49:12 -0700142 for (i = sidx; i < eidx; i++) {
143 if (test_bit(i, bdata->node_bootmem_map)) {
144 if (flags & BOOTMEM_EXCLUSIVE)
145 return -EBUSY;
146 }
147 }
148
149 return 0;
150
151}
152
153static void __init reserve_bootmem_core(bootmem_data_t *bdata,
154 unsigned long addr, unsigned long size, int flags)
155{
156 unsigned long sidx, eidx;
157 unsigned long i;
158
159 BUG_ON(!size);
160
161 /* out of range */
162 if (addr + size < bdata->node_boot_start ||
163 PFN_DOWN(addr) > bdata->node_low_pfn)
164 return;
165
166 /*
167 * Round up to index to the range.
168 */
169 if (addr > bdata->node_boot_start)
170 sidx= PFN_DOWN(addr - bdata->node_boot_start);
171 else
172 sidx = 0;
173
174 eidx = PFN_UP(addr + size - bdata->node_boot_start);
175 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
176 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
177
178 for (i = sidx; i < eidx; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
180#ifdef CONFIG_DEBUG_BOOTMEM
181 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
182#endif
183 }
Yinghai Lua5645a62008-03-18 12:49:12 -0700184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700187static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
188 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700190 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 unsigned long i;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700192
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700193 BUG_ON(!size);
194
195 /* out range */
196 if (addr + size < bdata->node_boot_start ||
197 PFN_DOWN(addr) > bdata->node_low_pfn)
198 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /*
200 * round down end of usable mem, partially free pages are
201 * considered reserved.
202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700204 if (addr >= bdata->node_boot_start && addr < bdata->last_success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 bdata->last_success = addr;
206
207 /*
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700208 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700210 if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
211 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
212 else
213 sidx = 0;
214
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700215 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700216 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
217 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 for (i = sidx; i < eidx; i++) {
220 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
221 BUG();
222 }
223}
224
225/*
226 * We 'merge' subsequent allocations to save space. We might 'lose'
227 * some fraction of a page if allocations cannot be satisfied due to
228 * size constraints on boxes where there is physical RAM space
229 * fragmentation - in these cases (mostly large memory boxes) this
230 * is not a problem.
231 *
232 * On low memory boxes we get it right in 100% of the cases.
233 *
234 * alignment has to be a power of 2 value.
235 *
236 * NOTE: This function is _not_ reentrant.
237 */
Andi Kleen267b4802006-03-25 16:31:10 +0100238void * __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
Yasunori Goto281dd252005-10-19 15:52:18 -0700240 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700242 unsigned long areasize, preferred;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700243 unsigned long i, start = 0, incr, eidx, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 void *ret;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700245 unsigned long node_boot_start;
246 void *node_bootmem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700248 if (!size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 printk("__alloc_bootmem_core(): zero-sized request\n");
250 BUG();
251 }
252 BUG_ON(align & (align-1));
253
Christian Krafft7c309a62006-12-06 20:32:41 -0800254 /* on nodes without memory - bootmem_map is NULL */
255 if (!bdata->node_bootmem_map)
256 return NULL;
257
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700258 /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
259 node_boot_start = bdata->node_boot_start;
260 node_bootmem_map = bdata->node_bootmem_map;
261 if (align) {
262 node_boot_start = ALIGN(bdata->node_boot_start, align);
263 if (node_boot_start > bdata->node_boot_start)
264 node_bootmem_map = (unsigned long *)bdata->node_bootmem_map +
265 PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG;
266 }
267
268 if (limit && node_boot_start >= limit)
269 return NULL;
270
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700271 end_pfn = bdata->node_low_pfn;
272 limit = PFN_DOWN(limit);
Yasunori Goto281dd252005-10-19 15:52:18 -0700273 if (limit && end_pfn > limit)
274 end_pfn = limit;
275
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700276 eidx = end_pfn - PFN_DOWN(node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /*
279 * We try to allocate bootmem pages above 'goal'
280 * first, then we try to allocate lower pages.
281 */
Yinghai Luad093152008-03-10 23:23:42 -0700282 preferred = 0;
283 if (goal && PFN_DOWN(goal) < end_pfn) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700284 if (goal > node_boot_start)
285 preferred = goal - node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700287 if (bdata->last_success > node_boot_start &&
288 bdata->last_success - node_boot_start >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700289 if (!limit || (limit && limit > bdata->last_success))
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700290 preferred = bdata->last_success - node_boot_start;
Yinghai Luad093152008-03-10 23:23:42 -0700291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700293 preferred = PFN_DOWN(ALIGN(preferred, align));
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700294 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 incr = align >> PAGE_SHIFT ? : 1;
296
297restart_scan:
Yinghai Luad093152008-03-10 23:23:42 -0700298 for (i = preferred; i < eidx;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unsigned long j;
Yinghai Luad093152008-03-10 23:23:42 -0700300
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700301 i = find_next_zero_bit(node_bootmem_map, eidx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 i = ALIGN(i, incr);
Haren Myneni66d43e92005-12-12 00:37:39 -0800303 if (i >= eidx)
304 break;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700305 if (test_bit(i, node_bootmem_map)) {
Yinghai Luad093152008-03-10 23:23:42 -0700306 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 continue;
Yinghai Luad093152008-03-10 23:23:42 -0700308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 for (j = i + 1; j < i + areasize; ++j) {
310 if (j >= eidx)
311 goto fail_block;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700312 if (test_bit(j, node_bootmem_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto fail_block;
314 }
315 start = i;
316 goto found;
317 fail_block:
318 i = ALIGN(j, incr);
Yinghai Luad093152008-03-10 23:23:42 -0700319 if (i == j)
320 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700323 if (preferred > 0) {
324 preferred = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto restart_scan;
326 }
327 return NULL;
328
329found:
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700330 bdata->last_success = PFN_PHYS(start) + node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 BUG_ON(start >= eidx);
332
333 /*
334 * Is the next page of the previous allocation-end the start
335 * of this allocation's buffer? If yes then we can 'merge'
336 * the previous partial page with this allocation.
337 */
338 if (align < PAGE_SIZE &&
339 bdata->last_offset && bdata->last_pos+1 == start) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700340 unsigned long offset, remaining_size;
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700341 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 BUG_ON(offset > PAGE_SIZE);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700343 remaining_size = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (size < remaining_size) {
345 areasize = 0;
346 /* last_pos unchanged */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700347 bdata->last_offset = offset + size;
348 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700349 offset + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 } else {
351 remaining_size = size - remaining_size;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700352 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
353 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700354 offset + node_boot_start);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700355 bdata->last_pos = start + areasize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 bdata->last_offset = remaining_size;
357 }
358 bdata->last_offset &= ~PAGE_MASK;
359 } else {
360 bdata->last_pos = start + areasize - 1;
361 bdata->last_offset = size & ~PAGE_MASK;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700362 ret = phys_to_virt(start * PAGE_SIZE + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
365 /*
366 * Reserve the area now:
367 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700368 for (i = start; i < start + areasize; i++)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700369 if (unlikely(test_and_set_bit(i, node_bootmem_map)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 BUG();
371 memset(ret, 0, size);
372 return ret;
373}
374
375static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
376{
377 struct page *page;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700378 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 bootmem_data_t *bdata = pgdat->bdata;
Johannes Weiner6b312c02008-07-23 21:26:58 -0700380 unsigned long i, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 unsigned long idx;
382 unsigned long *map;
383 int gofast = 0;
384
385 BUG_ON(!bdata->node_bootmem_map);
386
387 count = 0;
388 /* first extant page of the node */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700389 pfn = PFN_DOWN(bdata->node_boot_start);
390 idx = bdata->node_low_pfn - pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 map = bdata->node_bootmem_map;
Johannes Weiner6b312c02008-07-23 21:26:58 -0700392 /*
393 * Check if we are aligned to BITS_PER_LONG pages. If so, we might
394 * be able to free page orders of that size at once.
395 */
396 if (!(pfn & (BITS_PER_LONG-1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 gofast = 1;
Johannes Weiner6b312c02008-07-23 21:26:58 -0700398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 for (i = 0; i < idx; ) {
400 unsigned long v = ~map[i / BITS_PER_LONG];
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (gofast && v == ~0UL) {
David Howellsa226f6c2006-01-06 00:11:08 -0800403 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700405 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 count += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 order = ffs(BITS_PER_LONG) - 1;
David Howellsa226f6c2006-01-06 00:11:08 -0800408 __free_pages_bootmem(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 i += BITS_PER_LONG;
410 page += BITS_PER_LONG;
411 } else if (v) {
412 unsigned long m;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700413
414 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 for (m = 1; m && i < idx; m<<=1, page++, i++) {
416 if (v & m) {
417 count++;
David Howellsa226f6c2006-01-06 00:11:08 -0800418 __free_pages_bootmem(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 }
421 } else {
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700422 i += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700424 pfn += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /*
428 * Now free the allocator bitmap itself, it's not
429 * needed anymore:
430 */
431 page = virt_to_page(bdata->node_bootmem_map);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700432 idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
Johannes Weiner6b312c02008-07-23 21:26:58 -0700433 for (i = 0; i < idx; i++, page++)
David Howellsa226f6c2006-01-06 00:11:08 -0800434 __free_pages_bootmem(page, 0);
Johannes Weiner6b312c02008-07-23 21:26:58 -0700435 count += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 bdata->node_bootmem_map = NULL;
437
Johannes Weiner6b312c02008-07-23 21:26:58 -0700438 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700441unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700442 unsigned long startpfn, unsigned long endpfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700444 return init_bootmem_core(pgdat, freepfn, startpfn, endpfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Bernhard Walle71c27422008-06-21 19:01:02 +0200447int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800448 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Yinghai Lua5645a62008-03-18 12:49:12 -0700450 int ret;
451
452 ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
453 if (ret < 0)
Bernhard Walle71c27422008-06-21 19:01:02 +0200454 return -ENOMEM;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800455 reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
Bernhard Walle71c27422008-06-21 19:01:02 +0200456
457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700460void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
461 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 free_bootmem_core(pgdat->bdata, physaddr, size);
464}
465
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700466unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Yasunori Goto04753272008-04-28 02:13:31 -0700468 register_page_bootmem_info_node(pgdat);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700469 return free_all_bootmem_core(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700472unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 max_low_pfn = pages;
475 min_low_pfn = start;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700476 return init_bootmem_core(NODE_DATA(0), start, 0, pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800480int __init reserve_bootmem(unsigned long addr, unsigned long size,
481 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Yinghai Lua5645a62008-03-18 12:49:12 -0700483 bootmem_data_t *bdata;
484 int ret;
485
486 list_for_each_entry(bdata, &bdata_list, list) {
487 ret = can_reserve_bootmem_core(bdata, addr, size, flags);
488 if (ret < 0)
489 return ret;
490 }
491 list_for_each_entry(bdata, &bdata_list, list)
492 reserve_bootmem_core(bdata, addr, size, flags);
493
494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
497
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700498void __init free_bootmem(unsigned long addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700500 bootmem_data_t *bdata;
501 list_for_each_entry(bdata, &bdata_list, list)
502 free_bootmem_core(bdata, addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700505unsigned long __init free_all_bootmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700507 return free_all_bootmem_core(NODE_DATA(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700510void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
511 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800513 bootmem_data_t *bdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 void *ptr;
515
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700516 list_for_each_entry(bdata, &bdata_list, list) {
517 ptr = __alloc_bootmem_core(bdata, size, align, goal, 0);
518 if (ptr)
519 return ptr;
520 }
Andi Kleena8062232006-04-07 19:49:21 +0200521 return NULL;
522}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700524void * __init __alloc_bootmem(unsigned long size, unsigned long align,
525 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200526{
527 void *mem = __alloc_bootmem_nopanic(size,align,goal);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700528
Andi Kleena8062232006-04-07 19:49:21 +0200529 if (mem)
530 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /*
532 * Whoops, we cannot satisfy the allocation request.
533 */
534 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
535 panic("Out of memory");
536 return NULL;
537}
538
Yasunori Goto281dd252005-10-19 15:52:18 -0700539
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700540void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
541 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 void *ptr;
544
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800545 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (ptr)
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700547 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800549 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700552#ifdef CONFIG_SPARSEMEM
553void * __init alloc_bootmem_section(unsigned long size,
554 unsigned long section_nr)
555{
556 void *ptr;
557 unsigned long limit, goal, start_nr, end_nr, pfn;
558 struct pglist_data *pgdat;
559
560 pfn = section_nr_to_pfn(section_nr);
561 goal = PFN_PHYS(pfn);
562 limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1;
563 pgdat = NODE_DATA(early_pfn_to_nid(pfn));
564 ptr = __alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal,
565 limit);
566
567 if (!ptr)
568 return NULL;
569
570 start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr)));
571 end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size));
572 if (start_nr != section_nr || end_nr != section_nr) {
573 printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n",
574 section_nr);
575 free_bootmem_core(pgdat->bdata, __pa(ptr), size);
576 ptr = NULL;
577 }
578
579 return ptr;
580}
581#endif
582
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700583#ifndef ARCH_LOW_ADDRESS_LIMIT
584#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
585#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800586
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700587void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
588 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800589{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800590 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800591 void *ptr;
592
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700593 list_for_each_entry(bdata, &bdata_list, list) {
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700594 ptr = __alloc_bootmem_core(bdata, size, align, goal,
595 ARCH_LOW_ADDRESS_LIMIT);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700596 if (ptr)
597 return ptr;
598 }
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800599
600 /*
601 * Whoops, we cannot satisfy the allocation request.
602 */
603 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
604 panic("Out of low memory");
605 return NULL;
606}
607
608void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
609 unsigned long align, unsigned long goal)
610{
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700611 return __alloc_bootmem_core(pgdat->bdata, size, align, goal,
612 ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800613}