]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - mm/bootmem.c
bootmem: clean up arch-specific bootmem wrapping
[linux-2.6-omap-h63xx.git] / mm / bootmem.c
1 /*
2  *  bootmem - A boot-time physical memory allocator and configurator
3  *
4  *  Copyright (C) 1999 Ingo Molnar
5  *                1999 Kanoj Sarcar, SGI
6  *                2008 Johannes Weiner
7  *
8  * Access to this subsystem has to be serialized externally (which is true
9  * for the boot process anyway).
10  */
11 #include <linux/init.h>
12 #include <linux/pfn.h>
13 #include <linux/bootmem.h>
14 #include <linux/module.h>
15
16 #include <asm/bug.h>
17 #include <asm/io.h>
18 #include <asm/processor.h>
19
20 #include "internal.h"
21
22 unsigned long max_low_pfn;
23 unsigned long min_low_pfn;
24 unsigned long max_pfn;
25
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
34 bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
35
36 static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
37
38 static int bootmem_debug;
39
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
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,             \
61                         __func__, ## args);             \
62 })
63
64 static unsigned long __init bootmap_bytes(unsigned long pages)
65 {
66         unsigned long bytes = (pages + 7) / 8;
67
68         return ALIGN(bytes, sizeof(long));
69 }
70
71 /**
72  * bootmem_bootmap_pages - calculate bitmap size in pages
73  * @pages: number of pages the bitmap has to represent
74  */
75 unsigned long __init bootmem_bootmap_pages(unsigned long pages)
76 {
77         unsigned long bytes = bootmap_bytes(pages);
78
79         return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
80 }
81
82 /*
83  * link bdata in order
84  */
85 static void __init link_bootmem(bootmem_data_t *bdata)
86 {
87         struct list_head *iter;
88
89         list_for_each(iter, &bdata_list) {
90                 bootmem_data_t *ent;
91
92                 ent = list_entry(iter, bootmem_data_t, list);
93                 if (bdata->node_min_pfn < ent->node_min_pfn)
94                         break;
95         }
96         list_add_tail(&bdata->list, iter);
97 }
98
99 /*
100  * Called once to set up the allocator itself.
101  */
102 static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
103         unsigned long mapstart, unsigned long start, unsigned long end)
104 {
105         unsigned long mapsize;
106
107         mminit_validate_memmodel_limits(&start, &end);
108         bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
109         bdata->node_min_pfn = start;
110         bdata->node_low_pfn = end;
111         link_bootmem(bdata);
112
113         /*
114          * Initially all pages are reserved - setup_arch() has to
115          * register free RAM areas explicitly.
116          */
117         mapsize = bootmap_bytes(end - start);
118         memset(bdata->node_bootmem_map, 0xff, mapsize);
119
120         bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
121                 bdata - bootmem_node_data, start, mapstart, end, mapsize);
122
123         return mapsize;
124 }
125
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  */
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
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  */
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 {
157         int aligned;
158         struct page *page;
159         unsigned long start, end, pages, count = 0;
160
161         if (!bdata->node_bootmem_map)
162                 return 0;
163
164         start = bdata->node_min_pfn;
165         end = bdata->node_low_pfn;
166
167         /*
168          * If the start is aligned to the machines wordsize, we might
169          * be able to free pages in bulks of that order.
170          */
171         aligned = !(start & (BITS_PER_LONG - 1));
172
173         bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
174                 bdata - bootmem_node_data, start, end, aligned);
175
176         while (start < end) {
177                 unsigned long *map, idx, vec;
178
179                 map = bdata->node_bootmem_map;
180                 idx = start - bdata->node_min_pfn;
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);
187                         count += BITS_PER_LONG;
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);
194                                         __free_pages_bootmem(page, 0);
195                                         count++;
196                                 }
197                                 vec >>= 1;
198                                 off++;
199                         }
200                 }
201                 start += BITS_PER_LONG;
202         }
203
204         page = virt_to_page(bdata->node_bootmem_map);
205         pages = bdata->node_low_pfn - bdata->node_min_pfn;
206         pages = bootmem_bootmap_pages(pages);
207         count += pages;
208         while (pages--)
209                 __free_pages_bootmem(page++, 0);
210
211         bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
212
213         return count;
214 }
215
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  */
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
228 /**
229  * free_all_bootmem - release free pages to the buddy allocator
230  *
231  * Returns the number of pages actually released.
232  */
233 unsigned long __init free_all_bootmem(void)
234 {
235         return free_all_bootmem_core(NODE_DATA(0)->bdata);
236 }
237
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,
244                 sidx + bdata->node_min_pfn,
245                 eidx + bdata->node_min_pfn);
246
247         if (bdata->hint_idx > sidx)
248                 bdata->hint_idx = sidx;
249
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,
263                 sidx + bdata->node_min_pfn,
264                 eidx + bdata->node_min_pfn,
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",
274                                 idx + bdata->node_min_pfn);
275                 }
276         return 0;
277 }
278
279 static int __init mark_bootmem_node(bootmem_data_t *bdata,
280                                 unsigned long start, unsigned long end,
281                                 int reserve, int flags)
282 {
283         unsigned long sidx, eidx;
284
285         bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
286                 bdata - bootmem_node_data, start, end, reserve, flags);
287
288         BUG_ON(start < bdata->node_min_pfn);
289         BUG_ON(end > bdata->node_low_pfn);
290
291         sidx = start - bdata->node_min_pfn;
292         eidx = end - bdata->node_min_pfn;
293
294         if (reserve)
295                 return __reserve(bdata, sidx, eidx, flags);
296         else
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
312                 if (pos < bdata->node_min_pfn ||
313                     pos >= bdata->node_low_pfn) {
314                         BUG_ON(pos != start);
315                         continue;
316                 }
317
318                 max = min(bdata->node_low_pfn, end);
319
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                 }
325
326                 if (max == end)
327                         return 0;
328                 pos = bdata->node_low_pfn;
329         }
330         BUG();
331 }
332
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  *
341  * The range must reside completely on the specified node.
342  */
343 void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
344                               unsigned long size)
345 {
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);
352 }
353
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  *
361  * The range must be contiguous but may span node boundaries.
362  */
363 void __init free_bootmem(unsigned long addr, unsigned long size)
364 {
365         unsigned long start, end;
366
367         start = PFN_UP(addr);
368         end = PFN_DOWN(addr + size);
369
370         mark_bootmem(start, end, 0, 0);
371 }
372
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  *
382  * The range must reside completely on the specified node.
383  */
384 int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
385                                  unsigned long size, int flags)
386 {
387         unsigned long start, end;
388
389         start = PFN_DOWN(physaddr);
390         end = PFN_UP(physaddr + size);
391
392         return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
393 }
394
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  *
403  * The range must be contiguous but may span node boundaries.
404  */
405 int __init reserve_bootmem(unsigned long addr, unsigned long size,
406                             int flags)
407 {
408         unsigned long start, end;
409
410         start = PFN_DOWN(addr);
411         end = PFN_UP(addr + size);
412
413         return mark_bootmem(start, end, 1, flags);
414 }
415
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
439 static void * __init __alloc_bootmem_core(struct bootmem_data *bdata,
440                                 unsigned long size, unsigned long align,
441                                 unsigned long goal, unsigned long limit)
442 {
443         unsigned long fallback = 0;
444         unsigned long min, max, start, sidx, midx, step;
445
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
450         BUG_ON(!size);
451         BUG_ON(align & (align - 1));
452         BUG_ON(limit && goal + size > limit);
453
454         if (!bdata->node_bootmem_map)
455                 return NULL;
456
457         min = bdata->node_min_pfn;
458         max = bdata->node_low_pfn;
459
460         goal >>= PAGE_SHIFT;
461         limit >>= PAGE_SHIFT;
462
463         if (limit && max > limit)
464                 max = limit;
465         if (max <= min)
466                 return NULL;
467
468         step = max(align >> PAGE_SHIFT, 1UL);
469
470         if (goal && min < goal && goal < max)
471                 start = ALIGN(goal, step);
472         else
473                 start = ALIGN(min, step);
474
475         sidx = start - bdata->node_min_pfn;
476         midx = max - bdata->node_min_pfn;
477
478         if (bdata->hint_idx > sidx) {
479                 /*
480                  * Handle the valid case of sidx being zero and still
481                  * catch the fallback below.
482                  */
483                 fallback = sidx + 1;
484                 sidx = align_idx(bdata, bdata->hint_idx, step);
485         }
486
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);
493                 sidx = align_idx(bdata, sidx, step);
494                 eidx = sidx + PFN_UP(size);
495
496                 if (sidx >= midx || eidx > midx)
497                         break;
498
499                 for (i = sidx; i < eidx; i++)
500                         if (test_bit(i, bdata->node_bootmem_map)) {
501                                 sidx = align_idx(bdata, i, step);
502                                 if (sidx == i)
503                                         sidx += step;
504                                 goto find_block;
505                         }
506
507                 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
508                                 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
509                         start_off = align_off(bdata, bdata->last_end_off, align);
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                  */
522                 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
523                                 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
524                         BUG();
525
526                 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
527                                 start_off);
528                 memset(region, 0, size);
529                 return region;
530         }
531
532         if (fallback) {
533                 sidx = align_idx(bdata, fallback - 1, step);
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;
554                 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
555                         break;
556
557                 region = alloc_bootmem_core(bdata, size, align, goal, limit);
558                 if (region)
559                         return region;
560         }
561
562         if (goal) {
563                 goal = 0;
564                 goto restart;
565         }
566
567         return NULL;
568 }
569
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  */
583 void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
584                                         unsigned long goal)
585 {
586         return ___alloc_bootmem_nopanic(size, align, goal, 0);
587 }
588
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");
601         return NULL;
602 }
603
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  */
617 void * __init __alloc_bootmem(unsigned long size, unsigned long align,
618                               unsigned long goal)
619 {
620         return ___alloc_bootmem(size, align, goal, 0);
621 }
622
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
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  */
651 void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
652                                    unsigned long align, unsigned long goal)
653 {
654         return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
655 }
656
657 #ifdef CONFIG_SPARSEMEM
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  */
665 void * __init alloc_bootmem_section(unsigned long size,
666                                     unsigned long section_nr)
667 {
668         bootmem_data_t *bdata;
669         unsigned long pfn, goal, limit;
670
671         pfn = section_nr_to_pfn(section_nr);
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)];
675
676         return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
677 }
678 #endif
679
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
692 #ifndef ARCH_LOW_ADDRESS_LIMIT
693 #define ARCH_LOW_ADDRESS_LIMIT  0xffffffffUL
694 #endif
695
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  */
709 void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
710                                   unsigned long goal)
711 {
712         return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
713 }
714
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  */
730 void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
731                                        unsigned long align, unsigned long goal)
732 {
733         return ___alloc_bootmem_node(pgdat->bdata, size, align,
734                                 goal, ARCH_LOW_ADDRESS_LIMIT);
735 }