[ALSA] Fix resume of cs4231
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / bootmem.c
blob16b9465eb4ebb385114558dcacdaf7af21678612
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 #ifdef CONFIG_CRASH_DUMP
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.
41 unsigned long saved_max_pfn;
42 #endif
44 /* return the number of _pages_ that will be allocated for the boot bitmap */
45 unsigned long __init bootmem_bootmap_pages (unsigned long pages)
47 unsigned long mapsize;
49 mapsize = (pages+7)/8;
50 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
51 mapsize >>= PAGE_SHIFT;
53 return mapsize;
57 * Called once to set up the allocator itself.
59 static unsigned long __init init_bootmem_core (pg_data_t *pgdat,
60 unsigned long mapstart, unsigned long start, unsigned long end)
62 bootmem_data_t *bdata = pgdat->bdata;
63 unsigned long mapsize = ((end - start)+7)/8;
65 pgdat->pgdat_next = pgdat_list;
66 pgdat_list = pgdat;
68 mapsize = ALIGN(mapsize, sizeof(long));
69 bdata->node_bootmem_map = phys_to_virt(mapstart << PAGE_SHIFT);
70 bdata->node_boot_start = (start << PAGE_SHIFT);
71 bdata->node_low_pfn = end;
74 * Initially all pages are reserved - setup_arch() has to
75 * register free RAM areas explicitly.
77 memset(bdata->node_bootmem_map, 0xff, mapsize);
79 return mapsize;
83 * Marks a particular physical memory range as unallocatable. Usable RAM
84 * might be used for boot-time allocations - or it might get added
85 * to the free page pool later on.
87 static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr, unsigned long size)
89 unsigned long i;
91 * round up, partially reserved pages are considered
92 * fully reserved.
94 unsigned long sidx = (addr - bdata->node_boot_start)/PAGE_SIZE;
95 unsigned long eidx = (addr + size - bdata->node_boot_start +
96 PAGE_SIZE-1)/PAGE_SIZE;
97 unsigned long end = (addr + size + PAGE_SIZE-1)/PAGE_SIZE;
99 BUG_ON(!size);
100 BUG_ON(sidx >= eidx);
101 BUG_ON((addr >> PAGE_SHIFT) >= bdata->node_low_pfn);
102 BUG_ON(end > bdata->node_low_pfn);
104 for (i = sidx; i < eidx; i++)
105 if (test_and_set_bit(i, bdata->node_bootmem_map)) {
106 #ifdef CONFIG_DEBUG_BOOTMEM
107 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
108 #endif
112 static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, unsigned long size)
114 unsigned long i;
115 unsigned long start;
117 * round down end of usable mem, partially free pages are
118 * considered reserved.
120 unsigned long sidx;
121 unsigned long eidx = (addr + size - bdata->node_boot_start)/PAGE_SIZE;
122 unsigned long end = (addr + size)/PAGE_SIZE;
124 BUG_ON(!size);
125 BUG_ON(end > bdata->node_low_pfn);
127 if (addr < bdata->last_success)
128 bdata->last_success = addr;
131 * Round up the beginning of the address.
133 start = (addr + PAGE_SIZE-1) / PAGE_SIZE;
134 sidx = start - (bdata->node_boot_start/PAGE_SIZE);
136 for (i = sidx; i < eidx; i++) {
137 if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
138 BUG();
143 * We 'merge' subsequent allocations to save space. We might 'lose'
144 * some fraction of a page if allocations cannot be satisfied due to
145 * size constraints on boxes where there is physical RAM space
146 * fragmentation - in these cases (mostly large memory boxes) this
147 * is not a problem.
149 * On low memory boxes we get it right in 100% of the cases.
151 * alignment has to be a power of 2 value.
153 * NOTE: This function is _not_ reentrant.
155 static void * __init
156 __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
157 unsigned long align, unsigned long goal, unsigned long limit)
159 unsigned long offset, remaining_size, areasize, preferred;
160 unsigned long i, start = 0, incr, eidx, end_pfn = bdata->node_low_pfn;
161 void *ret;
163 if(!size) {
164 printk("__alloc_bootmem_core(): zero-sized request\n");
165 BUG();
167 BUG_ON(align & (align-1));
169 if (limit && bdata->node_boot_start >= limit)
170 return NULL;
172 limit >>=PAGE_SHIFT;
173 if (limit && end_pfn > limit)
174 end_pfn = limit;
176 eidx = end_pfn - (bdata->node_boot_start >> PAGE_SHIFT);
177 offset = 0;
178 if (align &&
179 (bdata->node_boot_start & (align - 1UL)) != 0)
180 offset = (align - (bdata->node_boot_start & (align - 1UL)));
181 offset >>= PAGE_SHIFT;
184 * We try to allocate bootmem pages above 'goal'
185 * first, then we try to allocate lower pages.
187 if (goal && (goal >= bdata->node_boot_start) &&
188 ((goal >> PAGE_SHIFT) < end_pfn)) {
189 preferred = goal - bdata->node_boot_start;
191 if (bdata->last_success >= preferred)
192 if (!limit || (limit && limit > bdata->last_success))
193 preferred = bdata->last_success;
194 } else
195 preferred = 0;
197 preferred = ALIGN(preferred, align) >> PAGE_SHIFT;
198 preferred += offset;
199 areasize = (size+PAGE_SIZE-1)/PAGE_SIZE;
200 incr = align >> PAGE_SHIFT ? : 1;
202 restart_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 (i >= eidx)
208 break;
209 if (test_bit(i, bdata->node_bootmem_map))
210 continue;
211 for (j = i + 1; j < i + areasize; ++j) {
212 if (j >= eidx)
213 goto fail_block;
214 if (test_bit (j, bdata->node_bootmem_map))
215 goto fail_block;
217 start = i;
218 goto found;
219 fail_block:
220 i = ALIGN(j, incr);
223 if (preferred > offset) {
224 preferred = offset;
225 goto restart_scan;
227 return NULL;
229 found:
230 bdata->last_success = start << PAGE_SHIFT;
231 BUG_ON(start >= eidx);
234 * Is the next page of the previous allocation-end the start
235 * of this allocation's buffer? If yes then we can 'merge'
236 * the previous partial page with this allocation.
238 if (align < PAGE_SIZE &&
239 bdata->last_offset && bdata->last_pos+1 == start) {
240 offset = ALIGN(bdata->last_offset, align);
241 BUG_ON(offset > PAGE_SIZE);
242 remaining_size = PAGE_SIZE-offset;
243 if (size < remaining_size) {
244 areasize = 0;
245 /* last_pos unchanged */
246 bdata->last_offset = offset+size;
247 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
248 bdata->node_boot_start);
249 } else {
250 remaining_size = size - remaining_size;
251 areasize = (remaining_size+PAGE_SIZE-1)/PAGE_SIZE;
252 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
253 bdata->node_boot_start);
254 bdata->last_pos = start+areasize-1;
255 bdata->last_offset = remaining_size;
257 bdata->last_offset &= ~PAGE_MASK;
258 } else {
259 bdata->last_pos = start + areasize - 1;
260 bdata->last_offset = size & ~PAGE_MASK;
261 ret = phys_to_virt(start * PAGE_SIZE + bdata->node_boot_start);
265 * Reserve the area now:
267 for (i = start; i < start+areasize; i++)
268 if (unlikely(test_and_set_bit(i, bdata->node_bootmem_map)))
269 BUG();
270 memset(ret, 0, size);
271 return ret;
274 static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat)
276 struct page *page;
277 unsigned long pfn;
278 bootmem_data_t *bdata = pgdat->bdata;
279 unsigned long i, count, total = 0;
280 unsigned long idx;
281 unsigned long *map;
282 int gofast = 0;
284 BUG_ON(!bdata->node_bootmem_map);
286 count = 0;
287 /* first extant page of the node */
288 pfn = bdata->node_boot_start >> PAGE_SHIFT;
289 idx = bdata->node_low_pfn - (bdata->node_boot_start >> PAGE_SHIFT);
290 map = bdata->node_bootmem_map;
291 /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */
292 if (bdata->node_boot_start == 0 ||
293 ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG))
294 gofast = 1;
295 for (i = 0; i < idx; ) {
296 unsigned long v = ~map[i / BITS_PER_LONG];
298 if (gofast && v == ~0UL) {
299 int j, order;
301 page = pfn_to_page(pfn);
302 count += BITS_PER_LONG;
303 __ClearPageReserved(page);
304 order = ffs(BITS_PER_LONG) - 1;
305 set_page_refs(page, order);
306 for (j = 1; j < BITS_PER_LONG; j++) {
307 if (j + 16 < BITS_PER_LONG)
308 prefetchw(page + j + 16);
309 __ClearPageReserved(page + j);
310 set_page_count(page + j, 0);
312 __free_pages(page, order);
313 i += BITS_PER_LONG;
314 page += BITS_PER_LONG;
315 } else if (v) {
316 unsigned long m;
318 page = pfn_to_page(pfn);
319 for (m = 1; m && i < idx; m<<=1, page++, i++) {
320 if (v & m) {
321 count++;
322 __ClearPageReserved(page);
323 set_page_refs(page, 0);
324 __free_page(page);
327 } else {
328 i+=BITS_PER_LONG;
330 pfn += BITS_PER_LONG;
332 total += count;
335 * Now free the allocator bitmap itself, it's not
336 * needed anymore:
338 page = virt_to_page(bdata->node_bootmem_map);
339 count = 0;
340 for (i = 0; i < ((bdata->node_low_pfn-(bdata->node_boot_start >> PAGE_SHIFT))/8 + PAGE_SIZE-1)/PAGE_SIZE; i++,page++) {
341 count++;
342 __ClearPageReserved(page);
343 set_page_count(page, 1);
344 __free_page(page);
346 total += count;
347 bdata->node_bootmem_map = NULL;
349 return total;
352 unsigned long __init init_bootmem_node (pg_data_t *pgdat, unsigned long freepfn, unsigned long startpfn, unsigned long endpfn)
354 return(init_bootmem_core(pgdat, freepfn, startpfn, endpfn));
357 void __init reserve_bootmem_node (pg_data_t *pgdat, unsigned long physaddr, unsigned long size)
359 reserve_bootmem_core(pgdat->bdata, physaddr, size);
362 void __init free_bootmem_node (pg_data_t *pgdat, unsigned long physaddr, unsigned long size)
364 free_bootmem_core(pgdat->bdata, physaddr, size);
367 unsigned long __init free_all_bootmem_node (pg_data_t *pgdat)
369 return(free_all_bootmem_core(pgdat));
372 unsigned long __init init_bootmem (unsigned long start, unsigned long pages)
374 max_low_pfn = pages;
375 min_low_pfn = start;
376 return(init_bootmem_core(NODE_DATA(0), start, 0, pages));
379 #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
380 void __init reserve_bootmem (unsigned long addr, unsigned long size)
382 reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size);
384 #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
386 void __init free_bootmem (unsigned long addr, unsigned long size)
388 free_bootmem_core(NODE_DATA(0)->bdata, addr, size);
391 unsigned long __init free_all_bootmem (void)
393 return(free_all_bootmem_core(NODE_DATA(0)));
396 void * __init __alloc_bootmem_limit (unsigned long size, unsigned long align, unsigned long goal,
397 unsigned long limit)
399 pg_data_t *pgdat = pgdat_list;
400 void *ptr;
402 for_each_pgdat(pgdat)
403 if ((ptr = __alloc_bootmem_core(pgdat->bdata, size,
404 align, goal, limit)))
405 return(ptr);
408 * Whoops, we cannot satisfy the allocation request.
410 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
411 panic("Out of memory");
412 return NULL;
416 void * __init __alloc_bootmem_node_limit (pg_data_t *pgdat, unsigned long size, unsigned long align,
417 unsigned long goal, unsigned long limit)
419 void *ptr;
421 ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, limit);
422 if (ptr)
423 return (ptr);
425 return __alloc_bootmem_limit(size, align, goal, limit);