Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-drm-fsl-dcu.git] / arch / sh / mm / init.c
1 /* $Id: init.c,v 1.19 2004/02/21 04:42:16 kkojima Exp $
2  *
3  *  linux/arch/sh/mm/init.c
4  *
5  *  Copyright (C) 1999  Niibe Yutaka
6  *  Copyright (C) 2002, 2004  Paul Mundt
7  *
8  *  Based on linux/arch/i386/mm/init.c:
9  *   Copyright (C) 1995  Linus Torvalds
10  */
11
12 #include <linux/signal.h>
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/string.h>
17 #include <linux/types.h>
18 #include <linux/ptrace.h>
19 #include <linux/mman.h>
20 #include <linux/mm.h>
21 #include <linux/swap.h>
22 #include <linux/smp.h>
23 #include <linux/init.h>
24 #include <linux/highmem.h>
25 #include <linux/bootmem.h>
26 #include <linux/pagemap.h>
27 #include <linux/proc_fs.h>
28 #include <asm/processor.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/pgalloc.h>
33 #include <asm/mmu_context.h>
34 #include <asm/io.h>
35 #include <asm/tlb.h>
36 #include <asm/cacheflush.h>
37 #include <asm/cache.h>
38
39 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
40 pgd_t swapper_pg_dir[PTRS_PER_PGD];
41
42 /*
43  * Cache of MMU context last used.
44  */
45 unsigned long mmu_context_cache = NO_CONTEXT;
46
47 #ifdef CONFIG_MMU
48 /* It'd be good if these lines were in the standard header file. */
49 #define START_PFN       (NODE_DATA(0)->bdata->node_boot_start >> PAGE_SHIFT)
50 #define MAX_LOW_PFN     (NODE_DATA(0)->bdata->node_low_pfn)
51 #endif
52
53 void (*copy_page)(void *from, void *to);
54 void (*clear_page)(void *to);
55
56 void show_mem(void)
57 {
58         int i, total = 0, reserved = 0;
59         int shared = 0, cached = 0;
60
61         printk("Mem-info:\n");
62         show_free_areas();
63         printk("Free swap:       %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
64         i = max_mapnr;
65         while (i-- > 0) {
66                 total++;
67                 if (PageReserved(mem_map+i))
68                         reserved++;
69                 else if (PageSwapCache(mem_map+i))
70                         cached++;
71                 else if (page_count(mem_map+i))
72                         shared += page_count(mem_map+i) - 1;
73         }
74         printk("%d pages of RAM\n",total);
75         printk("%d reserved pages\n",reserved);
76         printk("%d pages shared\n",shared);
77         printk("%d pages swap cached\n",cached);
78 }
79
80 #ifdef CONFIG_MMU
81 static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
82 {
83         pgd_t *pgd;
84         pud_t *pud;
85         pmd_t *pmd;
86         pte_t *pte;
87
88         pgd = pgd_offset_k(addr);
89         if (pgd_none(*pgd)) {
90                 pgd_ERROR(*pgd);
91                 return;
92         }
93
94         pud = pud_alloc(NULL, pgd, addr);
95         if (unlikely(!pud)) {
96                 pud_ERROR(*pud);
97                 return;
98         }
99
100         pmd = pmd_alloc(NULL, pud, addr);
101         if (unlikely(!pmd)) {
102                 pmd_ERROR(*pmd);
103                 return;
104         }
105
106         pte = pte_offset_kernel(pmd, addr);
107         if (!pte_none(*pte)) {
108                 pte_ERROR(*pte);
109                 return;
110         }
111
112         set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
113
114         __flush_tlb_page(get_asid(), addr);
115 }
116
117 /*
118  * As a performance optimization, other platforms preserve the fixmap mapping
119  * across a context switch, we don't presently do this, but this could be done
120  * in a similar fashion as to the wired TLB interface that sh64 uses (by way
121  * of the memorry mapped UTLB configuration) -- this unfortunately forces us to
122  * give up a TLB entry for each mapping we want to preserve. While this may be
123  * viable for a small number of fixmaps, it's not particularly useful for
124  * everything and needs to be carefully evaluated. (ie, we may want this for
125  * the vsyscall page).
126  *
127  * XXX: Perhaps add a _PAGE_WIRED flag or something similar that we can pass
128  * in at __set_fixmap() time to determine the appropriate behavior to follow.
129  *
130  *                                       -- PFM.
131  */
132 void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
133 {
134         unsigned long address = __fix_to_virt(idx);
135
136         if (idx >= __end_of_fixed_addresses) {
137                 BUG();
138                 return;
139         }
140
141         set_pte_phys(address, phys, prot);
142 }
143 #endif  /* CONFIG_MMU */
144
145 /* References to section boundaries */
146
147 extern char _text, _etext, _edata, __bss_start, _end;
148 extern char __init_begin, __init_end;
149
150 /*
151  * paging_init() sets up the page tables
152  */
153 void __init paging_init(void)
154 {
155         unsigned long zones_size[MAX_NR_ZONES] = { 0, };
156
157         /*
158          * Setup some defaults for the zone sizes.. these should be safe
159          * regardless of distcontiguous memory or MMU settings.
160          */
161         zones_size[ZONE_NORMAL] = __MEMORY_SIZE >> PAGE_SHIFT;
162 #ifdef CONFIG_HIGHMEM
163         zones_size[ZONE_HIGHMEM] = 0 >> PAGE_SHIFT;
164 #endif
165
166 #ifdef CONFIG_MMU
167         /*
168          * If we have an MMU, and want to be using it .. we need to adjust
169          * the zone sizes accordingly, in addition to turning it on.
170          */
171         {
172                 /* We don't need to map the kernel through the TLB, as
173                  * it is permanatly mapped using P1. So clear the
174                  * entire pgd. */
175                 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
176
177                 /* Turn on the MMU */
178                 enable_mmu();
179                 zones_size[ZONE_NORMAL] = MAX_LOW_PFN - START_PFN;
180         }
181
182         /* Set an initial value for the MMU.TTB so we don't have to
183          * check for a null value. */
184         set_TTB(swapper_pg_dir);
185
186 #elif defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
187         /*
188          * If we don't have CONFIG_MMU set and the processor in question
189          * still has an MMU, care needs to be taken to make sure it doesn't
190          * stay on.. Since the boot loader could have potentially already
191          * turned it on, and we clearly don't want it, we simply turn it off.
192          *
193          * We don't need to do anything special for the zone sizes, since the
194          * default values that were already configured up above should be
195          * satisfactory.
196          */
197         disable_mmu();
198 #endif
199         NODE_DATA(0)->node_mem_map = NULL;
200         free_area_init_node(0, NODE_DATA(0), zones_size, __MEMORY_START >> PAGE_SHIFT, 0);
201 }
202
203 static struct kcore_list kcore_mem, kcore_vmalloc;
204
205 void __init mem_init(void)
206 {
207         int codesize, reservedpages, datasize, initsize;
208         int tmp;
209         extern unsigned long memory_start;
210
211 #ifdef CONFIG_MMU
212         high_memory = (void *)__va(MAX_LOW_PFN * PAGE_SIZE);
213 #else
214         extern unsigned long memory_end;
215
216         high_memory = (void *)(memory_end & PAGE_MASK);
217 #endif
218
219         max_mapnr = num_physpages = MAP_NR(high_memory) - MAP_NR(memory_start);
220
221         /* clear the zero-page */
222         memset(empty_zero_page, 0, PAGE_SIZE);
223         __flush_wback_region(empty_zero_page, PAGE_SIZE);
224
225         /*
226          * Setup wrappers for copy/clear_page(), these will get overridden
227          * later in the boot process if a better method is available.
228          */
229 #ifdef CONFIG_MMU
230         copy_page = copy_page_slow;
231         clear_page = clear_page_slow;
232 #else
233         copy_page = copy_page_nommu;
234         clear_page = clear_page_nommu;
235 #endif
236
237         /* this will put all low memory onto the freelists */
238         totalram_pages += free_all_bootmem_node(NODE_DATA(0));
239         reservedpages = 0;
240         for (tmp = 0; tmp < num_physpages; tmp++)
241                 /*
242                  * Only count reserved RAM pages
243                  */
244                 if (PageReserved(mem_map+tmp))
245                         reservedpages++;
246
247         codesize =  (unsigned long) &_etext - (unsigned long) &_text;
248         datasize =  (unsigned long) &_edata - (unsigned long) &_etext;
249         initsize =  (unsigned long) &__init_end - (unsigned long) &__init_begin;
250
251         kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
252         kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
253                    VMALLOC_END - VMALLOC_START);
254
255         printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
256                "%dk reserved, %dk data, %dk init)\n",
257                 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
258                 max_mapnr << (PAGE_SHIFT-10),
259                 codesize >> 10,
260                 reservedpages << (PAGE_SHIFT-10),
261                 datasize >> 10,
262                 initsize >> 10);
263
264         p3_cache_init();
265
266         /* Initialize the vDSO */
267         vsyscall_init();
268 }
269
270 void free_initmem(void)
271 {
272         unsigned long addr;
273
274         addr = (unsigned long)(&__init_begin);
275         for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
276                 ClearPageReserved(virt_to_page(addr));
277                 init_page_count(virt_to_page(addr));
278                 free_page(addr);
279                 totalram_pages++;
280         }
281         printk ("Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
282 }
283
284 #ifdef CONFIG_BLK_DEV_INITRD
285 void free_initrd_mem(unsigned long start, unsigned long end)
286 {
287         unsigned long p;
288         for (p = start; p < end; p += PAGE_SIZE) {
289                 ClearPageReserved(virt_to_page(p));
290                 init_page_count(virt_to_page(p));
291                 free_page(p);
292                 totalram_pages++;
293         }
294         printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
295 }
296 #endif
297