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