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