blob: 7cceb2c44cb911936c40dc96ea57f0ba0accf9c7 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
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
Paul Mackerras14cf11a2005-09-26 16:04:21 +100026#include <linux/kernel.h>
27#include <linux/mm.h>
28#include <linux/init.h>
29#include <linux/highmem.h>
30
31#include <asm/prom.h>
32#include <asm/mmu.h>
33#include <asm/machdep.h>
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100034#include <asm/lmb.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100035
36#include "mmu_decl.h"
Paul Mackerras14cf11a2005-09-26 16:04:21 +100037
38PTE *Hash, *Hash_end;
39unsigned long Hash_size, Hash_mask;
40unsigned long _SDR1;
41
42union ubat { /* BAT register values to be loaded */
43 BAT bat;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100044 u32 word[2];
Jon Loeligeree0339f2006-06-17 17:52:44 -050045} BATS[8][2]; /* 8 pairs of IBAT, DBAT */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100046
47struct batrange { /* stores address ranges mapped by BATs */
48 unsigned long start;
49 unsigned long limit;
50 unsigned long phys;
Jon Loeligeree0339f2006-06-17 17:52:44 -050051} bat_addrs[8];
Paul Mackerras14cf11a2005-09-26 16:04:21 +100052
53/*
54 * Return PA for this VA if it is mapped by a BAT, or 0
55 */
56unsigned long v_mapped_by_bats(unsigned long va)
57{
58 int b;
59 for (b = 0; b < 4; ++b)
60 if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
61 return bat_addrs[b].phys + (va - bat_addrs[b].start);
62 return 0;
63}
64
65/*
66 * Return VA for a given PA or 0 if not mapped
67 */
68unsigned long p_mapped_by_bats(unsigned long pa)
69{
70 int b;
71 for (b = 0; b < 4; ++b)
72 if (pa >= bat_addrs[b].phys
73 && pa < (bat_addrs[b].limit-bat_addrs[b].start)
74 +bat_addrs[b].phys)
75 return bat_addrs[b].start+(pa-bat_addrs[b].phys);
76 return 0;
77}
78
79unsigned long __init mmu_mapin_ram(void)
80{
81#ifdef CONFIG_POWER4
82 return 0;
83#else
84 unsigned long tot, bl, done;
85 unsigned long max_size = (256<<20);
86 unsigned long align;
87
88 if (__map_without_bats)
89 return 0;
90
91 /* Set up BAT2 and if necessary BAT3 to cover RAM. */
92
93 /* Make sure we don't map a block larger than the
94 smallest alignment of the physical address. */
95 /* alignment of PPC_MEMSTART */
96 align = ~(PPC_MEMSTART-1) & PPC_MEMSTART;
97 /* set BAT block size to MIN(max_size, align) */
98 if (align && align < max_size)
99 max_size = align;
100
101 tot = total_lowmem;
102 for (bl = 128<<10; bl < max_size; bl <<= 1) {
103 if (bl * 2 > tot)
104 break;
105 }
106
107 setbat(2, KERNELBASE, PPC_MEMSTART, bl, _PAGE_RAM);
108 done = (unsigned long)bat_addrs[2].limit - KERNELBASE + 1;
109 if ((done < tot) && !bat_addrs[3].limit) {
110 /* use BAT3 to cover a bit more */
111 tot -= done;
112 for (bl = 128<<10; bl < max_size; bl <<= 1)
113 if (bl * 2 > tot)
114 break;
115 setbat(3, KERNELBASE+done, PPC_MEMSTART+done, bl, _PAGE_RAM);
116 done = (unsigned long)bat_addrs[3].limit - KERNELBASE + 1;
117 }
118
119 return done;
120#endif
121}
122
123/*
124 * Set up one of the I/D BAT (block address translation) register pairs.
125 * The parameters are not checked; in particular size must be a power
126 * of 2 between 128k and 256M.
127 */
128void __init setbat(int index, unsigned long virt, unsigned long phys,
129 unsigned int size, int flags)
130{
131 unsigned int bl;
132 int wimgxpp;
133 union ubat *bat = BATS[index];
134
135 if (((flags & _PAGE_NO_CACHE) == 0) &&
136 cpu_has_feature(CPU_FTR_NEED_COHERENT))
137 flags |= _PAGE_COHERENT;
138
139 bl = (size >> 17) - 1;
140 if (PVR_VER(mfspr(SPRN_PVR)) != 1) {
141 /* 603, 604, etc. */
142 /* Do DBAT first */
143 wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
144 | _PAGE_COHERENT | _PAGE_GUARDED);
145 wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
146 bat[1].word[0] = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
147 bat[1].word[1] = phys | wimgxpp;
148#ifndef CONFIG_KGDB /* want user access for breakpoints */
149 if (flags & _PAGE_USER)
150#endif
151 bat[1].bat.batu.vp = 1;
152 if (flags & _PAGE_GUARDED) {
153 /* G bit must be zero in IBATs */
154 bat[0].word[0] = bat[0].word[1] = 0;
155 } else {
156 /* make IBAT same as DBAT */
157 bat[0] = bat[1];
158 }
159 } else {
160 /* 601 cpu */
161 if (bl > BL_8M)
162 bl = BL_8M;
163 wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
164 | _PAGE_COHERENT);
165 wimgxpp |= (flags & _PAGE_RW)?
166 ((flags & _PAGE_USER)? PP_RWRW: PP_RWXX): PP_RXRX;
167 bat->word[0] = virt | wimgxpp | 4; /* Ks=0, Ku=1 */
168 bat->word[1] = phys | bl | 0x40; /* V=1 */
169 }
170
171 bat_addrs[index].start = virt;
172 bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
173 bat_addrs[index].phys = phys;
174}
175
176/*
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100177 * Preload a translation in the hash table
178 */
179void hash_preload(struct mm_struct *mm, unsigned long ea,
180 unsigned long access, unsigned long trap)
181{
182 pmd_t *pmd;
183
184 if (Hash == 0)
185 return;
Benjamin Herrenschmidt863c84b2005-11-07 00:57:58 -0800186 pmd = pmd_offset(pgd_offset(mm, ea), ea);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100187 if (!pmd_none(*pmd))
Paul Mackerras6218a762006-06-11 14:15:17 +1000188 add_hash_page(mm->context.id, ea, pmd_val(*pmd));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100189}
190
191/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000192 * Initialize the hash table and patch the instructions in hashtable.S.
193 */
194void __init MMU_init_hw(void)
195{
196 unsigned int hmask, mb, mb2;
197 unsigned int n_hpteg, lg_n_hpteg;
198
199 extern unsigned int hash_page_patch_A[];
200 extern unsigned int hash_page_patch_B[], hash_page_patch_C[];
201 extern unsigned int hash_page[];
202 extern unsigned int flush_hash_patch_A[], flush_hash_patch_B[];
203
204 if (!cpu_has_feature(CPU_FTR_HPTE_TABLE)) {
205 /*
206 * Put a blr (procedure return) instruction at the
207 * start of hash_page, since we can still get DSI
208 * exceptions on a 603.
209 */
210 hash_page[0] = 0x4e800020;
211 flush_icache_range((unsigned long) &hash_page[0],
212 (unsigned long) &hash_page[1]);
213 return;
214 }
215
216 if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
217
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000218#define LG_HPTEG_SIZE 6 /* 64 bytes per HPTEG */
219#define SDR1_LOW_BITS ((n_hpteg - 1) >> 10)
220#define MIN_N_HPTEG 1024 /* min 64kB hash table */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000221
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000222 /*
223 * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
224 * This is less than the recommended amount, but then
225 * Linux ain't AIX.
226 */
227 n_hpteg = total_memory / (PAGE_SIZE * 8);
228 if (n_hpteg < MIN_N_HPTEG)
229 n_hpteg = MIN_N_HPTEG;
230 lg_n_hpteg = __ilog2(n_hpteg);
231 if (n_hpteg & (n_hpteg - 1)) {
232 ++lg_n_hpteg; /* round up if not power of 2 */
233 n_hpteg = 1 << lg_n_hpteg;
234 }
235 Hash_size = n_hpteg << LG_HPTEG_SIZE;
236
237 /*
238 * Find some memory for the hash table.
239 */
240 if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000241 Hash = __va(lmb_alloc_base(Hash_size, Hash_size,
242 __initial_memory_limit));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000243 cacheable_memzero(Hash, Hash_size);
244 _SDR1 = __pa(Hash) | SDR1_LOW_BITS;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000245
246 Hash_end = (PTE *) ((unsigned long)Hash + Hash_size);
247
248 printk("Total memory = %ldMB; using %ldkB for hash table (at %p)\n",
249 total_memory >> 20, Hash_size >> 10, Hash);
250
251
252 /*
253 * Patch up the instructions in hashtable.S:create_hpte
254 */
255 if ( ppc_md.progress ) ppc_md.progress("hash:patch", 0x345);
256 Hash_mask = n_hpteg - 1;
257 hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
258 mb2 = mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
259 if (lg_n_hpteg > 16)
260 mb2 = 16 - LG_HPTEG_SIZE;
261
262 hash_page_patch_A[0] = (hash_page_patch_A[0] & ~0xffff)
263 | ((unsigned int)(Hash) >> 16);
264 hash_page_patch_A[1] = (hash_page_patch_A[1] & ~0x7c0) | (mb << 6);
265 hash_page_patch_A[2] = (hash_page_patch_A[2] & ~0x7c0) | (mb2 << 6);
266 hash_page_patch_B[0] = (hash_page_patch_B[0] & ~0xffff) | hmask;
267 hash_page_patch_C[0] = (hash_page_patch_C[0] & ~0xffff) | hmask;
268
269 /*
270 * Ensure that the locations we've patched have been written
271 * out from the data cache and invalidated in the instruction
272 * cache, on those machines with split caches.
273 */
274 flush_icache_range((unsigned long) &hash_page_patch_A[0],
275 (unsigned long) &hash_page_patch_C[1]);
276
277 /*
278 * Patch up the instructions in hashtable.S:flush_hash_page
279 */
280 flush_hash_patch_A[0] = (flush_hash_patch_A[0] & ~0xffff)
281 | ((unsigned int)(Hash) >> 16);
282 flush_hash_patch_A[1] = (flush_hash_patch_A[1] & ~0x7c0) | (mb << 6);
283 flush_hash_patch_A[2] = (flush_hash_patch_A[2] & ~0x7c0) | (mb2 << 6);
284 flush_hash_patch_B[0] = (flush_hash_patch_B[0] & ~0xffff) | hmask;
285 flush_icache_range((unsigned long) &flush_hash_patch_A[0],
286 (unsigned long) &flush_hash_patch_B[1]);
287
288 if ( ppc_md.progress ) ppc_md.progress("hash:done", 0x205);
289}