Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
57cfc29e | 2 | * bootmem - A boot-time physical memory allocator and configurator |
1da177e4 LT |
3 | * |
4 | * Copyright (C) 1999 Ingo Molnar | |
57cfc29e JW |
5 | * 1999 Kanoj Sarcar, SGI |
6 | * 2008 Johannes Weiner | |
1da177e4 | 7 | * |
57cfc29e JW |
8 | * Access to this subsystem has to be serialized externally (which is true |
9 | * for the boot process anyway). | |
1da177e4 | 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 | ||
1da177e4 LT |
22 | unsigned long max_low_pfn; |
23 | unsigned long min_low_pfn; | |
24 | unsigned long max_pfn; | |
25 | ||
92aa63a5 VG |
26 | #ifdef CONFIG_CRASH_DUMP |
27 | /* | |
28 | * If we have booted due to a crash, max_pfn will be a very low value. We need | |
29 | * to know the amount of memory that the previous kernel used. | |
30 | */ | |
31 | unsigned long saved_max_pfn; | |
32 | #endif | |
33 | ||
b61bfa3c JW |
34 | bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata; |
35 | ||
636cc40c JW |
36 | static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list); |
37 | ||
2e5237da JW |
38 | static int bootmem_debug; |
39 | ||
c1329375 TH |
40 | /* |
41 | * If an arch needs to apply workarounds to bootmem allocation, it can | |
42 | * set CONFIG_HAVE_ARCH_BOOTMEM and define a wrapper around | |
43 | * __alloc_bootmem_core(). | |
44 | */ | |
45 | #ifndef CONFIG_HAVE_ARCH_BOOTMEM | |
46 | #define alloc_bootmem_core(bdata, size, align, goal, limit) \ | |
47 | __alloc_bootmem_core((bdata), (size), (align), (goal), (limit)) | |
48 | #endif | |
49 | ||
2e5237da JW |
50 | static int __init bootmem_debug_setup(char *buf) |
51 | { | |
52 | bootmem_debug = 1; | |
53 | return 0; | |
54 | } | |
55 | early_param("bootmem_debug", bootmem_debug_setup); | |
56 | ||
57 | #define bdebug(fmt, args...) ({ \ | |
58 | if (unlikely(bootmem_debug)) \ | |
59 | printk(KERN_INFO \ | |
60 | "bootmem::%s " fmt, \ | |
80a914dc | 61 | __func__, ## args); \ |
2e5237da JW |
62 | }) |
63 | ||
df049a5f | 64 | static unsigned long __init bootmap_bytes(unsigned long pages) |
223e8dc9 | 65 | { |
df049a5f | 66 | unsigned long bytes = (pages + 7) / 8; |
223e8dc9 | 67 | |
df049a5f | 68 | return ALIGN(bytes, sizeof(long)); |
223e8dc9 JW |
69 | } |
70 | ||
a66fd7da JW |
71 | /** |
72 | * bootmem_bootmap_pages - calculate bitmap size in pages | |
73 | * @pages: number of pages the bitmap has to represent | |
74 | */ | |
f71bf0ca | 75 | unsigned long __init bootmem_bootmap_pages(unsigned long pages) |
1da177e4 | 76 | { |
df049a5f | 77 | unsigned long bytes = bootmap_bytes(pages); |
1da177e4 | 78 | |
df049a5f | 79 | return PAGE_ALIGN(bytes) >> PAGE_SHIFT; |
1da177e4 | 80 | } |
f71bf0ca | 81 | |
679bc9fb KH |
82 | /* |
83 | * link bdata in order | |
84 | */ | |
69d49e68 | 85 | static void __init link_bootmem(bootmem_data_t *bdata) |
679bc9fb | 86 | { |
636cc40c | 87 | struct list_head *iter; |
f71bf0ca | 88 | |
636cc40c JW |
89 | list_for_each(iter, &bdata_list) { |
90 | bootmem_data_t *ent; | |
91 | ||
92 | ent = list_entry(iter, bootmem_data_t, list); | |
3560e249 | 93 | if (bdata->node_min_pfn < ent->node_min_pfn) |
636cc40c | 94 | break; |
679bc9fb | 95 | } |
636cc40c | 96 | list_add_tail(&bdata->list, iter); |
679bc9fb KH |
97 | } |
98 | ||
1da177e4 LT |
99 | /* |
100 | * Called once to set up the allocator itself. | |
101 | */ | |
8ae04463 | 102 | static unsigned long __init init_bootmem_core(bootmem_data_t *bdata, |
1da177e4 LT |
103 | unsigned long mapstart, unsigned long start, unsigned long end) |
104 | { | |
bbc7b92e | 105 | unsigned long mapsize; |
1da177e4 | 106 | |
2dbb51c4 | 107 | mminit_validate_memmodel_limits(&start, &end); |
bbc7b92e | 108 | bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart)); |
3560e249 | 109 | bdata->node_min_pfn = start; |
1da177e4 | 110 | bdata->node_low_pfn = end; |
679bc9fb | 111 | link_bootmem(bdata); |
1da177e4 LT |
112 | |
113 | /* | |
114 | * Initially all pages are reserved - setup_arch() has to | |
115 | * register free RAM areas explicitly. | |
116 | */ | |
df049a5f | 117 | mapsize = bootmap_bytes(end - start); |
1da177e4 LT |
118 | memset(bdata->node_bootmem_map, 0xff, mapsize); |
119 | ||
2e5237da JW |
120 | bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n", |
121 | bdata - bootmem_node_data, start, mapstart, end, mapsize); | |
122 | ||
1da177e4 LT |
123 | return mapsize; |
124 | } | |
125 | ||
a66fd7da JW |
126 | /** |
127 | * init_bootmem_node - register a node as boot memory | |
128 | * @pgdat: node to register | |
129 | * @freepfn: pfn where the bitmap for this node is to be placed | |
130 | * @startpfn: first pfn on the node | |
131 | * @endpfn: first pfn after the node | |
132 | * | |
133 | * Returns the number of bytes needed to hold the bitmap for this node. | |
134 | */ | |
223e8dc9 JW |
135 | unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn, |
136 | unsigned long startpfn, unsigned long endpfn) | |
137 | { | |
138 | return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn); | |
139 | } | |
140 | ||
a66fd7da JW |
141 | /** |
142 | * init_bootmem - register boot memory | |
143 | * @start: pfn where the bitmap is to be placed | |
144 | * @pages: number of available physical pages | |
145 | * | |
146 | * Returns the number of bytes needed to hold the bitmap. | |
147 | */ | |
223e8dc9 JW |
148 | unsigned long __init init_bootmem(unsigned long start, unsigned long pages) |
149 | { | |
150 | max_low_pfn = pages; | |
151 | min_low_pfn = start; | |
152 | return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages); | |
153 | } | |
154 | ||
155 | static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata) | |
156 | { | |
41546c17 | 157 | int aligned; |
223e8dc9 | 158 | struct page *page; |
41546c17 JW |
159 | unsigned long start, end, pages, count = 0; |
160 | ||
161 | if (!bdata->node_bootmem_map) | |
162 | return 0; | |
163 | ||
3560e249 | 164 | start = bdata->node_min_pfn; |
41546c17 JW |
165 | end = bdata->node_low_pfn; |
166 | ||
223e8dc9 | 167 | /* |
41546c17 JW |
168 | * If the start is aligned to the machines wordsize, we might |
169 | * be able to free pages in bulks of that order. | |
223e8dc9 | 170 | */ |
41546c17 | 171 | aligned = !(start & (BITS_PER_LONG - 1)); |
223e8dc9 | 172 | |
41546c17 JW |
173 | bdebug("nid=%td start=%lx end=%lx aligned=%d\n", |
174 | bdata - bootmem_node_data, start, end, aligned); | |
223e8dc9 | 175 | |
41546c17 JW |
176 | while (start < end) { |
177 | unsigned long *map, idx, vec; | |
223e8dc9 | 178 | |
41546c17 | 179 | map = bdata->node_bootmem_map; |
3560e249 | 180 | idx = start - bdata->node_min_pfn; |
41546c17 JW |
181 | vec = ~map[idx / BITS_PER_LONG]; |
182 | ||
183 | if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) { | |
184 | int order = ilog2(BITS_PER_LONG); | |
185 | ||
186 | __free_pages_bootmem(pfn_to_page(start), order); | |
223e8dc9 | 187 | count += BITS_PER_LONG; |
41546c17 JW |
188 | } else { |
189 | unsigned long off = 0; | |
190 | ||
191 | while (vec && off < BITS_PER_LONG) { | |
192 | if (vec & 1) { | |
193 | page = pfn_to_page(start + off); | |
223e8dc9 | 194 | __free_pages_bootmem(page, 0); |
41546c17 | 195 | count++; |
223e8dc9 | 196 | } |
41546c17 JW |
197 | vec >>= 1; |
198 | off++; | |
223e8dc9 | 199 | } |
223e8dc9 | 200 | } |
41546c17 | 201 | start += BITS_PER_LONG; |
223e8dc9 JW |
202 | } |
203 | ||
223e8dc9 | 204 | page = virt_to_page(bdata->node_bootmem_map); |
3560e249 | 205 | pages = bdata->node_low_pfn - bdata->node_min_pfn; |
41546c17 JW |
206 | pages = bootmem_bootmap_pages(pages); |
207 | count += pages; | |
208 | while (pages--) | |
209 | __free_pages_bootmem(page++, 0); | |
223e8dc9 | 210 | |
2e5237da JW |
211 | bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count); |
212 | ||
223e8dc9 JW |
213 | return count; |
214 | } | |
215 | ||
a66fd7da JW |
216 | /** |
217 | * free_all_bootmem_node - release a node's free pages to the buddy allocator | |
218 | * @pgdat: node to be released | |
219 | * | |
220 | * Returns the number of pages actually released. | |
221 | */ | |
223e8dc9 JW |
222 | unsigned long __init free_all_bootmem_node(pg_data_t *pgdat) |
223 | { | |
224 | register_page_bootmem_info_node(pgdat); | |
225 | return free_all_bootmem_core(pgdat->bdata); | |
226 | } | |
227 | ||
a66fd7da JW |
228 | /** |
229 | * free_all_bootmem - release free pages to the buddy allocator | |
230 | * | |
231 | * Returns the number of pages actually released. | |
232 | */ | |
223e8dc9 JW |
233 | unsigned long __init free_all_bootmem(void) |
234 | { | |
235 | return free_all_bootmem_core(NODE_DATA(0)->bdata); | |
236 | } | |
237 | ||
d747fa4b JW |
238 | static void __init __free(bootmem_data_t *bdata, |
239 | unsigned long sidx, unsigned long eidx) | |
240 | { | |
241 | unsigned long idx; | |
242 | ||
243 | bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data, | |
3560e249 JW |
244 | sidx + bdata->node_min_pfn, |
245 | eidx + bdata->node_min_pfn); | |
d747fa4b | 246 | |
e2bf3cae JW |
247 | if (bdata->hint_idx > sidx) |
248 | bdata->hint_idx = sidx; | |
249 | ||
d747fa4b JW |
250 | for (idx = sidx; idx < eidx; idx++) |
251 | if (!test_and_clear_bit(idx, bdata->node_bootmem_map)) | |
252 | BUG(); | |
253 | } | |
254 | ||
255 | static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx, | |
256 | unsigned long eidx, int flags) | |
257 | { | |
258 | unsigned long idx; | |
259 | int exclusive = flags & BOOTMEM_EXCLUSIVE; | |
260 | ||
261 | bdebug("nid=%td start=%lx end=%lx flags=%x\n", | |
262 | bdata - bootmem_node_data, | |
3560e249 JW |
263 | sidx + bdata->node_min_pfn, |
264 | eidx + bdata->node_min_pfn, | |
d747fa4b JW |
265 | flags); |
266 | ||
267 | for (idx = sidx; idx < eidx; idx++) | |
268 | if (test_and_set_bit(idx, bdata->node_bootmem_map)) { | |
269 | if (exclusive) { | |
270 | __free(bdata, sidx, idx); | |
271 | return -EBUSY; | |
272 | } | |
273 | bdebug("silent double reserve of PFN %lx\n", | |
3560e249 | 274 | idx + bdata->node_min_pfn); |
d747fa4b JW |
275 | } |
276 | return 0; | |
277 | } | |
278 | ||
e2bf3cae JW |
279 | static int __init mark_bootmem_node(bootmem_data_t *bdata, |
280 | unsigned long start, unsigned long end, | |
281 | int reserve, int flags) | |
223e8dc9 JW |
282 | { |
283 | unsigned long sidx, eidx; | |
223e8dc9 | 284 | |
e2bf3cae JW |
285 | bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n", |
286 | bdata - bootmem_node_data, start, end, reserve, flags); | |
223e8dc9 | 287 | |
3560e249 | 288 | BUG_ON(start < bdata->node_min_pfn); |
e2bf3cae | 289 | BUG_ON(end > bdata->node_low_pfn); |
223e8dc9 | 290 | |
3560e249 JW |
291 | sidx = start - bdata->node_min_pfn; |
292 | eidx = end - bdata->node_min_pfn; | |
223e8dc9 | 293 | |
e2bf3cae JW |
294 | if (reserve) |
295 | return __reserve(bdata, sidx, eidx, flags); | |
223e8dc9 | 296 | else |
e2bf3cae JW |
297 | __free(bdata, sidx, eidx); |
298 | return 0; | |
299 | } | |
300 | ||
301 | static int __init mark_bootmem(unsigned long start, unsigned long end, | |
302 | int reserve, int flags) | |
303 | { | |
304 | unsigned long pos; | |
305 | bootmem_data_t *bdata; | |
306 | ||
307 | pos = start; | |
308 | list_for_each_entry(bdata, &bdata_list, list) { | |
309 | int err; | |
310 | unsigned long max; | |
311 | ||
3560e249 JW |
312 | if (pos < bdata->node_min_pfn || |
313 | pos >= bdata->node_low_pfn) { | |
e2bf3cae JW |
314 | BUG_ON(pos != start); |
315 | continue; | |
316 | } | |
317 | ||
318 | max = min(bdata->node_low_pfn, end); | |
223e8dc9 | 319 | |
e2bf3cae JW |
320 | err = mark_bootmem_node(bdata, pos, max, reserve, flags); |
321 | if (reserve && err) { | |
322 | mark_bootmem(start, pos, 0, 0); | |
323 | return err; | |
324 | } | |
223e8dc9 | 325 | |
e2bf3cae JW |
326 | if (max == end) |
327 | return 0; | |
328 | pos = bdata->node_low_pfn; | |
329 | } | |
330 | BUG(); | |
223e8dc9 JW |
331 | } |
332 | ||
a66fd7da JW |
333 | /** |
334 | * free_bootmem_node - mark a page range as usable | |
335 | * @pgdat: node the range resides on | |
336 | * @physaddr: starting address of the range | |
337 | * @size: size of the range in bytes | |
338 | * | |
339 | * Partial pages will be considered reserved and left as they are. | |
340 | * | |
e2bf3cae | 341 | * The range must reside completely on the specified node. |
a66fd7da | 342 | */ |
223e8dc9 JW |
343 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
344 | unsigned long size) | |
345 | { | |
e2bf3cae JW |
346 | unsigned long start, end; |
347 | ||
348 | start = PFN_UP(physaddr); | |
349 | end = PFN_DOWN(physaddr + size); | |
350 | ||
351 | mark_bootmem_node(pgdat->bdata, start, end, 0, 0); | |
223e8dc9 JW |
352 | } |
353 | ||
a66fd7da JW |
354 | /** |
355 | * free_bootmem - mark a page range as usable | |
356 | * @addr: starting address of the range | |
357 | * @size: size of the range in bytes | |
358 | * | |
359 | * Partial pages will be considered reserved and left as they are. | |
360 | * | |
e2bf3cae | 361 | * The range must be contiguous but may span node boundaries. |
a66fd7da | 362 | */ |
223e8dc9 JW |
363 | void __init free_bootmem(unsigned long addr, unsigned long size) |
364 | { | |
e2bf3cae | 365 | unsigned long start, end; |
a5645a61 | 366 | |
e2bf3cae JW |
367 | start = PFN_UP(addr); |
368 | end = PFN_DOWN(addr + size); | |
1da177e4 | 369 | |
e2bf3cae | 370 | mark_bootmem(start, end, 0, 0); |
1da177e4 LT |
371 | } |
372 | ||
a66fd7da JW |
373 | /** |
374 | * reserve_bootmem_node - mark a page range as reserved | |
375 | * @pgdat: node the range resides on | |
376 | * @physaddr: starting address of the range | |
377 | * @size: size of the range in bytes | |
378 | * @flags: reservation flags (see linux/bootmem.h) | |
379 | * | |
380 | * Partial pages will be reserved. | |
381 | * | |
e2bf3cae | 382 | * The range must reside completely on the specified node. |
a66fd7da | 383 | */ |
223e8dc9 JW |
384 | int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
385 | unsigned long size, int flags) | |
1da177e4 | 386 | { |
e2bf3cae | 387 | unsigned long start, end; |
1da177e4 | 388 | |
e2bf3cae JW |
389 | start = PFN_DOWN(physaddr); |
390 | end = PFN_UP(physaddr + size); | |
391 | ||
392 | return mark_bootmem_node(pgdat->bdata, start, end, 1, flags); | |
223e8dc9 | 393 | } |
5a982cbc | 394 | |
a66fd7da JW |
395 | /** |
396 | * reserve_bootmem - mark a page range as usable | |
397 | * @addr: starting address of the range | |
398 | * @size: size of the range in bytes | |
399 | * @flags: reservation flags (see linux/bootmem.h) | |
400 | * | |
401 | * Partial pages will be reserved. | |
402 | * | |
e2bf3cae | 403 | * The range must be contiguous but may span node boundaries. |
a66fd7da | 404 | */ |
223e8dc9 JW |
405 | int __init reserve_bootmem(unsigned long addr, unsigned long size, |
406 | int flags) | |
407 | { | |
e2bf3cae | 408 | unsigned long start, end; |
1da177e4 | 409 | |
e2bf3cae JW |
410 | start = PFN_DOWN(addr); |
411 | end = PFN_UP(addr + size); | |
223e8dc9 | 412 | |
e2bf3cae | 413 | return mark_bootmem(start, end, 1, flags); |
1da177e4 LT |
414 | } |
415 | ||
481ebd0d JW |
416 | static unsigned long align_idx(struct bootmem_data *bdata, unsigned long idx, |
417 | unsigned long step) | |
418 | { | |
419 | unsigned long base = bdata->node_min_pfn; | |
420 | ||
421 | /* | |
422 | * Align the index with respect to the node start so that the | |
423 | * combination of both satisfies the requested alignment. | |
424 | */ | |
425 | ||
426 | return ALIGN(base + idx, step) - base; | |
427 | } | |
428 | ||
429 | static unsigned long align_off(struct bootmem_data *bdata, unsigned long off, | |
430 | unsigned long align) | |
431 | { | |
432 | unsigned long base = PFN_PHYS(bdata->node_min_pfn); | |
433 | ||
434 | /* Same as align_idx for byte offsets */ | |
435 | ||
436 | return ALIGN(base + off, align) - base; | |
437 | } | |
438 | ||
c1329375 | 439 | static void * __init __alloc_bootmem_core(struct bootmem_data *bdata, |
5f2809e6 JW |
440 | unsigned long size, unsigned long align, |
441 | unsigned long goal, unsigned long limit) | |
1da177e4 | 442 | { |
0f3caba2 | 443 | unsigned long fallback = 0; |
5f2809e6 JW |
444 | unsigned long min, max, start, sidx, midx, step; |
445 | ||
594fe1a0 JW |
446 | bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n", |
447 | bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT, | |
448 | align, goal, limit); | |
449 | ||
5f2809e6 JW |
450 | BUG_ON(!size); |
451 | BUG_ON(align & (align - 1)); | |
452 | BUG_ON(limit && goal + size > limit); | |
1da177e4 | 453 | |
7c309a64 CK |
454 | if (!bdata->node_bootmem_map) |
455 | return NULL; | |
456 | ||
3560e249 | 457 | min = bdata->node_min_pfn; |
5f2809e6 | 458 | max = bdata->node_low_pfn; |
9a2dc04c | 459 | |
5f2809e6 JW |
460 | goal >>= PAGE_SHIFT; |
461 | limit >>= PAGE_SHIFT; | |
462 | ||
463 | if (limit && max > limit) | |
464 | max = limit; | |
465 | if (max <= min) | |
9a2dc04c YL |
466 | return NULL; |
467 | ||
5f2809e6 | 468 | step = max(align >> PAGE_SHIFT, 1UL); |
281dd25c | 469 | |
5f2809e6 JW |
470 | if (goal && min < goal && goal < max) |
471 | start = ALIGN(goal, step); | |
472 | else | |
473 | start = ALIGN(min, step); | |
1da177e4 | 474 | |
481ebd0d | 475 | sidx = start - bdata->node_min_pfn; |
3560e249 | 476 | midx = max - bdata->node_min_pfn; |
1da177e4 | 477 | |
5f2809e6 | 478 | if (bdata->hint_idx > sidx) { |
0f3caba2 JW |
479 | /* |
480 | * Handle the valid case of sidx being zero and still | |
481 | * catch the fallback below. | |
482 | */ | |
483 | fallback = sidx + 1; | |
481ebd0d | 484 | sidx = align_idx(bdata, bdata->hint_idx, step); |
5f2809e6 | 485 | } |
1da177e4 | 486 | |
5f2809e6 JW |
487 | while (1) { |
488 | int merge; | |
489 | void *region; | |
490 | unsigned long eidx, i, start_off, end_off; | |
491 | find_block: | |
492 | sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx); | |
481ebd0d | 493 | sidx = align_idx(bdata, sidx, step); |
5f2809e6 | 494 | eidx = sidx + PFN_UP(size); |
ad09315c | 495 | |
5f2809e6 | 496 | if (sidx >= midx || eidx > midx) |
66d43e98 | 497 | break; |
1da177e4 | 498 | |
5f2809e6 JW |
499 | for (i = sidx; i < eidx; i++) |
500 | if (test_bit(i, bdata->node_bootmem_map)) { | |
481ebd0d | 501 | sidx = align_idx(bdata, i, step); |
5f2809e6 JW |
502 | if (sidx == i) |
503 | sidx += step; | |
504 | goto find_block; | |
505 | } | |
1da177e4 | 506 | |
627240aa | 507 | if (bdata->last_end_off & (PAGE_SIZE - 1) && |
5f2809e6 | 508 | PFN_DOWN(bdata->last_end_off) + 1 == sidx) |
481ebd0d | 509 | start_off = align_off(bdata, bdata->last_end_off, align); |
5f2809e6 JW |
510 | else |
511 | start_off = PFN_PHYS(sidx); | |
512 | ||
513 | merge = PFN_DOWN(start_off) < sidx; | |
514 | end_off = start_off + size; | |
515 | ||
516 | bdata->last_end_off = end_off; | |
517 | bdata->hint_idx = PFN_UP(end_off); | |
518 | ||
519 | /* | |
520 | * Reserve the area now: | |
521 | */ | |
d747fa4b JW |
522 | if (__reserve(bdata, PFN_DOWN(start_off) + merge, |
523 | PFN_UP(end_off), BOOTMEM_EXCLUSIVE)) | |
524 | BUG(); | |
5f2809e6 | 525 | |
3560e249 JW |
526 | region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) + |
527 | start_off); | |
5f2809e6 JW |
528 | memset(region, 0, size); |
529 | return region; | |
1da177e4 LT |
530 | } |
531 | ||
0f3caba2 | 532 | if (fallback) { |
481ebd0d | 533 | sidx = align_idx(bdata, fallback - 1, step); |
0f3caba2 JW |
534 | fallback = 0; |
535 | goto find_block; | |
536 | } | |
537 | ||
538 | return NULL; | |
539 | } | |
540 | ||
541 | static void * __init ___alloc_bootmem_nopanic(unsigned long size, | |
542 | unsigned long align, | |
543 | unsigned long goal, | |
544 | unsigned long limit) | |
545 | { | |
546 | bootmem_data_t *bdata; | |
547 | ||
548 | restart: | |
549 | list_for_each_entry(bdata, &bdata_list, list) { | |
550 | void *region; | |
551 | ||
552 | if (goal && bdata->node_low_pfn <= PFN_DOWN(goal)) | |
553 | continue; | |
3560e249 | 554 | if (limit && bdata->node_min_pfn >= PFN_DOWN(limit)) |
0f3caba2 JW |
555 | break; |
556 | ||
557 | region = alloc_bootmem_core(bdata, size, align, goal, limit); | |
558 | if (region) | |
559 | return region; | |
560 | } | |
561 | ||
5f2809e6 JW |
562 | if (goal) { |
563 | goal = 0; | |
0f3caba2 | 564 | goto restart; |
5f2809e6 | 565 | } |
2e5237da | 566 | |
5f2809e6 | 567 | return NULL; |
1da177e4 LT |
568 | } |
569 | ||
a66fd7da JW |
570 | /** |
571 | * __alloc_bootmem_nopanic - allocate boot memory without panicking | |
572 | * @size: size of the request in bytes | |
573 | * @align: alignment of the region | |
574 | * @goal: preferred starting address of the region | |
575 | * | |
576 | * The goal is dropped if it can not be satisfied and the allocation will | |
577 | * fall back to memory below @goal. | |
578 | * | |
579 | * Allocation may happen on any node in the system. | |
580 | * | |
581 | * Returns NULL on failure. | |
582 | */ | |
bb0923a6 | 583 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, |
0f3caba2 | 584 | unsigned long goal) |
1da177e4 | 585 | { |
0f3caba2 JW |
586 | return ___alloc_bootmem_nopanic(size, align, goal, 0); |
587 | } | |
1da177e4 | 588 | |
0f3caba2 JW |
589 | static void * __init ___alloc_bootmem(unsigned long size, unsigned long align, |
590 | unsigned long goal, unsigned long limit) | |
591 | { | |
592 | void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit); | |
593 | ||
594 | if (mem) | |
595 | return mem; | |
596 | /* | |
597 | * Whoops, we cannot satisfy the allocation request. | |
598 | */ | |
599 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); | |
600 | panic("Out of memory"); | |
a8062231 AK |
601 | return NULL; |
602 | } | |
1da177e4 | 603 | |
a66fd7da JW |
604 | /** |
605 | * __alloc_bootmem - allocate boot memory | |
606 | * @size: size of the request in bytes | |
607 | * @align: alignment of the region | |
608 | * @goal: preferred starting address of the region | |
609 | * | |
610 | * The goal is dropped if it can not be satisfied and the allocation will | |
611 | * fall back to memory below @goal. | |
612 | * | |
613 | * Allocation may happen on any node in the system. | |
614 | * | |
615 | * The function panics if the request can not be satisfied. | |
616 | */ | |
bb0923a6 FBH |
617 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, |
618 | unsigned long goal) | |
a8062231 | 619 | { |
0f3caba2 | 620 | return ___alloc_bootmem(size, align, goal, 0); |
1da177e4 LT |
621 | } |
622 | ||
4cc278b7 JW |
623 | static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata, |
624 | unsigned long size, unsigned long align, | |
625 | unsigned long goal, unsigned long limit) | |
626 | { | |
627 | void *ptr; | |
628 | ||
629 | ptr = alloc_bootmem_core(bdata, size, align, goal, limit); | |
630 | if (ptr) | |
631 | return ptr; | |
632 | ||
633 | return ___alloc_bootmem(size, align, goal, limit); | |
634 | } | |
635 | ||
a66fd7da JW |
636 | /** |
637 | * __alloc_bootmem_node - allocate boot memory from a specific node | |
638 | * @pgdat: node to allocate from | |
639 | * @size: size of the request in bytes | |
640 | * @align: alignment of the region | |
641 | * @goal: preferred starting address of the region | |
642 | * | |
643 | * The goal is dropped if it can not be satisfied and the allocation will | |
644 | * fall back to memory below @goal. | |
645 | * | |
646 | * Allocation may fall back to any node in the system if the specified node | |
647 | * can not hold the requested memory. | |
648 | * | |
649 | * The function panics if the request can not be satisfied. | |
650 | */ | |
bb0923a6 FBH |
651 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
652 | unsigned long align, unsigned long goal) | |
1da177e4 | 653 | { |
4cc278b7 | 654 | return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0); |
1da177e4 LT |
655 | } |
656 | ||
e70260aa | 657 | #ifdef CONFIG_SPARSEMEM |
a66fd7da JW |
658 | /** |
659 | * alloc_bootmem_section - allocate boot memory from a specific section | |
660 | * @size: size of the request in bytes | |
661 | * @section_nr: sparse map section to allocate from | |
662 | * | |
663 | * Return NULL on failure. | |
664 | */ | |
e70260aa YG |
665 | void * __init alloc_bootmem_section(unsigned long size, |
666 | unsigned long section_nr) | |
667 | { | |
75a56cfe JW |
668 | bootmem_data_t *bdata; |
669 | unsigned long pfn, goal, limit; | |
e70260aa YG |
670 | |
671 | pfn = section_nr_to_pfn(section_nr); | |
75a56cfe JW |
672 | goal = pfn << PAGE_SHIFT; |
673 | limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT; | |
674 | bdata = &bootmem_node_data[early_pfn_to_nid(pfn)]; | |
e70260aa | 675 | |
75a56cfe | 676 | return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit); |
e70260aa YG |
677 | } |
678 | #endif | |
679 | ||
b54bbf7b AK |
680 | void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size, |
681 | unsigned long align, unsigned long goal) | |
682 | { | |
683 | void *ptr; | |
684 | ||
685 | ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); | |
686 | if (ptr) | |
687 | return ptr; | |
688 | ||
689 | return __alloc_bootmem_nopanic(size, align, goal); | |
690 | } | |
691 | ||
dfd54cbc HC |
692 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
693 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL | |
694 | #endif | |
008857c1 | 695 | |
a66fd7da JW |
696 | /** |
697 | * __alloc_bootmem_low - allocate low boot memory | |
698 | * @size: size of the request in bytes | |
699 | * @align: alignment of the region | |
700 | * @goal: preferred starting address of the region | |
701 | * | |
702 | * The goal is dropped if it can not be satisfied and the allocation will | |
703 | * fall back to memory below @goal. | |
704 | * | |
705 | * Allocation may happen on any node in the system. | |
706 | * | |
707 | * The function panics if the request can not be satisfied. | |
708 | */ | |
bb0923a6 FBH |
709 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, |
710 | unsigned long goal) | |
008857c1 | 711 | { |
0f3caba2 | 712 | return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT); |
008857c1 RT |
713 | } |
714 | ||
a66fd7da JW |
715 | /** |
716 | * __alloc_bootmem_low_node - allocate low boot memory from a specific node | |
717 | * @pgdat: node to allocate from | |
718 | * @size: size of the request in bytes | |
719 | * @align: alignment of the region | |
720 | * @goal: preferred starting address of the region | |
721 | * | |
722 | * The goal is dropped if it can not be satisfied and the allocation will | |
723 | * fall back to memory below @goal. | |
724 | * | |
725 | * Allocation may fall back to any node in the system if the specified node | |
726 | * can not hold the requested memory. | |
727 | * | |
728 | * The function panics if the request can not be satisfied. | |
729 | */ | |
008857c1 RT |
730 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, |
731 | unsigned long align, unsigned long goal) | |
732 | { | |
4cc278b7 JW |
733 | return ___alloc_bootmem_node(pgdat->bdata, size, align, |
734 | goal, ARCH_LOW_ADDRESS_LIMIT); | |
008857c1 | 735 | } |