[PATCH] remove set_page_count(page, 0) users (outside mm)
[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 */
10#include <linux/config.h>
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/ptrace.h>
14#include <linux/swap.h>
15#include <linux/init.h>
16#include <linux/bootmem.h>
17#include <linux/mman.h>
18#include <linux/nodemask.h>
19#include <linux/initrd.h>
20
21#include <asm/mach-types.h>
22#include <asm/hardware.h>
23#include <asm/setup.h>
24#include <asm/tlb.h>
25
26#include <asm/mach/arch.h>
27#include <asm/mach/map.h>
28
29#define TABLE_SIZE (2 * PTRS_PER_PTE * sizeof(pte_t))
30
31DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
32
33extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
34extern void _stext, _text, _etext, __data_start, _end, __init_begin, __init_end;
35extern unsigned long phys_initrd_start;
36extern unsigned long phys_initrd_size;
37
38/*
39 * The sole use of this is to pass memory configuration
40 * data from paging_init to mem_init.
41 */
42static struct meminfo meminfo __initdata = { 0, };
43
44/*
45 * empty_zero_page is a special page that is used for
46 * zero-initialized data and COW.
47 */
48struct page *empty_zero_page;
49
50void show_mem(void)
51{
52 int free = 0, total = 0, reserved = 0;
53 int shared = 0, cached = 0, slab = 0, node;
54
55 printk("Mem-info:\n");
56 show_free_areas();
57 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
58
59 for_each_online_node(node) {
60 struct page *page, *end;
61
62 page = NODE_MEM_MAP(node);
63 end = page + NODE_DATA(node)->node_spanned_pages;
64
65 do {
66 total++;
67 if (PageReserved(page))
68 reserved++;
69 else if (PageSwapCache(page))
70 cached++;
71 else if (PageSlab(page))
72 slab++;
73 else if (!page_count(page))
74 free++;
75 else
76 shared += page_count(page) - 1;
77 page++;
78 } while (page < end);
79 }
80
81 printk("%d pages of RAM\n", total);
82 printk("%d free pages\n", free);
83 printk("%d reserved pages\n", reserved);
84 printk("%d slab pages\n", slab);
85 printk("%d pages shared\n", shared);
86 printk("%d pages swap cached\n", cached);
87}
88
90072059
RK
89static inline pmd_t *pmd_off(pgd_t *pgd, unsigned long virt)
90{
91 return pmd_offset(pgd, virt);
92}
93
94static inline pmd_t *pmd_off_k(unsigned long virt)
95{
96 return pmd_off(pgd_offset_k(virt), virt);
97}
1da177e4 98
90072059
RK
99#define for_each_nodebank(iter,mi,no) \
100 for (iter = 0; iter < mi->nr_banks; iter++) \
101 if (mi->bank[iter].node == no)
1da177e4
LT
102
103/*
104 * FIXME: We really want to avoid allocating the bootmap bitmap
105 * over the top of the initrd. Hopefully, this is located towards
106 * the start of a bank, so if we allocate the bootmap bitmap at
107 * the end, we won't clash.
108 */
109static unsigned int __init
110find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
111{
112 unsigned int start_pfn, bank, bootmap_pfn;
113
90072059 114 start_pfn = PAGE_ALIGN(__pa(&_end)) >> PAGE_SHIFT;
1da177e4
LT
115 bootmap_pfn = 0;
116
90072059 117 for_each_nodebank(bank, mi, node) {
1da177e4
LT
118 unsigned int start, end;
119
92a8cbed
RK
120 start = mi->bank[bank].start >> PAGE_SHIFT;
121 end = (mi->bank[bank].size +
122 mi->bank[bank].start) >> PAGE_SHIFT;
1da177e4
LT
123
124 if (end < start_pfn)
125 continue;
126
127 if (start < start_pfn)
128 start = start_pfn;
129
130 if (end <= start)
131 continue;
132
133 if (end - start >= bootmap_pages) {
134 bootmap_pfn = start;
135 break;
136 }
137 }
138
139 if (bootmap_pfn == 0)
140 BUG();
141
142 return bootmap_pfn;
143}
144
1da177e4
LT
145static int __init check_initrd(struct meminfo *mi)
146{
147 int initrd_node = -2;
148#ifdef CONFIG_BLK_DEV_INITRD
149 unsigned long end = phys_initrd_start + phys_initrd_size;
150
151 /*
152 * Make sure that the initrd is within a valid area of
153 * memory.
154 */
155 if (phys_initrd_size) {
156 unsigned int i;
157
158 initrd_node = -1;
159
160 for (i = 0; i < mi->nr_banks; i++) {
161 unsigned long bank_end;
162
163 bank_end = mi->bank[i].start + mi->bank[i].size;
164
165 if (mi->bank[i].start <= phys_initrd_start &&
166 end <= bank_end)
167 initrd_node = mi->bank[i].node;
168 }
169 }
170
171 if (initrd_node == -1) {
172 printk(KERN_ERR "initrd (0x%08lx - 0x%08lx) extends beyond "
173 "physical memory - disabling initrd\n",
174 phys_initrd_start, end);
175 phys_initrd_start = phys_initrd_size = 0;
176 }
177#endif
178
179 return initrd_node;
180}
181
182/*
183 * Reserve the various regions of node 0
184 */
90072059 185static __init void reserve_node_zero(pg_data_t *pgdat)
1da177e4 186{
1da177e4
LT
187 unsigned long res_size = 0;
188
189 /*
190 * Register the kernel text and data with bootmem.
191 * Note that this can only be in node 0.
192 */
193#ifdef CONFIG_XIP_KERNEL
194 reserve_bootmem_node(pgdat, __pa(&__data_start), &_end - &__data_start);
195#else
196 reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
197#endif
198
199 /*
200 * Reserve the page tables. These are already in use,
201 * and can only be in node 0.
202 */
203 reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
204 PTRS_PER_PGD * sizeof(pgd_t));
205
1da177e4
LT
206 /*
207 * Hmm... This should go elsewhere, but we really really need to
208 * stop things allocating the low memory; ideally we need a better
209 * implementation of GFP_DMA which does not assume that DMA-able
210 * memory starts at zero.
211 */
212 if (machine_is_integrator() || machine_is_cintegrator())
213 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
214
215 /*
216 * These should likewise go elsewhere. They pre-reserve the
217 * screen memory region at the start of main system memory.
218 */
219 if (machine_is_edb7211())
220 res_size = 0x00020000;
221 if (machine_is_p720t())
222 res_size = 0x00014000;
223
224#ifdef CONFIG_SA1111
225 /*
226 * Because of the SA1111 DMA bug, we want to preserve our
227 * precious DMA-able memory...
228 */
229 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
230#endif
231 if (res_size)
232 reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size);
233}
234
90072059
RK
235void __init build_mem_type_table(void);
236void __init create_mapping(struct map_desc *md);
237
238static unsigned long __init
239bootmem_init_node(int node, int initrd_node, struct meminfo *mi)
1da177e4 240{
90072059
RK
241 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
242 unsigned long start_pfn, end_pfn, boot_pfn;
243 unsigned int boot_pages;
244 pg_data_t *pgdat;
245 int i;
1da177e4 246
90072059
RK
247 start_pfn = -1UL;
248 end_pfn = 0;
1da177e4 249
90072059
RK
250 /*
251 * Calculate the pfn range, and map the memory banks for this node.
252 */
253 for_each_nodebank(i, mi, node) {
254 unsigned long start, end;
255 struct map_desc map;
1da177e4 256
90072059
RK
257 start = mi->bank[i].start >> PAGE_SHIFT;
258 end = (mi->bank[i].start + mi->bank[i].size) >> PAGE_SHIFT;
1da177e4 259
90072059
RK
260 if (start_pfn > start)
261 start_pfn = start;
262 if (end_pfn < end)
263 end_pfn = end;
264
9769c246
DS
265 map.pfn = __phys_to_pfn(mi->bank[i].start);
266 map.virtual = __phys_to_virt(mi->bank[i].start);
90072059
RK
267 map.length = mi->bank[i].size;
268 map.type = MT_MEMORY;
269
270 create_mapping(&map);
271 }
1da177e4
LT
272
273 /*
90072059 274 * If there is no memory in this node, ignore it.
1da177e4 275 */
90072059
RK
276 if (end_pfn == 0)
277 return end_pfn;
1da177e4 278
90072059
RK
279 /*
280 * Allocate the bootmem bitmap page.
281 */
282 boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
283 boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
1da177e4 284
90072059
RK
285 /*
286 * Initialise the bootmem allocator for this node, handing the
287 * memory banks over to bootmem.
288 */
289 node_set_online(node);
290 pgdat = NODE_DATA(node);
291 init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
1da177e4 292
90072059
RK
293 for_each_nodebank(i, mi, node)
294 free_bootmem_node(pgdat, mi->bank[i].start, mi->bank[i].size);
295
296 /*
297 * Reserve the bootmem bitmap for this node.
298 */
299 reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
300 boot_pages << PAGE_SHIFT);
1da177e4
LT
301
302#ifdef CONFIG_BLK_DEV_INITRD
90072059
RK
303 /*
304 * If the initrd is in this node, reserve its memory.
305 */
306 if (node == initrd_node) {
307 reserve_bootmem_node(pgdat, phys_initrd_start,
1da177e4
LT
308 phys_initrd_size);
309 initrd_start = __phys_to_virt(phys_initrd_start);
310 initrd_end = initrd_start + phys_initrd_size;
311 }
312#endif
313
90072059
RK
314 /*
315 * Finally, reserve any node zero regions.
316 */
317 if (node == 0)
318 reserve_node_zero(pgdat);
319
320 /*
321 * initialise the zones within this node.
322 */
323 memset(zone_size, 0, sizeof(zone_size));
324 memset(zhole_size, 0, sizeof(zhole_size));
325
326 /*
327 * The size of this node has already been determined. If we need
328 * to do anything fancy with the allocation of this memory to the
329 * zones, now is the time to do it.
330 */
331 zone_size[0] = end_pfn - start_pfn;
332
333 /*
334 * For each bank in this node, calculate the size of the holes.
335 * holes = node_size - sum(bank_sizes_in_node)
336 */
337 zhole_size[0] = zone_size[0];
338 for_each_nodebank(i, mi, node)
339 zhole_size[0] -= mi->bank[i].size >> PAGE_SHIFT;
340
341 /*
342 * Adjust the sizes according to any special requirements for
343 * this machine type.
344 */
345 arch_adjust_zones(node, zone_size, zhole_size);
346
347 free_area_init_node(node, pgdat, zone_size, start_pfn, zhole_size);
348
349 return end_pfn;
1da177e4
LT
350}
351
90072059 352static void __init bootmem_init(struct meminfo *mi)
1da177e4 353{
90072059
RK
354 unsigned long addr, memend_pfn = 0;
355 int node, initrd_node, i;
1da177e4 356
90072059
RK
357 /*
358 * Invalidate the node number for empty or invalid memory banks
359 */
360 for (i = 0; i < mi->nr_banks; i++)
361 if (mi->bank[i].size == 0 || mi->bank[i].node >= MAX_NUMNODES)
362 mi->bank[i].node = -1;
1da177e4
LT
363
364 memcpy(&meminfo, mi, sizeof(meminfo));
365
366 /*
90072059 367 * Clear out all the mappings below the kernel image.
1da177e4 368 */
1a47ebc0
NP
369 for (addr = 0; addr < MODULE_START; addr += PGDIR_SIZE)
370 pmd_clear(pmd_off_k(addr));
371#ifdef CONFIG_XIP_KERNEL
372 /* The XIP kernel is mapped in the module area -- skip over it */
373 addr = ((unsigned long)&_etext + PGDIR_SIZE - 1) & PGDIR_MASK;
374#endif
375 for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
90072059 376 pmd_clear(pmd_off_k(addr));
1da177e4
LT
377
378 /*
90072059
RK
379 * Clear out all the kernel space mappings, except for the first
380 * memory bank, up to the end of the vmalloc region.
1da177e4 381 */
90072059
RK
382 for (addr = __phys_to_virt(mi->bank[0].start + mi->bank[0].size);
383 addr < VMALLOC_END; addr += PGDIR_SIZE)
384 pmd_clear(pmd_off_k(addr));
1da177e4
LT
385
386 /*
90072059 387 * Locate which node contains the ramdisk image, if any.
1da177e4 388 */
90072059 389 initrd_node = check_initrd(mi);
1da177e4 390
90072059
RK
391 /*
392 * Run through each node initialising the bootmem allocator.
393 */
394 for_each_node(node) {
395 unsigned long end_pfn;
1da177e4 396
90072059 397 end_pfn = bootmem_init_node(node, initrd_node, mi);
1da177e4
LT
398
399 /*
90072059 400 * Remember the highest memory PFN.
1da177e4 401 */
90072059
RK
402 if (end_pfn > memend_pfn)
403 memend_pfn = end_pfn;
404 }
1da177e4 405
90072059 406 high_memory = __va(memend_pfn << PAGE_SHIFT);
1da177e4 407
90072059
RK
408 /*
409 * This doesn't seem to be used by the Linux memory manager any
410 * more, but is used by ll_rw_block. If we can get rid of it, we
411 * also get rid of some of the stuff above as well.
412 *
413 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
414 * the system, not the maximum PFN.
415 */
416 max_pfn = max_low_pfn = memend_pfn - PHYS_PFN_OFFSET;
417}
1da177e4 418
90072059
RK
419/*
420 * Set up device the mappings. Since we clear out the page tables for all
421 * mappings above VMALLOC_END, we will remove any debug device mappings.
422 * This means you have to be careful how you debug this function, or any
02b30839
RK
423 * called function. This means you can't use any function or debugging
424 * method which may touch any device, otherwise the kernel _will_ crash.
90072059
RK
425 */
426static void __init devicemaps_init(struct machine_desc *mdesc)
427{
428 struct map_desc map;
429 unsigned long addr;
430 void *vectors;
1da177e4 431
02b30839
RK
432 /*
433 * Allocate the vector page early.
434 */
435 vectors = alloc_bootmem_low_pages(PAGE_SIZE);
436 BUG_ON(!vectors);
437
90072059
RK
438 for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
439 pmd_clear(pmd_off_k(addr));
1da177e4 440
1a47ebc0
NP
441 /*
442 * Map the kernel if it is XIP.
443 * It is always first in the modulearea.
444 */
445#ifdef CONFIG_XIP_KERNEL
446 map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & PGDIR_MASK);
447 map.virtual = MODULE_START;
448 map.length = ((unsigned long)&_etext - map.virtual + ~PGDIR_MASK) & PGDIR_MASK;
449 map.type = MT_ROM;
450 create_mapping(&map);
451#endif
452
90072059
RK
453 /*
454 * Map the cache flushing regions.
455 */
456#ifdef FLUSH_BASE
9769c246 457 map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
90072059
RK
458 map.virtual = FLUSH_BASE;
459 map.length = PGDIR_SIZE;
460 map.type = MT_CACHECLEAN;
461 create_mapping(&map);
462#endif
463#ifdef FLUSH_BASE_MINICACHE
9769c246 464 map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + PGDIR_SIZE);
90072059
RK
465 map.virtual = FLUSH_BASE_MINICACHE;
466 map.length = PGDIR_SIZE;
467 map.type = MT_MINICLEAN;
468 create_mapping(&map);
469#endif
470
90072059
RK
471 /*
472 * Create a mapping for the machine vectors at the high-vectors
473 * location (0xffff0000). If we aren't using high-vectors, also
474 * create a mapping at the low-vectors virtual address.
475 */
9769c246 476 map.pfn = __phys_to_pfn(virt_to_phys(vectors));
90072059
RK
477 map.virtual = 0xffff0000;
478 map.length = PAGE_SIZE;
479 map.type = MT_HIGH_VECTORS;
480 create_mapping(&map);
481
482 if (!vectors_high()) {
483 map.virtual = 0;
484 map.type = MT_LOW_VECTORS;
485 create_mapping(&map);
1da177e4
LT
486 }
487
488 /*
90072059 489 * Ask the machine support to map in the statically mapped devices.
90072059
RK
490 */
491 if (mdesc->map_io)
492 mdesc->map_io();
6bf7bd69
RK
493
494 /*
02b30839
RK
495 * Finally flush the caches and tlb to ensure that we're in a
496 * consistent state wrt the writebuffer. This also ensures that
497 * any write-allocated cache lines in the vector page are written
498 * back. After this point, we can start to touch devices again.
6bf7bd69
RK
499 */
500 local_flush_tlb_all();
02b30839 501 flush_cache_all();
90072059
RK
502}
503
504/*
505 * paging_init() sets up the page tables, initialises the zone memory
506 * maps, and sets up the zero page, bad page and bad page tables.
507 */
508void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
509{
510 void *zero_page;
511
512 build_mem_type_table();
513 bootmem_init(mi);
514 devicemaps_init(mdesc);
515
516 top_pmd = pmd_off_k(0xffff0000);
517
518 /*
519 * allocate the zero page. Note that we count on this going ok.
1da177e4 520 */
90072059 521 zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
1da177e4
LT
522 memzero(zero_page, PAGE_SIZE);
523 empty_zero_page = virt_to_page(zero_page);
524 flush_dcache_page(empty_zero_page);
525}
526
527static inline void free_area(unsigned long addr, unsigned long end, char *s)
528{
529 unsigned int size = (end - addr) >> 10;
530
531 for (; addr < end; addr += PAGE_SIZE) {
532 struct page *page = virt_to_page(addr);
533 ClearPageReserved(page);
534 set_page_count(page, 1);
535 free_page(addr);
536 totalram_pages++;
537 }
538
539 if (size && s)
540 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
541}
542
a013053d
RK
543static inline void
544free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
545{
546 struct page *start_pg, *end_pg;
547 unsigned long pg, pgend;
548
549 /*
550 * Convert start_pfn/end_pfn to a struct page pointer.
551 */
552 start_pg = pfn_to_page(start_pfn);
553 end_pg = pfn_to_page(end_pfn);
554
555 /*
556 * Convert to physical addresses, and
557 * round start upwards and end downwards.
558 */
559 pg = PAGE_ALIGN(__pa(start_pg));
560 pgend = __pa(end_pg) & PAGE_MASK;
561
562 /*
563 * If there are free pages between these,
564 * free the section of the memmap array.
565 */
566 if (pg < pgend)
567 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
568}
569
570/*
571 * The mem_map array can get very big. Free the unused area of the memory map.
572 */
573static void __init free_unused_memmap_node(int node, struct meminfo *mi)
574{
575 unsigned long bank_start, prev_bank_end = 0;
576 unsigned int i;
577
578 /*
579 * [FIXME] This relies on each bank being in address order. This
580 * may not be the case, especially if the user has provided the
581 * information on the command line.
582 */
90072059 583 for_each_nodebank(i, mi, node) {
a013053d
RK
584 bank_start = mi->bank[i].start >> PAGE_SHIFT;
585 if (bank_start < prev_bank_end) {
586 printk(KERN_ERR "MEM: unordered memory banks. "
587 "Not freeing memmap.\n");
588 break;
589 }
590
591 /*
592 * If we had a previous bank, and there is a space
593 * between the current bank and the previous, free it.
594 */
595 if (prev_bank_end && prev_bank_end != bank_start)
596 free_memmap(node, prev_bank_end, bank_start);
597
598 prev_bank_end = (mi->bank[i].start +
599 mi->bank[i].size) >> PAGE_SHIFT;
600 }
601}
602
1da177e4
LT
603/*
604 * mem_init() marks the free areas in the mem_map and tells us how much
605 * memory is free. This is done after various parts of the system have
606 * claimed their memory after the kernel image.
607 */
608void __init mem_init(void)
609{
610 unsigned int codepages, datapages, initpages;
611 int i, node;
612
613 codepages = &_etext - &_text;
614 datapages = &_end - &__data_start;
615 initpages = &__init_end - &__init_begin;
616
617#ifndef CONFIG_DISCONTIGMEM
618 max_mapnr = virt_to_page(high_memory) - mem_map;
619#endif
620
1da177e4
LT
621 /* this will put all unused low memory onto the freelists */
622 for_each_online_node(node) {
623 pg_data_t *pgdat = NODE_DATA(node);
624
a013053d
RK
625 free_unused_memmap_node(node, &meminfo);
626
1da177e4
LT
627 if (pgdat->node_spanned_pages != 0)
628 totalram_pages += free_all_bootmem_node(pgdat);
629 }
630
631#ifdef CONFIG_SA1111
632 /* now that our DMA memory is actually so designated, we can free it */
633 free_area(PAGE_OFFSET, (unsigned long)swapper_pg_dir, NULL);
634#endif
635
636 /*
637 * Since our memory may not be contiguous, calculate the
638 * real number of pages we have in this system
639 */
640 printk(KERN_INFO "Memory:");
641
642 num_physpages = 0;
643 for (i = 0; i < meminfo.nr_banks; i++) {
644 num_physpages += meminfo.bank[i].size >> PAGE_SHIFT;
645 printk(" %ldMB", meminfo.bank[i].size >> 20);
646 }
647
648 printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
649 printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
650 "%dK data, %dK init)\n",
651 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
652 codepages >> 10, datapages >> 10, initpages >> 10);
653
654 if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
655 extern int sysctl_overcommit_memory;
656 /*
657 * On a machine this small we won't get
658 * anywhere without overcommit, so turn
659 * it on by default.
660 */
661 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
662 }
663}
664
665void free_initmem(void)
666{
667 if (!machine_is_integrator() && !machine_is_cintegrator()) {
668 free_area((unsigned long)(&__init_begin),
669 (unsigned long)(&__init_end),
670 "init");
671 }
672}
673
674#ifdef CONFIG_BLK_DEV_INITRD
675
676static int keep_initrd;
677
678void free_initrd_mem(unsigned long start, unsigned long end)
679{
680 if (!keep_initrd)
681 free_area(start, end, "initrd");
682}
683
684static int __init keepinitrd_setup(char *__unused)
685{
686 keep_initrd = 1;
687 return 1;
688}
689
690__setup("keepinitrd", keepinitrd_setup);
691#endif