mm, show_mem: suppress page counts in non-blockable contexts
[pandora-kernel.git] / arch / unicore32 / mm / init.c
1 /*
2  *  linux/arch/unicore32/mm/init.c
3  *
4  *  Copyright (C) 2010 GUAN Xue-tao
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/swap.h>
13 #include <linux/init.h>
14 #include <linux/bootmem.h>
15 #include <linux/mman.h>
16 #include <linux/nodemask.h>
17 #include <linux/initrd.h>
18 #include <linux/highmem.h>
19 #include <linux/gfp.h>
20 #include <linux/memblock.h>
21 #include <linux/sort.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/export.h>
24
25 #include <asm/sections.h>
26 #include <asm/setup.h>
27 #include <asm/sizes.h>
28 #include <asm/tlb.h>
29 #include <mach/map.h>
30
31 #include "mm.h"
32
33 static unsigned long phys_initrd_start __initdata = 0x01000000;
34 static unsigned long phys_initrd_size __initdata = SZ_8M;
35
36 static int __init early_initrd(char *p)
37 {
38         unsigned long start, size;
39         char *endp;
40
41         start = memparse(p, &endp);
42         if (*endp == ',') {
43                 size = memparse(endp + 1, NULL);
44
45                 phys_initrd_start = start;
46                 phys_initrd_size = size;
47         }
48         return 0;
49 }
50 early_param("initrd", early_initrd);
51
52 /*
53  * This keeps memory configuration data used by a couple memory
54  * initialization functions, as well as show_mem() for the skipping
55  * of holes in the memory map.  It is populated by uc32_add_memory().
56  */
57 struct meminfo meminfo;
58
59 void show_mem(unsigned int filter)
60 {
61         int free = 0, total = 0, reserved = 0;
62         int shared = 0, cached = 0, slab = 0, i;
63         struct meminfo *mi = &meminfo;
64
65         printk(KERN_DEFAULT "Mem-info:\n");
66         show_free_areas(filter);
67
68         if (filter & SHOW_MEM_FILTER_PAGE_COUNT)
69                 return;
70
71         for_each_bank(i, mi) {
72                 struct membank *bank = &mi->bank[i];
73                 unsigned int pfn1, pfn2;
74                 struct page *page, *end;
75
76                 pfn1 = bank_pfn_start(bank);
77                 pfn2 = bank_pfn_end(bank);
78
79                 page = pfn_to_page(pfn1);
80                 end  = pfn_to_page(pfn2 - 1) + 1;
81
82                 do {
83                         total++;
84                         if (PageReserved(page))
85                                 reserved++;
86                         else if (PageSwapCache(page))
87                                 cached++;
88                         else if (PageSlab(page))
89                                 slab++;
90                         else if (!page_count(page))
91                                 free++;
92                         else
93                                 shared += page_count(page) - 1;
94                         page++;
95                 } while (page < end);
96         }
97
98         printk(KERN_DEFAULT "%d pages of RAM\n", total);
99         printk(KERN_DEFAULT "%d free pages\n", free);
100         printk(KERN_DEFAULT "%d reserved pages\n", reserved);
101         printk(KERN_DEFAULT "%d slab pages\n", slab);
102         printk(KERN_DEFAULT "%d pages shared\n", shared);
103         printk(KERN_DEFAULT "%d pages swap cached\n", cached);
104 }
105
106 static void __init find_limits(unsigned long *min, unsigned long *max_low,
107         unsigned long *max_high)
108 {
109         struct meminfo *mi = &meminfo;
110         int i;
111
112         *min = -1UL;
113         *max_low = *max_high = 0;
114
115         for_each_bank(i, mi) {
116                 struct membank *bank = &mi->bank[i];
117                 unsigned long start, end;
118
119                 start = bank_pfn_start(bank);
120                 end = bank_pfn_end(bank);
121
122                 if (*min > start)
123                         *min = start;
124                 if (*max_high < end)
125                         *max_high = end;
126                 if (bank->highmem)
127                         continue;
128                 if (*max_low < end)
129                         *max_low = end;
130         }
131 }
132
133 static void __init uc32_bootmem_init(unsigned long start_pfn,
134         unsigned long end_pfn)
135 {
136         struct memblock_region *reg;
137         unsigned int boot_pages;
138         phys_addr_t bitmap;
139         pg_data_t *pgdat;
140
141         /*
142          * Allocate the bootmem bitmap page.  This must be in a region
143          * of memory which has already been mapped.
144          */
145         boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
146         bitmap = memblock_alloc_base(boot_pages << PAGE_SHIFT, L1_CACHE_BYTES,
147                                 __pfn_to_phys(end_pfn));
148
149         /*
150          * Initialise the bootmem allocator, handing the
151          * memory banks over to bootmem.
152          */
153         node_set_online(0);
154         pgdat = NODE_DATA(0);
155         init_bootmem_node(pgdat, __phys_to_pfn(bitmap), start_pfn, end_pfn);
156
157         /* Free the lowmem regions from memblock into bootmem. */
158         for_each_memblock(memory, reg) {
159                 unsigned long start = memblock_region_memory_base_pfn(reg);
160                 unsigned long end = memblock_region_memory_end_pfn(reg);
161
162                 if (end >= end_pfn)
163                         end = end_pfn;
164                 if (start >= end)
165                         break;
166
167                 free_bootmem(__pfn_to_phys(start), (end - start) << PAGE_SHIFT);
168         }
169
170         /* Reserve the lowmem memblock reserved regions in bootmem. */
171         for_each_memblock(reserved, reg) {
172                 unsigned long start = memblock_region_reserved_base_pfn(reg);
173                 unsigned long end = memblock_region_reserved_end_pfn(reg);
174
175                 if (end >= end_pfn)
176                         end = end_pfn;
177                 if (start >= end)
178                         break;
179
180                 reserve_bootmem(__pfn_to_phys(start),
181                         (end - start) << PAGE_SHIFT, BOOTMEM_DEFAULT);
182         }
183 }
184
185 static void __init uc32_bootmem_free(unsigned long min, unsigned long max_low,
186         unsigned long max_high)
187 {
188         unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
189         struct memblock_region *reg;
190
191         /*
192          * initialise the zones.
193          */
194         memset(zone_size, 0, sizeof(zone_size));
195
196         /*
197          * The memory size has already been determined.  If we need
198          * to do anything fancy with the allocation of this memory
199          * to the zones, now is the time to do it.
200          */
201         zone_size[0] = max_low - min;
202
203         /*
204          * Calculate the size of the holes.
205          *  holes = node_size - sum(bank_sizes)
206          */
207         memcpy(zhole_size, zone_size, sizeof(zhole_size));
208         for_each_memblock(memory, reg) {
209                 unsigned long start = memblock_region_memory_base_pfn(reg);
210                 unsigned long end = memblock_region_memory_end_pfn(reg);
211
212                 if (start < max_low) {
213                         unsigned long low_end = min(end, max_low);
214                         zhole_size[0] -= low_end - start;
215                 }
216         }
217
218         /*
219          * Adjust the sizes according to any special requirements for
220          * this machine type.
221          */
222         arch_adjust_zones(zone_size, zhole_size);
223
224         free_area_init_node(0, zone_size, min, zhole_size);
225 }
226
227 int pfn_valid(unsigned long pfn)
228 {
229         return memblock_is_memory(pfn << PAGE_SHIFT);
230 }
231 EXPORT_SYMBOL(pfn_valid);
232
233 static void uc32_memory_present(void)
234 {
235 }
236
237 static int __init meminfo_cmp(const void *_a, const void *_b)
238 {
239         const struct membank *a = _a, *b = _b;
240         long cmp = bank_pfn_start(a) - bank_pfn_start(b);
241         return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
242 }
243
244 void __init uc32_memblock_init(struct meminfo *mi)
245 {
246         int i;
247
248         sort(&meminfo.bank, meminfo.nr_banks, sizeof(meminfo.bank[0]),
249                 meminfo_cmp, NULL);
250
251         memblock_init();
252         for (i = 0; i < mi->nr_banks; i++)
253                 memblock_add(mi->bank[i].start, mi->bank[i].size);
254
255         /* Register the kernel text, kernel data and initrd with memblock. */
256         memblock_reserve(__pa(_text), _end - _text);
257
258 #ifdef CONFIG_BLK_DEV_INITRD
259         if (phys_initrd_size) {
260                 memblock_reserve(phys_initrd_start, phys_initrd_size);
261
262                 /* Now convert initrd to virtual addresses */
263                 initrd_start = __phys_to_virt(phys_initrd_start);
264                 initrd_end = initrd_start + phys_initrd_size;
265         }
266 #endif
267
268         uc32_mm_memblock_reserve();
269
270         memblock_analyze();
271         memblock_dump_all();
272 }
273
274 void __init bootmem_init(void)
275 {
276         unsigned long min, max_low, max_high;
277
278         max_low = max_high = 0;
279
280         find_limits(&min, &max_low, &max_high);
281
282         uc32_bootmem_init(min, max_low);
283
284 #ifdef CONFIG_SWIOTLB
285         swiotlb_init(1);
286 #endif
287         /*
288          * Sparsemem tries to allocate bootmem in memory_present(),
289          * so must be done after the fixed reservations
290          */
291         uc32_memory_present();
292
293         /*
294          * sparse_init() needs the bootmem allocator up and running.
295          */
296         sparse_init();
297
298         /*
299          * Now free the memory - free_area_init_node needs
300          * the sparse mem_map arrays initialized by sparse_init()
301          * for memmap_init_zone(), otherwise all PFNs are invalid.
302          */
303         uc32_bootmem_free(min, max_low, max_high);
304
305         high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
306
307         /*
308          * This doesn't seem to be used by the Linux memory manager any
309          * more, but is used by ll_rw_block.  If we can get rid of it, we
310          * also get rid of some of the stuff above as well.
311          *
312          * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
313          * the system, not the maximum PFN.
314          */
315         max_low_pfn = max_low - PHYS_PFN_OFFSET;
316         max_pfn = max_high - PHYS_PFN_OFFSET;
317 }
318
319 static inline int free_area(unsigned long pfn, unsigned long end, char *s)
320 {
321         unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
322
323         for (; pfn < end; pfn++) {
324                 struct page *page = pfn_to_page(pfn);
325                 ClearPageReserved(page);
326                 init_page_count(page);
327                 __free_page(page);
328                 pages++;
329         }
330
331         if (size && s)
332                 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
333
334         return pages;
335 }
336
337 static inline void
338 free_memmap(unsigned long start_pfn, unsigned long end_pfn)
339 {
340         struct page *start_pg, *end_pg;
341         unsigned long pg, pgend;
342
343         /*
344          * Convert start_pfn/end_pfn to a struct page pointer.
345          */
346         start_pg = pfn_to_page(start_pfn - 1) + 1;
347         end_pg = pfn_to_page(end_pfn);
348
349         /*
350          * Convert to physical addresses, and
351          * round start upwards and end downwards.
352          */
353         pg = PAGE_ALIGN(__pa(start_pg));
354         pgend = __pa(end_pg) & PAGE_MASK;
355
356         /*
357          * If there are free pages between these,
358          * free the section of the memmap array.
359          */
360         if (pg < pgend)
361                 free_bootmem(pg, pgend - pg);
362 }
363
364 /*
365  * The mem_map array can get very big.  Free the unused area of the memory map.
366  */
367 static void __init free_unused_memmap(struct meminfo *mi)
368 {
369         unsigned long bank_start, prev_bank_end = 0;
370         unsigned int i;
371
372         /*
373          * This relies on each bank being in address order.
374          * The banks are sorted previously in bootmem_init().
375          */
376         for_each_bank(i, mi) {
377                 struct membank *bank = &mi->bank[i];
378
379                 bank_start = bank_pfn_start(bank);
380
381                 /*
382                  * If we had a previous bank, and there is a space
383                  * between the current bank and the previous, free it.
384                  */
385                 if (prev_bank_end && prev_bank_end < bank_start)
386                         free_memmap(prev_bank_end, bank_start);
387
388                 /*
389                  * Align up here since the VM subsystem insists that the
390                  * memmap entries are valid from the bank end aligned to
391                  * MAX_ORDER_NR_PAGES.
392                  */
393                 prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES);
394         }
395 }
396
397 /*
398  * mem_init() marks the free areas in the mem_map and tells us how much
399  * memory is free.  This is done after various parts of the system have
400  * claimed their memory after the kernel image.
401  */
402 void __init mem_init(void)
403 {
404         unsigned long reserved_pages, free_pages;
405         struct memblock_region *reg;
406         int i;
407
408         max_mapnr   = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
409
410         /* this will put all unused low memory onto the freelists */
411         free_unused_memmap(&meminfo);
412
413         totalram_pages += free_all_bootmem();
414
415         reserved_pages = free_pages = 0;
416
417         for_each_bank(i, &meminfo) {
418                 struct membank *bank = &meminfo.bank[i];
419                 unsigned int pfn1, pfn2;
420                 struct page *page, *end;
421
422                 pfn1 = bank_pfn_start(bank);
423                 pfn2 = bank_pfn_end(bank);
424
425                 page = pfn_to_page(pfn1);
426                 end  = pfn_to_page(pfn2 - 1) + 1;
427
428                 do {
429                         if (PageReserved(page))
430                                 reserved_pages++;
431                         else if (!page_count(page))
432                                 free_pages++;
433                         page++;
434                 } while (page < end);
435         }
436
437         /*
438          * Since our memory may not be contiguous, calculate the
439          * real number of pages we have in this system
440          */
441         printk(KERN_INFO "Memory:");
442         num_physpages = 0;
443         for_each_memblock(memory, reg) {
444                 unsigned long pages = memblock_region_memory_end_pfn(reg) -
445                         memblock_region_memory_base_pfn(reg);
446                 num_physpages += pages;
447                 printk(" %ldMB", pages >> (20 - PAGE_SHIFT));
448         }
449         printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
450
451         printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n",
452                 nr_free_pages() << (PAGE_SHIFT-10),
453                 free_pages << (PAGE_SHIFT-10),
454                 reserved_pages << (PAGE_SHIFT-10),
455                 totalhigh_pages << (PAGE_SHIFT-10));
456
457         printk(KERN_NOTICE "Virtual kernel memory layout:\n"
458                 "    vector  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
459                 "    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
460                 "    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
461                 "    modules : 0x%08lx - 0x%08lx   (%4ld MB)\n"
462                 "      .init : 0x%p" " - 0x%p" "   (%4d kB)\n"
463                 "      .text : 0x%p" " - 0x%p" "   (%4d kB)\n"
464                 "      .data : 0x%p" " - 0x%p" "   (%4d kB)\n",
465
466                 VECTORS_BASE, VECTORS_BASE + PAGE_SIZE,
467                 DIV_ROUND_UP(PAGE_SIZE, SZ_1K),
468                 VMALLOC_START, VMALLOC_END,
469                 DIV_ROUND_UP((VMALLOC_END - VMALLOC_START), SZ_1M),
470                 PAGE_OFFSET, (unsigned long)high_memory,
471                 DIV_ROUND_UP(((unsigned long)high_memory - PAGE_OFFSET), SZ_1M),
472                 MODULES_VADDR, MODULES_END,
473                 DIV_ROUND_UP((MODULES_END - MODULES_VADDR), SZ_1M),
474
475                 __init_begin, __init_end,
476                 DIV_ROUND_UP((__init_end - __init_begin), SZ_1K),
477                 _stext, _etext,
478                 DIV_ROUND_UP((_etext - _stext), SZ_1K),
479                 _sdata, _edata,
480                 DIV_ROUND_UP((_edata - _sdata), SZ_1K));
481
482         BUILD_BUG_ON(TASK_SIZE                          > MODULES_VADDR);
483         BUG_ON(TASK_SIZE                                > MODULES_VADDR);
484
485         if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
486                 /*
487                  * On a machine this small we won't get
488                  * anywhere without overcommit, so turn
489                  * it on by default.
490                  */
491                 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
492         }
493 }
494
495 void free_initmem(void)
496 {
497         totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
498                                     __phys_to_pfn(__pa(__init_end)),
499                                     "init");
500 }
501
502 #ifdef CONFIG_BLK_DEV_INITRD
503
504 static int keep_initrd;
505
506 void free_initrd_mem(unsigned long start, unsigned long end)
507 {
508         if (!keep_initrd)
509                 totalram_pages += free_area(__phys_to_pfn(__pa(start)),
510                                             __phys_to_pfn(__pa(end)),
511                                             "initrd");
512 }
513
514 static int __init keepinitrd_setup(char *__unused)
515 {
516         keep_initrd = 1;
517         return 1;
518 }
519
520 __setup("keepinitrd", keepinitrd_setup);
521 #endif