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