blob: b6791646143e9a24e7aeaacef415e572e619da45 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* return the number of _pages_ that will be allocated for the boot bitmap */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070040unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 unsigned long mapsize;
43
44 mapsize = (pages+7)/8;
45 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
46 mapsize >>= PAGE_SHIFT;
47
48 return mapsize;
49}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070050
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080051/*
52 * link bdata in order
53 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070054static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080055{
56 bootmem_data_t *ent;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070057
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080058 if (list_empty(&bdata_list)) {
59 list_add(&bdata->list, &bdata_list);
60 return;
61 }
62 /* insert in order */
63 list_for_each_entry(ent, &bdata_list, list) {
64 if (bdata->node_boot_start < ent->node_boot_start) {
65 list_add_tail(&bdata->list, &ent->list);
66 return;
67 }
68 }
69 list_add_tail(&bdata->list, &bdata_list);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080070}
71
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070072/*
73 * Given an initialised bdata, it returns the size of the boot bitmap
74 */
75static unsigned long __init get_mapsize(bootmem_data_t *bdata)
76{
77 unsigned long mapsize;
78 unsigned long start = PFN_DOWN(bdata->node_boot_start);
79 unsigned long end = bdata->node_low_pfn;
80
81 mapsize = ((end - start) + 7) / 8;
82 return ALIGN(mapsize, sizeof(long));
83}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * Called once to set up the allocator itself.
87 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070088static unsigned long __init init_bootmem_core(pg_data_t *pgdat,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 unsigned long mapstart, unsigned long start, unsigned long end)
90{
91 bootmem_data_t *bdata = pgdat->bdata;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070092 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070094 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
95 bdata->node_boot_start = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080097 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /*
100 * Initially all pages are reserved - setup_arch() has to
101 * register free RAM areas explicitly.
102 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700103 mapsize = get_mapsize(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 memset(bdata->node_bootmem_map, 0xff, mapsize);
105
106 return mapsize;
107}
108
109/*
110 * Marks a particular physical memory range as unallocatable. Usable RAM
111 * might be used for boot-time allocations - or it might get added
112 * to the free page pool later on.
113 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700114static int __init can_reserve_bootmem_core(bootmem_data_t *bdata,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800115 unsigned long addr, unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700117 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned long i;
Yinghai Lua5645a62008-03-18 12:49:12 -0700119
120 BUG_ON(!size);
121
122 /* out of range, don't hold other */
123 if (addr + size < bdata->node_boot_start ||
124 PFN_DOWN(addr) > bdata->node_low_pfn)
125 return 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /*
Yinghai Lua5645a62008-03-18 12:49:12 -0700128 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700130 if (addr > bdata->node_boot_start)
131 sidx= PFN_DOWN(addr - bdata->node_boot_start);
132 else
133 sidx = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700134
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700135 eidx = PFN_UP(addr + size - bdata->node_boot_start);
Yinghai Lua5645a62008-03-18 12:49:12 -0700136 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
137 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Yinghai Lua5645a62008-03-18 12:49:12 -0700139 for (i = sidx; i < eidx; i++) {
140 if (test_bit(i, bdata->node_bootmem_map)) {
141 if (flags & BOOTMEM_EXCLUSIVE)
142 return -EBUSY;
143 }
144 }
145
146 return 0;
147
148}
149
150static void __init reserve_bootmem_core(bootmem_data_t *bdata,
151 unsigned long addr, unsigned long size, int flags)
152{
153 unsigned long sidx, eidx;
154 unsigned long i;
155
156 BUG_ON(!size);
157
158 /* out of range */
159 if (addr + size < bdata->node_boot_start ||
160 PFN_DOWN(addr) > bdata->node_low_pfn)
161 return;
162
163 /*
164 * Round up to index to the range.
165 */
166 if (addr > bdata->node_boot_start)
167 sidx= PFN_DOWN(addr - bdata->node_boot_start);
168 else
169 sidx = 0;
170
171 eidx = PFN_UP(addr + size - bdata->node_boot_start);
172 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
173 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
174
175 for (i = sidx; i < eidx; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
177#ifdef CONFIG_DEBUG_BOOTMEM
178 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
179#endif
180 }
Yinghai Lua5645a62008-03-18 12:49:12 -0700181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700184static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
185 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700187 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned long i;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700189
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700190 BUG_ON(!size);
191
192 /* out range */
193 if (addr + size < bdata->node_boot_start ||
194 PFN_DOWN(addr) > bdata->node_low_pfn)
195 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
197 * round down end of usable mem, partially free pages are
198 * considered reserved.
199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700201 if (addr >= bdata->node_boot_start && addr < bdata->last_success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 bdata->last_success = addr;
203
204 /*
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700205 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700207 if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
208 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
209 else
210 sidx = 0;
211
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700212 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700213 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
214 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 for (i = sidx; i < eidx; i++) {
217 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
218 BUG();
219 }
220}
221
222/*
223 * We 'merge' subsequent allocations to save space. We might 'lose'
224 * some fraction of a page if allocations cannot be satisfied due to
225 * size constraints on boxes where there is physical RAM space
226 * fragmentation - in these cases (mostly large memory boxes) this
227 * is not a problem.
228 *
229 * On low memory boxes we get it right in 100% of the cases.
230 *
231 * alignment has to be a power of 2 value.
232 *
233 * NOTE: This function is _not_ reentrant.
234 */
Andi Kleen267b4802006-03-25 16:31:10 +0100235void * __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
Yasunori Goto281dd252005-10-19 15:52:18 -0700237 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700239 unsigned long areasize, preferred;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700240 unsigned long i, start = 0, incr, eidx, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 void *ret;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700242 unsigned long node_boot_start;
243 void *node_bootmem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700245 if (!size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 printk("__alloc_bootmem_core(): zero-sized request\n");
247 BUG();
248 }
249 BUG_ON(align & (align-1));
250
Christian Krafft7c309a62006-12-06 20:32:41 -0800251 /* on nodes without memory - bootmem_map is NULL */
252 if (!bdata->node_bootmem_map)
253 return NULL;
254
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700255 /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
256 node_boot_start = bdata->node_boot_start;
257 node_bootmem_map = bdata->node_bootmem_map;
258 if (align) {
259 node_boot_start = ALIGN(bdata->node_boot_start, align);
260 if (node_boot_start > bdata->node_boot_start)
261 node_bootmem_map = (unsigned long *)bdata->node_bootmem_map +
262 PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG;
263 }
264
265 if (limit && node_boot_start >= limit)
266 return NULL;
267
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700268 end_pfn = bdata->node_low_pfn;
269 limit = PFN_DOWN(limit);
Yasunori Goto281dd252005-10-19 15:52:18 -0700270 if (limit && end_pfn > limit)
271 end_pfn = limit;
272
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700273 eidx = end_pfn - PFN_DOWN(node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * We try to allocate bootmem pages above 'goal'
277 * first, then we try to allocate lower pages.
278 */
Yinghai Luad093152008-03-10 23:23:42 -0700279 preferred = 0;
280 if (goal && PFN_DOWN(goal) < end_pfn) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700281 if (goal > node_boot_start)
282 preferred = goal - node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700284 if (bdata->last_success > node_boot_start &&
285 bdata->last_success - node_boot_start >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700286 if (!limit || (limit && limit > bdata->last_success))
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700287 preferred = bdata->last_success - node_boot_start;
Yinghai Luad093152008-03-10 23:23:42 -0700288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700290 preferred = PFN_DOWN(ALIGN(preferred, align));
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700291 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 incr = align >> PAGE_SHIFT ? : 1;
293
294restart_scan:
Yinghai Luad093152008-03-10 23:23:42 -0700295 for (i = preferred; i < eidx;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 unsigned long j;
Yinghai Luad093152008-03-10 23:23:42 -0700297
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700298 i = find_next_zero_bit(node_bootmem_map, eidx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 i = ALIGN(i, incr);
Haren Myneni66d43e92005-12-12 00:37:39 -0800300 if (i >= eidx)
301 break;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700302 if (test_bit(i, node_bootmem_map)) {
Yinghai Luad093152008-03-10 23:23:42 -0700303 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 continue;
Yinghai Luad093152008-03-10 23:23:42 -0700305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 for (j = i + 1; j < i + areasize; ++j) {
307 if (j >= eidx)
308 goto fail_block;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700309 if (test_bit(j, node_bootmem_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto fail_block;
311 }
312 start = i;
313 goto found;
314 fail_block:
315 i = ALIGN(j, incr);
Yinghai Luad093152008-03-10 23:23:42 -0700316 if (i == j)
317 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700320 if (preferred > 0) {
321 preferred = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 goto restart_scan;
323 }
324 return NULL;
325
326found:
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700327 bdata->last_success = PFN_PHYS(start) + node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 BUG_ON(start >= eidx);
329
330 /*
331 * Is the next page of the previous allocation-end the start
332 * of this allocation's buffer? If yes then we can 'merge'
333 * the previous partial page with this allocation.
334 */
335 if (align < PAGE_SIZE &&
336 bdata->last_offset && bdata->last_pos+1 == start) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700337 unsigned long offset, remaining_size;
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700338 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 BUG_ON(offset > PAGE_SIZE);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700340 remaining_size = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (size < remaining_size) {
342 areasize = 0;
343 /* last_pos unchanged */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700344 bdata->last_offset = offset + size;
345 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700346 offset + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 } else {
348 remaining_size = size - remaining_size;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700349 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
350 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700351 offset + node_boot_start);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700352 bdata->last_pos = start + areasize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 bdata->last_offset = remaining_size;
354 }
355 bdata->last_offset &= ~PAGE_MASK;
356 } else {
357 bdata->last_pos = start + areasize - 1;
358 bdata->last_offset = size & ~PAGE_MASK;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700359 ret = phys_to_virt(start * PAGE_SIZE + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
362 /*
363 * Reserve the area now:
364 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700365 for (i = start; i < start + areasize; i++)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700366 if (unlikely(test_and_set_bit(i, node_bootmem_map)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 BUG();
368 memset(ret, 0, size);
369 return ret;
370}
371
372static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
373{
374 struct page *page;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700375 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 bootmem_data_t *bdata = pgdat->bdata;
377 unsigned long i, count, total = 0;
378 unsigned long idx;
379 unsigned long *map;
380 int gofast = 0;
381
382 BUG_ON(!bdata->node_bootmem_map);
383
384 count = 0;
385 /* first extant page of the node */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700386 pfn = PFN_DOWN(bdata->node_boot_start);
387 idx = bdata->node_low_pfn - pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 map = bdata->node_bootmem_map;
389 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
390 if (bdata->node_boot_start == 0 ||
391 ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG))
392 gofast = 1;
393 for (i = 0; i < idx; ) {
394 unsigned long v = ~map[i / BITS_PER_LONG];
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (gofast && v == ~0UL) {
David Howellsa226f6c2006-01-06 00:11:08 -0800397 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700399 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 count += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 order = ffs(BITS_PER_LONG) - 1;
David Howellsa226f6c2006-01-06 00:11:08 -0800402 __free_pages_bootmem(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 i += BITS_PER_LONG;
404 page += BITS_PER_LONG;
405 } else if (v) {
406 unsigned long m;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700407
408 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 for (m = 1; m && i < idx; m<<=1, page++, i++) {
410 if (v & m) {
411 count++;
David Howellsa226f6c2006-01-06 00:11:08 -0800412 __free_pages_bootmem(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415 } else {
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700416 i += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700418 pfn += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 total += count;
421
422 /*
423 * Now free the allocator bitmap itself, it's not
424 * needed anymore:
425 */
426 page = virt_to_page(bdata->node_bootmem_map);
427 count = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700428 idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
429 for (i = 0; i < idx; i++, page++) {
David Howellsa226f6c2006-01-06 00:11:08 -0800430 __free_pages_bootmem(page, 0);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700431 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 total += count;
434 bdata->node_bootmem_map = NULL;
435
436 return total;
437}
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{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700442 return init_bootmem_core(pgdat, freepfn, startpfn, endpfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700445void __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)
452 return;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800453 reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700456void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
457 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 free_bootmem_core(pgdat->bdata, physaddr, size);
460}
461
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700462unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700464 return free_all_bootmem_core(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700467unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 max_low_pfn = pages;
470 min_low_pfn = start;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700471 return init_bootmem_core(NODE_DATA(0), start, 0, pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800475int __init reserve_bootmem(unsigned long addr, unsigned long size,
476 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Yinghai Lua5645a62008-03-18 12:49:12 -0700478 bootmem_data_t *bdata;
479 int ret;
480
481 list_for_each_entry(bdata, &bdata_list, list) {
482 ret = can_reserve_bootmem_core(bdata, addr, size, flags);
483 if (ret < 0)
484 return ret;
485 }
486 list_for_each_entry(bdata, &bdata_list, list)
487 reserve_bootmem_core(bdata, addr, size, flags);
488
489 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
492
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700493void __init free_bootmem(unsigned long addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700495 bootmem_data_t *bdata;
496 list_for_each_entry(bdata, &bdata_list, list)
497 free_bootmem_core(bdata, addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700500unsigned long __init free_all_bootmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700502 return free_all_bootmem_core(NODE_DATA(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700505void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
506 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800508 bootmem_data_t *bdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 void *ptr;
510
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700511 list_for_each_entry(bdata, &bdata_list, list) {
512 ptr = __alloc_bootmem_core(bdata, size, align, goal, 0);
513 if (ptr)
514 return ptr;
515 }
Andi Kleena8062232006-04-07 19:49:21 +0200516 return NULL;
517}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700519void * __init __alloc_bootmem(unsigned long size, unsigned long align,
520 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200521{
522 void *mem = __alloc_bootmem_nopanic(size,align,goal);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700523
Andi Kleena8062232006-04-07 19:49:21 +0200524 if (mem)
525 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /*
527 * Whoops, we cannot satisfy the allocation request.
528 */
529 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
530 panic("Out of memory");
531 return NULL;
532}
533
Yasunori Goto281dd252005-10-19 15:52:18 -0700534
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700535void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
536 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 void *ptr;
539
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800540 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (ptr)
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700542 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800544 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700547#ifndef ARCH_LOW_ADDRESS_LIMIT
548#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
549#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800550
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700551void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
552 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800553{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800554 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800555 void *ptr;
556
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700557 list_for_each_entry(bdata, &bdata_list, list) {
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700558 ptr = __alloc_bootmem_core(bdata, size, align, goal,
559 ARCH_LOW_ADDRESS_LIMIT);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700560 if (ptr)
561 return ptr;
562 }
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800563
564 /*
565 * Whoops, we cannot satisfy the allocation request.
566 */
567 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
568 panic("Out of low memory");
569 return NULL;
570}
571
572void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
573 unsigned long align, unsigned long goal)
574{
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700575 return __alloc_bootmem_core(pgdat->bdata, size, align, goal,
576 ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800577}