Merge branch 'intelfb-patches' of master.kernel.org:/pub/scm/linux/kernel/git/airlied...
[pandora-kernel.git] / arch / arm / mm / init.c
1 /*
2  *  linux/arch/arm/mm/init.c
3  *
4  *  Copyright (C) 1995-2005 Russell King
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/ptrace.h>
13 #include <linux/swap.h>
14 #include <linux/init.h>
15 #include <linux/bootmem.h>
16 #include <linux/mman.h>
17 #include <linux/nodemask.h>
18 #include <linux/initrd.h>
19
20 #include <asm/mach-types.h>
21 #include <asm/setup.h>
22 #include <asm/sizes.h>
23 #include <asm/tlb.h>
24
25 #include <asm/mach/arch.h>
26 #include <asm/mach/map.h>
27
28 #include "mm.h"
29
30 extern void _text, _etext, __data_start, _end, __init_begin, __init_end;
31 extern unsigned long phys_initrd_start;
32 extern unsigned long phys_initrd_size;
33
34 /*
35  * The sole use of this is to pass memory configuration
36  * data from paging_init to mem_init.
37  */
38 static struct meminfo meminfo __initdata = { 0, };
39
40 void show_mem(void)
41 {
42         int free = 0, total = 0, reserved = 0;
43         int shared = 0, cached = 0, slab = 0, node;
44
45         printk("Mem-info:\n");
46         show_free_areas();
47         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
48
49         for_each_online_node(node) {
50                 struct page *page, *end;
51
52                 page = NODE_MEM_MAP(node);
53                 end  = page + NODE_DATA(node)->node_spanned_pages;
54
55                 do {
56                         total++;
57                         if (PageReserved(page))
58                                 reserved++;
59                         else if (PageSwapCache(page))
60                                 cached++;
61                         else if (PageSlab(page))
62                                 slab++;
63                         else if (!page_count(page))
64                                 free++;
65                         else
66                                 shared += page_count(page) - 1;
67                         page++;
68                 } while (page < end);
69         }
70
71         printk("%d pages of RAM\n", total);
72         printk("%d free pages\n", free);
73         printk("%d reserved pages\n", reserved);
74         printk("%d slab pages\n", slab);
75         printk("%d pages shared\n", shared);
76         printk("%d pages swap cached\n", cached);
77 }
78
79 #define for_each_nodebank(iter,mi,no)                   \
80         for (iter = 0; iter < mi->nr_banks; iter++)     \
81                 if (mi->bank[iter].node == no)
82
83 /*
84  * FIXME: We really want to avoid allocating the bootmap bitmap
85  * over the top of the initrd.  Hopefully, this is located towards
86  * the start of a bank, so if we allocate the bootmap bitmap at
87  * the end, we won't clash.
88  */
89 static unsigned int __init
90 find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
91 {
92         unsigned int start_pfn, bank, bootmap_pfn;
93
94         start_pfn   = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT;
95         bootmap_pfn = 0;
96
97         for_each_nodebank(bank, mi, node) {
98                 unsigned int start, end;
99
100                 start = mi->bank[bank].start >> PAGE_SHIFT;
101                 end   = (mi->bank[bank].size +
102                          mi->bank[bank].start) >> PAGE_SHIFT;
103
104                 if (end < start_pfn)
105                         continue;
106
107                 if (start < start_pfn)
108                         start = start_pfn;
109
110                 if (end <= start)
111                         continue;
112
113                 if (end - start >= bootmap_pages) {
114                         bootmap_pfn = start;
115                         break;
116                 }
117         }
118
119         if (bootmap_pfn == 0)
120                 BUG();
121
122         return bootmap_pfn;
123 }
124
125 static int __init check_initrd(struct meminfo *mi)
126 {
127         int initrd_node = -2;
128 #ifdef CONFIG_BLK_DEV_INITRD
129         unsigned long end = phys_initrd_start + phys_initrd_size;
130
131         /*
132          * Make sure that the initrd is within a valid area of
133          * memory.
134          */
135         if (phys_initrd_size) {
136                 unsigned int i;
137
138                 initrd_node = -1;
139
140                 for (i = 0; i < mi->nr_banks; i++) {
141                         unsigned long bank_end;
142
143                         bank_end = mi->bank[i].start + mi->bank[i].size;
144
145                         if (mi->bank[i].start <= phys_initrd_start &&
146                             end <= bank_end)
147                                 initrd_node = mi->bank[i].node;
148                 }
149         }
150
151         if (initrd_node == -1) {
152                 printk(KERN_ERR "initrd (0x%08lx - 0x%08lx) extends beyond "
153                        "physical memory - disabling initrd\n",
154                        phys_initrd_start, end);
155                 phys_initrd_start = phys_initrd_size = 0;
156         }
157 #endif
158
159         return initrd_node;
160 }
161
162 static inline void map_memory_bank(struct membank *bank)
163 {
164 #ifdef CONFIG_MMU
165         struct map_desc map;
166
167         map.pfn = __phys_to_pfn(bank->start);
168         map.virtual = __phys_to_virt(bank->start);
169         map.length = bank->size;
170         map.type = MT_MEMORY;
171
172         create_mapping(&map);
173 #endif
174 }
175
176 static unsigned long __init
177 bootmem_init_node(int node, int initrd_node, struct meminfo *mi)
178 {
179         unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
180         unsigned long start_pfn, end_pfn, boot_pfn;
181         unsigned int boot_pages;
182         pg_data_t *pgdat;
183         int i;
184
185         start_pfn = -1UL;
186         end_pfn = 0;
187
188         /*
189          * Calculate the pfn range, and map the memory banks for this node.
190          */
191         for_each_nodebank(i, mi, node) {
192                 struct membank *bank = &mi->bank[i];
193                 unsigned long start, end;
194
195                 start = bank->start >> PAGE_SHIFT;
196                 end = (bank->start + bank->size) >> PAGE_SHIFT;
197
198                 if (start_pfn > start)
199                         start_pfn = start;
200                 if (end_pfn < end)
201                         end_pfn = end;
202
203                 map_memory_bank(bank);
204         }
205
206         /*
207          * If there is no memory in this node, ignore it.
208          */
209         if (end_pfn == 0)
210                 return end_pfn;
211
212         /*
213          * Allocate the bootmem bitmap page.
214          */
215         boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
216         boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
217
218         /*
219          * Initialise the bootmem allocator for this node, handing the
220          * memory banks over to bootmem.
221          */
222         node_set_online(node);
223         pgdat = NODE_DATA(node);
224         init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
225
226         for_each_nodebank(i, mi, node)
227                 free_bootmem_node(pgdat, mi->bank[i].start, mi->bank[i].size);
228
229         /*
230          * Reserve the bootmem bitmap for this node.
231          */
232         reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
233                              boot_pages << PAGE_SHIFT);
234
235 #ifdef CONFIG_BLK_DEV_INITRD
236         /*
237          * If the initrd is in this node, reserve its memory.
238          */
239         if (node == initrd_node) {
240                 reserve_bootmem_node(pgdat, phys_initrd_start,
241                                      phys_initrd_size);
242                 initrd_start = __phys_to_virt(phys_initrd_start);
243                 initrd_end = initrd_start + phys_initrd_size;
244         }
245 #endif
246
247         /*
248          * Finally, reserve any node zero regions.
249          */
250         if (node == 0)
251                 reserve_node_zero(pgdat);
252
253         /*
254          * initialise the zones within this node.
255          */
256         memset(zone_size, 0, sizeof(zone_size));
257         memset(zhole_size, 0, sizeof(zhole_size));
258
259         /*
260          * The size of this node has already been determined.  If we need
261          * to do anything fancy with the allocation of this memory to the
262          * zones, now is the time to do it.
263          */
264         zone_size[0] = end_pfn - start_pfn;
265
266         /*
267          * For each bank in this node, calculate the size of the holes.
268          *  holes = node_size - sum(bank_sizes_in_node)
269          */
270         zhole_size[0] = zone_size[0];
271         for_each_nodebank(i, mi, node)
272                 zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
273
274         /*
275          * Adjust the sizes according to any special requirements for
276          * this machine type.
277          */
278         arch_adjust_zones(node, zone_size, zhole_size);
279
280         free_area_init_node(node, pgdat, zone_size, start_pfn, zhole_size);
281
282         return end_pfn;
283 }
284
285 void __init bootmem_init(struct meminfo *mi)
286 {
287         unsigned long memend_pfn = 0;
288         int node, initrd_node, i;
289
290         /*
291          * Invalidate the node number for empty or invalid memory banks
292          */
293         for (i = 0; i < mi->nr_banks; i++)
294                 if (mi->bank[i].size == 0 || mi->bank[i].node >= MAX_NUMNODES)
295                         mi->bank[i].node = -1;
296
297         memcpy(&meminfo, mi, sizeof(meminfo));
298
299         /*
300          * Locate which node contains the ramdisk image, if any.
301          */
302         initrd_node = check_initrd(mi);
303
304         /*
305          * Run through each node initialising the bootmem allocator.
306          */
307         for_each_node(node) {
308                 unsigned long end_pfn;
309
310                 end_pfn = bootmem_init_node(node, initrd_node, mi);
311
312                 /*
313                  * Remember the highest memory PFN.
314                  */
315                 if (end_pfn > memend_pfn)
316                         memend_pfn = end_pfn;
317         }
318
319         high_memory = __va(memend_pfn << PAGE_SHIFT);
320
321         /*
322          * This doesn't seem to be used by the Linux memory manager any
323          * more, but is used by ll_rw_block.  If we can get rid of it, we
324          * also get rid of some of the stuff above as well.
325          *
326          * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
327          * the system, not the maximum PFN.
328          */
329         max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
330 }
331
332 static inline void free_area(unsigned long addr, unsigned long end, char *s)
333 {
334         unsigned int size = (end - addr) >> 10;
335
336         for (; addr < end; addr += PAGE_SIZE) {
337                 struct page *page = virt_to_page(addr);
338                 ClearPageReserved(page);
339                 init_page_count(page);
340                 free_page(addr);
341                 totalram_pages++;
342         }
343
344         if (size && s)
345                 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
346 }
347
348 static inline void
349 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
350 {
351         struct page *start_pg, *end_pg;
352         unsigned long pg, pgend;
353
354         /*
355          * Convert start_pfn/end_pfn to a struct page pointer.
356          */
357         start_pg = pfn_to_page(start_pfn);
358         end_pg = pfn_to_page(end_pfn);
359
360         /*
361          * Convert to physical addresses, and
362          * round start upwards and end downwards.
363          */
364         pg = PAGE_ALIGN(__pa(start_pg));
365         pgend = __pa(end_pg) & PAGE_MASK;
366
367         /*
368          * If there are free pages between these,
369          * free the section of the memmap array.
370          */
371         if (pg < pgend)
372                 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
373 }
374
375 /*
376  * The mem_map array can get very big.  Free the unused area of the memory map.
377  */
378 static void __init free_unused_memmap_node(int node, struct meminfo *mi)
379 {
380         unsigned long bank_start, prev_bank_end = 0;
381         unsigned int i;
382
383         /*
384          * [FIXME] This relies on each bank being in address order.  This
385          * may not be the case, especially if the user has provided the
386          * information on the command line.
387          */
388         for_each_nodebank(i, mi, node) {
389                 bank_start = mi->bank[i].start >> PAGE_SHIFT;
390                 if (bank_start < prev_bank_end) {
391                         printk(KERN_ERR "MEM: unordered memory banks.  "
392                                 "Not freeing memmap.\n");
393                         break;
394                 }
395
396                 /*
397                  * If we had a previous bank, and there is a space
398                  * between the current bank and the previous, free it.
399                  */
400                 if (prev_bank_end && prev_bank_end != bank_start)
401                         free_memmap(node, prev_bank_end, bank_start);
402
403                 prev_bank_end = (mi->bank[i].start +
404                                  mi->bank[i].size) >> PAGE_SHIFT;
405         }
406 }
407
408 /*
409  * mem_init() marks the free areas in the mem_map and tells us how much
410  * memory is free.  This is done after various parts of the system have
411  * claimed their memory after the kernel image.
412  */
413 void __init mem_init(void)
414 {
415         unsigned int codepages, datapages, initpages;
416         int i, node;
417
418         codepages = &_etext - &_text;
419         datapages = &_end - &__data_start;
420         initpages = &__init_end - &__init_begin;
421
422 #ifndef CONFIG_DISCONTIGMEM
423         max_mapnr   = virt_to_page(high_memory) - mem_map;
424 #endif
425
426         /* this will put all unused low memory onto the freelists */
427         for_each_online_node(node) {
428                 pg_data_t *pgdat = NODE_DATA(node);
429
430                 free_unused_memmap_node(node, &meminfo);
431
432                 if (pgdat->node_spanned_pages != 0)
433                         totalram_pages += free_all_bootmem_node(pgdat);
434         }
435
436 #ifdef CONFIG_SA1111
437         /* now that our DMA memory is actually so designated, we can free it */
438         free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
439 #endif
440
441         /*
442          * Since our memory may not be contiguous, calculate the
443          * real number of pages we have in this system
444          */
445         printk(KERN_INFO "Memory:");
446
447         num_physpages = 0;
448         for (i = 0; i < meminfo.nr_banks; i++) {
449                 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
450                 printk(" %ldMB", meminfo.bank[i].size >> 20);
451         }
452
453         printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
454         printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
455                 "%dK data, %dK init)\n",
456                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
457                 codepages >> 10, datapages >> 10, initpages >> 10);
458
459         if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
460                 extern int sysctl_overcommit_memory;
461                 /*
462                  * On a machine this small we won't get
463                  * anywhere without overcommit, so turn
464                  * it on by default.
465                  */
466                 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
467         }
468 }
469
470 void free_initmem(void)
471 {
472         if (!machine_is_integrator() && !machine_is_cintegrator()) {
473                 free_area((unsigned long)(&__init_begin),
474                           (unsigned long)(&__init_end),
475                           "init");
476         }
477 }
478
479 #ifdef CONFIG_BLK_DEV_INITRD
480
481 static int keep_initrd;
482
483 void free_initrd_mem(unsigned long start, unsigned long end)
484 {
485         if (!keep_initrd)
486                 free_area(start, end, "initrd");
487 }
488
489 static int __init keepinitrd_setup(char *__unused)
490 {
491         keep_initrd = 1;
492         return 1;
493 }
494
495 __setup("keepinitrd", keepinitrd_setup);
496 #endif