blob: 8ec4e4c2a179f5fc1a8d7ddb648dad0d9f69cdbb [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 */
11
12#include <linux/mm.h>
13#include <linux/kernel_stat.h>
14#include <linux/swap.h>
15#include <linux/interrupt.h>
16#include <linux/init.h>
17#include <linux/bootmem.h>
18#include <linux/mmzone.h>
19#include <linux/module.h>
20#include <asm/dma.h>
21#include <asm/io.h>
22#include "internal.h"
23
24/*
25 * Access to this subsystem has to be serialized externally. (this is
26 * true for the boot process anyway)
27 */
28unsigned long max_low_pfn;
29unsigned long min_low_pfn;
30unsigned long max_pfn;
31
32EXPORT_SYMBOL(max_pfn); /* This is exported so
33 * dma_get_required_mask(), which uses
34 * it, can be an inline function */
35
Vivek Goyal92aa63a2005-06-25 14:58:18 -070036#ifdef CONFIG_CRASH_DUMP
37/*
38 * If we have booted due to a crash, max_pfn will be a very low value. We need
39 * to know the amount of memory that the previous kernel used.
40 */
41unsigned long saved_max_pfn;
42#endif
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* return the number of _pages_ that will be allocated for the boot bitmap */
45unsigned long __init bootmem_bootmap_pages (unsigned long pages)
46{
47 unsigned long mapsize;
48
49 mapsize = (pages+7)/8;
50 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
51 mapsize >>= PAGE_SHIFT;
52
53 return mapsize;
54}
55
56/*
57 * Called once to set up the allocator itself.
58 */
59static unsigned long __init init_bootmem_core (pg_data_t *pgdat,
60 unsigned long mapstart, unsigned long start, unsigned long end)
61{
62 bootmem_data_t *bdata = pgdat->bdata;
63 unsigned long mapsize = ((end - start)+7)/8;
Andi Kleen5d3d0f72005-09-12 18:49:24 +020064 static struct pglist_data *pgdat_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Andi Kleen5d3d0f72005-09-12 18:49:24 +020066 pgdat->pgdat_next = NULL;
67 /* Add new nodes last so that bootmem always starts
68 searching in the first nodes, not the last ones */
69 if (pgdat_last)
70 pgdat_last->pgdat_next = pgdat;
71 else {
72 pgdat_list = pgdat;
73 pgdat_last = pgdat;
74 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Nick Wilson8c0e33c2005-06-25 14:59:00 -070076 mapsize = ALIGN(mapsize, sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 bdata->node_bootmem_map = phys_to_virt(mapstart << PAGE_SHIFT);
78 bdata->node_boot_start = (start << PAGE_SHIFT);
79 bdata->node_low_pfn = end;
80
81 /*
82 * Initially all pages are reserved - setup_arch() has to
83 * register free RAM areas explicitly.
84 */
85 memset(bdata->node_bootmem_map, 0xff, mapsize);
86
87 return mapsize;
88}
89
90/*
91 * Marks a particular physical memory range as unallocatable. Usable RAM
92 * might be used for boot-time allocations - or it might get added
93 * to the free page pool later on.
94 */
95static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr, unsigned long size)
96{
97 unsigned long i;
98 /*
99 * round up, partially reserved pages are considered
100 * fully reserved.
101 */
102 unsigned long sidx = (addr - bdata->node_boot_start)/PAGE_SIZE;
103 unsigned long eidx = (addr + size - bdata->node_boot_start +
104 PAGE_SIZE-1)/PAGE_SIZE;
105 unsigned long end = (addr + size + PAGE_SIZE-1)/PAGE_SIZE;
106
107 BUG_ON(!size);
108 BUG_ON(sidx >= eidx);
109 BUG_ON((addr >> PAGE_SHIFT) >= bdata->node_low_pfn);
110 BUG_ON(end > bdata->node_low_pfn);
111
112 for (i = sidx; i < eidx; i++)
113 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
114#ifdef CONFIG_DEBUG_BOOTMEM
115 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
116#endif
117 }
118}
119
120static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, unsigned long size)
121{
122 unsigned long i;
123 unsigned long start;
124 /*
125 * round down end of usable mem, partially free pages are
126 * considered reserved.
127 */
128 unsigned long sidx;
129 unsigned long eidx = (addr + size - bdata->node_boot_start)/PAGE_SIZE;
130 unsigned long end = (addr + size)/PAGE_SIZE;
131
132 BUG_ON(!size);
133 BUG_ON(end > bdata->node_low_pfn);
134
135 if (addr < bdata->last_success)
136 bdata->last_success = addr;
137
138 /*
139 * Round up the beginning of the address.
140 */
141 start = (addr + PAGE_SIZE-1) / PAGE_SIZE;
142 sidx = start - (bdata->node_boot_start/PAGE_SIZE);
143
144 for (i = sidx; i < eidx; i++) {
145 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
146 BUG();
147 }
148}
149
150/*
151 * We 'merge' subsequent allocations to save space. We might 'lose'
152 * some fraction of a page if allocations cannot be satisfied due to
153 * size constraints on boxes where there is physical RAM space
154 * fragmentation - in these cases (mostly large memory boxes) this
155 * is not a problem.
156 *
157 * On low memory boxes we get it right in 100% of the cases.
158 *
159 * alignment has to be a power of 2 value.
160 *
161 * NOTE: This function is _not_ reentrant.
162 */
163static void * __init
164__alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
165 unsigned long align, unsigned long goal)
166{
167 unsigned long offset, remaining_size, areasize, preferred;
168 unsigned long i, start = 0, incr, eidx;
169 void *ret;
170
171 if(!size) {
172 printk("__alloc_bootmem_core(): zero-sized request\n");
173 BUG();
174 }
175 BUG_ON(align & (align-1));
176
177 eidx = bdata->node_low_pfn - (bdata->node_boot_start >> PAGE_SHIFT);
178 offset = 0;
179 if (align &&
180 (bdata->node_boot_start & (align - 1UL)) != 0)
181 offset = (align - (bdata->node_boot_start & (align - 1UL)));
182 offset >>= PAGE_SHIFT;
183
184 /*
185 * We try to allocate bootmem pages above 'goal'
186 * first, then we try to allocate lower pages.
187 */
188 if (goal && (goal >= bdata->node_boot_start) &&
189 ((goal >> PAGE_SHIFT) < bdata->node_low_pfn)) {
190 preferred = goal - bdata->node_boot_start;
191
192 if (bdata->last_success >= preferred)
193 preferred = bdata->last_success;
194 } else
195 preferred = 0;
196
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700197 preferred = ALIGN(preferred, align) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 preferred += offset;
199 areasize = (size+PAGE_SIZE-1)/PAGE_SIZE;
200 incr = align >> PAGE_SHIFT ? : 1;
201
202restart_scan:
203 for (i = preferred; i < eidx; i += incr) {
204 unsigned long j;
205 i = find_next_zero_bit(bdata->node_bootmem_map, eidx, i);
206 i = ALIGN(i, incr);
207 if (test_bit(i, bdata->node_bootmem_map))
208 continue;
209 for (j = i + 1; j < i + areasize; ++j) {
210 if (j >= eidx)
211 goto fail_block;
212 if (test_bit (j, bdata->node_bootmem_map))
213 goto fail_block;
214 }
215 start = i;
216 goto found;
217 fail_block:
218 i = ALIGN(j, incr);
219 }
220
221 if (preferred > offset) {
222 preferred = offset;
223 goto restart_scan;
224 }
225 return NULL;
226
227found:
228 bdata->last_success = start << PAGE_SHIFT;
229 BUG_ON(start >= eidx);
230
231 /*
232 * Is the next page of the previous allocation-end the start
233 * of this allocation's buffer? If yes then we can 'merge'
234 * the previous partial page with this allocation.
235 */
236 if (align < PAGE_SIZE &&
237 bdata->last_offset && bdata->last_pos+1 == start) {
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700238 offset = ALIGN(bdata->last_offset, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 BUG_ON(offset > PAGE_SIZE);
240 remaining_size = PAGE_SIZE-offset;
241 if (size < remaining_size) {
242 areasize = 0;
243 /* last_pos unchanged */
244 bdata->last_offset = offset+size;
245 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
246 bdata->node_boot_start);
247 } else {
248 remaining_size = size - remaining_size;
249 areasize = (remaining_size+PAGE_SIZE-1)/PAGE_SIZE;
250 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
251 bdata->node_boot_start);
252 bdata->last_pos = start+areasize-1;
253 bdata->last_offset = remaining_size;
254 }
255 bdata->last_offset &= ~PAGE_MASK;
256 } else {
257 bdata->last_pos = start + areasize - 1;
258 bdata->last_offset = size & ~PAGE_MASK;
259 ret = phys_to_virt(start * PAGE_SIZE + bdata->node_boot_start);
260 }
261
262 /*
263 * Reserve the area now:
264 */
265 for (i = start; i < start+areasize; i++)
266 if (unlikely(test_and_set_bit(i, bdata->node_bootmem_map)))
267 BUG();
268 memset(ret, 0, size);
269 return ret;
270}
271
272static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
273{
274 struct page *page;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700275 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 bootmem_data_t *bdata = pgdat->bdata;
277 unsigned long i, count, total = 0;
278 unsigned long idx;
279 unsigned long *map;
280 int gofast = 0;
281
282 BUG_ON(!bdata->node_bootmem_map);
283
284 count = 0;
285 /* first extant page of the node */
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700286 pfn = bdata->node_boot_start >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 idx = bdata->node_low_pfn - (bdata->node_boot_start >> PAGE_SHIFT);
288 map = bdata->node_bootmem_map;
289 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
290 if (bdata->node_boot_start == 0 ||
291 ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG))
292 gofast = 1;
293 for (i = 0; i < idx; ) {
294 unsigned long v = ~map[i / BITS_PER_LONG];
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (gofast && v == ~0UL) {
297 int j, order;
298
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700299 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 count += BITS_PER_LONG;
301 __ClearPageReserved(page);
302 order = ffs(BITS_PER_LONG) - 1;
303 set_page_refs(page, order);
304 for (j = 1; j < BITS_PER_LONG; j++) {
305 if (j + 16 < BITS_PER_LONG)
306 prefetchw(page + j + 16);
307 __ClearPageReserved(page + j);
308 }
309 __free_pages(page, order);
310 i += BITS_PER_LONG;
311 page += BITS_PER_LONG;
312 } else if (v) {
313 unsigned long m;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700314
315 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 for (m = 1; m && i < idx; m<<=1, page++, i++) {
317 if (v & m) {
318 count++;
319 __ClearPageReserved(page);
320 set_page_refs(page, 0);
321 __free_page(page);
322 }
323 }
324 } else {
325 i+=BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700327 pfn += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 total += count;
330
331 /*
332 * Now free the allocator bitmap itself, it's not
333 * needed anymore:
334 */
335 page = virt_to_page(bdata->node_bootmem_map);
336 count = 0;
337 for (i = 0; i < ((bdata->node_low_pfn-(bdata->node_boot_start >> PAGE_SHIFT))/8 + PAGE_SIZE-1)/PAGE_SIZE; i++,page++) {
338 count++;
339 __ClearPageReserved(page);
340 set_page_count(page, 1);
341 __free_page(page);
342 }
343 total += count;
344 bdata->node_bootmem_map = NULL;
345
346 return total;
347}
348
349unsigned long __init init_bootmem_node (pg_data_t *pgdat, unsigned long freepfn, unsigned long startpfn, unsigned long endpfn)
350{
351 return(init_bootmem_core(pgdat, freepfn, startpfn, endpfn));
352}
353
354void __init reserve_bootmem_node (pg_data_t *pgdat, unsigned long physaddr, unsigned long size)
355{
356 reserve_bootmem_core(pgdat->bdata, physaddr, size);
357}
358
359void __init free_bootmem_node (pg_data_t *pgdat, unsigned long physaddr, unsigned long size)
360{
361 free_bootmem_core(pgdat->bdata, physaddr, size);
362}
363
364unsigned long __init free_all_bootmem_node (pg_data_t *pgdat)
365{
366 return(free_all_bootmem_core(pgdat));
367}
368
369unsigned long __init init_bootmem (unsigned long start, unsigned long pages)
370{
371 max_low_pfn = pages;
372 min_low_pfn = start;
373 return(init_bootmem_core(NODE_DATA(0), start, 0, pages));
374}
375
376#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
377void __init reserve_bootmem (unsigned long addr, unsigned long size)
378{
379 reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size);
380}
381#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
382
383void __init free_bootmem (unsigned long addr, unsigned long size)
384{
385 free_bootmem_core(NODE_DATA(0)->bdata, addr, size);
386}
387
388unsigned long __init free_all_bootmem (void)
389{
390 return(free_all_bootmem_core(NODE_DATA(0)));
391}
392
393void * __init __alloc_bootmem (unsigned long size, unsigned long align, unsigned long goal)
394{
395 pg_data_t *pgdat = pgdat_list;
396 void *ptr;
397
398 for_each_pgdat(pgdat)
399 if ((ptr = __alloc_bootmem_core(pgdat->bdata, size,
400 align, goal)))
401 return(ptr);
402
403 /*
404 * Whoops, we cannot satisfy the allocation request.
405 */
406 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
407 panic("Out of memory");
408 return NULL;
409}
410
411void * __init __alloc_bootmem_node (pg_data_t *pgdat, unsigned long size, unsigned long align, unsigned long goal)
412{
413 void *ptr;
414
415 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal);
416 if (ptr)
417 return (ptr);
418
419 return __alloc_bootmem(size, align, goal);
420}
421