Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | */ | |
1da177e4 | 11 | #include <linux/init.h> |
bbc7b92e | 12 | #include <linux/pfn.h> |
1da177e4 | 13 | #include <linux/bootmem.h> |
1da177e4 | 14 | #include <linux/module.h> |
e786e86a FBH |
15 | |
16 | #include <asm/bug.h> | |
1da177e4 | 17 | #include <asm/io.h> |
dfd54cbc | 18 | #include <asm/processor.h> |
e786e86a | 19 | |
1da177e4 LT |
20 | #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 | */ | |
26 | unsigned long max_low_pfn; | |
27 | unsigned long min_low_pfn; | |
28 | unsigned long max_pfn; | |
29 | ||
679bc9fb | 30 | static LIST_HEAD(bdata_list); |
92aa63a5 VG |
31 | #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 | */ | |
36 | unsigned long saved_max_pfn; | |
37 | #endif | |
38 | ||
1da177e4 | 39 | /* return the number of _pages_ that will be allocated for the boot bitmap */ |
f71bf0ca | 40 | unsigned long __init bootmem_bootmap_pages(unsigned long pages) |
1da177e4 LT |
41 | { |
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 | } | |
f71bf0ca | 50 | |
679bc9fb KH |
51 | /* |
52 | * link bdata in order | |
53 | */ | |
69d49e68 | 54 | static void __init link_bootmem(bootmem_data_t *bdata) |
679bc9fb KH |
55 | { |
56 | bootmem_data_t *ent; | |
f71bf0ca | 57 | |
679bc9fb KH |
58 | 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); | |
679bc9fb KH |
70 | } |
71 | ||
bbc7b92e FBH |
72 | /* |
73 | * Given an initialised bdata, it returns the size of the boot bitmap | |
74 | */ | |
75 | static 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 | } | |
1da177e4 LT |
84 | |
85 | /* | |
86 | * Called once to set up the allocator itself. | |
87 | */ | |
f71bf0ca | 88 | static unsigned long __init init_bootmem_core(pg_data_t *pgdat, |
1da177e4 LT |
89 | unsigned long mapstart, unsigned long start, unsigned long end) |
90 | { | |
91 | bootmem_data_t *bdata = pgdat->bdata; | |
bbc7b92e | 92 | unsigned long mapsize; |
1da177e4 | 93 | |
bbc7b92e FBH |
94 | bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart)); |
95 | bdata->node_boot_start = PFN_PHYS(start); | |
1da177e4 | 96 | bdata->node_low_pfn = end; |
679bc9fb | 97 | link_bootmem(bdata); |
1da177e4 LT |
98 | |
99 | /* | |
100 | * Initially all pages are reserved - setup_arch() has to | |
101 | * register free RAM areas explicitly. | |
102 | */ | |
bbc7b92e | 103 | mapsize = get_mapsize(bdata); |
1da177e4 LT |
104 | memset(bdata->node_bootmem_map, 0xff, mapsize); |
105 | ||
106 | return mapsize; | |
107 | } | |
108 | ||
109 | /* | |
110 | * Marks a particular physical memory range as unallocatable. Usable RAM | |
111 | * might be used for boot-time allocations - or it might get added | |
112 | * to the free page pool later on. | |
113 | */ | |
72a7fe39 BW |
114 | static int __init reserve_bootmem_core(bootmem_data_t *bdata, |
115 | unsigned long addr, unsigned long size, int flags) | |
1da177e4 | 116 | { |
bbc7b92e | 117 | unsigned long sidx, eidx; |
1da177e4 | 118 | unsigned long i; |
72a7fe39 | 119 | int ret; |
bbc7b92e | 120 | |
1da177e4 LT |
121 | /* |
122 | * round up, partially reserved pages are considered | |
123 | * fully reserved. | |
124 | */ | |
1da177e4 | 125 | BUG_ON(!size); |
bbc7b92e FBH |
126 | BUG_ON(PFN_DOWN(addr) >= bdata->node_low_pfn); |
127 | BUG_ON(PFN_UP(addr + size) > bdata->node_low_pfn); | |
128 | ||
129 | sidx = PFN_DOWN(addr - bdata->node_boot_start); | |
130 | eidx = PFN_UP(addr + size - bdata->node_boot_start); | |
1da177e4 LT |
131 | |
132 | for (i = sidx; i < eidx; i++) | |
133 | if (test_and_set_bit(i, bdata->node_bootmem_map)) { | |
134 | #ifdef CONFIG_DEBUG_BOOTMEM | |
135 | printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE); | |
136 | #endif | |
72a7fe39 BW |
137 | if (flags & BOOTMEM_EXCLUSIVE) { |
138 | ret = -EBUSY; | |
139 | goto err; | |
140 | } | |
1da177e4 | 141 | } |
72a7fe39 BW |
142 | |
143 | return 0; | |
144 | ||
145 | err: | |
146 | /* unreserve memory we accidentally reserved */ | |
147 | for (i--; i >= sidx; i--) | |
148 | clear_bit(i, bdata->node_bootmem_map); | |
149 | ||
150 | return ret; | |
1da177e4 LT |
151 | } |
152 | ||
bb0923a6 FBH |
153 | static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, |
154 | unsigned long size) | |
1da177e4 | 155 | { |
bbc7b92e | 156 | unsigned long sidx, eidx; |
1da177e4 | 157 | unsigned long i; |
bbc7b92e | 158 | |
1da177e4 LT |
159 | /* |
160 | * round down end of usable mem, partially free pages are | |
161 | * considered reserved. | |
162 | */ | |
1da177e4 | 163 | BUG_ON(!size); |
bbc7b92e | 164 | BUG_ON(PFN_DOWN(addr + size) > bdata->node_low_pfn); |
1da177e4 LT |
165 | |
166 | if (addr < bdata->last_success) | |
167 | bdata->last_success = addr; | |
168 | ||
169 | /* | |
170 | * Round up the beginning of the address. | |
171 | */ | |
bbc7b92e FBH |
172 | sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start); |
173 | eidx = PFN_DOWN(addr + size - bdata->node_boot_start); | |
1da177e4 LT |
174 | |
175 | for (i = sidx; i < eidx; i++) { | |
176 | if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map))) | |
177 | BUG(); | |
178 | } | |
179 | } | |
180 | ||
181 | /* | |
182 | * We 'merge' subsequent allocations to save space. We might 'lose' | |
183 | * some fraction of a page if allocations cannot be satisfied due to | |
184 | * size constraints on boxes where there is physical RAM space | |
185 | * fragmentation - in these cases (mostly large memory boxes) this | |
186 | * is not a problem. | |
187 | * | |
188 | * On low memory boxes we get it right in 100% of the cases. | |
189 | * | |
190 | * alignment has to be a power of 2 value. | |
191 | * | |
192 | * NOTE: This function is _not_ reentrant. | |
193 | */ | |
267b4801 | 194 | void * __init |
1da177e4 | 195 | __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size, |
281dd25c | 196 | unsigned long align, unsigned long goal, unsigned long limit) |
1da177e4 LT |
197 | { |
198 | unsigned long offset, remaining_size, areasize, preferred; | |
bbc7b92e | 199 | unsigned long i, start = 0, incr, eidx, end_pfn; |
1da177e4 LT |
200 | void *ret; |
201 | ||
f71bf0ca | 202 | if (!size) { |
1da177e4 LT |
203 | printk("__alloc_bootmem_core(): zero-sized request\n"); |
204 | BUG(); | |
205 | } | |
206 | BUG_ON(align & (align-1)); | |
207 | ||
281dd25c YG |
208 | if (limit && bdata->node_boot_start >= limit) |
209 | return NULL; | |
210 | ||
7c309a64 CK |
211 | /* on nodes without memory - bootmem_map is NULL */ |
212 | if (!bdata->node_bootmem_map) | |
213 | return NULL; | |
214 | ||
bbc7b92e FBH |
215 | end_pfn = bdata->node_low_pfn; |
216 | limit = PFN_DOWN(limit); | |
281dd25c YG |
217 | if (limit && end_pfn > limit) |
218 | end_pfn = limit; | |
219 | ||
bbc7b92e | 220 | eidx = end_pfn - PFN_DOWN(bdata->node_boot_start); |
1da177e4 | 221 | offset = 0; |
bbc7b92e FBH |
222 | if (align && (bdata->node_boot_start & (align - 1UL)) != 0) |
223 | offset = align - (bdata->node_boot_start & (align - 1UL)); | |
224 | offset = PFN_DOWN(offset); | |
1da177e4 LT |
225 | |
226 | /* | |
227 | * We try to allocate bootmem pages above 'goal' | |
228 | * first, then we try to allocate lower pages. | |
229 | */ | |
bbc7b92e | 230 | if (goal && goal >= bdata->node_boot_start && PFN_DOWN(goal) < end_pfn) { |
1da177e4 LT |
231 | preferred = goal - bdata->node_boot_start; |
232 | ||
233 | if (bdata->last_success >= preferred) | |
281dd25c YG |
234 | if (!limit || (limit && limit > bdata->last_success)) |
235 | preferred = bdata->last_success; | |
1da177e4 LT |
236 | } else |
237 | preferred = 0; | |
238 | ||
bbc7b92e FBH |
239 | preferred = PFN_DOWN(ALIGN(preferred, align)) + offset; |
240 | areasize = (size + PAGE_SIZE-1) / PAGE_SIZE; | |
1da177e4 LT |
241 | incr = align >> PAGE_SHIFT ? : 1; |
242 | ||
243 | restart_scan: | |
244 | for (i = preferred; i < eidx; i += incr) { | |
245 | unsigned long j; | |
246 | i = find_next_zero_bit(bdata->node_bootmem_map, eidx, i); | |
247 | i = ALIGN(i, incr); | |
66d43e98 HM |
248 | if (i >= eidx) |
249 | break; | |
1da177e4 LT |
250 | if (test_bit(i, bdata->node_bootmem_map)) |
251 | continue; | |
252 | for (j = i + 1; j < i + areasize; ++j) { | |
253 | if (j >= eidx) | |
254 | goto fail_block; | |
f71bf0ca | 255 | if (test_bit(j, bdata->node_bootmem_map)) |
1da177e4 LT |
256 | goto fail_block; |
257 | } | |
258 | start = i; | |
259 | goto found; | |
260 | fail_block: | |
261 | i = ALIGN(j, incr); | |
262 | } | |
263 | ||
264 | if (preferred > offset) { | |
265 | preferred = offset; | |
266 | goto restart_scan; | |
267 | } | |
268 | return NULL; | |
269 | ||
270 | found: | |
bbc7b92e | 271 | bdata->last_success = PFN_PHYS(start); |
1da177e4 LT |
272 | BUG_ON(start >= eidx); |
273 | ||
274 | /* | |
275 | * Is the next page of the previous allocation-end the start | |
276 | * of this allocation's buffer? If yes then we can 'merge' | |
277 | * the previous partial page with this allocation. | |
278 | */ | |
279 | if (align < PAGE_SIZE && | |
280 | bdata->last_offset && bdata->last_pos+1 == start) { | |
8c0e33c1 | 281 | offset = ALIGN(bdata->last_offset, align); |
1da177e4 | 282 | BUG_ON(offset > PAGE_SIZE); |
f71bf0ca | 283 | remaining_size = PAGE_SIZE - offset; |
1da177e4 LT |
284 | if (size < remaining_size) { |
285 | areasize = 0; | |
286 | /* last_pos unchanged */ | |
f71bf0ca FBH |
287 | bdata->last_offset = offset + size; |
288 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + | |
289 | offset + | |
290 | bdata->node_boot_start); | |
1da177e4 LT |
291 | } else { |
292 | remaining_size = size - remaining_size; | |
f71bf0ca FBH |
293 | areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE; |
294 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + | |
295 | offset + | |
296 | bdata->node_boot_start); | |
297 | bdata->last_pos = start + areasize - 1; | |
1da177e4 LT |
298 | bdata->last_offset = remaining_size; |
299 | } | |
300 | bdata->last_offset &= ~PAGE_MASK; | |
301 | } else { | |
302 | bdata->last_pos = start + areasize - 1; | |
303 | bdata->last_offset = size & ~PAGE_MASK; | |
304 | ret = phys_to_virt(start * PAGE_SIZE + bdata->node_boot_start); | |
305 | } | |
306 | ||
307 | /* | |
308 | * Reserve the area now: | |
309 | */ | |
f71bf0ca | 310 | for (i = start; i < start + areasize; i++) |
1da177e4 LT |
311 | if (unlikely(test_and_set_bit(i, bdata->node_bootmem_map))) |
312 | BUG(); | |
313 | memset(ret, 0, size); | |
314 | return ret; | |
315 | } | |
316 | ||
317 | static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat) | |
318 | { | |
319 | struct page *page; | |
d41dee36 | 320 | unsigned long pfn; |
1da177e4 LT |
321 | bootmem_data_t *bdata = pgdat->bdata; |
322 | unsigned long i, count, total = 0; | |
323 | unsigned long idx; | |
324 | unsigned long *map; | |
325 | int gofast = 0; | |
326 | ||
327 | BUG_ON(!bdata->node_bootmem_map); | |
328 | ||
329 | count = 0; | |
330 | /* first extant page of the node */ | |
bbc7b92e FBH |
331 | pfn = PFN_DOWN(bdata->node_boot_start); |
332 | idx = bdata->node_low_pfn - pfn; | |
1da177e4 LT |
333 | map = bdata->node_bootmem_map; |
334 | /* Check physaddr is O(LOG2(BITS_PER_LONG)) page aligned */ | |
335 | if (bdata->node_boot_start == 0 || | |
336 | ffs(bdata->node_boot_start) - PAGE_SHIFT > ffs(BITS_PER_LONG)) | |
337 | gofast = 1; | |
338 | for (i = 0; i < idx; ) { | |
339 | unsigned long v = ~map[i / BITS_PER_LONG]; | |
d41dee36 | 340 | |
1da177e4 | 341 | if (gofast && v == ~0UL) { |
a226f6c8 | 342 | int order; |
1da177e4 | 343 | |
d41dee36 | 344 | page = pfn_to_page(pfn); |
1da177e4 | 345 | count += BITS_PER_LONG; |
1da177e4 | 346 | order = ffs(BITS_PER_LONG) - 1; |
a226f6c8 | 347 | __free_pages_bootmem(page, order); |
1da177e4 LT |
348 | i += BITS_PER_LONG; |
349 | page += BITS_PER_LONG; | |
350 | } else if (v) { | |
351 | unsigned long m; | |
d41dee36 AW |
352 | |
353 | page = pfn_to_page(pfn); | |
1da177e4 LT |
354 | for (m = 1; m && i < idx; m<<=1, page++, i++) { |
355 | if (v & m) { | |
356 | count++; | |
a226f6c8 | 357 | __free_pages_bootmem(page, 0); |
1da177e4 LT |
358 | } |
359 | } | |
360 | } else { | |
f71bf0ca | 361 | i += BITS_PER_LONG; |
1da177e4 | 362 | } |
d41dee36 | 363 | pfn += BITS_PER_LONG; |
1da177e4 LT |
364 | } |
365 | total += count; | |
366 | ||
367 | /* | |
368 | * Now free the allocator bitmap itself, it's not | |
369 | * needed anymore: | |
370 | */ | |
371 | page = virt_to_page(bdata->node_bootmem_map); | |
372 | count = 0; | |
bbc7b92e FBH |
373 | idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT; |
374 | for (i = 0; i < idx; i++, page++) { | |
a226f6c8 | 375 | __free_pages_bootmem(page, 0); |
bbc7b92e | 376 | count++; |
1da177e4 LT |
377 | } |
378 | total += count; | |
379 | bdata->node_bootmem_map = NULL; | |
380 | ||
381 | return total; | |
382 | } | |
383 | ||
f71bf0ca | 384 | unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn, |
bb0923a6 | 385 | unsigned long startpfn, unsigned long endpfn) |
1da177e4 | 386 | { |
f71bf0ca | 387 | return init_bootmem_core(pgdat, freepfn, startpfn, endpfn); |
1da177e4 LT |
388 | } |
389 | ||
f71bf0ca | 390 | void __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
72a7fe39 | 391 | unsigned long size, int flags) |
1da177e4 | 392 | { |
72a7fe39 | 393 | reserve_bootmem_core(pgdat->bdata, physaddr, size, flags); |
1da177e4 LT |
394 | } |
395 | ||
f71bf0ca FBH |
396 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
397 | unsigned long size) | |
1da177e4 LT |
398 | { |
399 | free_bootmem_core(pgdat->bdata, physaddr, size); | |
400 | } | |
401 | ||
f71bf0ca | 402 | unsigned long __init free_all_bootmem_node(pg_data_t *pgdat) |
1da177e4 | 403 | { |
f71bf0ca | 404 | return free_all_bootmem_core(pgdat); |
1da177e4 LT |
405 | } |
406 | ||
f71bf0ca | 407 | unsigned long __init init_bootmem(unsigned long start, unsigned long pages) |
1da177e4 LT |
408 | { |
409 | max_low_pfn = pages; | |
410 | min_low_pfn = start; | |
f71bf0ca | 411 | return init_bootmem_core(NODE_DATA(0), start, 0, pages); |
1da177e4 LT |
412 | } |
413 | ||
414 | #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE | |
72a7fe39 BW |
415 | int __init reserve_bootmem(unsigned long addr, unsigned long size, |
416 | int flags) | |
1da177e4 | 417 | { |
72a7fe39 | 418 | return reserve_bootmem_core(NODE_DATA(0)->bdata, addr, size, flags); |
1da177e4 LT |
419 | } |
420 | #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */ | |
421 | ||
f71bf0ca | 422 | void __init free_bootmem(unsigned long addr, unsigned long size) |
1da177e4 LT |
423 | { |
424 | free_bootmem_core(NODE_DATA(0)->bdata, addr, size); | |
425 | } | |
426 | ||
f71bf0ca | 427 | unsigned long __init free_all_bootmem(void) |
1da177e4 | 428 | { |
f71bf0ca | 429 | return free_all_bootmem_core(NODE_DATA(0)); |
1da177e4 LT |
430 | } |
431 | ||
bb0923a6 FBH |
432 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, |
433 | unsigned long goal) | |
1da177e4 | 434 | { |
679bc9fb | 435 | bootmem_data_t *bdata; |
1da177e4 LT |
436 | void *ptr; |
437 | ||
f71bf0ca FBH |
438 | list_for_each_entry(bdata, &bdata_list, list) { |
439 | ptr = __alloc_bootmem_core(bdata, size, align, goal, 0); | |
440 | if (ptr) | |
441 | return ptr; | |
442 | } | |
a8062231 AK |
443 | return NULL; |
444 | } | |
1da177e4 | 445 | |
bb0923a6 FBH |
446 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, |
447 | unsigned long goal) | |
a8062231 AK |
448 | { |
449 | void *mem = __alloc_bootmem_nopanic(size,align,goal); | |
f71bf0ca | 450 | |
a8062231 AK |
451 | if (mem) |
452 | return mem; | |
1da177e4 LT |
453 | /* |
454 | * Whoops, we cannot satisfy the allocation request. | |
455 | */ | |
456 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); | |
457 | panic("Out of memory"); | |
458 | return NULL; | |
459 | } | |
460 | ||
281dd25c | 461 | |
bb0923a6 FBH |
462 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
463 | unsigned long align, unsigned long goal) | |
1da177e4 LT |
464 | { |
465 | void *ptr; | |
466 | ||
008857c1 | 467 | ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); |
1da177e4 | 468 | if (ptr) |
f71bf0ca | 469 | return ptr; |
1da177e4 | 470 | |
008857c1 | 471 | return __alloc_bootmem(size, align, goal); |
1da177e4 LT |
472 | } |
473 | ||
dfd54cbc HC |
474 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
475 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL | |
476 | #endif | |
008857c1 | 477 | |
bb0923a6 FBH |
478 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, |
479 | unsigned long goal) | |
008857c1 | 480 | { |
679bc9fb | 481 | bootmem_data_t *bdata; |
008857c1 RT |
482 | void *ptr; |
483 | ||
f71bf0ca | 484 | list_for_each_entry(bdata, &bdata_list, list) { |
dfd54cbc HC |
485 | ptr = __alloc_bootmem_core(bdata, size, align, goal, |
486 | ARCH_LOW_ADDRESS_LIMIT); | |
f71bf0ca FBH |
487 | if (ptr) |
488 | return ptr; | |
489 | } | |
008857c1 RT |
490 | |
491 | /* | |
492 | * Whoops, we cannot satisfy the allocation request. | |
493 | */ | |
494 | printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size); | |
495 | panic("Out of low memory"); | |
496 | return NULL; | |
497 | } | |
498 | ||
499 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, | |
500 | unsigned long align, unsigned long goal) | |
501 | { | |
dfd54cbc HC |
502 | return __alloc_bootmem_core(pgdat->bdata, size, align, goal, |
503 | ARCH_LOW_ADDRESS_LIMIT); | |
008857c1 | 504 | } |