blob: 9f4bbc5da73fde70173527a2d9e18efe93528c72 [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
Mel Gorman2dbb51c2008-07-23 21:26:52 -070094 mminit_validate_memmodel_limits(&start, &end);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070095 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
96 bdata->node_boot_start = PFN_PHYS(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080098 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /*
101 * Initially all pages are reserved - setup_arch() has to
102 * register free RAM areas explicitly.
103 */
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700104 mapsize = get_mapsize(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 memset(bdata->node_bootmem_map, 0xff, mapsize);
106
107 return mapsize;
108}
109
110/*
111 * Marks a particular physical memory range as unallocatable. Usable RAM
112 * might be used for boot-time allocations - or it might get added
113 * to the free page pool later on.
114 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700115static int __init can_reserve_bootmem_core(bootmem_data_t *bdata,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800116 unsigned long addr, unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700118 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned long i;
Yinghai Lua5645a62008-03-18 12:49:12 -0700120
121 BUG_ON(!size);
122
123 /* out of range, don't hold other */
124 if (addr + size < bdata->node_boot_start ||
125 PFN_DOWN(addr) > bdata->node_low_pfn)
126 return 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /*
Yinghai Lua5645a62008-03-18 12:49:12 -0700129 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
Yinghai Lua5645a62008-03-18 12:49:12 -0700131 if (addr > bdata->node_boot_start)
132 sidx= PFN_DOWN(addr - bdata->node_boot_start);
133 else
134 sidx = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700135
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700136 eidx = PFN_UP(addr + size - bdata->node_boot_start);
Yinghai Lua5645a62008-03-18 12:49:12 -0700137 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
138 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Yinghai Lua5645a62008-03-18 12:49:12 -0700140 for (i = sidx; i < eidx; i++) {
141 if (test_bit(i, bdata->node_bootmem_map)) {
142 if (flags & BOOTMEM_EXCLUSIVE)
143 return -EBUSY;
144 }
145 }
146
147 return 0;
148
149}
150
151static void __init reserve_bootmem_core(bootmem_data_t *bdata,
152 unsigned long addr, unsigned long size, int flags)
153{
154 unsigned long sidx, eidx;
155 unsigned long i;
156
157 BUG_ON(!size);
158
159 /* out of range */
160 if (addr + size < bdata->node_boot_start ||
161 PFN_DOWN(addr) > bdata->node_low_pfn)
162 return;
163
164 /*
165 * Round up to index to the range.
166 */
167 if (addr > bdata->node_boot_start)
168 sidx= PFN_DOWN(addr - bdata->node_boot_start);
169 else
170 sidx = 0;
171
172 eidx = PFN_UP(addr + size - bdata->node_boot_start);
173 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
174 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
175
176 for (i = sidx; i < eidx; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
178#ifdef CONFIG_DEBUG_BOOTMEM
179 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
180#endif
181 }
Yinghai Lua5645a62008-03-18 12:49:12 -0700182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700185static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
186 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700188 unsigned long sidx, eidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned long i;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700190
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700191 BUG_ON(!size);
192
193 /* out range */
194 if (addr + size < bdata->node_boot_start ||
195 PFN_DOWN(addr) > bdata->node_low_pfn)
196 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /*
198 * round down end of usable mem, partially free pages are
199 * considered reserved.
200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700202 if (addr >= bdata->node_boot_start && addr < bdata->last_success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 bdata->last_success = addr;
204
205 /*
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700206 * Round up to index to the range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700208 if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
209 sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
210 else
211 sidx = 0;
212
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700213 eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700214 if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
215 eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 for (i = sidx; i < eidx; i++) {
218 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
219 BUG();
220 }
221}
222
223/*
224 * We 'merge' subsequent allocations to save space. We might 'lose'
225 * some fraction of a page if allocations cannot be satisfied due to
226 * size constraints on boxes where there is physical RAM space
227 * fragmentation - in these cases (mostly large memory boxes) this
228 * is not a problem.
229 *
230 * On low memory boxes we get it right in 100% of the cases.
231 *
232 * alignment has to be a power of 2 value.
233 *
234 * NOTE: This function is _not_ reentrant.
235 */
Andi Kleen267b4802006-03-25 16:31:10 +0100236void * __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
Yasunori Goto281dd252005-10-19 15:52:18 -0700238 unsigned long align, unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700240 unsigned long areasize, preferred;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700241 unsigned long i, start = 0, incr, eidx, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 void *ret;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700243 unsigned long node_boot_start;
244 void *node_bootmem_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700246 if (!size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 printk("__alloc_bootmem_core(): zero-sized request\n");
248 BUG();
249 }
250 BUG_ON(align & (align-1));
251
Christian Krafft7c309a62006-12-06 20:32:41 -0800252 /* on nodes without memory - bootmem_map is NULL */
253 if (!bdata->node_bootmem_map)
254 return NULL;
255
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700256 /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
257 node_boot_start = bdata->node_boot_start;
258 node_bootmem_map = bdata->node_bootmem_map;
259 if (align) {
260 node_boot_start = ALIGN(bdata->node_boot_start, align);
261 if (node_boot_start > bdata->node_boot_start)
262 node_bootmem_map = (unsigned long *)bdata->node_bootmem_map +
263 PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG;
264 }
265
266 if (limit && node_boot_start >= limit)
267 return NULL;
268
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700269 end_pfn = bdata->node_low_pfn;
270 limit = PFN_DOWN(limit);
Yasunori Goto281dd252005-10-19 15:52:18 -0700271 if (limit && end_pfn > limit)
272 end_pfn = limit;
273
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700274 eidx = end_pfn - PFN_DOWN(node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /*
277 * We try to allocate bootmem pages above 'goal'
278 * first, then we try to allocate lower pages.
279 */
Yinghai Luad093152008-03-10 23:23:42 -0700280 preferred = 0;
281 if (goal && PFN_DOWN(goal) < end_pfn) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700282 if (goal > node_boot_start)
283 preferred = goal - node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700285 if (bdata->last_success > node_boot_start &&
286 bdata->last_success - node_boot_start >= preferred)
Yasunori Goto281dd252005-10-19 15:52:18 -0700287 if (!limit || (limit && limit > bdata->last_success))
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700288 preferred = bdata->last_success - node_boot_start;
Yinghai Luad093152008-03-10 23:23:42 -0700289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700291 preferred = PFN_DOWN(ALIGN(preferred, align));
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700292 areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 incr = align >> PAGE_SHIFT ? : 1;
294
295restart_scan:
Yinghai Luad093152008-03-10 23:23:42 -0700296 for (i = preferred; i < eidx;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unsigned long j;
Yinghai Luad093152008-03-10 23:23:42 -0700298
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700299 i = find_next_zero_bit(node_bootmem_map, eidx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 i = ALIGN(i, incr);
Haren Myneni66d43e92005-12-12 00:37:39 -0800301 if (i >= eidx)
302 break;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700303 if (test_bit(i, node_bootmem_map)) {
Yinghai Luad093152008-03-10 23:23:42 -0700304 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 continue;
Yinghai Luad093152008-03-10 23:23:42 -0700306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 for (j = i + 1; j < i + areasize; ++j) {
308 if (j >= eidx)
309 goto fail_block;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700310 if (test_bit(j, node_bootmem_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto fail_block;
312 }
313 start = i;
314 goto found;
315 fail_block:
316 i = ALIGN(j, incr);
Yinghai Luad093152008-03-10 23:23:42 -0700317 if (i == j)
318 i += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700321 if (preferred > 0) {
322 preferred = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto restart_scan;
324 }
325 return NULL;
326
327found:
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700328 bdata->last_success = PFN_PHYS(start) + node_boot_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 BUG_ON(start >= eidx);
330
331 /*
332 * Is the next page of the previous allocation-end the start
333 * of this allocation's buffer? If yes then we can 'merge'
334 * the previous partial page with this allocation.
335 */
336 if (align < PAGE_SIZE &&
337 bdata->last_offset && bdata->last_pos+1 == start) {
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700338 unsigned long offset, remaining_size;
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700339 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 BUG_ON(offset > PAGE_SIZE);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700341 remaining_size = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (size < remaining_size) {
343 areasize = 0;
344 /* last_pos unchanged */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700345 bdata->last_offset = offset + size;
346 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700347 offset + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 } else {
349 remaining_size = size - remaining_size;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700350 areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
351 ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700352 offset + node_boot_start);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700353 bdata->last_pos = start + areasize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 bdata->last_offset = remaining_size;
355 }
356 bdata->last_offset &= ~PAGE_MASK;
357 } else {
358 bdata->last_pos = start + areasize - 1;
359 bdata->last_offset = size & ~PAGE_MASK;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700360 ret = phys_to_virt(start * PAGE_SIZE + node_boot_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
363 /*
364 * Reserve the area now:
365 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700366 for (i = start; i < start + areasize; i++)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700367 if (unlikely(test_and_set_bit(i, node_bootmem_map)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 BUG();
369 memset(ret, 0, size);
370 return ret;
371}
372
373static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
374{
375 struct page *page;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700376 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 bootmem_data_t *bdata = pgdat->bdata;
378 unsigned long i, count, total = 0;
379 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;
390 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
391 if (bdata->node_boot_start == 0 ||
392 ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG))
393 gofast = 1;
394 for (i = 0; i < idx; ) {
395 unsigned long v = ~map[i / BITS_PER_LONG];
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (gofast && v == ~0UL) {
David Howellsa226f6c2006-01-06 00:11:08 -0800398 int order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700400 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 count += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 order = ffs(BITS_PER_LONG) - 1;
David Howellsa226f6c2006-01-06 00:11:08 -0800403 __free_pages_bootmem(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 i += BITS_PER_LONG;
405 page += BITS_PER_LONG;
406 } else if (v) {
407 unsigned long m;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700408
409 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 for (m = 1; m && i < idx; m<<=1, page++, i++) {
411 if (v & m) {
412 count++;
David Howellsa226f6c2006-01-06 00:11:08 -0800413 __free_pages_bootmem(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 }
416 } else {
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700417 i += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700419 pfn += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 total += count;
422
423 /*
424 * Now free the allocator bitmap itself, it's not
425 * needed anymore:
426 */
427 page = virt_to_page(bdata->node_bootmem_map);
428 count = 0;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700429 idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT;
430 for (i = 0; i < idx; i++, page++) {
David Howellsa226f6c2006-01-06 00:11:08 -0800431 __free_pages_bootmem(page, 0);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700432 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 total += count;
435 bdata->node_bootmem_map = NULL;
436
437 return total;
438}
439
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700440unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700441 unsigned long startpfn, unsigned long endpfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700443 return init_bootmem_core(pgdat, freepfn, startpfn, endpfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Bernhard Walle71c27422008-06-21 19:01:02 +0200446int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800447 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Yinghai Lua5645a62008-03-18 12:49:12 -0700449 int ret;
450
451 ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
452 if (ret < 0)
Bernhard Walle71c27422008-06-21 19:01:02 +0200453 return -ENOMEM;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800454 reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
Bernhard Walle71c27422008-06-21 19:01:02 +0200455
456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700459void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
460 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 free_bootmem_core(pgdat->bdata, physaddr, size);
463}
464
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700465unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Yasunori Goto04753272008-04-28 02:13:31 -0700467 register_page_bootmem_info_node(pgdat);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700468 return free_all_bootmem_core(pgdat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700471unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 max_low_pfn = pages;
474 min_low_pfn = start;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700475 return init_bootmem_core(NODE_DATA(0), start, 0, pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800479int __init reserve_bootmem(unsigned long addr, unsigned long size,
480 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Yinghai Lua5645a62008-03-18 12:49:12 -0700482 bootmem_data_t *bdata;
483 int ret;
484
485 list_for_each_entry(bdata, &bdata_list, list) {
486 ret = can_reserve_bootmem_core(bdata, addr, size, flags);
487 if (ret < 0)
488 return ret;
489 }
490 list_for_each_entry(bdata, &bdata_list, list)
491 reserve_bootmem_core(bdata, addr, size, flags);
492
493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
496
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700497void __init free_bootmem(unsigned long addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Yinghai Lu5a982cb2008-03-24 12:29:45 -0700499 bootmem_data_t *bdata;
500 list_for_each_entry(bdata, &bdata_list, list)
501 free_bootmem_core(bdata, addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700504unsigned long __init free_all_bootmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700506 return free_all_bootmem_core(NODE_DATA(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700509void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
510 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800512 bootmem_data_t *bdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 void *ptr;
514
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700515 list_for_each_entry(bdata, &bdata_list, list) {
516 ptr = __alloc_bootmem_core(bdata, size, align, goal, 0);
517 if (ptr)
518 return ptr;
519 }
Andi Kleena8062232006-04-07 19:49:21 +0200520 return NULL;
521}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700523void * __init __alloc_bootmem(unsigned long size, unsigned long align,
524 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200525{
526 void *mem = __alloc_bootmem_nopanic(size,align,goal);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700527
Andi Kleena8062232006-04-07 19:49:21 +0200528 if (mem)
529 return mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /*
531 * Whoops, we cannot satisfy the allocation request.
532 */
533 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
534 panic("Out of memory");
535 return NULL;
536}
537
Yasunori Goto281dd252005-10-19 15:52:18 -0700538
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700539void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
540 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 void *ptr;
543
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800544 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (ptr)
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700546 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800548 return __alloc_bootmem(size, align, goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700551#ifdef CONFIG_SPARSEMEM
552void * __init alloc_bootmem_section(unsigned long size,
553 unsigned long section_nr)
554{
555 void *ptr;
556 unsigned long limit, goal, start_nr, end_nr, pfn;
557 struct pglist_data *pgdat;
558
559 pfn = section_nr_to_pfn(section_nr);
560 goal = PFN_PHYS(pfn);
561 limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1;
562 pgdat = NODE_DATA(early_pfn_to_nid(pfn));
563 ptr = __alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal,
564 limit);
565
566 if (!ptr)
567 return NULL;
568
569 start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr)));
570 end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size));
571 if (start_nr != section_nr || end_nr != section_nr) {
572 printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n",
573 section_nr);
574 free_bootmem_core(pgdat->bdata, __pa(ptr), size);
575 ptr = NULL;
576 }
577
578 return ptr;
579}
580#endif
581
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700582#ifndef ARCH_LOW_ADDRESS_LIMIT
583#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
584#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800585
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700586void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
587 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800588{
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800589 bootmem_data_t *bdata;
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800590 void *ptr;
591
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700592 list_for_each_entry(bdata, &bdata_list, list) {
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700593 ptr = __alloc_bootmem_core(bdata, size, align, goal,
594 ARCH_LOW_ADDRESS_LIMIT);
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -0700595 if (ptr)
596 return ptr;
597 }
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800598
599 /*
600 * Whoops, we cannot satisfy the allocation request.
601 */
602 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
603 panic("Out of low memory");
604 return NULL;
605}
606
607void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
608 unsigned long align, unsigned long goal)
609{
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700610 return __alloc_bootmem_core(pgdat->bdata, size, align, goal,
611 ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800612}