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