blob: 251c66c5d96a774ac46d3175a44ecb3683568d84 [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 */
Johannes Weiner8ae04462008-07-23 21:26:59 -070090static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 unsigned long mapstart, unsigned long start, unsigned long end)
92{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070093 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Mel Gorman2dbb51c2008-07-23 21:26:52 -070095 mminit_validate_memmodel_limits(&start, &end);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070096 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
97 bdata->node_boot_start = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080099 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /*
102 * Initially all pages are reserved - setup_arch() has to
103 * register free RAM areas explicitly.
104 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700105 mapsize = get_mapsize(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 memset(bdata->node_bootmem_map, 0xff, mapsize);
107
108 return mapsize;
109}
110
111/*
112 * Marks a particular physical memory range as unallocatable. Usable RAM
113 * might be used for boot-time allocations - or it might get added
114 * to the free page pool later on.
115 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700116static int __init can_reserve_bootmem_core(bootmem_data_t *bdata,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800117 unsigned long addr, unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700119 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned long i;
Yinghai Lua5645a62008-03-18 12:49:12 -0700121
122 BUG_ON(!size);
123
124 /* out of range, don't hold other */
125 if (addr + size < bdata->node_boot_start ||
126 PFN_DOWN(addr) > bdata->node_low_pfn)
127 return 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /*
Yinghai Lua5645a62008-03-18 12:49:12 -0700130 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700132 if (addr > bdata->node_boot_start)
133 sidx= PFN_DOWN(addr - bdata->node_boot_start);
134 else
135 sidx = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700136
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700137 eidx = PFN_UP(addr + size - bdata->node_boot_start);
Yinghai Lua5645a62008-03-18 12:49:12 -0700138 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
139 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Yinghai Lua5645a62008-03-18 12:49:12 -0700141 for (i = sidx; i < eidx; i++) {
142 if (test_bit(i, bdata->node_bootmem_map)) {
143 if (flags & BOOTMEM_EXCLUSIVE)
144 return -EBUSY;
145 }
146 }
147
148 return 0;
149
150}
151
152static void __init reserve_bootmem_core(bootmem_data_t *bdata,
153 unsigned long addr, unsigned long size, int flags)
154{
155 unsigned long sidx, eidx;
156 unsigned long i;
157
158 BUG_ON(!size);
159
160 /* out of range */
161 if (addr + size < bdata->node_boot_start ||
162 PFN_DOWN(addr) > bdata->node_low_pfn)
163 return;
164
165 /*
166 * Round up to index to the range.
167 */
168 if (addr > bdata->node_boot_start)
169 sidx= PFN_DOWN(addr - bdata->node_boot_start);
170 else
171 sidx = 0;
172
173 eidx = PFN_UP(addr + size - bdata->node_boot_start);
174 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
175 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
176
177 for (i = sidx; i < eidx; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
179#ifdef CONFIG_DEBUG_BOOTMEM
180 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
181#endif
182 }
Yinghai Lua5645a62008-03-18 12:49:12 -0700183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700186static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
187 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700189 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned long i;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700191
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700192 BUG_ON(!size);
193
194 /* out range */
195 if (addr + size < bdata->node_boot_start ||
196 PFN_DOWN(addr) > bdata->node_low_pfn)
197 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /*
199 * round down end of usable mem, partially free pages are
200 * considered reserved.
201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700203 if (addr >= bdata->node_boot_start && addr < bdata->last_success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 bdata->last_success = addr;
205
206 /*
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700207 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700209 if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
210 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
211 else
212 sidx = 0;
213
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700214 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700215 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
216 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 for (i = sidx; i < eidx; i++) {
219 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
220 BUG();
221 }
222}
223
224/*
225 * We 'merge' subsequent allocations to save space. We might 'lose'
226 * some fraction of a page if allocations cannot be satisfied due to
227 * size constraints on boxes where there is physical RAM space
228 * fragmentation - in these cases (mostly large memory boxes) this
229 * is not a problem.
230 *
231 * On low memory boxes we get it right in 100% of the cases.
232 *
233 * alignment has to be a power of 2 value.
234 *
235 * NOTE: This function is _not_ reentrant.
236 */
Andi Kleen267b4802006-03-25 16:31:10 +0100237void * __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
Yasunori Goto281dd252005-10-19 15:52:18 -0700239 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700241 unsigned long areasize, preferred;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700242 unsigned long i, start = 0, incr, eidx, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 void *ret;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700244 unsigned long node_boot_start;
245 void *node_bootmem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700247 if (!size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 printk("__alloc_bootmem_core(): zero-sized request\n");
249 BUG();
250 }
251 BUG_ON(align & (align-1));
252
Christian Krafft7c309a62006-12-06 20:32:41 -0800253 /* on nodes without memory - bootmem_map is NULL */
254 if (!bdata->node_bootmem_map)
255 return NULL;
256
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700257 /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
258 node_boot_start = bdata->node_boot_start;
259 node_bootmem_map = bdata->node_bootmem_map;
260 if (align) {
261 node_boot_start = ALIGN(bdata->node_boot_start, align);
262 if (node_boot_start > bdata->node_boot_start)
263 node_bootmem_map = (unsigned long *)bdata->node_bootmem_map +
264 PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG;
265 }
266
267 if (limit && node_boot_start >= limit)
268 return NULL;
269
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700270 end_pfn = bdata->node_low_pfn;
271 limit = PFN_DOWN(limit);
Yasunori Goto281dd252005-10-19 15:52:18 -0700272 if (limit && end_pfn > limit)
273 end_pfn = limit;
274
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700275 eidx = end_pfn - PFN_DOWN(node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /*
278 * We try to allocate bootmem pages above 'goal'
279 * first, then we try to allocate lower pages.
280 */
Yinghai Luad093152008-03-10 23:23:42 -0700281 preferred = 0;
282 if (goal && PFN_DOWN(goal) < end_pfn) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700283 if (goal > node_boot_start)
284 preferred = goal - node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700286 if (bdata->last_success > node_boot_start &&
287 bdata->last_success - node_boot_start >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700288 if (!limit || (limit && limit > bdata->last_success))
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700289 preferred = bdata->last_success - node_boot_start;
Yinghai Luad093152008-03-10 23:23:42 -0700290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700292 preferred = PFN_DOWN(ALIGN(preferred, align));
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700293 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 incr = align >> PAGE_SHIFT ? : 1;
295
296restart_scan:
Yinghai Luad093152008-03-10 23:23:42 -0700297 for (i = preferred; i < eidx;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 unsigned long j;
Yinghai Luad093152008-03-10 23:23:42 -0700299
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700300 i = find_next_zero_bit(node_bootmem_map, eidx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 i = ALIGN(i, incr);
Haren Myneni66d43e92005-12-12 00:37:39 -0800302 if (i >= eidx)
303 break;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700304 if (test_bit(i, node_bootmem_map)) {
Yinghai Luad093152008-03-10 23:23:42 -0700305 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 continue;
Yinghai Luad093152008-03-10 23:23:42 -0700307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 for (j = i + 1; j < i + areasize; ++j) {
309 if (j >= eidx)
310 goto fail_block;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700311 if (test_bit(j, node_bootmem_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto fail_block;
313 }
314 start = i;
315 goto found;
316 fail_block:
317 i = ALIGN(j, incr);
Yinghai Luad093152008-03-10 23:23:42 -0700318 if (i == j)
319 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700322 if (preferred > 0) {
323 preferred = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 goto restart_scan;
325 }
326 return NULL;
327
328found:
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700329 bdata->last_success = PFN_PHYS(start) + node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 BUG_ON(start >= eidx);
331
332 /*
333 * Is the next page of the previous allocation-end the start
334 * of this allocation's buffer? If yes then we can 'merge'
335 * the previous partial page with this allocation.
336 */
337 if (align < PAGE_SIZE &&
338 bdata->last_offset && bdata->last_pos+1 == start) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700339 unsigned long offset, remaining_size;
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700340 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 BUG_ON(offset > PAGE_SIZE);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700342 remaining_size = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (size < remaining_size) {
344 areasize = 0;
345 /* last_pos unchanged */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700346 bdata->last_offset = offset + size;
347 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700348 offset + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 } else {
350 remaining_size = size - remaining_size;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700351 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
352 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700353 offset + node_boot_start);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700354 bdata->last_pos = start + areasize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 bdata->last_offset = remaining_size;
356 }
357 bdata->last_offset &= ~PAGE_MASK;
358 } else {
359 bdata->last_pos = start + areasize - 1;
360 bdata->last_offset = size & ~PAGE_MASK;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700361 ret = phys_to_virt(start * PAGE_SIZE + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 /*
365 * Reserve the area now:
366 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700367 for (i = start; i < start + areasize; i++)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700368 if (unlikely(test_and_set_bit(i, node_bootmem_map)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 BUG();
370 memset(ret, 0, size);
371 return ret;
372}
373
Johannes Weiner8ae04462008-07-23 21:26:59 -0700374static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct page *page;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700377 unsigned long pfn;
Johannes Weiner6b312c02008-07-23 21:26:58 -0700378 unsigned long i, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned long idx;
380 unsigned long *map;
381 int gofast = 0;
382
383 BUG_ON(!bdata->node_bootmem_map);
384
385 count = 0;
386 /* first extant page of the node */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700387 pfn = PFN_DOWN(bdata->node_boot_start);
388 idx = bdata->node_low_pfn - pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 map = bdata->node_bootmem_map;
Johannes Weiner6b312c02008-07-23 21:26:58 -0700390 /*
391 * Check if we are aligned to BITS_PER_LONG pages. If so, we might
392 * be able to free page orders of that size at once.
393 */
394 if (!(pfn & (BITS_PER_LONG-1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 gofast = 1;
Johannes Weiner6b312c02008-07-23 21:26:58 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 for (i = 0; i < idx; ) {
398 unsigned long v = ~map[i / BITS_PER_LONG];
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (gofast && v == ~0UL) {
David Howellsa226f6c2006-01-06 00:11:08 -0800401 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700403 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 count += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 order = ffs(BITS_PER_LONG) - 1;
David Howellsa226f6c2006-01-06 00:11:08 -0800406 __free_pages_bootmem(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 i += BITS_PER_LONG;
408 page += BITS_PER_LONG;
409 } else if (v) {
410 unsigned long m;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700411
412 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 for (m = 1; m && i < idx; m<<=1, page++, i++) {
414 if (v & m) {
415 count++;
David Howellsa226f6c2006-01-06 00:11:08 -0800416 __free_pages_bootmem(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418 }
419 } else {
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700420 i += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700422 pfn += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /*
426 * Now free the allocator bitmap itself, it's not
427 * needed anymore:
428 */
429 page = virt_to_page(bdata->node_bootmem_map);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700430 idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
Johannes Weiner6b312c02008-07-23 21:26:58 -0700431 for (i = 0; i < idx; i++, page++)
David Howellsa226f6c2006-01-06 00:11:08 -0800432 __free_pages_bootmem(page, 0);
Johannes Weiner6b312c02008-07-23 21:26:58 -0700433 count += i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 bdata->node_bootmem_map = NULL;
435
Johannes Weiner6b312c02008-07-23 21:26:58 -0700436 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700439unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700440 unsigned long startpfn, unsigned long endpfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Johannes Weiner8ae04462008-07-23 21:26:59 -0700442 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Bernhard Walle71c27422008-06-21 19:01:02 +0200445int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800446 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Yinghai Lua5645a62008-03-18 12:49:12 -0700448 int ret;
449
450 ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
451 if (ret < 0)
Bernhard Walle71c27422008-06-21 19:01:02 +0200452 return -ENOMEM;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800453 reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
Bernhard Walle71c27422008-06-21 19:01:02 +0200454
455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700458void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
459 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 free_bootmem_core(pgdat->bdata, physaddr, size);
462}
463
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700464unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Yasunori Goto04753272008-04-28 02:13:31 -0700466 register_page_bootmem_info_node(pgdat);
Johannes Weiner8ae04462008-07-23 21:26:59 -0700467 return free_all_bootmem_core(pgdat->bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700470unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 max_low_pfn = pages;
473 min_low_pfn = start;
Johannes Weiner8ae04462008-07-23 21:26:59 -0700474 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800478int __init reserve_bootmem(unsigned long addr, unsigned long size,
479 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Yinghai Lua5645a62008-03-18 12:49:12 -0700481 bootmem_data_t *bdata;
482 int ret;
483
484 list_for_each_entry(bdata, &bdata_list, list) {
485 ret = can_reserve_bootmem_core(bdata, addr, size, flags);
486 if (ret < 0)
487 return ret;
488 }
489 list_for_each_entry(bdata, &bdata_list, list)
490 reserve_bootmem_core(bdata, addr, size, flags);
491
492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
495
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700496void __init free_bootmem(unsigned long addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700498 bootmem_data_t *bdata;
499 list_for_each_entry(bdata, &bdata_list, list)
500 free_bootmem_core(bdata, addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700503unsigned long __init free_all_bootmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Johannes Weiner8ae04462008-07-23 21:26:59 -0700505 return free_all_bootmem_core(NODE_DATA(0)->bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700508void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
509 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800511 bootmem_data_t *bdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 void *ptr;
513
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700514 list_for_each_entry(bdata, &bdata_list, list) {
515 ptr = __alloc_bootmem_core(bdata, size, align, goal, 0);
516 if (ptr)
517 return ptr;
518 }
Andi Kleena8062232006-04-07 19:49:21 +0200519 return NULL;
520}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700522void * __init __alloc_bootmem(unsigned long size, unsigned long align,
523 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200524{
525 void *mem = __alloc_bootmem_nopanic(size,align,goal);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700526
Andi Kleena8062232006-04-07 19:49:21 +0200527 if (mem)
528 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
530 * Whoops, we cannot satisfy the allocation request.
531 */
532 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
533 panic("Out of memory");
534 return NULL;
535}
536
Yasunori Goto281dd252005-10-19 15:52:18 -0700537
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700538void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
539 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 void *ptr;
542
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800543 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (ptr)
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700545 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800547 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700550#ifdef CONFIG_SPARSEMEM
551void * __init alloc_bootmem_section(unsigned long size,
552 unsigned long section_nr)
553{
554 void *ptr;
555 unsigned long limit, goal, start_nr, end_nr, pfn;
556 struct pglist_data *pgdat;
557
558 pfn = section_nr_to_pfn(section_nr);
559 goal = PFN_PHYS(pfn);
560 limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1;
561 pgdat = NODE_DATA(early_pfn_to_nid(pfn));
562 ptr = __alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal,
563 limit);
564
565 if (!ptr)
566 return NULL;
567
568 start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr)));
569 end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size));
570 if (start_nr != section_nr || end_nr != section_nr) {
571 printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n",
572 section_nr);
573 free_bootmem_core(pgdat->bdata, __pa(ptr), size);
574 ptr = NULL;
575 }
576
577 return ptr;
578}
579#endif
580
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700581#ifndef ARCH_LOW_ADDRESS_LIMIT
582#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
583#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800584
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700585void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
586 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800587{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800588 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800589 void *ptr;
590
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700591 list_for_each_entry(bdata, &bdata_list, list) {
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700592 ptr = __alloc_bootmem_core(bdata, size, align, goal,
593 ARCH_LOW_ADDRESS_LIMIT);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700594 if (ptr)
595 return ptr;
596 }
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800597
598 /*
599 * Whoops, we cannot satisfy the allocation request.
600 */
601 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
602 panic("Out of low memory");
603 return NULL;
604}
605
606void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
607 unsigned long align, unsigned long goal)
608{
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700609 return __alloc_bootmem_core(pgdat->bdata, size, align, goal,
610 ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800611}