Fix last change.
[linux-flexiantxendom0-3.2.10.git] / arch / ppc64 / mm / hash_low.S
1 /*
2  * ppc64 MMU hashtable management routines
3  *
4  * (c) Copyright IBM Corp. 2003
5  *
6  * Maintained by: Benjamin Herrenschmidt
7  *                <benh@kernel.crashing.org>
8  *
9  * This file is covered by the GNU Public Licence v2 as
10  * described in the kernel's COPYING file.
11  */
12
13 #include <linux/config.h>
14 #include <asm/processor.h>
15 #include <asm/pgtable.h>
16 #include <asm/mmu.h>
17 #include <asm/page.h>
18 #include <asm/types.h>
19 #include <asm/ppc_asm.h>
20 #include <asm/offsets.h>
21 #include <asm/cputable.h>
22
23         .text
24
25 /*
26  * Stackframe:
27  *              
28  *         +-> Back chain                       (SP + 256)
29  *         |   General register save area       (SP + 112)
30  *         |   Parameter save area              (SP + 48)
31  *         |   TOC save area                    (SP + 40)
32  *         |   link editor doubleword           (SP + 32)
33  *         |   compiler doubleword              (SP + 24)
34  *         |   LR save area                     (SP + 16)
35  *         |   CR save area                     (SP + 8)
36  * SP ---> +-- Back chain                       (SP + 0)
37  */
38 #define STACKFRAMESIZE  256
39
40 /* Save parameters offsets */
41 #define STK_PARM(i)     (STACKFRAMESIZE + 48 + ((i)-3)*8)
42
43 /* Save non-volatile offsets */
44 #define STK_REG(i)      (112 + ((i)-14)*8)
45
46 /*
47  * _hash_page(unsigned long ea, unsigned long access, unsigned long vsid,
48  *              pte_t *ptep, unsigned long trap, int local)
49  *
50  * Adds a page to the hash table. This is the non-LPAR version for now
51  */
52
53 _GLOBAL(__hash_page)
54         mflr    r0
55         std     r0,16(r1)
56         stdu    r1,-STACKFRAMESIZE(r1)
57         /* Save all params that we need after a function call */
58         std     r6,STK_PARM(r6)(r1)
59         std     r8,STK_PARM(r8)(r1)
60         
61         /* Add _PAGE_PRESENT to access */
62         ori     r4,r4,_PAGE_PRESENT
63
64         /* Save non-volatile registers.
65          * r31 will hold "old PTE"
66          * r30 is "new PTE"
67          * r29 is "va"
68          * r28 is a hash value
69          * r27 is hashtab mask (maybe dynamic patched instead ?)
70          */
71         std     r27,STK_REG(r27)(r1)
72         std     r28,STK_REG(r28)(r1)
73         std     r29,STK_REG(r29)(r1)
74         std     r30,STK_REG(r30)(r1)
75         std     r31,STK_REG(r31)(r1)
76         
77         /* Step 1:
78          *
79          * Check permissions, atomically mark the linux PTE busy
80          * and hashed.
81          */ 
82 1:
83         ldarx   r31,0,r6
84         /* Check access rights (access & ~(pte_val(*ptep))) */
85         andc.   r0,r4,r31
86         bne-    htab_wrong_access
87         /* Check if PTE is busy */
88         andi.   r0,r31,_PAGE_BUSY
89         bne-    1b
90         /* Prepare new PTE value (turn access RW into DIRTY, then
91          * add BUSY,HASHPTE and ACCESSED)
92          */
93         rlwinm  r30,r4,5,24,24  /* _PAGE_RW -> _PAGE_DIRTY */
94         or      r30,r30,r31
95         ori     r30,r30,_PAGE_BUSY | _PAGE_ACCESSED | _PAGE_HASHPTE
96         /* Write the linux PTE atomically (setting busy) */
97         stdcx.  r30,0,r6
98         bne-    1b
99         
100
101         /* Step 2:
102          *
103          * Insert/Update the HPTE in the hash table. At this point,
104          * r4 (access) is re-useable, we use it for the new HPTE flags
105          */
106
107         /* Calc va and put it in r29 */
108         rldicr  r29,r5,28,63-28
109         rldicl  r3,r3,0,36
110         or      r29,r3,r29
111
112         /* Calculate hash value for primary slot and store it in r28 */
113         rldicl  r5,r5,0,25              /* vsid & 0x0000007fffffffff */
114         rldicl  r0,r3,64-12,48          /* (ea >> 12) & 0xffff */
115         xor     r28,r5,r0
116         
117         /* Convert linux PTE bits into HW equivalents
118          */
119         andi.   r3,r30,0x1fa            /* Get basic set of flags */
120         rlwinm  r0,r30,32-2+1,30,30     /* _PAGE_RW -> _PAGE_USER (r0) */
121         rlwinm  r4,r30,32-7+1,30,30     /* _PAGE_DIRTY -> _PAGE_USER (r4) */
122         and     r0,r0,r4                /* _PAGE_RW & _PAGE_DIRTY -> r0 bit 30 */
123         andc    r0,r30,r0               /* r0 = pte & ~r0 */
124         rlwimi  r3,r0,32-1,31,31        /* Insert result into PP lsb */
125
126         /* We eventually do the icache sync here (maybe inline that
127          * code rather than call a C function...) 
128          */
129 BEGIN_FTR_SECTION
130         mr      r4,r30
131         mr      r5,r7
132         bl      .hash_page_do_lazy_icache
133 END_FTR_SECTION_IFSET(CPU_FTR_NOEXECUTE)
134
135         /* At this point, r3 contains new PP bits, save them in
136          * place of "access" in the param area (sic)
137          */
138         std     r3,STK_PARM(r4)(r1)
139
140         /* Get htab_hash_mask */
141         ld      r4,htab_data@got(2)
142         ld      r27,16(r4)      /* htab_data.htab_hash_mask -> r27 */
143
144         /* Check if we may already be in the hashtable, in this case, we
145          * go to out-of-line code to try to modify the HPTE
146          */
147         andi.   r0,r31,_PAGE_HASHPTE
148         bne     htab_modify_pte
149
150 htab_insert_pte:
151         /* Clear hpte bits in new pte (we also clear BUSY btw) and
152          * add _PAGE_HASHPTE
153          */
154         lis     r0,_PAGE_HPTEFLAGS@h
155         ori     r0,r0,_PAGE_HPTEFLAGS@l
156         andc    r30,r30,r0
157         ori     r30,r30,_PAGE_HASHPTE
158
159 1:
160         /* page number in r5 */
161         rldicl  r5,r31,64-PTE_SHIFT,PTE_SHIFT
162
163         /* Calculate primary group hash */
164         and     r0,r28,r27
165         rldicr  r3,r0,3,63-3    /* r0 = (hash & mask) << 3 */
166
167         /* Call ppc_md.hpte_insert */
168         ld      r7,STK_PARM(r4)(r1)     /* Retreive new pp bits */
169         mr      r4,r29                  /* Retreive va */
170         li      r6,0                    /* primary slot *
171         li      r8,0                    /* not bolted and not large */
172         li      r9,0
173 _GLOBAL(htab_call_hpte_insert1)
174         bl      .                       /* Will be patched by htab_finish_init() */
175         cmpi    0,r3,0
176         bge     htab_pte_insert_ok      /* Insertion successful */
177         cmpi    0,r3,-2                 /* Critical failure */
178         beq-    htab_pte_insert_failure
179
180         /* Now try secondary slot */
181         ori     r30,r30,_PAGE_SECONDARY
182         
183         /* page number in r5 */
184         rldicl  r5,r31,64-PTE_SHIFT,PTE_SHIFT
185
186         /* Calculate secondary group hash */
187         not     r3,r28
188         and     r0,r3,r27
189         rldicr  r3,r0,3,63-3    /* r0 = (~hash & mask) << 3 */
190         
191         /* Call ppc_md.hpte_insert */
192         ld      r7,STK_PARM(r4)(r1)     /* Retreive new pp bits */
193         mr      r4,r29                  /* Retreive va */
194         li      r6,1                    /* secondary slot *
195         li      r8,0                    /* not bolted and not large */
196         li      r9,0
197 _GLOBAL(htab_call_hpte_insert2)
198         bl      .                       /* Will be patched by htab_finish_init() */
199         cmpi    0,r3,0
200         bge+    htab_pte_insert_ok      /* Insertion successful */
201         cmpi    0,r3,-2                 /* Critical failure */
202         beq-    htab_pte_insert_failure
203
204         /* Both are full, we need to evict something */
205         mftb    r0
206         /* Pick a random group based on TB */
207         andi.   r0,r0,1
208         mr      r5,r28
209         bne     2f
210         not     r5,r5
211 2:      and     r0,r5,r27
212         rldicr  r3,r0,3,63-3    /* r0 = (hash & mask) << 3 */   
213         /* Call ppc_md.hpte_remove */
214 _GLOBAL(htab_call_hpte_remove)
215         bl      .                       /* Will be patched by htab_finish_init() */
216
217         /* Try all again */
218         b       1b      
219
220 htab_pte_insert_ok:
221         /* Insert slot number in PTE */
222         rldimi  r30,r3,12,63-14
223                 
224         /* Write out the PTE with a normal write
225          * (maybe add eieio may be good still ?)
226          */
227 htab_write_out_pte:
228         ld      r6,STK_PARM(r6)(r1)
229         std     r30,0(r6)
230         li      r3, 0
231 bail:
232         ld      r27,STK_REG(r27)(r1)
233         ld      r28,STK_REG(r28)(r1)
234         ld      r29,STK_REG(r29)(r1)
235         ld      r30,STK_REG(r30)(r1)
236         ld      r31,STK_REG(r31)(r1)
237         addi    r1,r1,STACKFRAMESIZE
238         ld      r0,16(r1)
239         mtlr    r0
240         blr
241
242 htab_modify_pte:
243         /* Keep PP bits in r4 and slot idx from the PTE around in r3 */
244         mr      r4,r3
245         rlwinm  r3,r31,32-12,29,31
246
247         /* Secondary group ? if yes, get a inverted hash value */
248         mr      r5,r28
249         andi.   r0,r31,_PAGE_SECONDARY
250         beq     1f
251         not     r5,r5
252 1:
253         /* Calculate proper slot value for ppc_md.hpte_updatepp */
254         and     r0,r5,r27
255         rldicr  r0,r0,3,63-3    /* r0 = (hash & mask) << 3 */
256         add     r3,r0,r3        /* add slot idx */
257
258         /* Call ppc_md.hpte_updatepp */
259         mr      r5,r29                  /* va */
260         li      r6,0                    /* large is 0 */
261         ld      r7,STK_PARM(r8)(r1)     /* get "local" param */
262 _GLOBAL(htab_call_hpte_updatepp)
263         bl      .                       /* Will be patched by htab_finish_init() */
264
265         /* if we failed because typically the HPTE wasn't really here
266          * we try an insertion. 
267          */
268         cmpi    0,r3,-1
269         beq-    htab_insert_pte
270
271         /* Clear the BUSY bit and Write out the PTE */
272         li      r0,_PAGE_BUSY
273         andc    r30,r30,r0
274         b       htab_write_out_pte
275
276 htab_wrong_access:
277         /* Bail out clearing reservation */
278         stdcx.  r31,0,r6
279         li      r3,1
280         b       bail
281
282 htab_pte_insert_failure:
283         b       .htab_insert_failure
284
285