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 | ||
b61bfa3c JW |
39 | bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata; |
40 | ||
1da177e4 | 41 | /* return the number of _pages_ that will be allocated for the boot bitmap */ |
f71bf0ca | 42 | unsigned long __init bootmem_bootmap_pages(unsigned long pages) |
1da177e4 LT |
43 | { |
44 | unsigned long mapsize; | |
45 | ||
46 | mapsize = (pages+7)/8; | |
47 | mapsize = (mapsize + ~PAGE_MASK) & PAGE_MASK; | |
48 | mapsize >>= PAGE_SHIFT; | |
49 | ||
50 | return mapsize; | |
51 | } | |
f71bf0ca | 52 | |
679bc9fb KH |
53 | /* |
54 | * link bdata in order | |
55 | */ | |
69d49e68 | 56 | static void __init link_bootmem(bootmem_data_t *bdata) |
679bc9fb KH |
57 | { |
58 | bootmem_data_t *ent; | |
f71bf0ca | 59 | |
679bc9fb KH |
60 | if (list_empty(&bdata_list)) { |
61 | list_add(&bdata->list, &bdata_list); | |
62 | return; | |
63 | } | |
64 | /* insert in order */ | |
65 | list_for_each_entry(ent, &bdata_list, list) { | |
66 | if (bdata->node_boot_start < ent->node_boot_start) { | |
67 | list_add_tail(&bdata->list, &ent->list); | |
68 | return; | |
69 | } | |
70 | } | |
71 | list_add_tail(&bdata->list, &bdata_list); | |
679bc9fb KH |
72 | } |
73 | ||
bbc7b92e FBH |
74 | /* |
75 | * Given an initialised bdata, it returns the size of the boot bitmap | |
76 | */ | |
77 | static unsigned long __init get_mapsize(bootmem_data_t *bdata) | |
78 | { | |
79 | unsigned long mapsize; | |
80 | unsigned long start = PFN_DOWN(bdata->node_boot_start); | |
81 | unsigned long end = bdata->node_low_pfn; | |
82 | ||
83 | mapsize = ((end - start) + 7) / 8; | |
84 | return ALIGN(mapsize, sizeof(long)); | |
85 | } | |
1da177e4 LT |
86 | |
87 | /* | |
88 | * Called once to set up the allocator itself. | |
89 | */ | |
f71bf0ca | 90 | static unsigned long __init init_bootmem_core(pg_data_t *pgdat, |
1da177e4 LT |
91 | unsigned long mapstart, unsigned long start, unsigned long end) |
92 | { | |
93 | bootmem_data_t *bdata = pgdat->bdata; | |
bbc7b92e | 94 | unsigned long mapsize; |
1da177e4 | 95 | |
2dbb51c4 | 96 | mminit_validate_memmodel_limits(&start, &end); |
bbc7b92e FBH |
97 | bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart)); |
98 | bdata->node_boot_start = PFN_PHYS(start); | |
1da177e4 | 99 | bdata->node_low_pfn = end; |
679bc9fb | 100 | link_bootmem(bdata); |
1da177e4 LT |
101 | |
102 | /* | |
103 | * Initially all pages are reserved - setup_arch() has to | |
104 | * register free RAM areas explicitly. | |
105 | */ | |
bbc7b92e | 106 | mapsize = get_mapsize(bdata); |
1da177e4 LT |
107 | memset(bdata->node_bootmem_map, 0xff, mapsize); |
108 | ||
109 | return mapsize; | |
110 | } | |
111 | ||
112 | /* | |
113 | * Marks a particular physical memory range as unallocatable. Usable RAM | |
114 | * might be used for boot-time allocations - or it might get added | |
115 | * to the free page pool later on. | |
116 | */ | |
a5645a61 | 117 | static int __init can_reserve_bootmem_core(bootmem_data_t *bdata, |
72a7fe39 | 118 | unsigned long addr, unsigned long size, int flags) |
1da177e4 | 119 | { |
bbc7b92e | 120 | unsigned long sidx, eidx; |
1da177e4 | 121 | unsigned long i; |
a5645a61 YL |
122 | |
123 | BUG_ON(!size); | |
124 | ||
125 | /* out of range, don't hold other */ | |
126 | if (addr + size < bdata->node_boot_start || | |
127 | PFN_DOWN(addr) > bdata->node_low_pfn) | |
128 | return 0; | |
bbc7b92e | 129 | |
1da177e4 | 130 | /* |
a5645a61 | 131 | * Round up to index to the range. |
1da177e4 | 132 | */ |
a5645a61 YL |
133 | if (addr > bdata->node_boot_start) |
134 | sidx= PFN_DOWN(addr - bdata->node_boot_start); | |
135 | else | |
136 | sidx = 0; | |
137 | ||
138 | eidx = PFN_UP(addr + size - bdata->node_boot_start); | |
139 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) | |
140 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); | |
141 | ||
142 | for (i = sidx; i < eidx; i++) { | |
143 | if (test_bit(i, bdata->node_bootmem_map)) { | |
144 | if (flags & BOOTMEM_EXCLUSIVE) | |
145 | return -EBUSY; | |
146 | } | |
147 | } | |
148 | ||
149 | return 0; | |
150 | ||
151 | } | |
152 | ||
153 | static void __init reserve_bootmem_core(bootmem_data_t *bdata, | |
154 | unsigned long addr, unsigned long size, int flags) | |
155 | { | |
156 | unsigned long sidx, eidx; | |
157 | unsigned long i; | |
158 | ||
1da177e4 | 159 | BUG_ON(!size); |
bbc7b92e | 160 | |
a5645a61 YL |
161 | /* out of range */ |
162 | if (addr + size < bdata->node_boot_start || | |
163 | PFN_DOWN(addr) > bdata->node_low_pfn) | |
164 | return; | |
165 | ||
166 | /* | |
167 | * Round up to index to the range. | |
168 | */ | |
169 | if (addr > bdata->node_boot_start) | |
170 | sidx= PFN_DOWN(addr - bdata->node_boot_start); | |
171 | else | |
172 | sidx = 0; | |
173 | ||
bbc7b92e | 174 | eidx = PFN_UP(addr + size - bdata->node_boot_start); |
a5645a61 YL |
175 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) |
176 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); | |
1da177e4 | 177 | |
a5645a61 | 178 | for (i = sidx; i < eidx; i++) { |
1da177e4 LT |
179 | if (test_and_set_bit(i, bdata->node_bootmem_map)) { |
180 | #ifdef CONFIG_DEBUG_BOOTMEM | |
181 | printk("hm, page %08lx reserved twice.\n", i*PAGE_SIZE); | |
182 | #endif | |
183 | } | |
a5645a61 | 184 | } |
1da177e4 LT |
185 | } |
186 | ||
bb0923a6 FBH |
187 | static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, |
188 | unsigned long size) | |
1da177e4 | 189 | { |
bbc7b92e | 190 | unsigned long sidx, eidx; |
1da177e4 | 191 | unsigned long i; |
bbc7b92e | 192 | |
5a982cbc YL |
193 | BUG_ON(!size); |
194 | ||
195 | /* out range */ | |
196 | if (addr + size < bdata->node_boot_start || | |
197 | PFN_DOWN(addr) > bdata->node_low_pfn) | |
198 | return; | |
1da177e4 LT |
199 | /* |
200 | * round down end of usable mem, partially free pages are | |
201 | * considered reserved. | |
202 | */ | |
1da177e4 | 203 | |
5a982cbc | 204 | if (addr >= bdata->node_boot_start && addr < bdata->last_success) |
1da177e4 LT |
205 | bdata->last_success = addr; |
206 | ||
207 | /* | |
5a982cbc | 208 | * Round up to index to the range. |
1da177e4 | 209 | */ |
5a982cbc YL |
210 | if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start)) |
211 | sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start); | |
212 | else | |
213 | sidx = 0; | |
214 | ||
bbc7b92e | 215 | eidx = PFN_DOWN(addr + size - bdata->node_boot_start); |
5a982cbc YL |
216 | if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start)) |
217 | eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start); | |
1da177e4 LT |
218 | |
219 | for (i = sidx; i < eidx; i++) { | |
220 | if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map))) | |
221 | BUG(); | |
222 | } | |
223 | } | |
224 | ||
225 | /* | |
226 | * We 'merge' subsequent allocations to save space. We might 'lose' | |
227 | * some fraction of a page if allocations cannot be satisfied due to | |
228 | * size constraints on boxes where there is physical RAM space | |
229 | * fragmentation - in these cases (mostly large memory boxes) this | |
230 | * is not a problem. | |
231 | * | |
232 | * On low memory boxes we get it right in 100% of the cases. | |
233 | * | |
234 | * alignment has to be a power of 2 value. | |
235 | * | |
236 | * NOTE: This function is _not_ reentrant. | |
237 | */ | |
267b4801 | 238 | void * __init |
1da177e4 | 239 | __alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size, |
281dd25c | 240 | unsigned long align, unsigned long goal, unsigned long limit) |
1da177e4 | 241 | { |
9a2dc04c | 242 | unsigned long areasize, preferred; |
bbc7b92e | 243 | unsigned long i, start = 0, incr, eidx, end_pfn; |
1da177e4 | 244 | void *ret; |
9a2dc04c YL |
245 | unsigned long node_boot_start; |
246 | void *node_bootmem_map; | |
1da177e4 | 247 | |
f71bf0ca | 248 | if (!size) { |
1da177e4 LT |
249 | printk("__alloc_bootmem_core(): zero-sized request\n"); |
250 | BUG(); | |
251 | } | |
252 | BUG_ON(align & (align-1)); | |
253 | ||
7c309a64 CK |
254 | /* on nodes without memory - bootmem_map is NULL */ |
255 | if (!bdata->node_bootmem_map) | |
256 | return NULL; | |
257 | ||
9a2dc04c YL |
258 | /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */ |
259 | node_boot_start = bdata->node_boot_start; | |
260 | node_bootmem_map = bdata->node_bootmem_map; | |
261 | if (align) { | |
262 | node_boot_start = ALIGN(bdata->node_boot_start, align); | |
263 | if (node_boot_start > bdata->node_boot_start) | |
264 | node_bootmem_map = (unsigned long *)bdata->node_bootmem_map + | |
265 | PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG; | |
266 | } | |
267 | ||
268 | if (limit && node_boot_start >= limit) | |
269 | return NULL; | |
270 | ||
bbc7b92e FBH |
271 | end_pfn = bdata->node_low_pfn; |
272 | limit = PFN_DOWN(limit); | |
281dd25c YG |
273 | if (limit && end_pfn > limit) |
274 | end_pfn = limit; | |
275 | ||
9a2dc04c | 276 | eidx = end_pfn - PFN_DOWN(node_boot_start); |
1da177e4 LT |
277 | |
278 | /* | |
279 | * We try to allocate bootmem pages above 'goal' | |
280 | * first, then we try to allocate lower pages. | |
281 | */ | |
ad09315c YL |
282 | preferred = 0; |
283 | if (goal && PFN_DOWN(goal) < end_pfn) { | |
9a2dc04c YL |
284 | if (goal > node_boot_start) |
285 | preferred = goal - node_boot_start; | |
1da177e4 | 286 | |
9a2dc04c YL |
287 | if (bdata->last_success > node_boot_start && |
288 | bdata->last_success - node_boot_start >= preferred) | |
281dd25c | 289 | if (!limit || (limit && limit > bdata->last_success)) |
9a2dc04c | 290 | preferred = bdata->last_success - node_boot_start; |
ad09315c | 291 | } |
1da177e4 | 292 | |
9a2dc04c | 293 | preferred = PFN_DOWN(ALIGN(preferred, align)); |
bbc7b92e | 294 | areasize = (size + PAGE_SIZE-1) / PAGE_SIZE; |
1da177e4 LT |
295 | incr = align >> PAGE_SHIFT ? : 1; |
296 | ||
297 | restart_scan: | |
ad09315c | 298 | for (i = preferred; i < eidx;) { |
1da177e4 | 299 | unsigned long j; |
ad09315c | 300 | |
9a2dc04c | 301 | i = find_next_zero_bit(node_bootmem_map, eidx, i); |
1da177e4 | 302 | i = ALIGN(i, incr); |
66d43e98 HM |
303 | if (i >= eidx) |
304 | break; | |
9a2dc04c | 305 | if (test_bit(i, node_bootmem_map)) { |
ad09315c | 306 | i += incr; |
1da177e4 | 307 | continue; |
ad09315c | 308 | } |
1da177e4 LT |
309 | for (j = i + 1; j < i + areasize; ++j) { |
310 | if (j >= eidx) | |
311 | goto fail_block; | |
9a2dc04c | 312 | if (test_bit(j, node_bootmem_map)) |
1da177e4 LT |
313 | goto fail_block; |
314 | } | |
315 | start = i; | |
316 | goto found; | |
317 | fail_block: | |
318 | i = ALIGN(j, incr); | |
ad09315c YL |
319 | if (i == j) |
320 | i += incr; | |
1da177e4 LT |
321 | } |
322 | ||
9a2dc04c YL |
323 | if (preferred > 0) { |
324 | preferred = 0; | |
1da177e4 LT |
325 | goto restart_scan; |
326 | } | |
327 | return NULL; | |
328 | ||
329 | found: | |
9a2dc04c | 330 | bdata->last_success = PFN_PHYS(start) + node_boot_start; |
1da177e4 LT |
331 | BUG_ON(start >= eidx); |
332 | ||
333 | /* | |
334 | * Is the next page of the previous allocation-end the start | |
335 | * of this allocation's buffer? If yes then we can 'merge' | |
336 | * the previous partial page with this allocation. | |
337 | */ | |
338 | if (align < PAGE_SIZE && | |
339 | bdata->last_offset && bdata->last_pos+1 == start) { | |
9a2dc04c | 340 | unsigned long offset, remaining_size; |
8c0e33c1 | 341 | offset = ALIGN(bdata->last_offset, align); |
1da177e4 | 342 | BUG_ON(offset > PAGE_SIZE); |
f71bf0ca | 343 | remaining_size = PAGE_SIZE - offset; |
1da177e4 LT |
344 | if (size < remaining_size) { |
345 | areasize = 0; | |
346 | /* last_pos unchanged */ | |
f71bf0ca FBH |
347 | bdata->last_offset = offset + size; |
348 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + | |
9a2dc04c | 349 | offset + node_boot_start); |
1da177e4 LT |
350 | } else { |
351 | remaining_size = size - remaining_size; | |
f71bf0ca FBH |
352 | areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE; |
353 | ret = phys_to_virt(bdata->last_pos * PAGE_SIZE + | |
9a2dc04c | 354 | offset + node_boot_start); |
f71bf0ca | 355 | bdata->last_pos = start + areasize - 1; |
1da177e4 LT |
356 | bdata->last_offset = remaining_size; |
357 | } | |
358 | bdata->last_offset &= ~PAGE_MASK; | |
359 | } else { | |
360 | bdata->last_pos = start + areasize - 1; | |
361 | bdata->last_offset = size & ~PAGE_MASK; | |
9a2dc04c | 362 | ret = phys_to_virt(start * PAGE_SIZE + node_boot_start); |
1da177e4 LT |
363 | } |
364 | ||
365 | /* | |
366 | * Reserve the area now: | |
367 | */ | |
f71bf0ca | 368 | for (i = start; i < start + areasize; i++) |
9a2dc04c | 369 | if (unlikely(test_and_set_bit(i, node_bootmem_map))) |
1da177e4 LT |
370 | BUG(); |
371 | memset(ret, 0, size); | |
372 | return ret; | |
373 | } | |
374 | ||
375 | static unsigned long __init free_all_bootmem_core(pg_data_t *pgdat) | |
376 | { | |
377 | struct page *page; | |
d41dee36 | 378 | unsigned long pfn; |
1da177e4 | 379 | bootmem_data_t *bdata = pgdat->bdata; |
6b312c0e | 380 | unsigned long i, count; |
1da177e4 LT |
381 | unsigned long idx; |
382 | unsigned long *map; | |
383 | int gofast = 0; | |
384 | ||
385 | BUG_ON(!bdata->node_bootmem_map); | |
386 | ||
387 | count = 0; | |
388 | /* first extant page of the node */ | |
bbc7b92e FBH |
389 | pfn = PFN_DOWN(bdata->node_boot_start); |
390 | idx = bdata->node_low_pfn - pfn; | |
1da177e4 | 391 | map = bdata->node_bootmem_map; |
6b312c0e JW |
392 | /* |
393 | * Check if we are aligned to BITS_PER_LONG pages. If so, we might | |
394 | * be able to free page orders of that size at once. | |
395 | */ | |
396 | if (!(pfn & (BITS_PER_LONG-1))) | |
1da177e4 | 397 | gofast = 1; |
6b312c0e | 398 | |
1da177e4 LT |
399 | for (i = 0; i < idx; ) { |
400 | unsigned long v = ~map[i / BITS_PER_LONG]; | |
d41dee36 | 401 | |
1da177e4 | 402 | if (gofast && v == ~0UL) { |
a226f6c8 | 403 | int order; |
1da177e4 | 404 | |
d41dee36 | 405 | page = pfn_to_page(pfn); |
1da177e4 | 406 | count += BITS_PER_LONG; |
1da177e4 | 407 | order = ffs(BITS_PER_LONG) - 1; |
a226f6c8 | 408 | __free_pages_bootmem(page, order); |
1da177e4 LT |
409 | i += BITS_PER_LONG; |
410 | page += BITS_PER_LONG; | |
411 | } else if (v) { | |
412 | unsigned long m; | |
d41dee36 AW |
413 | |
414 | page = pfn_to_page(pfn); | |
1da177e4 LT |
415 | for (m = 1; m && i < idx; m<<=1, page++, i++) { |
416 | if (v & m) { | |
417 | count++; | |
a226f6c8 | 418 | __free_pages_bootmem(page, 0); |
1da177e4 LT |
419 | } |
420 | } | |
421 | } else { | |
f71bf0ca | 422 | i += BITS_PER_LONG; |
1da177e4 | 423 | } |
d41dee36 | 424 | pfn += BITS_PER_LONG; |
1da177e4 | 425 | } |
1da177e4 LT |
426 | |
427 | /* | |
428 | * Now free the allocator bitmap itself, it's not | |
429 | * needed anymore: | |
430 | */ | |
431 | page = virt_to_page(bdata->node_bootmem_map); | |
bbc7b92e | 432 | idx = (get_mapsize(bdata) + PAGE_SIZE-1) >> PAGE_SHIFT; |
6b312c0e | 433 | for (i = 0; i < idx; i++, page++) |
a226f6c8 | 434 | __free_pages_bootmem(page, 0); |
6b312c0e | 435 | count += i; |
1da177e4 LT |
436 | bdata->node_bootmem_map = NULL; |
437 | ||
6b312c0e | 438 | return count; |
1da177e4 LT |
439 | } |
440 | ||
f71bf0ca | 441 | unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn, |
bb0923a6 | 442 | unsigned long startpfn, unsigned long endpfn) |
1da177e4 | 443 | { |
f71bf0ca | 444 | return init_bootmem_core(pgdat, freepfn, startpfn, endpfn); |
1da177e4 LT |
445 | } |
446 | ||
71c2742f | 447 | int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
72a7fe39 | 448 | unsigned long size, int flags) |
1da177e4 | 449 | { |
a5645a61 YL |
450 | int ret; |
451 | ||
452 | ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags); | |
453 | if (ret < 0) | |
71c2742f | 454 | return -ENOMEM; |
72a7fe39 | 455 | reserve_bootmem_core(pgdat->bdata, physaddr, size, flags); |
71c2742f BW |
456 | |
457 | return 0; | |
1da177e4 LT |
458 | } |
459 | ||
f71bf0ca FBH |
460 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
461 | unsigned long size) | |
1da177e4 LT |
462 | { |
463 | free_bootmem_core(pgdat->bdata, physaddr, size); | |
464 | } | |
465 | ||
f71bf0ca | 466 | unsigned long __init free_all_bootmem_node(pg_data_t *pgdat) |
1da177e4 | 467 | { |
04753278 | 468 | register_page_bootmem_info_node(pgdat); |
f71bf0ca | 469 | return free_all_bootmem_core(pgdat); |
1da177e4 LT |
470 | } |
471 | ||
f71bf0ca | 472 | unsigned long __init init_bootmem(unsigned long start, unsigned long pages) |
1da177e4 LT |
473 | { |
474 | max_low_pfn = pages; | |
475 | min_low_pfn = start; | |
f71bf0ca | 476 | return init_bootmem_core(NODE_DATA(0), start, 0, pages); |
1da177e4 LT |
477 | } |
478 | ||
479 | #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE | |
72a7fe39 BW |
480 | int __init reserve_bootmem(unsigned long addr, unsigned long size, |
481 | int flags) | |
1da177e4 | 482 | { |
a5645a61 YL |
483 | bootmem_data_t *bdata; |
484 | int ret; | |
485 | ||
486 | list_for_each_entry(bdata, &bdata_list, list) { | |
487 | ret = can_reserve_bootmem_core(bdata, addr, size, flags); | |
488 | if (ret < 0) | |
489 | return ret; | |
490 | } | |
491 | list_for_each_entry(bdata, &bdata_list, list) | |
492 | reserve_bootmem_core(bdata, addr, size, flags); | |
493 | ||
494 | return 0; | |
1da177e4 LT |
495 | } |
496 | #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */ | |
497 | ||
f71bf0ca | 498 | void __init free_bootmem(unsigned long addr, unsigned long size) |
1da177e4 | 499 | { |
5a982cbc YL |
500 | bootmem_data_t *bdata; |
501 | list_for_each_entry(bdata, &bdata_list, list) | |
502 | free_bootmem_core(bdata, addr, size); | |
1da177e4 LT |
503 | } |
504 | ||
f71bf0ca | 505 | unsigned long __init free_all_bootmem(void) |
1da177e4 | 506 | { |
f71bf0ca | 507 | return free_all_bootmem_core(NODE_DATA(0)); |
1da177e4 LT |
508 | } |
509 | ||
bb0923a6 FBH |
510 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, |
511 | unsigned long goal) | |
1da177e4 | 512 | { |
679bc9fb | 513 | bootmem_data_t *bdata; |
1da177e4 LT |
514 | void *ptr; |
515 | ||
f71bf0ca FBH |
516 | list_for_each_entry(bdata, &bdata_list, list) { |
517 | ptr = __alloc_bootmem_core(bdata, size, align, goal, 0); | |
518 | if (ptr) | |
519 | return ptr; | |
520 | } | |
a8062231 AK |
521 | return NULL; |
522 | } | |
1da177e4 | 523 | |
bb0923a6 FBH |
524 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, |
525 | unsigned long goal) | |
a8062231 AK |
526 | { |
527 | void *mem = __alloc_bootmem_nopanic(size,align,goal); | |
f71bf0ca | 528 | |
a8062231 AK |
529 | if (mem) |
530 | return mem; | |
1da177e4 LT |
531 | /* |
532 | * Whoops, we cannot satisfy the allocation request. | |
533 | */ | |
534 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); | |
535 | panic("Out of memory"); | |
536 | return NULL; | |
537 | } | |
538 | ||
281dd25c | 539 | |
bb0923a6 FBH |
540 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
541 | unsigned long align, unsigned long goal) | |
1da177e4 LT |
542 | { |
543 | void *ptr; | |
544 | ||
008857c1 | 545 | ptr = __alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); |
1da177e4 | 546 | if (ptr) |
f71bf0ca | 547 | return ptr; |
1da177e4 | 548 | |
008857c1 | 549 | return __alloc_bootmem(size, align, goal); |
1da177e4 LT |
550 | } |
551 | ||
e70260aa YG |
552 | #ifdef CONFIG_SPARSEMEM |
553 | void * __init alloc_bootmem_section(unsigned long size, | |
554 | unsigned long section_nr) | |
555 | { | |
556 | void *ptr; | |
557 | unsigned long limit, goal, start_nr, end_nr, pfn; | |
558 | struct pglist_data *pgdat; | |
559 | ||
560 | pfn = section_nr_to_pfn(section_nr); | |
561 | goal = PFN_PHYS(pfn); | |
562 | limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1; | |
563 | pgdat = NODE_DATA(early_pfn_to_nid(pfn)); | |
564 | ptr = __alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal, | |
565 | limit); | |
566 | ||
567 | if (!ptr) | |
568 | return NULL; | |
569 | ||
570 | start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr))); | |
571 | end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size)); | |
572 | if (start_nr != section_nr || end_nr != section_nr) { | |
573 | printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n", | |
574 | section_nr); | |
575 | free_bootmem_core(pgdat->bdata, __pa(ptr), size); | |
576 | ptr = NULL; | |
577 | } | |
578 | ||
579 | return ptr; | |
580 | } | |
581 | #endif | |
582 | ||
dfd54cbc HC |
583 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
584 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL | |
585 | #endif | |
008857c1 | 586 | |
bb0923a6 FBH |
587 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, |
588 | unsigned long goal) | |
008857c1 | 589 | { |
679bc9fb | 590 | bootmem_data_t *bdata; |
008857c1 RT |
591 | void *ptr; |
592 | ||
f71bf0ca | 593 | list_for_each_entry(bdata, &bdata_list, list) { |
dfd54cbc HC |
594 | ptr = __alloc_bootmem_core(bdata, size, align, goal, |
595 | ARCH_LOW_ADDRESS_LIMIT); | |
f71bf0ca FBH |
596 | if (ptr) |
597 | return ptr; | |
598 | } | |
008857c1 RT |
599 | |
600 | /* | |
601 | * Whoops, we cannot satisfy the allocation request. | |
602 | */ | |
603 | printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size); | |
604 | panic("Out of low memory"); | |
605 | return NULL; | |
606 | } | |
607 | ||
608 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, | |
609 | unsigned long align, unsigned long goal) | |
610 | { | |
dfd54cbc HC |
611 | return __alloc_bootmem_core(pgdat->bdata, size, align, goal, |
612 | ARCH_LOW_ADDRESS_LIMIT); | |
008857c1 | 613 | } |