Merge branch 'fixes' of git://ftp.arm.linux.org.uk/~rmk/linux-arm
[cascardo/linux.git] / arch / powerpc / mm / tlb_low_64e.S
1 /*
2  *  Low level TLB miss handlers for Book3E
3  *
4  *  Copyright (C) 2008-2009
5  *      Ben. Herrenschmidt (benh@kernel.crashing.org), IBM Corp.
6  *
7  *  This program is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU General Public License
9  *  as published by the Free Software Foundation; either version
10  *  2 of the License, or (at your option) any later version.
11  */
12
13 #include <asm/processor.h>
14 #include <asm/reg.h>
15 #include <asm/page.h>
16 #include <asm/mmu.h>
17 #include <asm/ppc_asm.h>
18 #include <asm/asm-offsets.h>
19 #include <asm/cputable.h>
20 #include <asm/pgtable.h>
21 #include <asm/exception-64e.h>
22 #include <asm/ppc-opcode.h>
23 #include <asm/kvm_asm.h>
24 #include <asm/kvm_booke_hv_asm.h>
25
26 #ifdef CONFIG_PPC_64K_PAGES
27 #define VPTE_PMD_SHIFT  (PTE_INDEX_SIZE+1)
28 #else
29 #define VPTE_PMD_SHIFT  (PTE_INDEX_SIZE)
30 #endif
31 #define VPTE_PUD_SHIFT  (VPTE_PMD_SHIFT + PMD_INDEX_SIZE)
32 #define VPTE_PGD_SHIFT  (VPTE_PUD_SHIFT + PUD_INDEX_SIZE)
33 #define VPTE_INDEX_SIZE (VPTE_PGD_SHIFT + PGD_INDEX_SIZE)
34
35 /**********************************************************************
36  *                                                                    *
37  * TLB miss handling for Book3E with a bolted linear mapping          *
38  * No virtual page table, no nested TLB misses                        *
39  *                                                                    *
40  **********************************************************************/
41
42 /*
43  * Note that, unlike non-bolted handlers, TLB_EXFRAME is not
44  * modified by the TLB miss handlers themselves, since the TLB miss
45  * handler code will not itself cause a recursive TLB miss.
46  *
47  * TLB_EXFRAME will be modified when crit/mc/debug exceptions are
48  * entered/exited.
49  */
50 .macro tlb_prolog_bolted intnum addr
51         mtspr   SPRN_SPRG_GEN_SCRATCH,r12
52         mfspr   r12,SPRN_SPRG_TLB_EXFRAME
53         std     r13,EX_TLB_R13(r12)
54         std     r10,EX_TLB_R10(r12)
55         mfspr   r13,SPRN_SPRG_PACA
56
57         mfcr    r10
58         std     r11,EX_TLB_R11(r12)
59 #ifdef CONFIG_KVM_BOOKE_HV
60 BEGIN_FTR_SECTION
61         mfspr   r11, SPRN_SRR1
62 END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
63 #endif
64         DO_KVM  \intnum, SPRN_SRR1
65         std     r16,EX_TLB_R16(r12)
66         mfspr   r16,\addr               /* get faulting address */
67         std     r14,EX_TLB_R14(r12)
68         ld      r14,PACAPGD(r13)
69         std     r15,EX_TLB_R15(r12)
70         std     r10,EX_TLB_CR(r12)
71         TLB_MISS_PROLOG_STATS
72 .endm
73
74 .macro tlb_epilog_bolted
75         ld      r14,EX_TLB_CR(r12)
76         ld      r10,EX_TLB_R10(r12)
77         ld      r11,EX_TLB_R11(r12)
78         ld      r13,EX_TLB_R13(r12)
79         mtcr    r14
80         ld      r14,EX_TLB_R14(r12)
81         ld      r15,EX_TLB_R15(r12)
82         TLB_MISS_RESTORE_STATS
83         ld      r16,EX_TLB_R16(r12)
84         mfspr   r12,SPRN_SPRG_GEN_SCRATCH
85 .endm
86
87 /* Data TLB miss */
88         START_EXCEPTION(data_tlb_miss_bolted)
89         tlb_prolog_bolted BOOKE_INTERRUPT_DTLB_MISS SPRN_DEAR
90
91         /* We need _PAGE_PRESENT and  _PAGE_ACCESSED set */
92
93         /* We do the user/kernel test for the PID here along with the RW test
94          */
95         /* We pre-test some combination of permissions to avoid double
96          * faults:
97          *
98          * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE
99          * ESR_ST   is 0x00800000
100          * _PAGE_BAP_SW is 0x00000010
101          * So the shift is >> 19. This tests for supervisor writeability.
102          * If the page happens to be supervisor writeable and not user
103          * writeable, we will take a new fault later, but that should be
104          * a rare enough case.
105          *
106          * We also move ESR_ST in _PAGE_DIRTY position
107          * _PAGE_DIRTY is 0x00001000 so the shift is >> 11
108          *
109          * MAS1 is preset for all we need except for TID that needs to
110          * be cleared for kernel translations
111          */
112
113         mfspr   r11,SPRN_ESR
114
115         srdi    r15,r16,60              /* get region */
116         rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
117         bne-    dtlb_miss_fault_bolted  /* Bail if fault addr is invalid */
118
119         rlwinm  r10,r11,32-19,27,27
120         rlwimi  r10,r11,32-16,19,19
121         cmpwi   r15,0                   /* user vs kernel check */
122         ori     r10,r10,_PAGE_PRESENT
123         oris    r11,r10,_PAGE_ACCESSED@h
124
125         TLB_MISS_STATS_SAVE_INFO_BOLTED
126         bne     tlb_miss_kernel_bolted
127
128 tlb_miss_common_bolted:
129 /*
130  * This is the guts of the TLB miss handler for bolted-linear.
131  * We are entered with:
132  *
133  * r16 = faulting address
134  * r15 = crap (free to use)
135  * r14 = page table base
136  * r13 = PACA
137  * r11 = PTE permission mask
138  * r10 = crap (free to use)
139  */
140         rldicl  r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3
141         cmpldi  cr0,r14,0
142         clrrdi  r15,r15,3
143         beq     tlb_miss_fault_bolted   /* No PGDIR, bail */
144
145 BEGIN_MMU_FTR_SECTION
146         /* Set the TLB reservation and search for existing entry. Then load
147          * the entry.
148          */
149         PPC_TLBSRX_DOT(0,R16)
150         ldx     r14,r14,r15             /* grab pgd entry */
151         beq     tlb_miss_done_bolted    /* tlb exists already, bail */
152 MMU_FTR_SECTION_ELSE
153         ldx     r14,r14,r15             /* grab pgd entry */
154 ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_USE_TLBRSRV)
155
156 #ifndef CONFIG_PPC_64K_PAGES
157         rldicl  r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3
158         clrrdi  r15,r15,3
159         cmpdi   cr0,r14,0
160         bge     tlb_miss_fault_bolted   /* Bad pgd entry or hugepage; bail */
161         ldx     r14,r14,r15             /* grab pud entry */
162 #endif /* CONFIG_PPC_64K_PAGES */
163
164         rldicl  r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3
165         clrrdi  r15,r15,3
166         cmpdi   cr0,r14,0
167         bge     tlb_miss_fault_bolted
168         ldx     r14,r14,r15             /* Grab pmd entry */
169
170         rldicl  r15,r16,64-PAGE_SHIFT+3,64-PTE_INDEX_SIZE-3
171         clrrdi  r15,r15,3
172         cmpdi   cr0,r14,0
173         bge     tlb_miss_fault_bolted
174         ldx     r14,r14,r15             /* Grab PTE, normal (!huge) page */
175
176         /* Check if required permissions are met */
177         andc.   r15,r11,r14
178         rldicr  r15,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
179         bne-    tlb_miss_fault_bolted
180
181         /* Now we build the MAS:
182          *
183          * MAS 0   :    Fully setup with defaults in MAS4 and TLBnCFG
184          * MAS 1   :    Almost fully setup
185          *               - PID already updated by caller if necessary
186          *               - TSIZE need change if !base page size, not
187          *                 yet implemented for now
188          * MAS 2   :    Defaults not useful, need to be redone
189          * MAS 3+7 :    Needs to be done
190          */
191         clrrdi  r11,r16,12              /* Clear low crap in EA */
192         clrldi  r15,r15,12              /* Clear crap at the top */
193         rlwimi  r11,r14,32-19,27,31     /* Insert WIMGE */
194         rlwimi  r15,r14,32-8,22,25      /* Move in U bits */
195         mtspr   SPRN_MAS2,r11
196         andi.   r11,r14,_PAGE_DIRTY
197         rlwimi  r15,r14,32-2,26,31      /* Move in BAP bits */
198
199         /* Mask out SW and UW if !DIRTY (XXX optimize this !) */
200         bne     1f
201         li      r11,MAS3_SW|MAS3_UW
202         andc    r15,r15,r11
203 1:
204         mtspr   SPRN_MAS7_MAS3,r15
205         tlbwe
206
207 tlb_miss_done_bolted:
208         TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
209         tlb_epilog_bolted
210         rfi
211
212 itlb_miss_kernel_bolted:
213         li      r11,_PAGE_PRESENT|_PAGE_BAP_SX  /* Base perm */
214         oris    r11,r11,_PAGE_ACCESSED@h
215 tlb_miss_kernel_bolted:
216         mfspr   r10,SPRN_MAS1
217         ld      r14,PACA_KERNELPGD(r13)
218         cmpldi  cr0,r15,8               /* Check for vmalloc region */
219         rlwinm  r10,r10,0,16,1          /* Clear TID */
220         mtspr   SPRN_MAS1,r10
221         beq+    tlb_miss_common_bolted
222
223 tlb_miss_fault_bolted:
224         /* We need to check if it was an instruction miss */
225         andi.   r10,r11,_PAGE_EXEC|_PAGE_BAP_SX
226         bne     itlb_miss_fault_bolted
227 dtlb_miss_fault_bolted:
228         TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
229         tlb_epilog_bolted
230         b       exc_data_storage_book3e
231 itlb_miss_fault_bolted:
232         TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
233         tlb_epilog_bolted
234         b       exc_instruction_storage_book3e
235
236 /* Instruction TLB miss */
237         START_EXCEPTION(instruction_tlb_miss_bolted)
238         tlb_prolog_bolted BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR0
239
240         rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
241         srdi    r15,r16,60              /* get region */
242         TLB_MISS_STATS_SAVE_INFO_BOLTED
243         bne-    itlb_miss_fault_bolted
244
245         li      r11,_PAGE_PRESENT|_PAGE_EXEC    /* Base perm */
246
247         /* We do the user/kernel test for the PID here along with the RW test
248          */
249
250         cmpldi  cr0,r15,0                       /* Check for user region */
251         oris    r11,r11,_PAGE_ACCESSED@h
252         beq     tlb_miss_common_bolted
253         b       itlb_miss_kernel_bolted
254
255 #ifdef CONFIG_PPC_FSL_BOOK3E
256 /*
257  * TLB miss handling for e6500 and derivatives, using hardware tablewalk.
258  *
259  * Linear mapping is bolted: no virtual page table or nested TLB misses
260  * Indirect entries in TLB1, hardware loads resulting direct entries
261  *    into TLB0
262  * No HES or NV hint on TLB1, so we need to do software round-robin
263  * No tlbsrx. so we need a spinlock, and we have to deal
264  *    with MAS-damage caused by tlbsx
265  * 4K pages only
266  */
267
268         START_EXCEPTION(instruction_tlb_miss_e6500)
269         tlb_prolog_bolted BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR0
270
271         ld      r11,PACA_TCD_PTR(r13)
272         srdi.   r15,r16,60              /* get region */
273         ori     r16,r16,1
274
275         TLB_MISS_STATS_SAVE_INFO_BOLTED
276         bne     tlb_miss_kernel_e6500   /* user/kernel test */
277
278         b       tlb_miss_common_e6500
279
280         START_EXCEPTION(data_tlb_miss_e6500)
281         tlb_prolog_bolted BOOKE_INTERRUPT_DTLB_MISS SPRN_DEAR
282
283         ld      r11,PACA_TCD_PTR(r13)
284         srdi.   r15,r16,60              /* get region */
285         rldicr  r16,r16,0,62
286
287         TLB_MISS_STATS_SAVE_INFO_BOLTED
288         bne     tlb_miss_kernel_e6500   /* user vs kernel check */
289
290 /*
291  * This is the guts of the TLB miss handler for e6500 and derivatives.
292  * We are entered with:
293  *
294  * r16 = page of faulting address (low bit 0 if data, 1 if instruction)
295  * r15 = crap (free to use)
296  * r14 = page table base
297  * r13 = PACA
298  * r11 = tlb_per_core ptr
299  * r10 = cpu number
300  */
301 tlb_miss_common_e6500:
302         /*
303          * Search if we already have an indirect entry for that virtual
304          * address, and if we do, bail out.
305          *
306          * MAS6:IND should be already set based on MAS4
307          */
308 1:      lbarx   r15,0,r11
309         lhz     r10,PACAPACAINDEX(r13)
310         cmpdi   r15,0
311         cmpdi   cr1,r15,1       /* set cr1.eq = 0 for non-recursive */
312         bne     2f
313         stbcx.  r10,0,r11
314         bne     1b
315 3:
316         .subsection 1
317 2:      cmpd    cr1,r15,r10     /* recursive lock due to mcheck/crit/etc? */
318         beq     cr1,3b          /* unlock will happen if cr1.eq = 0 */
319         lbz     r15,0(r11)
320         cmpdi   r15,0
321         bne     2b
322         b       1b
323         .previous
324
325         mfspr   r15,SPRN_MAS2
326
327         tlbsx   0,r16
328         mfspr   r10,SPRN_MAS1
329         andis.  r10,r10,MAS1_VALID@h
330         bne     tlb_miss_done_e6500
331
332         /* Undo MAS-damage from the tlbsx */
333         mfspr   r10,SPRN_MAS1
334         oris    r10,r10,MAS1_VALID@h
335         mtspr   SPRN_MAS1,r10
336         mtspr   SPRN_MAS2,r15
337
338         /* Now, we need to walk the page tables. First check if we are in
339          * range.
340          */
341         rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
342         bne-    tlb_miss_fault_e6500
343
344         rldicl  r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3
345         cmpldi  cr0,r14,0
346         clrrdi  r15,r15,3
347         beq-    tlb_miss_fault_e6500 /* No PGDIR, bail */
348         ldx     r14,r14,r15             /* grab pgd entry */
349
350         rldicl  r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3
351         clrrdi  r15,r15,3
352         cmpdi   cr0,r14,0
353         bge     tlb_miss_fault_e6500    /* Bad pgd entry or hugepage; bail */
354         ldx     r14,r14,r15             /* grab pud entry */
355
356         rldicl  r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3
357         clrrdi  r15,r15,3
358         cmpdi   cr0,r14,0
359         bge     tlb_miss_fault_e6500
360         ldx     r14,r14,r15             /* Grab pmd entry */
361
362         mfspr   r10,SPRN_MAS0
363         cmpdi   cr0,r14,0
364         bge     tlb_miss_fault_e6500
365
366         /* Now we build the MAS for a 2M indirect page:
367          *
368          * MAS 0   :    ESEL needs to be filled by software round-robin
369          * MAS 1   :    Fully set up
370          *               - PID already updated by caller if necessary
371          *               - TSIZE for now is base ind page size always
372          *               - TID already cleared if necessary
373          * MAS 2   :    Default not 2M-aligned, need to be redone
374          * MAS 3+7 :    Needs to be done
375          */
376
377         ori     r14,r14,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT)
378         mtspr   SPRN_MAS7_MAS3,r14
379
380         clrrdi  r15,r16,21              /* make EA 2M-aligned */
381         mtspr   SPRN_MAS2,r15
382
383         lbz     r15,TCD_ESEL_NEXT(r11)
384         lbz     r16,TCD_ESEL_MAX(r11)
385         lbz     r14,TCD_ESEL_FIRST(r11)
386         rlwimi  r10,r15,16,0x00ff0000   /* insert esel_next into MAS0 */
387         addi    r15,r15,1               /* increment esel_next */
388         mtspr   SPRN_MAS0,r10
389         cmpw    r15,r16
390         iseleq  r15,r14,r15             /* if next == last use first */
391         stb     r15,TCD_ESEL_NEXT(r11)
392
393         tlbwe
394
395 tlb_miss_done_e6500:
396         .macro  tlb_unlock_e6500
397         beq     cr1,1f          /* no unlock if lock was recursively grabbed */
398         li      r15,0
399         isync
400         stb     r15,0(r11)
401 1:
402         .endm
403
404         tlb_unlock_e6500
405         TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
406         tlb_epilog_bolted
407         rfi
408
409 tlb_miss_kernel_e6500:
410         mfspr   r10,SPRN_MAS1
411         ld      r14,PACA_KERNELPGD(r13)
412         cmpldi  cr0,r15,8               /* Check for vmalloc region */
413         rlwinm  r10,r10,0,16,1          /* Clear TID */
414         mtspr   SPRN_MAS1,r10
415         beq+    tlb_miss_common_e6500
416
417 tlb_miss_fault_e6500:
418         tlb_unlock_e6500
419         /* We need to check if it was an instruction miss */
420         andi.   r16,r16,1
421         bne     itlb_miss_fault_e6500
422 dtlb_miss_fault_e6500:
423         TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
424         tlb_epilog_bolted
425         b       exc_data_storage_book3e
426 itlb_miss_fault_e6500:
427         TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
428         tlb_epilog_bolted
429         b       exc_instruction_storage_book3e
430 #endif /* CONFIG_PPC_FSL_BOOK3E */
431
432 /**********************************************************************
433  *                                                                    *
434  * TLB miss handling for Book3E with TLB reservation and HES support  *
435  *                                                                    *
436  **********************************************************************/
437
438
439 /* Data TLB miss */
440         START_EXCEPTION(data_tlb_miss)
441         TLB_MISS_PROLOG
442
443         /* Now we handle the fault proper. We only save DEAR in normal
444          * fault case since that's the only interesting values here.
445          * We could probably also optimize by not saving SRR0/1 in the
446          * linear mapping case but I'll leave that for later
447          */
448         mfspr   r14,SPRN_ESR
449         mfspr   r16,SPRN_DEAR           /* get faulting address */
450         srdi    r15,r16,60              /* get region */
451         cmpldi  cr0,r15,0xc             /* linear mapping ? */
452         TLB_MISS_STATS_SAVE_INFO
453         beq     tlb_load_linear         /* yes -> go to linear map load */
454
455         /* The page tables are mapped virtually linear. At this point, though,
456          * we don't know whether we are trying to fault in a first level
457          * virtual address or a virtual page table address. We can get that
458          * from bit 0x1 of the region ID which we have set for a page table
459          */
460         andi.   r10,r15,0x1
461         bne-    virt_page_table_tlb_miss
462
463         std     r14,EX_TLB_ESR(r12);    /* save ESR */
464         std     r16,EX_TLB_DEAR(r12);   /* save DEAR */
465
466          /* We need _PAGE_PRESENT and  _PAGE_ACCESSED set */
467         li      r11,_PAGE_PRESENT
468         oris    r11,r11,_PAGE_ACCESSED@h
469
470         /* We do the user/kernel test for the PID here along with the RW test
471          */
472         cmpldi  cr0,r15,0               /* Check for user region */
473
474         /* We pre-test some combination of permissions to avoid double
475          * faults:
476          *
477          * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE
478          * ESR_ST   is 0x00800000
479          * _PAGE_BAP_SW is 0x00000010
480          * So the shift is >> 19. This tests for supervisor writeability.
481          * If the page happens to be supervisor writeable and not user
482          * writeable, we will take a new fault later, but that should be
483          * a rare enough case.
484          *
485          * We also move ESR_ST in _PAGE_DIRTY position
486          * _PAGE_DIRTY is 0x00001000 so the shift is >> 11
487          *
488          * MAS1 is preset for all we need except for TID that needs to
489          * be cleared for kernel translations
490          */
491         rlwimi  r11,r14,32-19,27,27
492         rlwimi  r11,r14,32-16,19,19
493         beq     normal_tlb_miss
494         /* XXX replace the RMW cycles with immediate loads + writes */
495 1:      mfspr   r10,SPRN_MAS1
496         cmpldi  cr0,r15,8               /* Check for vmalloc region */
497         rlwinm  r10,r10,0,16,1          /* Clear TID */
498         mtspr   SPRN_MAS1,r10
499         beq+    normal_tlb_miss
500
501         /* We got a crappy address, just fault with whatever DEAR and ESR
502          * are here
503          */
504         TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
505         TLB_MISS_EPILOG_ERROR
506         b       exc_data_storage_book3e
507
508 /* Instruction TLB miss */
509         START_EXCEPTION(instruction_tlb_miss)
510         TLB_MISS_PROLOG
511
512         /* If we take a recursive fault, the second level handler may need
513          * to know whether we are handling a data or instruction fault in
514          * order to get to the right store fault handler. We provide that
515          * info by writing a crazy value in ESR in our exception frame
516          */
517         li      r14,-1  /* store to exception frame is done later */
518
519         /* Now we handle the fault proper. We only save DEAR in the non
520          * linear mapping case since we know the linear mapping case will
521          * not re-enter. We could indeed optimize and also not save SRR0/1
522          * in the linear mapping case but I'll leave that for later
523          *
524          * Faulting address is SRR0 which is already in r16
525          */
526         srdi    r15,r16,60              /* get region */
527         cmpldi  cr0,r15,0xc             /* linear mapping ? */
528         TLB_MISS_STATS_SAVE_INFO
529         beq     tlb_load_linear         /* yes -> go to linear map load */
530
531         /* We do the user/kernel test for the PID here along with the RW test
532          */
533         li      r11,_PAGE_PRESENT|_PAGE_EXEC    /* Base perm */
534         oris    r11,r11,_PAGE_ACCESSED@h
535
536         cmpldi  cr0,r15,0                       /* Check for user region */
537         std     r14,EX_TLB_ESR(r12)             /* write crazy -1 to frame */
538         beq     normal_tlb_miss
539
540         li      r11,_PAGE_PRESENT|_PAGE_BAP_SX  /* Base perm */
541         oris    r11,r11,_PAGE_ACCESSED@h
542         /* XXX replace the RMW cycles with immediate loads + writes */
543         mfspr   r10,SPRN_MAS1
544         cmpldi  cr0,r15,8                       /* Check for vmalloc region */
545         rlwinm  r10,r10,0,16,1                  /* Clear TID */
546         mtspr   SPRN_MAS1,r10
547         beq+    normal_tlb_miss
548
549         /* We got a crappy address, just fault */
550         TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
551         TLB_MISS_EPILOG_ERROR
552         b       exc_instruction_storage_book3e
553
554 /*
555  * This is the guts of the first-level TLB miss handler for direct
556  * misses. We are entered with:
557  *
558  * r16 = faulting address
559  * r15 = region ID
560  * r14 = crap (free to use)
561  * r13 = PACA
562  * r12 = TLB exception frame in PACA
563  * r11 = PTE permission mask
564  * r10 = crap (free to use)
565  */
566 normal_tlb_miss:
567         /* So we first construct the page table address. We do that by
568          * shifting the bottom of the address (not the region ID) by
569          * PAGE_SHIFT-3, clearing the bottom 3 bits (get a PTE ptr) and
570          * or'ing the fourth high bit.
571          *
572          * NOTE: For 64K pages, we do things slightly differently in
573          * order to handle the weird page table format used by linux
574          */
575         ori     r10,r15,0x1
576 #ifdef CONFIG_PPC_64K_PAGES
577         /* For the top bits, 16 bytes per PTE */
578         rldicl  r14,r16,64-(PAGE_SHIFT-4),PAGE_SHIFT-4+4
579         /* Now create the bottom bits as 0 in position 0x8000 and
580          * the rest calculated for 8 bytes per PTE
581          */
582         rldicl  r15,r16,64-(PAGE_SHIFT-3),64-15
583         /* Insert the bottom bits in */
584         rlwimi  r14,r15,0,16,31
585 #else
586         rldicl  r14,r16,64-(PAGE_SHIFT-3),PAGE_SHIFT-3+4
587 #endif
588         sldi    r15,r10,60
589         clrrdi  r14,r14,3
590         or      r10,r15,r14
591
592 BEGIN_MMU_FTR_SECTION
593         /* Set the TLB reservation and search for existing entry. Then load
594          * the entry.
595          */
596         PPC_TLBSRX_DOT(0,R16)
597         ld      r14,0(r10)
598         beq     normal_tlb_miss_done
599 MMU_FTR_SECTION_ELSE
600         ld      r14,0(r10)
601 ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_USE_TLBRSRV)
602
603 finish_normal_tlb_miss:
604         /* Check if required permissions are met */
605         andc.   r15,r11,r14
606         bne-    normal_tlb_miss_access_fault
607
608         /* Now we build the MAS:
609          *
610          * MAS 0   :    Fully setup with defaults in MAS4 and TLBnCFG
611          * MAS 1   :    Almost fully setup
612          *               - PID already updated by caller if necessary
613          *               - TSIZE need change if !base page size, not
614          *                 yet implemented for now
615          * MAS 2   :    Defaults not useful, need to be redone
616          * MAS 3+7 :    Needs to be done
617          *
618          * TODO: mix up code below for better scheduling
619          */
620         clrrdi  r11,r16,12              /* Clear low crap in EA */
621         rlwimi  r11,r14,32-19,27,31     /* Insert WIMGE */
622         mtspr   SPRN_MAS2,r11
623
624         /* Check page size, if not standard, update MAS1 */
625         rldicl  r11,r14,64-8,64-8
626 #ifdef CONFIG_PPC_64K_PAGES
627         cmpldi  cr0,r11,BOOK3E_PAGESZ_64K
628 #else
629         cmpldi  cr0,r11,BOOK3E_PAGESZ_4K
630 #endif
631         beq-    1f
632         mfspr   r11,SPRN_MAS1
633         rlwimi  r11,r14,31,21,24
634         rlwinm  r11,r11,0,21,19
635         mtspr   SPRN_MAS1,r11
636 1:
637         /* Move RPN in position */
638         rldicr  r11,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
639         clrldi  r15,r11,12              /* Clear crap at the top */
640         rlwimi  r15,r14,32-8,22,25      /* Move in U bits */
641         rlwimi  r15,r14,32-2,26,31      /* Move in BAP bits */
642
643         /* Mask out SW and UW if !DIRTY (XXX optimize this !) */
644         andi.   r11,r14,_PAGE_DIRTY
645         bne     1f
646         li      r11,MAS3_SW|MAS3_UW
647         andc    r15,r15,r11
648 1:
649 BEGIN_MMU_FTR_SECTION
650         srdi    r16,r15,32
651         mtspr   SPRN_MAS3,r15
652         mtspr   SPRN_MAS7,r16
653 MMU_FTR_SECTION_ELSE
654         mtspr   SPRN_MAS7_MAS3,r15
655 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
656
657         tlbwe
658
659 normal_tlb_miss_done:
660         /* We don't bother with restoring DEAR or ESR since we know we are
661          * level 0 and just going back to userland. They are only needed
662          * if you are going to take an access fault
663          */
664         TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
665         TLB_MISS_EPILOG_SUCCESS
666         rfi
667
668 normal_tlb_miss_access_fault:
669         /* We need to check if it was an instruction miss */
670         andi.   r10,r11,_PAGE_EXEC
671         bne     1f
672         ld      r14,EX_TLB_DEAR(r12)
673         ld      r15,EX_TLB_ESR(r12)
674         mtspr   SPRN_DEAR,r14
675         mtspr   SPRN_ESR,r15
676         TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
677         TLB_MISS_EPILOG_ERROR
678         b       exc_data_storage_book3e
679 1:      TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
680         TLB_MISS_EPILOG_ERROR
681         b       exc_instruction_storage_book3e
682
683
684 /*
685  * This is the guts of the second-level TLB miss handler for direct
686  * misses. We are entered with:
687  *
688  * r16 = virtual page table faulting address
689  * r15 = region (top 4 bits of address)
690  * r14 = crap (free to use)
691  * r13 = PACA
692  * r12 = TLB exception frame in PACA
693  * r11 = crap (free to use)
694  * r10 = crap (free to use)
695  *
696  * Note that this should only ever be called as a second level handler
697  * with the current scheme when using SW load.
698  * That means we can always get the original fault DEAR at
699  * EX_TLB_DEAR-EX_TLB_SIZE(r12)
700  *
701  * It can be re-entered by the linear mapping miss handler. However, to
702  * avoid too much complication, it will restart the whole fault at level
703  * 0 so we don't care too much about clobbers
704  *
705  * XXX That code was written back when we couldn't clobber r14. We can now,
706  * so we could probably optimize things a bit
707  */
708 virt_page_table_tlb_miss:
709         /* Are we hitting a kernel page table ? */
710         andi.   r10,r15,0x8
711
712         /* The cool thing now is that r10 contains 0 for user and 8 for kernel,
713          * and we happen to have the swapper_pg_dir at offset 8 from the user
714          * pgdir in the PACA :-).
715          */
716         add     r11,r10,r13
717
718         /* If kernel, we need to clear MAS1 TID */
719         beq     1f
720         /* XXX replace the RMW cycles with immediate loads + writes */
721         mfspr   r10,SPRN_MAS1
722         rlwinm  r10,r10,0,16,1                  /* Clear TID */
723         mtspr   SPRN_MAS1,r10
724 1:
725 BEGIN_MMU_FTR_SECTION
726         /* Search if we already have a TLB entry for that virtual address, and
727          * if we do, bail out.
728          */
729         PPC_TLBSRX_DOT(0,R16)
730         beq     virt_page_table_tlb_miss_done
731 END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
732
733         /* Now, we need to walk the page tables. First check if we are in
734          * range.
735          */
736         rldicl. r10,r16,64-(VPTE_INDEX_SIZE+3),VPTE_INDEX_SIZE+3+4
737         bne-    virt_page_table_tlb_miss_fault
738
739         /* Get the PGD pointer */
740         ld      r15,PACAPGD(r11)
741         cmpldi  cr0,r15,0
742         beq-    virt_page_table_tlb_miss_fault
743
744         /* Get to PGD entry */
745         rldicl  r11,r16,64-VPTE_PGD_SHIFT,64-PGD_INDEX_SIZE-3
746         clrrdi  r10,r11,3
747         ldx     r15,r10,r15
748         cmpdi   cr0,r15,0
749         bge     virt_page_table_tlb_miss_fault
750
751 #ifndef CONFIG_PPC_64K_PAGES
752         /* Get to PUD entry */
753         rldicl  r11,r16,64-VPTE_PUD_SHIFT,64-PUD_INDEX_SIZE-3
754         clrrdi  r10,r11,3
755         ldx     r15,r10,r15
756         cmpdi   cr0,r15,0
757         bge     virt_page_table_tlb_miss_fault
758 #endif /* CONFIG_PPC_64K_PAGES */
759
760         /* Get to PMD entry */
761         rldicl  r11,r16,64-VPTE_PMD_SHIFT,64-PMD_INDEX_SIZE-3
762         clrrdi  r10,r11,3
763         ldx     r15,r10,r15
764         cmpdi   cr0,r15,0
765         bge     virt_page_table_tlb_miss_fault
766
767         /* Ok, we're all right, we can now create a kernel translation for
768          * a 4K or 64K page from r16 -> r15.
769          */
770         /* Now we build the MAS:
771          *
772          * MAS 0   :    Fully setup with defaults in MAS4 and TLBnCFG
773          * MAS 1   :    Almost fully setup
774          *               - PID already updated by caller if necessary
775          *               - TSIZE for now is base page size always
776          * MAS 2   :    Use defaults
777          * MAS 3+7 :    Needs to be done
778          *
779          * So we only do MAS 2 and 3 for now...
780          */
781         clrldi  r11,r15,4               /* remove region ID from RPN */
782         ori     r10,r11,1               /* Or-in SR */
783
784 BEGIN_MMU_FTR_SECTION
785         srdi    r16,r10,32
786         mtspr   SPRN_MAS3,r10
787         mtspr   SPRN_MAS7,r16
788 MMU_FTR_SECTION_ELSE
789         mtspr   SPRN_MAS7_MAS3,r10
790 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
791
792         tlbwe
793
794 BEGIN_MMU_FTR_SECTION
795 virt_page_table_tlb_miss_done:
796
797         /* We have overriden MAS2:EPN but currently our primary TLB miss
798          * handler will always restore it so that should not be an issue,
799          * if we ever optimize the primary handler to not write MAS2 on
800          * some cases, we'll have to restore MAS2:EPN here based on the
801          * original fault's DEAR. If we do that we have to modify the
802          * ITLB miss handler to also store SRR0 in the exception frame
803          * as DEAR.
804          *
805          * However, one nasty thing we did is we cleared the reservation
806          * (well, potentially we did). We do a trick here thus if we
807          * are not a level 0 exception (we interrupted the TLB miss) we
808          * offset the return address by -4 in order to replay the tlbsrx
809          * instruction there
810          */
811         subf    r10,r13,r12
812         cmpldi  cr0,r10,PACA_EXTLB+EX_TLB_SIZE
813         bne-    1f
814         ld      r11,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
815         addi    r10,r11,-4
816         std     r10,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
817 1:
818 END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
819         /* Return to caller, normal case */
820         TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK);
821         TLB_MISS_EPILOG_SUCCESS
822         rfi
823
824 virt_page_table_tlb_miss_fault:
825         /* If we fault here, things are a little bit tricky. We need to call
826          * either data or instruction store fault, and we need to retrieve
827          * the original fault address and ESR (for data).
828          *
829          * The thing is, we know that in normal circumstances, this is
830          * always called as a second level tlb miss for SW load or as a first
831          * level TLB miss for HW load, so we should be able to peek at the
832          * relevant information in the first exception frame in the PACA.
833          *
834          * However, we do need to double check that, because we may just hit
835          * a stray kernel pointer or a userland attack trying to hit those
836          * areas. If that is the case, we do a data fault. (We can't get here
837          * from an instruction tlb miss anyway).
838          *
839          * Note also that when going to a fault, we must unwind the previous
840          * level as well. Since we are doing that, we don't need to clear or
841          * restore the TLB reservation neither.
842          */
843         subf    r10,r13,r12
844         cmpldi  cr0,r10,PACA_EXTLB+EX_TLB_SIZE
845         bne-    virt_page_table_tlb_miss_whacko_fault
846
847         /* We dig the original DEAR and ESR from slot 0 */
848         ld      r15,EX_TLB_DEAR+PACA_EXTLB(r13)
849         ld      r16,EX_TLB_ESR+PACA_EXTLB(r13)
850
851         /* We check for the "special" ESR value for instruction faults */
852         cmpdi   cr0,r16,-1
853         beq     1f
854         mtspr   SPRN_DEAR,r15
855         mtspr   SPRN_ESR,r16
856         TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT);
857         TLB_MISS_EPILOG_ERROR
858         b       exc_data_storage_book3e
859 1:      TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT);
860         TLB_MISS_EPILOG_ERROR
861         b       exc_instruction_storage_book3e
862
863 virt_page_table_tlb_miss_whacko_fault:
864         /* The linear fault will restart everything so ESR and DEAR will
865          * not have been clobbered, let's just fault with what we have
866          */
867         TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_FAULT);
868         TLB_MISS_EPILOG_ERROR
869         b       exc_data_storage_book3e
870
871
872 /**************************************************************
873  *                                                            *
874  * TLB miss handling for Book3E with hw page table support    *
875  *                                                            *
876  **************************************************************/
877
878
879 /* Data TLB miss */
880         START_EXCEPTION(data_tlb_miss_htw)
881         TLB_MISS_PROLOG
882
883         /* Now we handle the fault proper. We only save DEAR in normal
884          * fault case since that's the only interesting values here.
885          * We could probably also optimize by not saving SRR0/1 in the
886          * linear mapping case but I'll leave that for later
887          */
888         mfspr   r14,SPRN_ESR
889         mfspr   r16,SPRN_DEAR           /* get faulting address */
890         srdi    r11,r16,60              /* get region */
891         cmpldi  cr0,r11,0xc             /* linear mapping ? */
892         TLB_MISS_STATS_SAVE_INFO
893         beq     tlb_load_linear         /* yes -> go to linear map load */
894
895         /* We do the user/kernel test for the PID here along with the RW test
896          */
897         cmpldi  cr0,r11,0               /* Check for user region */
898         ld      r15,PACAPGD(r13)        /* Load user pgdir */
899         beq     htw_tlb_miss
900
901         /* XXX replace the RMW cycles with immediate loads + writes */
902 1:      mfspr   r10,SPRN_MAS1
903         cmpldi  cr0,r11,8               /* Check for vmalloc region */
904         rlwinm  r10,r10,0,16,1          /* Clear TID */
905         mtspr   SPRN_MAS1,r10
906         ld      r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */
907         beq+    htw_tlb_miss
908
909         /* We got a crappy address, just fault with whatever DEAR and ESR
910          * are here
911          */
912         TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
913         TLB_MISS_EPILOG_ERROR
914         b       exc_data_storage_book3e
915
916 /* Instruction TLB miss */
917         START_EXCEPTION(instruction_tlb_miss_htw)
918         TLB_MISS_PROLOG
919
920         /* If we take a recursive fault, the second level handler may need
921          * to know whether we are handling a data or instruction fault in
922          * order to get to the right store fault handler. We provide that
923          * info by keeping a crazy value for ESR in r14
924          */
925         li      r14,-1  /* store to exception frame is done later */
926
927         /* Now we handle the fault proper. We only save DEAR in the non
928          * linear mapping case since we know the linear mapping case will
929          * not re-enter. We could indeed optimize and also not save SRR0/1
930          * in the linear mapping case but I'll leave that for later
931          *
932          * Faulting address is SRR0 which is already in r16
933          */
934         srdi    r11,r16,60              /* get region */
935         cmpldi  cr0,r11,0xc             /* linear mapping ? */
936         TLB_MISS_STATS_SAVE_INFO
937         beq     tlb_load_linear         /* yes -> go to linear map load */
938
939         /* We do the user/kernel test for the PID here along with the RW test
940          */
941         cmpldi  cr0,r11,0                       /* Check for user region */
942         ld      r15,PACAPGD(r13)                /* Load user pgdir */
943         beq     htw_tlb_miss
944
945         /* XXX replace the RMW cycles with immediate loads + writes */
946 1:      mfspr   r10,SPRN_MAS1
947         cmpldi  cr0,r11,8                       /* Check for vmalloc region */
948         rlwinm  r10,r10,0,16,1                  /* Clear TID */
949         mtspr   SPRN_MAS1,r10
950         ld      r15,PACA_KERNELPGD(r13)         /* Load kernel pgdir */
951         beq+    htw_tlb_miss
952
953         /* We got a crappy address, just fault */
954         TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
955         TLB_MISS_EPILOG_ERROR
956         b       exc_instruction_storage_book3e
957
958
959 /*
960  * This is the guts of the second-level TLB miss handler for direct
961  * misses. We are entered with:
962  *
963  * r16 = virtual page table faulting address
964  * r15 = PGD pointer
965  * r14 = ESR
966  * r13 = PACA
967  * r12 = TLB exception frame in PACA
968  * r11 = crap (free to use)
969  * r10 = crap (free to use)
970  *
971  * It can be re-entered by the linear mapping miss handler. However, to
972  * avoid too much complication, it will save/restore things for us
973  */
974 htw_tlb_miss:
975         /* Search if we already have a TLB entry for that virtual address, and
976          * if we do, bail out.
977          *
978          * MAS1:IND should be already set based on MAS4
979          */
980         PPC_TLBSRX_DOT(0,R16)
981         beq     htw_tlb_miss_done
982
983         /* Now, we need to walk the page tables. First check if we are in
984          * range.
985          */
986         rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
987         bne-    htw_tlb_miss_fault
988
989         /* Get the PGD pointer */
990         cmpldi  cr0,r15,0
991         beq-    htw_tlb_miss_fault
992
993         /* Get to PGD entry */
994         rldicl  r11,r16,64-(PGDIR_SHIFT-3),64-PGD_INDEX_SIZE-3
995         clrrdi  r10,r11,3
996         ldx     r15,r10,r15
997         cmpdi   cr0,r15,0
998         bge     htw_tlb_miss_fault
999
1000 #ifndef CONFIG_PPC_64K_PAGES
1001         /* Get to PUD entry */
1002         rldicl  r11,r16,64-(PUD_SHIFT-3),64-PUD_INDEX_SIZE-3
1003         clrrdi  r10,r11,3
1004         ldx     r15,r10,r15
1005         cmpdi   cr0,r15,0
1006         bge     htw_tlb_miss_fault
1007 #endif /* CONFIG_PPC_64K_PAGES */
1008
1009         /* Get to PMD entry */
1010         rldicl  r11,r16,64-(PMD_SHIFT-3),64-PMD_INDEX_SIZE-3
1011         clrrdi  r10,r11,3
1012         ldx     r15,r10,r15
1013         cmpdi   cr0,r15,0
1014         bge     htw_tlb_miss_fault
1015
1016         /* Ok, we're all right, we can now create an indirect entry for
1017          * a 1M or 256M page.
1018          *
1019          * The last trick is now that because we use "half" pages for
1020          * the HTW (1M IND is 2K and 256M IND is 32K) we need to account
1021          * for an added LSB bit to the RPN. For 64K pages, there is no
1022          * problem as we already use 32K arrays (half PTE pages), but for
1023          * 4K page we need to extract a bit from the virtual address and
1024          * insert it into the "PA52" bit of the RPN.
1025          */
1026 #ifndef CONFIG_PPC_64K_PAGES
1027         rlwimi  r15,r16,32-9,20,20
1028 #endif
1029         /* Now we build the MAS:
1030          *
1031          * MAS 0   :    Fully setup with defaults in MAS4 and TLBnCFG
1032          * MAS 1   :    Almost fully setup
1033          *               - PID already updated by caller if necessary
1034          *               - TSIZE for now is base ind page size always
1035          * MAS 2   :    Use defaults
1036          * MAS 3+7 :    Needs to be done
1037          */
1038 #ifdef CONFIG_PPC_64K_PAGES
1039         ori     r10,r15,(BOOK3E_PAGESZ_64K << MAS3_SPSIZE_SHIFT)
1040 #else
1041         ori     r10,r15,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT)
1042 #endif
1043
1044 BEGIN_MMU_FTR_SECTION
1045         srdi    r16,r10,32
1046         mtspr   SPRN_MAS3,r10
1047         mtspr   SPRN_MAS7,r16
1048 MMU_FTR_SECTION_ELSE
1049         mtspr   SPRN_MAS7_MAS3,r10
1050 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
1051
1052         tlbwe
1053
1054 htw_tlb_miss_done:
1055         /* We don't bother with restoring DEAR or ESR since we know we are
1056          * level 0 and just going back to userland. They are only needed
1057          * if you are going to take an access fault
1058          */
1059         TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK)
1060         TLB_MISS_EPILOG_SUCCESS
1061         rfi
1062
1063 htw_tlb_miss_fault:
1064         /* We need to check if it was an instruction miss. We know this
1065          * though because r14 would contain -1
1066          */
1067         cmpdi   cr0,r14,-1
1068         beq     1f
1069         mtspr   SPRN_DEAR,r16
1070         mtspr   SPRN_ESR,r14
1071         TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT)
1072         TLB_MISS_EPILOG_ERROR
1073         b       exc_data_storage_book3e
1074 1:      TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT)
1075         TLB_MISS_EPILOG_ERROR
1076         b       exc_instruction_storage_book3e
1077
1078 /*
1079  * This is the guts of "any" level TLB miss handler for kernel linear
1080  * mapping misses. We are entered with:
1081  *
1082  *
1083  * r16 = faulting address
1084  * r15 = crap (free to use)
1085  * r14 = ESR (data) or -1 (instruction)
1086  * r13 = PACA
1087  * r12 = TLB exception frame in PACA
1088  * r11 = crap (free to use)
1089  * r10 = crap (free to use)
1090  *
1091  * In addition we know that we will not re-enter, so in theory, we could
1092  * use a simpler epilog not restoring SRR0/1 etc.. but we'll do that later.
1093  *
1094  * We also need to be careful about MAS registers here & TLB reservation,
1095  * as we know we'll have clobbered them if we interrupt the main TLB miss
1096  * handlers in which case we probably want to do a full restart at level
1097  * 0 rather than saving / restoring the MAS.
1098  *
1099  * Note: If we care about performance of that core, we can easily shuffle
1100  *       a few things around
1101  */
1102 tlb_load_linear:
1103         /* For now, we assume the linear mapping is contiguous and stops at
1104          * linear_map_top. We also assume the size is a multiple of 1G, thus
1105          * we only use 1G pages for now. That might have to be changed in a
1106          * final implementation, especially when dealing with hypervisors
1107          */
1108         ld      r11,PACATOC(r13)
1109         ld      r11,linear_map_top@got(r11)
1110         ld      r10,0(r11)
1111         tovirt(10,10)
1112         cmpld   cr0,r16,r10
1113         bge     tlb_load_linear_fault
1114
1115         /* MAS1 need whole new setup. */
1116         li      r15,(BOOK3E_PAGESZ_1GB<<MAS1_TSIZE_SHIFT)
1117         oris    r15,r15,MAS1_VALID@h    /* MAS1 needs V and TSIZE */
1118         mtspr   SPRN_MAS1,r15
1119
1120         /* Already somebody there ? */
1121         PPC_TLBSRX_DOT(0,R16)
1122         beq     tlb_load_linear_done
1123
1124         /* Now we build the remaining MAS. MAS0 and 2 should be fine
1125          * with their defaults, which leaves us with MAS 3 and 7. The
1126          * mapping is linear, so we just take the address, clear the
1127          * region bits, and or in the permission bits which are currently
1128          * hard wired
1129          */
1130         clrrdi  r10,r16,30              /* 1G page index */
1131         clrldi  r10,r10,4               /* clear region bits */
1132         ori     r10,r10,MAS3_SR|MAS3_SW|MAS3_SX
1133
1134 BEGIN_MMU_FTR_SECTION
1135         srdi    r16,r10,32
1136         mtspr   SPRN_MAS3,r10
1137         mtspr   SPRN_MAS7,r16
1138 MMU_FTR_SECTION_ELSE
1139         mtspr   SPRN_MAS7_MAS3,r10
1140 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
1141
1142         tlbwe
1143
1144 tlb_load_linear_done:
1145         /* We use the "error" epilog for success as we do want to
1146          * restore to the initial faulting context, whatever it was.
1147          * We do that because we can't resume a fault within a TLB
1148          * miss handler, due to MAS and TLB reservation being clobbered.
1149          */
1150         TLB_MISS_STATS_X(MMSTAT_TLB_MISS_LINEAR)
1151         TLB_MISS_EPILOG_ERROR
1152         rfi
1153
1154 tlb_load_linear_fault:
1155         /* We keep the DEAR and ESR around, this shouldn't have happened */
1156         cmpdi   cr0,r14,-1
1157         beq     1f
1158         TLB_MISS_EPILOG_ERROR_SPECIAL
1159         b       exc_data_storage_book3e
1160 1:      TLB_MISS_EPILOG_ERROR_SPECIAL
1161         b       exc_instruction_storage_book3e
1162
1163
1164 #ifdef CONFIG_BOOK3E_MMU_TLB_STATS
1165 .tlb_stat_inc:
1166 1:      ldarx   r8,0,r9
1167         addi    r8,r8,1
1168         stdcx.  r8,0,r9
1169         bne-    1b
1170         blr
1171 #endif