Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1 | /* |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2 | * $Id: hashtable.S,v 1.6 1999/10/08 01:56:15 paulus Exp $ |
| 3 | * |
| 4 | * PowerPC version |
| 5 | * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) |
| 6 | * Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP |
| 7 | * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu> |
| 8 | * Adapted for Power Macintosh by Paul Mackerras. |
| 9 | * Low-level exception handlers and MMU support |
| 10 | * rewritten by Paul Mackerras. |
| 11 | * Copyright (C) 1996 Paul Mackerras. |
| 12 | * |
| 13 | * This file contains low-level assembler routines for managing |
| 14 | * the PowerPC MMU hash table. (PPC 8xx processors don't use a |
| 15 | * hash table, so this file is not used on them.) |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License |
| 19 | * as published by the Free Software Foundation; either version |
| 20 | * 2 of the License, or (at your option) any later version. |
| 21 | * |
| 22 | */ |
| 23 | |
Paul Mackerras | b3b8dc6 | 2005-10-10 22:20:10 +1000 | [diff] [blame] | 24 | #include <asm/reg.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 25 | #include <asm/page.h> |
| 26 | #include <asm/pgtable.h> |
| 27 | #include <asm/cputable.h> |
| 28 | #include <asm/ppc_asm.h> |
| 29 | #include <asm/thread_info.h> |
| 30 | #include <asm/asm-offsets.h> |
| 31 | |
| 32 | #ifdef CONFIG_SMP |
Kumar Gala | 991eb43 | 2007-05-14 17:11:58 -0500 | [diff] [blame] | 33 | .section .bss |
| 34 | .align 2 |
| 35 | .globl mmu_hash_lock |
| 36 | mmu_hash_lock: |
| 37 | .space 4 |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 38 | #endif /* CONFIG_SMP */ |
| 39 | |
| 40 | /* |
| 41 | * Sync CPUs with hash_page taking & releasing the hash |
| 42 | * table lock |
| 43 | */ |
| 44 | #ifdef CONFIG_SMP |
| 45 | .text |
| 46 | _GLOBAL(hash_page_sync) |
Benjamin Herrenschmidt | b991f05 | 2008-03-31 07:49:27 +1100 | [diff] [blame] | 47 | mfmsr r10 |
| 48 | rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */ |
| 49 | mtmsr r0 |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 50 | lis r8,mmu_hash_lock@h |
| 51 | ori r8,r8,mmu_hash_lock@l |
| 52 | lis r0,0x0fff |
| 53 | b 10f |
| 54 | 11: lwz r6,0(r8) |
| 55 | cmpwi 0,r6,0 |
| 56 | bne 11b |
| 57 | 10: lwarx r6,0,r8 |
| 58 | cmpwi 0,r6,0 |
| 59 | bne- 11b |
| 60 | stwcx. r0,0,r8 |
| 61 | bne- 10b |
| 62 | isync |
| 63 | eieio |
| 64 | li r0,0 |
| 65 | stw r0,0(r8) |
Benjamin Herrenschmidt | b991f05 | 2008-03-31 07:49:27 +1100 | [diff] [blame] | 66 | mtmsr r10 |
| 67 | blr |
| 68 | #endif /* CONFIG_SMP */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Load a PTE into the hash table, if possible. |
| 72 | * The address is in r4, and r3 contains an access flag: |
| 73 | * _PAGE_RW (0x400) if a write. |
| 74 | * r9 contains the SRR1 value, from which we use the MSR_PR bit. |
| 75 | * SPRG3 contains the physical address of the current task's thread. |
| 76 | * |
| 77 | * Returns to the caller if the access is illegal or there is no |
| 78 | * mapping for the address. Otherwise it places an appropriate PTE |
| 79 | * in the hash table and returns from the exception. |
| 80 | * Uses r0, r3 - r8, ctr, lr. |
| 81 | */ |
| 82 | .text |
| 83 | _GLOBAL(hash_page) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 84 | tophys(r7,0) /* gets -KERNELBASE into r7 */ |
| 85 | #ifdef CONFIG_SMP |
| 86 | addis r8,r7,mmu_hash_lock@h |
| 87 | ori r8,r8,mmu_hash_lock@l |
| 88 | lis r0,0x0fff |
| 89 | b 10f |
| 90 | 11: lwz r6,0(r8) |
| 91 | cmpwi 0,r6,0 |
| 92 | bne 11b |
| 93 | 10: lwarx r6,0,r8 |
| 94 | cmpwi 0,r6,0 |
| 95 | bne- 11b |
| 96 | stwcx. r0,0,r8 |
| 97 | bne- 10b |
| 98 | isync |
| 99 | #endif |
| 100 | /* Get PTE (linux-style) and check access */ |
| 101 | lis r0,KERNELBASE@h /* check if kernel address */ |
| 102 | cmplw 0,r4,r0 |
| 103 | mfspr r8,SPRN_SPRG3 /* current task's THREAD (phys) */ |
| 104 | ori r3,r3,_PAGE_USER|_PAGE_PRESENT /* test low addresses as user */ |
| 105 | lwz r5,PGDIR(r8) /* virt page-table root */ |
| 106 | blt+ 112f /* assume user more likely */ |
| 107 | lis r5,swapper_pg_dir@ha /* if kernel address, use */ |
| 108 | addi r5,r5,swapper_pg_dir@l /* kernel page table */ |
| 109 | rlwimi r3,r9,32-12,29,29 /* MSR_PR -> _PAGE_USER */ |
| 110 | 112: add r5,r5,r7 /* convert to phys addr */ |
| 111 | rlwimi r5,r4,12,20,29 /* insert top 10 bits of address */ |
| 112 | lwz r8,0(r5) /* get pmd entry */ |
| 113 | rlwinm. r8,r8,0,0,19 /* extract address of pte page */ |
| 114 | #ifdef CONFIG_SMP |
| 115 | beq- hash_page_out /* return if no mapping */ |
| 116 | #else |
| 117 | /* XXX it seems like the 601 will give a machine fault on the |
| 118 | rfi if its alignment is wrong (bottom 4 bits of address are |
| 119 | 8 or 0xc) and we have had a not-taken conditional branch |
| 120 | to the address following the rfi. */ |
| 121 | beqlr- |
| 122 | #endif |
| 123 | rlwimi r8,r4,22,20,29 /* insert next 10 bits of address */ |
| 124 | rlwinm r0,r3,32-3,24,24 /* _PAGE_RW access -> _PAGE_DIRTY */ |
| 125 | ori r0,r0,_PAGE_ACCESSED|_PAGE_HASHPTE |
| 126 | |
| 127 | /* |
| 128 | * Update the linux PTE atomically. We do the lwarx up-front |
| 129 | * because almost always, there won't be a permission violation |
| 130 | * and there won't already be an HPTE, and thus we will have |
| 131 | * to update the PTE to set _PAGE_HASHPTE. -- paulus. |
| 132 | */ |
| 133 | retry: |
| 134 | lwarx r6,0,r8 /* get linux-style pte */ |
| 135 | andc. r5,r3,r6 /* check access & ~permission */ |
| 136 | #ifdef CONFIG_SMP |
| 137 | bne- hash_page_out /* return if access not permitted */ |
| 138 | #else |
| 139 | bnelr- |
| 140 | #endif |
| 141 | or r5,r0,r6 /* set accessed/dirty bits */ |
| 142 | stwcx. r5,0,r8 /* attempt to update PTE */ |
| 143 | bne- retry /* retry if someone got there first */ |
| 144 | |
| 145 | mfsrin r3,r4 /* get segment reg for segment */ |
| 146 | mfctr r0 |
| 147 | stw r0,_CTR(r11) |
| 148 | bl create_hpte /* add the hash table entry */ |
| 149 | |
| 150 | #ifdef CONFIG_SMP |
| 151 | eieio |
| 152 | addis r8,r7,mmu_hash_lock@ha |
| 153 | li r0,0 |
| 154 | stw r0,mmu_hash_lock@l(r8) |
| 155 | #endif |
| 156 | |
| 157 | /* Return from the exception */ |
| 158 | lwz r5,_CTR(r11) |
| 159 | mtctr r5 |
| 160 | lwz r0,GPR0(r11) |
| 161 | lwz r7,GPR7(r11) |
| 162 | lwz r8,GPR8(r11) |
| 163 | b fast_exception_return |
| 164 | |
| 165 | #ifdef CONFIG_SMP |
| 166 | hash_page_out: |
| 167 | eieio |
| 168 | addis r8,r7,mmu_hash_lock@ha |
| 169 | li r0,0 |
| 170 | stw r0,mmu_hash_lock@l(r8) |
| 171 | blr |
| 172 | #endif /* CONFIG_SMP */ |
| 173 | |
| 174 | /* |
| 175 | * Add an entry for a particular page to the hash table. |
| 176 | * |
| 177 | * add_hash_page(unsigned context, unsigned long va, unsigned long pmdval) |
| 178 | * |
| 179 | * We assume any necessary modifications to the pte (e.g. setting |
| 180 | * the accessed bit) have already been done and that there is actually |
| 181 | * a hash table in use (i.e. we're not on a 603). |
| 182 | */ |
| 183 | _GLOBAL(add_hash_page) |
| 184 | mflr r0 |
| 185 | stw r0,4(r1) |
| 186 | |
| 187 | /* Convert context and va to VSID */ |
| 188 | mulli r3,r3,897*16 /* multiply context by context skew */ |
| 189 | rlwinm r0,r4,4,28,31 /* get ESID (top 4 bits of va) */ |
| 190 | mulli r0,r0,0x111 /* multiply by ESID skew */ |
| 191 | add r3,r3,r0 /* note create_hpte trims to 24 bits */ |
| 192 | |
| 193 | #ifdef CONFIG_SMP |
Kumar Gala | f608600 | 2008-04-24 06:29:36 +1000 | [diff] [blame^] | 194 | rlwinm r8,r1,0,0,(31-THREAD_SHIFT) /* use cpu number to make tag */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 195 | lwz r8,TI_CPU(r8) /* to go in mmu_hash_lock */ |
| 196 | oris r8,r8,12 |
| 197 | #endif /* CONFIG_SMP */ |
| 198 | |
| 199 | /* |
| 200 | * We disable interrupts here, even on UP, because we don't |
| 201 | * want to race with hash_page, and because we want the |
| 202 | * _PAGE_HASHPTE bit to be a reliable indication of whether |
| 203 | * the HPTE exists (or at least whether one did once). |
| 204 | * We also turn off the MMU for data accesses so that we |
| 205 | * we can't take a hash table miss (assuming the code is |
| 206 | * covered by a BAT). -- paulus |
| 207 | */ |
| 208 | mfmsr r10 |
| 209 | SYNC |
| 210 | rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */ |
| 211 | rlwinm r0,r0,0,28,26 /* clear MSR_DR */ |
| 212 | mtmsr r0 |
| 213 | SYNC_601 |
| 214 | isync |
| 215 | |
| 216 | tophys(r7,0) |
| 217 | |
| 218 | #ifdef CONFIG_SMP |
| 219 | addis r9,r7,mmu_hash_lock@ha |
| 220 | addi r9,r9,mmu_hash_lock@l |
| 221 | 10: lwarx r0,0,r9 /* take the mmu_hash_lock */ |
| 222 | cmpi 0,r0,0 |
| 223 | bne- 11f |
| 224 | stwcx. r8,0,r9 |
| 225 | beq+ 12f |
| 226 | 11: lwz r0,0(r9) |
| 227 | cmpi 0,r0,0 |
| 228 | beq 10b |
| 229 | b 11b |
| 230 | 12: isync |
| 231 | #endif |
| 232 | |
| 233 | /* |
| 234 | * Fetch the linux pte and test and set _PAGE_HASHPTE atomically. |
| 235 | * If _PAGE_HASHPTE was already set, we don't replace the existing |
| 236 | * HPTE, so we just unlock and return. |
| 237 | */ |
| 238 | mr r8,r5 |
| 239 | rlwimi r8,r4,22,20,29 |
| 240 | 1: lwarx r6,0,r8 |
| 241 | andi. r0,r6,_PAGE_HASHPTE |
| 242 | bne 9f /* if HASHPTE already set, done */ |
| 243 | ori r5,r6,_PAGE_HASHPTE |
| 244 | stwcx. r5,0,r8 |
| 245 | bne- 1b |
| 246 | |
| 247 | bl create_hpte |
| 248 | |
| 249 | 9: |
| 250 | #ifdef CONFIG_SMP |
| 251 | eieio |
| 252 | li r0,0 |
| 253 | stw r0,0(r9) /* clear mmu_hash_lock */ |
| 254 | #endif |
| 255 | |
| 256 | /* reenable interrupts and DR */ |
| 257 | mtmsr r10 |
| 258 | SYNC_601 |
| 259 | isync |
| 260 | |
| 261 | lwz r0,4(r1) |
| 262 | mtlr r0 |
| 263 | blr |
| 264 | |
| 265 | /* |
| 266 | * This routine adds a hardware PTE to the hash table. |
| 267 | * It is designed to be called with the MMU either on or off. |
| 268 | * r3 contains the VSID, r4 contains the virtual address, |
| 269 | * r5 contains the linux PTE, r6 contains the old value of the |
| 270 | * linux PTE (before setting _PAGE_HASHPTE) and r7 contains the |
| 271 | * offset to be added to addresses (0 if the MMU is on, |
| 272 | * -KERNELBASE if it is off). |
| 273 | * On SMP, the caller should have the mmu_hash_lock held. |
| 274 | * We assume that the caller has (or will) set the _PAGE_HASHPTE |
| 275 | * bit in the linux PTE in memory. The value passed in r6 should |
| 276 | * be the old linux PTE value; if it doesn't have _PAGE_HASHPTE set |
| 277 | * this routine will skip the search for an existing HPTE. |
| 278 | * This procedure modifies r0, r3 - r6, r8, cr0. |
| 279 | * -- paulus. |
| 280 | * |
| 281 | * For speed, 4 of the instructions get patched once the size and |
| 282 | * physical address of the hash table are known. These definitions |
| 283 | * of Hash_base and Hash_bits below are just an example. |
| 284 | */ |
| 285 | Hash_base = 0xc0180000 |
| 286 | Hash_bits = 12 /* e.g. 256kB hash table */ |
| 287 | Hash_msk = (((1 << Hash_bits) - 1) * 64) |
| 288 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 289 | /* defines for the PTE format for 32-bit PPCs */ |
| 290 | #define PTE_SIZE 8 |
| 291 | #define PTEG_SIZE 64 |
| 292 | #define LG_PTEG_SIZE 6 |
| 293 | #define LDPTEu lwzu |
Benjamin Herrenschmidt | ee4f2ea | 2007-04-12 15:30:22 +1000 | [diff] [blame] | 294 | #define LDPTE lwz |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 295 | #define STPTE stw |
| 296 | #define CMPPTE cmpw |
| 297 | #define PTE_H 0x40 |
| 298 | #define PTE_V 0x80000000 |
| 299 | #define TST_V(r) rlwinm. r,r,0,0,0 |
| 300 | #define SET_V(r) oris r,r,PTE_V@h |
| 301 | #define CLR_V(r,t) rlwinm r,r,0,1,31 |
| 302 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 303 | #define HASH_LEFT 31-(LG_PTEG_SIZE+Hash_bits-1) |
| 304 | #define HASH_RIGHT 31-LG_PTEG_SIZE |
| 305 | |
| 306 | _GLOBAL(create_hpte) |
| 307 | /* Convert linux-style PTE (r5) to low word of PPC-style PTE (r8) */ |
| 308 | rlwinm r8,r5,32-10,31,31 /* _PAGE_RW -> PP lsb */ |
| 309 | rlwinm r0,r5,32-7,31,31 /* _PAGE_DIRTY -> PP lsb */ |
| 310 | and r8,r8,r0 /* writable if _RW & _DIRTY */ |
| 311 | rlwimi r5,r5,32-1,30,30 /* _PAGE_USER -> PP msb */ |
| 312 | rlwimi r5,r5,32-2,31,31 /* _PAGE_USER -> PP lsb */ |
| 313 | ori r8,r8,0xe14 /* clear out reserved bits and M */ |
| 314 | andc r8,r5,r8 /* PP = user? (rw&dirty? 2: 3): 0 */ |
| 315 | BEGIN_FTR_SECTION |
| 316 | ori r8,r8,_PAGE_COHERENT /* set M (coherence required) */ |
| 317 | END_FTR_SECTION_IFSET(CPU_FTR_NEED_COHERENT) |
| 318 | |
| 319 | /* Construct the high word of the PPC-style PTE (r5) */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 320 | rlwinm r5,r3,7,1,24 /* put VSID in 0x7fffff80 bits */ |
| 321 | rlwimi r5,r4,10,26,31 /* put in API (abbrev page index) */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 322 | SET_V(r5) /* set V (valid) bit */ |
| 323 | |
| 324 | /* Get the address of the primary PTE group in the hash table (r3) */ |
| 325 | _GLOBAL(hash_page_patch_A) |
| 326 | addis r0,r7,Hash_base@h /* base address of hash table */ |
| 327 | rlwimi r0,r3,LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* VSID -> hash */ |
| 328 | rlwinm r3,r4,20+LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* PI -> hash */ |
| 329 | xor r3,r3,r0 /* make primary hash */ |
| 330 | li r0,8 /* PTEs/group */ |
| 331 | |
| 332 | /* |
| 333 | * Test the _PAGE_HASHPTE bit in the old linux PTE, and skip the search |
| 334 | * if it is clear, meaning that the HPTE isn't there already... |
| 335 | */ |
| 336 | andi. r6,r6,_PAGE_HASHPTE |
| 337 | beq+ 10f /* no PTE: go look for an empty slot */ |
| 338 | tlbie r4 |
| 339 | |
| 340 | addis r4,r7,htab_hash_searches@ha |
| 341 | lwz r6,htab_hash_searches@l(r4) |
| 342 | addi r6,r6,1 /* count how many searches we do */ |
| 343 | stw r6,htab_hash_searches@l(r4) |
| 344 | |
| 345 | /* Search the primary PTEG for a PTE whose 1st (d)word matches r5 */ |
| 346 | mtctr r0 |
| 347 | addi r4,r3,-PTE_SIZE |
| 348 | 1: LDPTEu r6,PTE_SIZE(r4) /* get next PTE */ |
| 349 | CMPPTE 0,r6,r5 |
| 350 | bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */ |
| 351 | beq+ found_slot |
| 352 | |
| 353 | /* Search the secondary PTEG for a matching PTE */ |
| 354 | ori r5,r5,PTE_H /* set H (secondary hash) bit */ |
| 355 | _GLOBAL(hash_page_patch_B) |
| 356 | xoris r4,r3,Hash_msk>>16 /* compute secondary hash */ |
| 357 | xori r4,r4,(-PTEG_SIZE & 0xffff) |
| 358 | addi r4,r4,-PTE_SIZE |
| 359 | mtctr r0 |
| 360 | 2: LDPTEu r6,PTE_SIZE(r4) |
| 361 | CMPPTE 0,r6,r5 |
| 362 | bdnzf 2,2b |
| 363 | beq+ found_slot |
| 364 | xori r5,r5,PTE_H /* clear H bit again */ |
| 365 | |
| 366 | /* Search the primary PTEG for an empty slot */ |
| 367 | 10: mtctr r0 |
| 368 | addi r4,r3,-PTE_SIZE /* search primary PTEG */ |
| 369 | 1: LDPTEu r6,PTE_SIZE(r4) /* get next PTE */ |
| 370 | TST_V(r6) /* test valid bit */ |
| 371 | bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */ |
| 372 | beq+ found_empty |
| 373 | |
| 374 | /* update counter of times that the primary PTEG is full */ |
| 375 | addis r4,r7,primary_pteg_full@ha |
| 376 | lwz r6,primary_pteg_full@l(r4) |
| 377 | addi r6,r6,1 |
| 378 | stw r6,primary_pteg_full@l(r4) |
| 379 | |
| 380 | /* Search the secondary PTEG for an empty slot */ |
| 381 | ori r5,r5,PTE_H /* set H (secondary hash) bit */ |
| 382 | _GLOBAL(hash_page_patch_C) |
| 383 | xoris r4,r3,Hash_msk>>16 /* compute secondary hash */ |
| 384 | xori r4,r4,(-PTEG_SIZE & 0xffff) |
| 385 | addi r4,r4,-PTE_SIZE |
| 386 | mtctr r0 |
| 387 | 2: LDPTEu r6,PTE_SIZE(r4) |
| 388 | TST_V(r6) |
| 389 | bdnzf 2,2b |
| 390 | beq+ found_empty |
| 391 | xori r5,r5,PTE_H /* clear H bit again */ |
| 392 | |
| 393 | /* |
| 394 | * Choose an arbitrary slot in the primary PTEG to overwrite. |
| 395 | * Since both the primary and secondary PTEGs are full, and we |
| 396 | * have no information that the PTEs in the primary PTEG are |
| 397 | * more important or useful than those in the secondary PTEG, |
| 398 | * and we know there is a definite (although small) speed |
| 399 | * advantage to putting the PTE in the primary PTEG, we always |
| 400 | * put the PTE in the primary PTEG. |
Benjamin Herrenschmidt | ee4f2ea | 2007-04-12 15:30:22 +1000 | [diff] [blame] | 401 | * |
| 402 | * In addition, we skip any slot that is mapping kernel text in |
| 403 | * order to avoid a deadlock when not using BAT mappings if |
| 404 | * trying to hash in the kernel hash code itself after it has |
| 405 | * already taken the hash table lock. This works in conjunction |
| 406 | * with pre-faulting of the kernel text. |
| 407 | * |
| 408 | * If the hash table bucket is full of kernel text entries, we'll |
| 409 | * lockup here but that shouldn't happen |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 410 | */ |
Benjamin Herrenschmidt | ee4f2ea | 2007-04-12 15:30:22 +1000 | [diff] [blame] | 411 | |
| 412 | 1: addis r4,r7,next_slot@ha /* get next evict slot */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 413 | lwz r6,next_slot@l(r4) |
Benjamin Herrenschmidt | ee4f2ea | 2007-04-12 15:30:22 +1000 | [diff] [blame] | 414 | addi r6,r6,PTE_SIZE /* search for candidate */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 415 | andi. r6,r6,7*PTE_SIZE |
| 416 | stw r6,next_slot@l(r4) |
| 417 | add r4,r3,r6 |
Benjamin Herrenschmidt | ee4f2ea | 2007-04-12 15:30:22 +1000 | [diff] [blame] | 418 | LDPTE r0,PTE_SIZE/2(r4) /* get PTE second word */ |
| 419 | clrrwi r0,r0,12 |
| 420 | lis r6,etext@h |
| 421 | ori r6,r6,etext@l /* get etext */ |
| 422 | tophys(r6,r6) |
| 423 | cmpl cr0,r0,r6 /* compare and try again */ |
| 424 | blt 1b |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 425 | |
| 426 | #ifndef CONFIG_SMP |
| 427 | /* Store PTE in PTEG */ |
| 428 | found_empty: |
| 429 | STPTE r5,0(r4) |
| 430 | found_slot: |
| 431 | STPTE r8,PTE_SIZE/2(r4) |
| 432 | |
| 433 | #else /* CONFIG_SMP */ |
| 434 | /* |
| 435 | * Between the tlbie above and updating the hash table entry below, |
| 436 | * another CPU could read the hash table entry and put it in its TLB. |
| 437 | * There are 3 cases: |
| 438 | * 1. using an empty slot |
| 439 | * 2. updating an earlier entry to change permissions (i.e. enable write) |
| 440 | * 3. taking over the PTE for an unrelated address |
| 441 | * |
| 442 | * In each case it doesn't really matter if the other CPUs have the old |
| 443 | * PTE in their TLB. So we don't need to bother with another tlbie here, |
| 444 | * which is convenient as we've overwritten the register that had the |
| 445 | * address. :-) The tlbie above is mainly to make sure that this CPU comes |
| 446 | * and gets the new PTE from the hash table. |
| 447 | * |
| 448 | * We do however have to make sure that the PTE is never in an invalid |
| 449 | * state with the V bit set. |
| 450 | */ |
| 451 | found_empty: |
| 452 | found_slot: |
| 453 | CLR_V(r5,r0) /* clear V (valid) bit in PTE */ |
| 454 | STPTE r5,0(r4) |
| 455 | sync |
| 456 | TLBSYNC |
| 457 | STPTE r8,PTE_SIZE/2(r4) /* put in correct RPN, WIMG, PP bits */ |
| 458 | sync |
| 459 | SET_V(r5) |
| 460 | STPTE r5,0(r4) /* finally set V bit in PTE */ |
| 461 | #endif /* CONFIG_SMP */ |
| 462 | |
| 463 | sync /* make sure pte updates get to memory */ |
| 464 | blr |
| 465 | |
Kumar Gala | 991eb43 | 2007-05-14 17:11:58 -0500 | [diff] [blame] | 466 | .section .bss |
| 467 | .align 2 |
| 468 | next_slot: |
| 469 | .space 4 |
| 470 | primary_pteg_full: |
| 471 | .space 4 |
| 472 | htab_hash_searches: |
| 473 | .space 4 |
| 474 | .previous |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 475 | |
| 476 | /* |
| 477 | * Flush the entry for a particular page from the hash table. |
| 478 | * |
| 479 | * flush_hash_pages(unsigned context, unsigned long va, unsigned long pmdval, |
| 480 | * int count) |
| 481 | * |
| 482 | * We assume that there is a hash table in use (Hash != 0). |
| 483 | */ |
| 484 | _GLOBAL(flush_hash_pages) |
| 485 | tophys(r7,0) |
| 486 | |
| 487 | /* |
| 488 | * We disable interrupts here, even on UP, because we want |
| 489 | * the _PAGE_HASHPTE bit to be a reliable indication of |
| 490 | * whether the HPTE exists (or at least whether one did once). |
| 491 | * We also turn off the MMU for data accesses so that we |
| 492 | * we can't take a hash table miss (assuming the code is |
| 493 | * covered by a BAT). -- paulus |
| 494 | */ |
| 495 | mfmsr r10 |
| 496 | SYNC |
| 497 | rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */ |
| 498 | rlwinm r0,r0,0,28,26 /* clear MSR_DR */ |
| 499 | mtmsr r0 |
| 500 | SYNC_601 |
| 501 | isync |
| 502 | |
| 503 | /* First find a PTE in the range that has _PAGE_HASHPTE set */ |
| 504 | rlwimi r5,r4,22,20,29 |
| 505 | 1: lwz r0,0(r5) |
| 506 | cmpwi cr1,r6,1 |
| 507 | andi. r0,r0,_PAGE_HASHPTE |
| 508 | bne 2f |
| 509 | ble cr1,19f |
| 510 | addi r4,r4,0x1000 |
| 511 | addi r5,r5,4 |
| 512 | addi r6,r6,-1 |
| 513 | b 1b |
| 514 | |
| 515 | /* Convert context and va to VSID */ |
| 516 | 2: mulli r3,r3,897*16 /* multiply context by context skew */ |
| 517 | rlwinm r0,r4,4,28,31 /* get ESID (top 4 bits of va) */ |
| 518 | mulli r0,r0,0x111 /* multiply by ESID skew */ |
| 519 | add r3,r3,r0 /* note code below trims to 24 bits */ |
| 520 | |
| 521 | /* Construct the high word of the PPC-style PTE (r11) */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 522 | rlwinm r11,r3,7,1,24 /* put VSID in 0x7fffff80 bits */ |
| 523 | rlwimi r11,r4,10,26,31 /* put in API (abbrev page index) */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 524 | SET_V(r11) /* set V (valid) bit */ |
| 525 | |
| 526 | #ifdef CONFIG_SMP |
| 527 | addis r9,r7,mmu_hash_lock@ha |
| 528 | addi r9,r9,mmu_hash_lock@l |
Kumar Gala | f608600 | 2008-04-24 06:29:36 +1000 | [diff] [blame^] | 529 | rlwinm r8,r1,0,0,(31-THREAD_SHIFT) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 530 | add r8,r8,r7 |
| 531 | lwz r8,TI_CPU(r8) |
| 532 | oris r8,r8,9 |
| 533 | 10: lwarx r0,0,r9 |
| 534 | cmpi 0,r0,0 |
| 535 | bne- 11f |
| 536 | stwcx. r8,0,r9 |
| 537 | beq+ 12f |
| 538 | 11: lwz r0,0(r9) |
| 539 | cmpi 0,r0,0 |
| 540 | beq 10b |
| 541 | b 11b |
| 542 | 12: isync |
| 543 | #endif |
| 544 | |
| 545 | /* |
| 546 | * Check the _PAGE_HASHPTE bit in the linux PTE. If it is |
| 547 | * already clear, we're done (for this pte). If not, |
| 548 | * clear it (atomically) and proceed. -- paulus. |
| 549 | */ |
| 550 | 33: lwarx r8,0,r5 /* fetch the pte */ |
| 551 | andi. r0,r8,_PAGE_HASHPTE |
| 552 | beq 8f /* done if HASHPTE is already clear */ |
| 553 | rlwinm r8,r8,0,31,29 /* clear HASHPTE bit */ |
| 554 | stwcx. r8,0,r5 /* update the pte */ |
| 555 | bne- 33b |
| 556 | |
| 557 | /* Get the address of the primary PTE group in the hash table (r3) */ |
| 558 | _GLOBAL(flush_hash_patch_A) |
| 559 | addis r8,r7,Hash_base@h /* base address of hash table */ |
| 560 | rlwimi r8,r3,LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* VSID -> hash */ |
| 561 | rlwinm r0,r4,20+LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* PI -> hash */ |
| 562 | xor r8,r0,r8 /* make primary hash */ |
| 563 | |
| 564 | /* Search the primary PTEG for a PTE whose 1st (d)word matches r5 */ |
| 565 | li r0,8 /* PTEs/group */ |
| 566 | mtctr r0 |
| 567 | addi r12,r8,-PTE_SIZE |
| 568 | 1: LDPTEu r0,PTE_SIZE(r12) /* get next PTE */ |
| 569 | CMPPTE 0,r0,r11 |
| 570 | bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */ |
| 571 | beq+ 3f |
| 572 | |
| 573 | /* Search the secondary PTEG for a matching PTE */ |
| 574 | ori r11,r11,PTE_H /* set H (secondary hash) bit */ |
| 575 | li r0,8 /* PTEs/group */ |
| 576 | _GLOBAL(flush_hash_patch_B) |
| 577 | xoris r12,r8,Hash_msk>>16 /* compute secondary hash */ |
| 578 | xori r12,r12,(-PTEG_SIZE & 0xffff) |
| 579 | addi r12,r12,-PTE_SIZE |
| 580 | mtctr r0 |
| 581 | 2: LDPTEu r0,PTE_SIZE(r12) |
| 582 | CMPPTE 0,r0,r11 |
| 583 | bdnzf 2,2b |
| 584 | xori r11,r11,PTE_H /* clear H again */ |
| 585 | bne- 4f /* should rarely fail to find it */ |
| 586 | |
| 587 | 3: li r0,0 |
| 588 | STPTE r0,0(r12) /* invalidate entry */ |
| 589 | 4: sync |
| 590 | tlbie r4 /* in hw tlb too */ |
| 591 | sync |
| 592 | |
| 593 | 8: ble cr1,9f /* if all ptes checked */ |
| 594 | 81: addi r6,r6,-1 |
| 595 | addi r5,r5,4 /* advance to next pte */ |
| 596 | addi r4,r4,0x1000 |
| 597 | lwz r0,0(r5) /* check next pte */ |
| 598 | cmpwi cr1,r6,1 |
| 599 | andi. r0,r0,_PAGE_HASHPTE |
| 600 | bne 33b |
| 601 | bgt cr1,81b |
| 602 | |
| 603 | 9: |
| 604 | #ifdef CONFIG_SMP |
| 605 | TLBSYNC |
| 606 | li r0,0 |
| 607 | stw r0,0(r9) /* clear mmu_hash_lock */ |
| 608 | #endif |
| 609 | |
| 610 | 19: mtmsr r10 |
| 611 | SYNC_601 |
| 612 | isync |
| 613 | blr |