Merge ../linus
[pandora-kernel.git] / arch / powerpc / mm / ppc_mmu_32.c
1 /*
2  * This file contains the routines for handling the MMU on those
3  * PowerPC implementations where the MMU substantially follows the
4  * architecture specification.  This includes the 6xx, 7xx, 7xxx,
5  * 8260, and POWER3 implementations but excludes the 8xx and 4xx.
6  *  -- paulus
7  *
8  *  Derived from arch/ppc/mm/init.c:
9  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
10  *
11  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
12  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
13  *    Copyright (C) 1996 Paul Mackerras
14  *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
15  *
16  *  Derived from "arch/i386/mm/init.c"
17  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
18  *
19  *  This program is free software; you can redistribute it and/or
20  *  modify it under the terms of the GNU General Public License
21  *  as published by the Free Software Foundation; either version
22  *  2 of the License, or (at your option) any later version.
23  *
24  */
25
26 #include <linux/config.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/init.h>
30 #include <linux/highmem.h>
31
32 #include <asm/prom.h>
33 #include <asm/mmu.h>
34 #include <asm/machdep.h>
35 #include <asm/lmb.h>
36
37 #include "mmu_decl.h"
38
39 PTE *Hash, *Hash_end;
40 unsigned long Hash_size, Hash_mask;
41 unsigned long _SDR1;
42
43 union ubat {                    /* BAT register values to be loaded */
44         BAT     bat;
45         u32     word[2];
46 } BATS[8][2];                   /* 8 pairs of IBAT, DBAT */
47
48 struct batrange {               /* stores address ranges mapped by BATs */
49         unsigned long start;
50         unsigned long limit;
51         unsigned long phys;
52 } bat_addrs[8];
53
54 /*
55  * Return PA for this VA if it is mapped by a BAT, or 0
56  */
57 unsigned long v_mapped_by_bats(unsigned long va)
58 {
59         int b;
60         for (b = 0; b < 4; ++b)
61                 if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
62                         return bat_addrs[b].phys + (va - bat_addrs[b].start);
63         return 0;
64 }
65
66 /*
67  * Return VA for a given PA or 0 if not mapped
68  */
69 unsigned long p_mapped_by_bats(unsigned long pa)
70 {
71         int b;
72         for (b = 0; b < 4; ++b)
73                 if (pa >= bat_addrs[b].phys
74                     && pa < (bat_addrs[b].limit-bat_addrs[b].start)
75                               +bat_addrs[b].phys)
76                         return bat_addrs[b].start+(pa-bat_addrs[b].phys);
77         return 0;
78 }
79
80 unsigned long __init mmu_mapin_ram(void)
81 {
82 #ifdef CONFIG_POWER4
83         return 0;
84 #else
85         unsigned long tot, bl, done;
86         unsigned long max_size = (256<<20);
87         unsigned long align;
88
89         if (__map_without_bats)
90                 return 0;
91
92         /* Set up BAT2 and if necessary BAT3 to cover RAM. */
93
94         /* Make sure we don't map a block larger than the
95            smallest alignment of the physical address. */
96         /* alignment of PPC_MEMSTART */
97         align = ~(PPC_MEMSTART-1) & PPC_MEMSTART;
98         /* set BAT block size to MIN(max_size, align) */
99         if (align && align < max_size)
100                 max_size = align;
101
102         tot = total_lowmem;
103         for (bl = 128<<10; bl < max_size; bl <<= 1) {
104                 if (bl * 2 > tot)
105                         break;
106         }
107
108         setbat(2, KERNELBASE, PPC_MEMSTART, bl, _PAGE_RAM);
109         done = (unsigned long)bat_addrs[2].limit - KERNELBASE + 1;
110         if ((done < tot) && !bat_addrs[3].limit) {
111                 /* use BAT3 to cover a bit more */
112                 tot -= done;
113                 for (bl = 128<<10; bl < max_size; bl <<= 1)
114                         if (bl * 2 > tot)
115                                 break;
116                 setbat(3, KERNELBASE+done, PPC_MEMSTART+done, bl, _PAGE_RAM);
117                 done = (unsigned long)bat_addrs[3].limit - KERNELBASE + 1;
118         }
119
120         return done;
121 #endif
122 }
123
124 /*
125  * Set up one of the I/D BAT (block address translation) register pairs.
126  * The parameters are not checked; in particular size must be a power
127  * of 2 between 128k and 256M.
128  */
129 void __init setbat(int index, unsigned long virt, unsigned long phys,
130                    unsigned int size, int flags)
131 {
132         unsigned int bl;
133         int wimgxpp;
134         union ubat *bat = BATS[index];
135
136         if (((flags & _PAGE_NO_CACHE) == 0) &&
137             cpu_has_feature(CPU_FTR_NEED_COHERENT))
138                 flags |= _PAGE_COHERENT;
139
140         bl = (size >> 17) - 1;
141         if (PVR_VER(mfspr(SPRN_PVR)) != 1) {
142                 /* 603, 604, etc. */
143                 /* Do DBAT first */
144                 wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
145                                    | _PAGE_COHERENT | _PAGE_GUARDED);
146                 wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
147                 bat[1].word[0] = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
148                 bat[1].word[1] = phys | wimgxpp;
149 #ifndef CONFIG_KGDB /* want user access for breakpoints */
150                 if (flags & _PAGE_USER)
151 #endif
152                         bat[1].bat.batu.vp = 1;
153                 if (flags & _PAGE_GUARDED) {
154                         /* G bit must be zero in IBATs */
155                         bat[0].word[0] = bat[0].word[1] = 0;
156                 } else {
157                         /* make IBAT same as DBAT */
158                         bat[0] = bat[1];
159                 }
160         } else {
161                 /* 601 cpu */
162                 if (bl > BL_8M)
163                         bl = BL_8M;
164                 wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
165                                    | _PAGE_COHERENT);
166                 wimgxpp |= (flags & _PAGE_RW)?
167                         ((flags & _PAGE_USER)? PP_RWRW: PP_RWXX): PP_RXRX;
168                 bat->word[0] = virt | wimgxpp | 4;      /* Ks=0, Ku=1 */
169                 bat->word[1] = phys | bl | 0x40;        /* V=1 */
170         }
171
172         bat_addrs[index].start = virt;
173         bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
174         bat_addrs[index].phys = phys;
175 }
176
177 /*
178  * Preload a translation in the hash table
179  */
180 void hash_preload(struct mm_struct *mm, unsigned long ea,
181                   unsigned long access, unsigned long trap)
182 {
183         pmd_t *pmd;
184
185         if (Hash == 0)
186                 return;
187         pmd = pmd_offset(pgd_offset(mm, ea), ea);
188         if (!pmd_none(*pmd))
189                 add_hash_page(mm->context.id, ea, pmd_val(*pmd));
190 }
191
192 /*
193  * Initialize the hash table and patch the instructions in hashtable.S.
194  */
195 void __init MMU_init_hw(void)
196 {
197         unsigned int hmask, mb, mb2;
198         unsigned int n_hpteg, lg_n_hpteg;
199
200         extern unsigned int hash_page_patch_A[];
201         extern unsigned int hash_page_patch_B[], hash_page_patch_C[];
202         extern unsigned int hash_page[];
203         extern unsigned int flush_hash_patch_A[], flush_hash_patch_B[];
204
205         if (!cpu_has_feature(CPU_FTR_HPTE_TABLE)) {
206                 /*
207                  * Put a blr (procedure return) instruction at the
208                  * start of hash_page, since we can still get DSI
209                  * exceptions on a 603.
210                  */
211                 hash_page[0] = 0x4e800020;
212                 flush_icache_range((unsigned long) &hash_page[0],
213                                    (unsigned long) &hash_page[1]);
214                 return;
215         }
216
217         if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
218
219 #define LG_HPTEG_SIZE   6               /* 64 bytes per HPTEG */
220 #define SDR1_LOW_BITS   ((n_hpteg - 1) >> 10)
221 #define MIN_N_HPTEG     1024            /* min 64kB hash table */
222
223         /*
224          * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
225          * This is less than the recommended amount, but then
226          * Linux ain't AIX.
227          */
228         n_hpteg = total_memory / (PAGE_SIZE * 8);
229         if (n_hpteg < MIN_N_HPTEG)
230                 n_hpteg = MIN_N_HPTEG;
231         lg_n_hpteg = __ilog2(n_hpteg);
232         if (n_hpteg & (n_hpteg - 1)) {
233                 ++lg_n_hpteg;           /* round up if not power of 2 */
234                 n_hpteg = 1 << lg_n_hpteg;
235         }
236         Hash_size = n_hpteg << LG_HPTEG_SIZE;
237
238         /*
239          * Find some memory for the hash table.
240          */
241         if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
242         Hash = __va(lmb_alloc_base(Hash_size, Hash_size,
243                                    __initial_memory_limit));
244         cacheable_memzero(Hash, Hash_size);
245         _SDR1 = __pa(Hash) | SDR1_LOW_BITS;
246
247         Hash_end = (PTE *) ((unsigned long)Hash + Hash_size);
248
249         printk("Total memory = %ldMB; using %ldkB for hash table (at %p)\n",
250                total_memory >> 20, Hash_size >> 10, Hash);
251
252
253         /*
254          * Patch up the instructions in hashtable.S:create_hpte
255          */
256         if ( ppc_md.progress ) ppc_md.progress("hash:patch", 0x345);
257         Hash_mask = n_hpteg - 1;
258         hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
259         mb2 = mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
260         if (lg_n_hpteg > 16)
261                 mb2 = 16 - LG_HPTEG_SIZE;
262
263         hash_page_patch_A[0] = (hash_page_patch_A[0] & ~0xffff)
264                 | ((unsigned int)(Hash) >> 16);
265         hash_page_patch_A[1] = (hash_page_patch_A[1] & ~0x7c0) | (mb << 6);
266         hash_page_patch_A[2] = (hash_page_patch_A[2] & ~0x7c0) | (mb2 << 6);
267         hash_page_patch_B[0] = (hash_page_patch_B[0] & ~0xffff) | hmask;
268         hash_page_patch_C[0] = (hash_page_patch_C[0] & ~0xffff) | hmask;
269
270         /*
271          * Ensure that the locations we've patched have been written
272          * out from the data cache and invalidated in the instruction
273          * cache, on those machines with split caches.
274          */
275         flush_icache_range((unsigned long) &hash_page_patch_A[0],
276                            (unsigned long) &hash_page_patch_C[1]);
277
278         /*
279          * Patch up the instructions in hashtable.S:flush_hash_page
280          */
281         flush_hash_patch_A[0] = (flush_hash_patch_A[0] & ~0xffff)
282                 | ((unsigned int)(Hash) >> 16);
283         flush_hash_patch_A[1] = (flush_hash_patch_A[1] & ~0x7c0) | (mb << 6);
284         flush_hash_patch_A[2] = (flush_hash_patch_A[2] & ~0x7c0) | (mb2 << 6);
285         flush_hash_patch_B[0] = (flush_hash_patch_B[0] & ~0xffff) | hmask;
286         flush_icache_range((unsigned long) &flush_hash_patch_A[0],
287                            (unsigned long) &flush_hash_patch_B[1]);
288
289         if ( ppc_md.progress ) ppc_md.progress("hash:done", 0x205);
290 }