Import 2.4.0-test6pre7
[davej-history.git] / mm / bootmem.c
blob0e11fe9ed716f651d9b475fc6d6a0011be0cb9b9
1 /*
2 * linux/mm/initmem.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/swapctl.h>
16 #include <linux/interrupt.h>
17 #include <linux/init.h>
18 #include <linux/bootmem.h>
19 #include <linux/mmzone.h>
20 #include <asm/dma.h>
23 * Access to this subsystem has to be serialized externally. (this is
24 * true for the boot process anyway)
26 unsigned long max_low_pfn;
27 unsigned long min_low_pfn;
29 /* return the number of _pages_ that will be allocated for the boot bitmap */
30 unsigned long __init bootmem_bootmap_pages (unsigned long pages)
32 unsigned long mapsize;
34 mapsize = (pages+7)/8;
35 mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK;
36 mapsize >>= PAGE_SHIFT;
38 return mapsize;
42 * Called once to set up the allocator itself.
44 static unsigned long __init init_bootmem_core (bootmem_data_t *bdata,
45 unsigned long mapstart, unsigned long start, unsigned long end)
47 unsigned long mapsize = ((end - start)+7)/8;
49 mapsize = (mapsize + (sizeof(long) - 1UL)) & ~(sizeof(long) - 1UL);
50 bdata->node_bootmem_map = phys_to_virt(mapstart << PAGE_SHIFT);
51 bdata->node_boot_start = (start << PAGE_SHIFT);
52 bdata->node_low_pfn = end;
55 * Initially all pages are reserved - setup_arch() has to
56 * register free RAM areas explicitly.
58 memset(bdata->node_bootmem_map, 0xff, mapsize);
60 return mapsize;
64 * Marks a particular physical memory range as unallocatable. Usable RAM
65 * might be used for boot-time allocations - or it might get added
66 * to the free page pool later on.
68 static void __init reserve_bootmem_core(bootmem_data_t *bdata, unsigned long addr, unsigned long size)
70 unsigned long i;
72 * round up, partially reserved pages are considered
73 * fully reserved.
75 unsigned long sidx = (addr - bdata->node_boot_start)/PAGE_SIZE;
76 unsigned long eidx = (addr + size - bdata->node_boot_start +
77 PAGE_SIZE-1)/PAGE_SIZE;
78 unsigned long end = (addr + size + PAGE_SIZE-1)/PAGE_SIZE;
80 if (!size) BUG();
82 if (end > bdata->node_low_pfn)
83 BUG();
84 for (i = sidx; i < eidx; i++)
85 if (test_and_set_bit(i, bdata->node_bootmem_map))
86 printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE);
89 static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, unsigned long size)
91 unsigned long i;
92 unsigned long start;
94 * round down end of usable mem, partially free pages are
95 * considered reserved.
97 unsigned long sidx;
98 unsigned long eidx = (addr + size - bdata->node_boot_start)/PAGE_SIZE;
99 unsigned long end = (addr + size)/PAGE_SIZE;
101 if (!size) BUG();
102 if (end > bdata->node_low_pfn)
103 BUG();
106 * Round up the beginning of the address.
108 start = (addr + PAGE_SIZE-1) / PAGE_SIZE;
109 sidx = start - (bdata->node_boot_start/PAGE_SIZE);
111 for (i = sidx; i < eidx; i++) {
112 if (!test_and_clear_bit(i, bdata->node_bootmem_map))
113 BUG();
118 * We 'merge' subsequent allocations to save space. We might 'lose'
119 * some fraction of a page if allocations cannot be satisfied due to
120 * size constraints on boxes where there is physical RAM space
121 * fragmentation - in these cases * (mostly large memory boxes) this
122 * is not a problem.
124 * On low memory boxes we get it right in 100% of the cases.
128 * alignment has to be a power of 2 value.
130 static void * __init __alloc_bootmem_core (bootmem_data_t *bdata,
131 unsigned long size, unsigned long align, unsigned long goal)
133 unsigned long i, start = 0;
134 void *ret;
135 unsigned long offset, remaining_size;
136 unsigned long areasize, preferred, incr;
137 unsigned long eidx = bdata->node_low_pfn - (bdata->node_boot_start >>
138 PAGE_SHIFT);
140 if (!size) BUG();
143 * We try to allocate bootmem pages above 'goal'
144 * first, then we try to allocate lower pages.
146 if (goal && (goal >= bdata->node_boot_start) &&
147 ((goal >> PAGE_SHIFT) < bdata->node_low_pfn)) {
148 preferred = goal - bdata->node_boot_start;
149 } else
150 preferred = 0;
152 preferred = ((preferred + align - 1) & ~(align - 1)) >> PAGE_SHIFT;
153 areasize = (size+PAGE_SIZE-1)/PAGE_SIZE;
154 incr = align >> PAGE_SHIFT ? : 1;
156 restart_scan:
157 for (i = preferred; i < eidx; i += incr) {
158 unsigned long j;
159 if (test_bit(i, bdata->node_bootmem_map))
160 continue;
161 for (j = i + 1; j < i + areasize; ++j) {
162 if (j >= eidx)
163 goto fail_block;
164 if (test_bit (j, bdata->node_bootmem_map))
165 goto fail_block;
167 start = i;
168 goto found;
169 fail_block:;
171 if (preferred) {
172 preferred = 0;
173 goto restart_scan;
176 * Whoops, we cannot satisfy the allocation request.
178 BUG();
179 found:
180 if (start >= eidx)
181 BUG();
184 * Is the next page of the previous allocation-end the start
185 * of this allocation's buffer? If yes then we can 'merge'
186 * the previous partial page with this allocation.
188 if (align <= PAGE_SIZE
189 && bdata->last_offset && bdata->last_pos+1 == start) {
190 offset = (bdata->last_offset+align-1) & ~(align-1);
191 if (offset > PAGE_SIZE)
192 BUG();
193 remaining_size = PAGE_SIZE-offset;
194 if (size < remaining_size) {
195 areasize = 0;
196 // last_pos unchanged
197 bdata->last_offset = offset+size;
198 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
199 bdata->node_boot_start);
200 } else {
201 remaining_size = size - remaining_size;
202 areasize = (remaining_size+PAGE_SIZE-1)/PAGE_SIZE;
203 ret = phys_to_virt(bdata->last_pos*PAGE_SIZE + offset +
204 bdata->node_boot_start);
205 bdata->last_pos = start+areasize-1;
206 bdata->last_offset = remaining_size;
208 bdata->last_offset &= ~PAGE_MASK;
209 } else {
210 bdata->last_pos = start + areasize - 1;
211 bdata->last_offset = size & ~PAGE_MASK;
212 ret = phys_to_virt(start * PAGE_SIZE + bdata->node_boot_start);
215 * Reserve the area now:
217 for (i = start; i < start+areasize; i++)
218 if (test_and_set_bit(i, bdata->node_bootmem_map))
219 BUG();
220 memset(ret, 0, size);
221 return ret;
224 static unsigned long __init free_all_bootmem_core(int nid, bootmem_data_t *bdata)
226 struct page * page;
227 unsigned long i, count, total = 0;
228 unsigned long idx;
230 if (!bdata->node_bootmem_map) BUG();
232 page = NODE_MEM_MAP(nid);
233 count = 0;
234 idx = bdata->node_low_pfn - (bdata->node_boot_start >> PAGE_SHIFT);
235 for (i = 0; i < idx; i++, page++) {
236 if (!test_bit(i, bdata->node_bootmem_map)) {
237 count++;
238 ClearPageReserved(page);
239 set_page_count(page, 1);
240 __free_page(page);
243 total += count;
246 * Now free the allocator bitmap itself, it's not
247 * needed anymore:
249 page = mem_map + MAP_NR(bdata->node_bootmem_map);
250 count = 0;
251 for (i = 0; i < ((bdata->node_low_pfn-(bdata->node_boot_start >> PAGE_SHIFT))/8 + PAGE_SIZE-1)/PAGE_SIZE; i++,page++) {
252 count++;
253 ClearPageReserved(page);
254 set_page_count(page, 1);
255 __free_page(page);
257 total += count;
258 bdata->node_bootmem_map = NULL;
260 return total;
263 unsigned long __init init_bootmem_node (int nid, unsigned long freepfn, unsigned long startpfn, unsigned long endpfn)
265 return(init_bootmem_core(NODE_DATA(nid)->bdata, freepfn, startpfn, endpfn));
268 void __init reserve_bootmem_node (int nid, unsigned long physaddr, unsigned long size)
270 reserve_bootmem_core(NODE_DATA(nid)->bdata, physaddr, size);
273 void __init free_bootmem_node (int nid, unsigned long physaddr, unsigned long size)
275 return(free_bootmem_core(NODE_DATA(nid)->bdata, physaddr, size));
278 unsigned long __init free_all_bootmem_node (int nid)
280 return(free_all_bootmem_core(nid, NODE_DATA(nid)->bdata));
283 unsigned long __init init_bootmem (unsigned long start, unsigned long pages)
285 max_low_pfn = pages;
286 min_low_pfn = start;
287 return(init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages));
290 void __init reserve_bootmem (unsigned long addr, unsigned long size)
292 reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size);
295 void __init free_bootmem (unsigned long addr, unsigned long size)
297 return(free_bootmem_core(NODE_DATA(0)->bdata, addr, size));
300 unsigned long __init free_all_bootmem (void)
302 return(free_all_bootmem_core(0, NODE_DATA(0)->bdata));
305 void * __init __alloc_bootmem (unsigned long size, unsigned long align, unsigned long goal)
308 * In the discontigmem case, all non-node specific allocations come
309 * from the first node, node 0.
311 return(__alloc_bootmem_core(NODE_DATA(0)->bdata, size, align, goal));
314 void * __init __alloc_bootmem_node (int nid, unsigned long size, unsigned long align, unsigned long goal)
316 return(__alloc_bootmem_core(NODE_DATA(nid)->bdata, size, align, goal));