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