[PATCH] fix update_mmu_cache in fremap.c
[linux-2.6.22.y-op.git] / mm / bootmem.c
blobd213feded10df30d04fedd2e744fc89ab3f6f934
1 /*
2 * linux/mm/bootmem.c
4 * Copyright (C) 1999 Ingo Molnar
5 * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
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.
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"
25 * Access to this subsystem has to be serialized externally. (this is
26 * true for the boot process anyway)
28 unsigned long max_low_pfn;
29 unsigned long min_low_pfn;
30 unsigned long max_pfn;
32 EXPORT_SYMBOL(max_pfn); /* This is exported so
33 * dma_get_required_mask(), which uses
34 * it, can be an inline function */
36 static LIST_HEAD(bdata_list);
37 #ifdef CONFIG_CRASH_DUMP
39 * If we have booted due to a crash, max_pfn will be a very low value. We need
40 * to know the amount of memory that the previous kernel used.
42 unsigned long saved_max_pfn;
43 #endif
45 /* return the number of _pages_ that will be allocated for the boot bitmap */
46 unsigned long __init bootmem_bootmap_pages (unsigned long pages)
48 unsigned long mapsize;
50 mapsize = (pages+7)/8;
51 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
52 mapsize >>= PAGE_SHIFT;
54 return mapsize;
57 * link bdata in order
59 static void link_bootmem(bootmem_data_t *bdata)
61 bootmem_data_t *ent;
62 if (list_empty(&bdata_list)) {
63 list_add(&bdata->list, &bdata_list);
64 return;
66 /* insert in order */
67 list_for_each_entry(ent, &bdata_list, list) {
68 if (bdata->node_boot_start < ent->node_boot_start) {
69 list_add_tail(&bdata->list, &ent->list);
70 return;
73 list_add_tail(&bdata->list, &bdata_list);
74 return;
79 * Called once to set up the allocator itself.
81 static unsigned long __init init_bootmem_core (pg_data_t *pgdat,
82 unsigned long mapstart, unsigned long start, unsigned long end)
84 bootmem_data_t *bdata = pgdat->bdata;
85 unsigned long mapsize = ((end - start)+7)/8;
87 mapsize = ALIGN(mapsize, sizeof(long));
88 bdata->node_bootmem_map = phys_to_virt(mapstart << PAGE_SHIFT);
89 bdata->node_boot_start = (start << PAGE_SHIFT);
90 bdata->node_low_pfn = end;
91 link_bootmem(bdata);
94 * Initially all pages are reserved - setup_arch() has to
95 * register free RAM areas explicitly.
97 memset(bdata->node_bootmem_map, 0xff, mapsize);
99 return mapsize;
103 * Marks a particular physical memory range as unallocatable. Usable RAM
104 * might be used for boot-time allocations - or it might get added
105 * to the free page pool later on.
107 static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr, unsigned long size)
109 unsigned long i;
111 * round up, partially reserved pages are considered
112 * fully reserved.
114 unsigned long sidx = (addr - bdata->node_boot_start)/PAGE_SIZE;
115 unsigned long eidx = (addr + size - bdata->node_boot_start +
116 PAGE_SIZE-1)/PAGE_SIZE;
117 unsigned long end = (addr + size + PAGE_SIZE-1)/PAGE_SIZE;
119 BUG_ON(!size);
120 BUG_ON(sidx >= eidx);
121 BUG_ON((addr >> PAGE_SHIFT) >= bdata->node_low_pfn);
122 BUG_ON(end > bdata->node_low_pfn);
124 for (i = sidx; i < eidx; i++)
125 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
126 #ifdef CONFIG_DEBUG_BOOTMEM
127 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
128 #endif
132 static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, unsigned long size)
134 unsigned long i;
135 unsigned long start;
137 * round down end of usable mem, partially free pages are
138 * considered reserved.
140 unsigned long sidx;
141 unsigned long eidx = (addr + size - bdata->node_boot_start)/PAGE_SIZE;
142 unsigned long end = (addr + size)/PAGE_SIZE;
144 BUG_ON(!size);
145 BUG_ON(end > bdata->node_low_pfn);
147 if (addr < bdata->last_success)
148 bdata->last_success = addr;
151 * Round up the beginning of the address.
153 start = (addr + PAGE_SIZE-1) / PAGE_SIZE;
154 sidx = start - (bdata->node_boot_start/PAGE_SIZE);
156 for (i = sidx; i < eidx; i++) {
157 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
158 BUG();
163 * We 'merge' subsequent allocations to save space. We might 'lose'
164 * some fraction of a page if allocations cannot be satisfied due to
165 * size constraints on boxes where there is physical RAM space
166 * fragmentation - in these cases (mostly large memory boxes) this
167 * is not a problem.
169 * On low memory boxes we get it right in 100% of the cases.
171 * alignment has to be a power of 2 value.
173 * NOTE: This function is _not_ reentrant.
175 void * __init
176 __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
177 unsigned long align, unsigned long goal, unsigned long limit)
179 unsigned long offset, remaining_size, areasize, preferred;
180 unsigned long i, start = 0, incr, eidx, end_pfn = bdata->node_low_pfn;
181 void *ret;
183 if(!size) {
184 printk("__alloc_bootmem_core(): zero-sized request\n");
185 BUG();
187 BUG_ON(align & (align-1));
189 if (limit && bdata->node_boot_start >= limit)
190 return NULL;
192 limit >>=PAGE_SHIFT;
193 if (limit && end_pfn > limit)
194 end_pfn = limit;
196 eidx = end_pfn - (bdata->node_boot_start >> PAGE_SHIFT);
197 offset = 0;
198 if (align &&
199 (bdata->node_boot_start & (align - 1UL)) != 0)
200 offset = (align - (bdata->node_boot_start & (align - 1UL)));
201 offset >>= PAGE_SHIFT;
204 * We try to allocate bootmem pages above 'goal'
205 * first, then we try to allocate lower pages.
207 if (goal && (goal >= bdata->node_boot_start) &&
208 ((goal >> PAGE_SHIFT) < end_pfn)) {
209 preferred = goal - bdata->node_boot_start;
211 if (bdata->last_success >= preferred)
212 if (!limit || (limit && limit > bdata->last_success))
213 preferred = bdata->last_success;
214 } else
215 preferred = 0;
217 preferred = ALIGN(preferred, align) >> PAGE_SHIFT;
218 preferred += offset;
219 areasize = (size+PAGE_SIZE-1)/PAGE_SIZE;
220 incr = align >> PAGE_SHIFT ? : 1;
222 restart_scan:
223 for (i = preferred; i < eidx; i += incr) {
224 unsigned long j;
225 i = find_next_zero_bit(bdata->node_bootmem_map, eidx, i);
226 i = ALIGN(i, incr);
227 if (i >= eidx)
228 break;
229 if (test_bit(i, bdata->node_bootmem_map))
230 continue;
231 for (j = i + 1; j < i + areasize; ++j) {
232 if (j >= eidx)
233 goto fail_block;
234 if (test_bit (j, bdata->node_bootmem_map))
235 goto fail_block;
237 start = i;
238 goto found;
239 fail_block:
240 i = ALIGN(j, incr);
243 if (preferred > offset) {
244 preferred = offset;
245 goto restart_scan;
247 return NULL;
249 found:
250 bdata->last_success = start << PAGE_SHIFT;
251 BUG_ON(start >= eidx);
254 * Is the next page of the previous allocation-end the start
255 * of this allocation's buffer? If yes then we can 'merge'
256 * the previous partial page with this allocation.
258 if (align < PAGE_SIZE &&
259 bdata->last_offset && bdata->last_pos+1 == start) {
260 offset = ALIGN(bdata->last_offset, align);
261 BUG_ON(offset > PAGE_SIZE);
262 remaining_size = PAGE_SIZE-offset;
263 if (size < remaining_size) {
264 areasize = 0;
265 /* last_pos unchanged */
266 bdata->last_offset = offset+size;
267 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
268 bdata->node_boot_start);
269 } else {
270 remaining_size = size - remaining_size;
271 areasize = (remaining_size+PAGE_SIZE-1)/PAGE_SIZE;
272 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
273 bdata->node_boot_start);
274 bdata->last_pos = start+areasize-1;
275 bdata->last_offset = remaining_size;
277 bdata->last_offset &= ~PAGE_MASK;
278 } else {
279 bdata->last_pos = start + areasize - 1;
280 bdata->last_offset = size & ~PAGE_MASK;
281 ret = phys_to_virt(start * PAGE_SIZE + bdata->node_boot_start);
285 * Reserve the area now:
287 for (i = start; i < start+areasize; i++)
288 if (unlikely(test_and_set_bit(i, bdata->node_bootmem_map)))
289 BUG();
290 memset(ret, 0, size);
291 return ret;
294 static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
296 struct page *page;
297 unsigned long pfn;
298 bootmem_data_t *bdata = pgdat->bdata;
299 unsigned long i, count, total = 0;
300 unsigned long idx;
301 unsigned long *map;
302 int gofast = 0;
304 BUG_ON(!bdata->node_bootmem_map);
306 count = 0;
307 /* first extant page of the node */
308 pfn = bdata->node_boot_start >> PAGE_SHIFT;
309 idx = bdata->node_low_pfn - (bdata->node_boot_start >> PAGE_SHIFT);
310 map = bdata->node_bootmem_map;
311 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
312 if (bdata->node_boot_start == 0 ||
313 ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG))
314 gofast = 1;
315 for (i = 0; i < idx; ) {
316 unsigned long v = ~map[i / BITS_PER_LONG];
318 if (gofast && v == ~0UL) {
319 int order;
321 page = pfn_to_page(pfn);
322 count += BITS_PER_LONG;
323 order = ffs(BITS_PER_LONG) - 1;
324 __free_pages_bootmem(page, order);
325 i += BITS_PER_LONG;
326 page += BITS_PER_LONG;
327 } else if (v) {
328 unsigned long m;
330 page = pfn_to_page(pfn);
331 for (m = 1; m && i < idx; m<<=1, page++, i++) {
332 if (v & m) {
333 count++;
334 __free_pages_bootmem(page, 0);
337 } else {
338 i+=BITS_PER_LONG;
340 pfn += BITS_PER_LONG;
342 total += count;
345 * Now free the allocator bitmap itself, it's not
346 * needed anymore:
348 page = virt_to_page(bdata->node_bootmem_map);
349 count = 0;
350 for (i = 0; i < ((bdata->node_low_pfn-(bdata->node_boot_start >> PAGE_SHIFT))/8 + PAGE_SIZE-1)/PAGE_SIZE; i++,page++) {
351 count++;
352 __free_pages_bootmem(page, 0);
354 total += count;
355 bdata->node_bootmem_map = NULL;
357 return total;
360 unsigned long __init init_bootmem_node (pg_data_t *pgdat, unsigned long freepfn, unsigned long startpfn, unsigned long endpfn)
362 return(init_bootmem_core(pgdat, freepfn, startpfn, endpfn));
365 void __init reserve_bootmem_node (pg_data_t *pgdat, unsigned long physaddr, unsigned long size)
367 reserve_bootmem_core(pgdat->bdata, physaddr, size);
370 void __init free_bootmem_node (pg_data_t *pgdat, unsigned long physaddr, unsigned long size)
372 free_bootmem_core(pgdat->bdata, physaddr, size);
375 unsigned long __init free_all_bootmem_node (pg_data_t *pgdat)
377 return(free_all_bootmem_core(pgdat));
380 unsigned long __init init_bootmem (unsigned long start, unsigned long pages)
382 max_low_pfn = pages;
383 min_low_pfn = start;
384 return(init_bootmem_core(NODE_DATA(0), start, 0, pages));
387 #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
388 void __init reserve_bootmem (unsigned long addr, unsigned long size)
390 reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size);
392 #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
394 void __init free_bootmem (unsigned long addr, unsigned long size)
396 free_bootmem_core(NODE_DATA(0)->bdata, addr, size);
399 unsigned long __init free_all_bootmem (void)
401 return(free_all_bootmem_core(NODE_DATA(0)));
404 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, unsigned long goal)
406 bootmem_data_t *bdata;
407 void *ptr;
409 list_for_each_entry(bdata, &bdata_list, list)
410 if ((ptr = __alloc_bootmem_core(bdata, size, align, goal, 0)))
411 return(ptr);
412 return NULL;
415 void * __init __alloc_bootmem(unsigned long size, unsigned long align, unsigned long goal)
417 void *mem = __alloc_bootmem_nopanic(size,align,goal);
418 if (mem)
419 return mem;
421 * Whoops, we cannot satisfy the allocation request.
423 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
424 panic("Out of memory");
425 return NULL;
429 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, unsigned long align,
430 unsigned long goal)
432 void *ptr;
434 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
435 if (ptr)
436 return (ptr);
438 return __alloc_bootmem(size, align, goal);
441 #define LOW32LIMIT 0xffffffff
443 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, unsigned long goal)
445 bootmem_data_t *bdata;
446 void *ptr;
448 list_for_each_entry(bdata, &bdata_list, list)
449 if ((ptr = __alloc_bootmem_core(bdata, size,
450 align, goal, LOW32LIMIT)))
451 return(ptr);
454 * Whoops, we cannot satisfy the allocation request.
456 printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
457 panic("Out of low memory");
458 return NULL;
461 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
462 unsigned long align, unsigned long goal)
464 return __alloc_bootmem_core(pgdat->bdata, size, align, goal, LOW32LIMIT);