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