xref: /linux/arch/powerpc/mm/nohash/tlb_low_64e.S (revision 0db46aaabe641e5565238dc3ef7ac2396a0c6286)
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 *  Low level TLB miss handlers for Book3E
4 *
5 *  Copyright (C) 2008-2009
6 *      Ben. Herrenschmidt (benh@kernel.crashing.org), IBM Corp.
7 */
8
9#include <linux/pgtable.h>
10#include <asm/processor.h>
11#include <asm/reg.h>
12#include <asm/page.h>
13#include <asm/mmu.h>
14#include <asm/ppc_asm.h>
15#include <asm/asm-offsets.h>
16#include <asm/cputable.h>
17#include <asm/exception-64e.h>
18#include <asm/ppc-opcode.h>
19#include <asm/kvm_asm.h>
20#include <asm/kvm_booke_hv_asm.h>
21#include <asm/feature-fixups.h>
22
23#define VPTE_PMD_SHIFT	(PTE_INDEX_SIZE)
24#define VPTE_PUD_SHIFT	(VPTE_PMD_SHIFT + PMD_INDEX_SIZE)
25#define VPTE_PGD_SHIFT	(VPTE_PUD_SHIFT + PUD_INDEX_SIZE)
26#define VPTE_INDEX_SIZE (VPTE_PGD_SHIFT + PGD_INDEX_SIZE)
27
28/**********************************************************************
29 *                                                                    *
30 * TLB miss handling for Book3E with a bolted linear mapping          *
31 * No virtual page table, no nested TLB misses                        *
32 *                                                                    *
33 **********************************************************************/
34
35/*
36 * Note that, unlike non-bolted handlers, TLB_EXFRAME is not
37 * modified by the TLB miss handlers themselves, since the TLB miss
38 * handler code will not itself cause a recursive TLB miss.
39 *
40 * TLB_EXFRAME will be modified when crit/mc/debug exceptions are
41 * entered/exited.
42 */
43.macro tlb_prolog_bolted intnum addr
44	mtspr	SPRN_SPRG_GEN_SCRATCH,r12
45	mfspr	r12,SPRN_SPRG_TLB_EXFRAME
46	std	r13,EX_TLB_R13(r12)
47	std	r10,EX_TLB_R10(r12)
48	mfspr	r13,SPRN_SPRG_PACA
49
50	mfcr	r10
51	std	r11,EX_TLB_R11(r12)
52#ifdef CONFIG_KVM_BOOKE_HV
53BEGIN_FTR_SECTION
54	mfspr	r11, SPRN_SRR1
55END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
56#endif
57	DO_KVM	\intnum, SPRN_SRR1
58	std	r16,EX_TLB_R16(r12)
59	mfspr	r16,\addr		/* get faulting address */
60	std	r14,EX_TLB_R14(r12)
61	ld	r14,PACAPGD(r13)
62	std	r15,EX_TLB_R15(r12)
63	std	r10,EX_TLB_CR(r12)
64START_BTB_FLUSH_SECTION
65	mfspr r11, SPRN_SRR1
66	andi. r10,r11,MSR_PR
67	beq 1f
68	BTB_FLUSH(r10)
691:
70END_BTB_FLUSH_SECTION
71	std	r7,EX_TLB_R7(r12)
72.endm
73
74.macro tlb_epilog_bolted
75	ld	r14,EX_TLB_CR(r12)
76	ld	r7,EX_TLB_R7(r12)
77	ld	r10,EX_TLB_R10(r12)
78	ld	r11,EX_TLB_R11(r12)
79	ld	r13,EX_TLB_R13(r12)
80	mtcr	r14
81	ld	r14,EX_TLB_R14(r12)
82	ld	r15,EX_TLB_R15(r12)
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	bne	tlb_miss_kernel_bolted
126
127tlb_miss_user_bolted:
128#ifdef CONFIG_PPC_KUAP
129	mfspr	r10,SPRN_MAS1
130	rlwinm.	r10,r10,0,0x3fff0000
131	beq-	tlb_miss_fault_bolted /* KUAP fault */
132#endif
133
134tlb_miss_common_bolted:
135/*
136 * This is the guts of the TLB miss handler for bolted-linear.
137 * We are entered with:
138 *
139 * r16 = faulting address
140 * r15 = crap (free to use)
141 * r14 = page table base
142 * r13 = PACA
143 * r11 = PTE permission mask
144 * r10 = crap (free to use)
145 */
146	rldicl	r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3
147	cmpldi	cr0,r14,0
148	clrrdi	r15,r15,3
149	beq	tlb_miss_fault_bolted	/* No PGDIR, bail */
150
151	ldx	r14,r14,r15		/* grab pgd entry */
152
153	rldicl	r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3
154	clrrdi	r15,r15,3
155	cmpdi	cr0,r14,0
156	bge	tlb_miss_fault_bolted	/* Bad pgd entry or hugepage; bail */
157	ldx	r14,r14,r15		/* grab pud entry */
158
159	rldicl	r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3
160	clrrdi	r15,r15,3
161	cmpdi	cr0,r14,0
162	bge	tlb_miss_fault_bolted
163	ldx	r14,r14,r15		/* Grab pmd entry */
164
165	rldicl	r15,r16,64-PAGE_SHIFT+3,64-PTE_INDEX_SIZE-3
166	clrrdi	r15,r15,3
167	cmpdi	cr0,r14,0
168	bge	tlb_miss_fault_bolted
169	ldx	r14,r14,r15		/* Grab PTE, normal (!huge) page */
170
171	/* Check if required permissions are met */
172	andc.	r15,r11,r14
173	rldicr	r15,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
174	bne-	tlb_miss_fault_bolted
175
176	/* Now we build the MAS:
177	 *
178	 * MAS 0   :	Fully setup with defaults in MAS4 and TLBnCFG
179	 * MAS 1   :	Almost fully setup
180	 *               - PID already updated by caller if necessary
181	 *               - TSIZE need change if !base page size, not
182	 *                 yet implemented for now
183	 * MAS 2   :	Defaults not useful, need to be redone
184	 * MAS 3+7 :	Needs to be done
185	 */
186	clrrdi	r11,r16,12		/* Clear low crap in EA */
187	clrldi	r15,r15,12		/* Clear crap at the top */
188	rlwimi	r11,r14,32-19,27,31	/* Insert WIMGE */
189	rlwimi	r15,r14,32-8,22,25	/* Move in U bits */
190	mtspr	SPRN_MAS2,r11
191	andi.	r11,r14,_PAGE_DIRTY
192	rlwimi	r15,r14,32-2,26,31	/* Move in BAP bits */
193
194	/* Mask out SW and UW if !DIRTY (XXX optimize this !) */
195	bne	1f
196	li	r11,MAS3_SW|MAS3_UW
197	andc	r15,r15,r11
1981:
199	mtspr	SPRN_MAS7_MAS3,r15
200	tlbwe
201
202tlb_miss_done_bolted:
203	tlb_epilog_bolted
204	rfi
205
206itlb_miss_kernel_bolted:
207	li	r11,_PAGE_PRESENT|_PAGE_BAP_SX	/* Base perm */
208	oris	r11,r11,_PAGE_ACCESSED@h
209tlb_miss_kernel_bolted:
210	mfspr	r10,SPRN_MAS1
211	ld	r14,PACA_KERNELPGD(r13)
212	srdi	r15,r16,44		/* get kernel region */
213	andi.	r15,r15,1		/* Check for vmalloc region */
214	rlwinm	r10,r10,0,16,1		/* Clear TID */
215	mtspr	SPRN_MAS1,r10
216	bne+	tlb_miss_common_bolted
217
218tlb_miss_fault_bolted:
219	/* We need to check if it was an instruction miss */
220	andi.	r10,r11,_PAGE_BAP_UX|_PAGE_BAP_SX
221	bne	itlb_miss_fault_bolted
222dtlb_miss_fault_bolted:
223	tlb_epilog_bolted
224	b	exc_data_storage_book3e
225itlb_miss_fault_bolted:
226	tlb_epilog_bolted
227	b	exc_instruction_storage_book3e
228
229/* Instruction TLB miss */
230	START_EXCEPTION(instruction_tlb_miss_bolted)
231	tlb_prolog_bolted BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR0
232
233	rldicl.	r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
234	srdi	r15,r16,60		/* get region */
235	bne-	itlb_miss_fault_bolted
236
237	li	r11,_PAGE_PRESENT|_PAGE_BAP_UX	/* Base perm */
238
239	/* We do the user/kernel test for the PID here along with the RW test
240	 */
241
242	cmpldi	cr0,r15,0			/* Check for user region */
243	oris	r11,r11,_PAGE_ACCESSED@h
244	beq	tlb_miss_user_bolted
245	b	itlb_miss_kernel_bolted
246
247/*
248 * TLB miss handling for e6500 and derivatives, using hardware tablewalk.
249 *
250 * Linear mapping is bolted: no virtual page table or nested TLB misses
251 * Indirect entries in TLB1, hardware loads resulting direct entries
252 *    into TLB0
253 * No HES or NV hint on TLB1, so we need to do software round-robin
254 * No tlbsrx. so we need a spinlock, and we have to deal
255 *    with MAS-damage caused by tlbsx
256 * 4K pages only
257 */
258
259	START_EXCEPTION(instruction_tlb_miss_e6500)
260	tlb_prolog_bolted BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR0
261
262	ld	r11,PACA_TCD_PTR(r13)
263	srdi.	r15,r16,60		/* get region */
264	ori	r16,r16,1
265
266	bne	tlb_miss_kernel_e6500	/* user/kernel test */
267
268	b	tlb_miss_common_e6500
269
270	START_EXCEPTION(data_tlb_miss_e6500)
271	tlb_prolog_bolted BOOKE_INTERRUPT_DTLB_MISS SPRN_DEAR
272
273	ld	r11,PACA_TCD_PTR(r13)
274	srdi.	r15,r16,60		/* get region */
275	rldicr	r16,r16,0,62
276
277	bne	tlb_miss_kernel_e6500	/* user vs kernel check */
278
279/*
280 * This is the guts of the TLB miss handler for e6500 and derivatives.
281 * We are entered with:
282 *
283 * r16 = page of faulting address (low bit 0 if data, 1 if instruction)
284 * r15 = crap (free to use)
285 * r14 = page table base
286 * r13 = PACA
287 * r11 = tlb_per_core ptr
288 * r10 = crap (free to use)
289 * r7  = esel_next
290 */
291tlb_miss_common_e6500:
292	crmove	cr2*4+2,cr0*4+2		/* cr2.eq != 0 if kernel address */
293
294BEGIN_FTR_SECTION		/* CPU_FTR_SMT */
295	/*
296	 * Search if we already have an indirect entry for that virtual
297	 * address, and if we do, bail out.
298	 *
299	 * MAS6:IND should be already set based on MAS4
300	 */
301	lhz	r10,PACAPACAINDEX(r13)
302	addi	r10,r10,1
303	crclr	cr1*4+eq	/* set cr1.eq = 0 for non-recursive */
3041:	lbarx	r15,0,r11
305	cmpdi	r15,0
306	bne	2f
307	stbcx.	r10,0,r11
308	bne	1b
3093:
310	.subsection 1
3112:	cmpd	cr1,r15,r10	/* recursive lock due to mcheck/crit/etc? */
312	beq	cr1,3b		/* unlock will happen if cr1.eq = 0 */
31310:	lbz	r15,0(r11)
314	cmpdi	r15,0
315	bne	10b
316	b	1b
317	.previous
318END_FTR_SECTION_IFSET(CPU_FTR_SMT)
319
320	lbz	r7,TCD_ESEL_NEXT(r11)
321
322BEGIN_FTR_SECTION		/* CPU_FTR_SMT */
323	/*
324	 * Erratum A-008139 says that we can't use tlbwe to change
325	 * an indirect entry in any way (including replacing or
326	 * invalidating) if the other thread could be in the process
327	 * of a lookup.  The workaround is to invalidate the entry
328	 * with tlbilx before overwriting.
329	 */
330
331	rlwinm	r10,r7,16,0xff0000
332	oris	r10,r10,MAS0_TLBSEL(1)@h
333	mtspr	SPRN_MAS0,r10
334	isync
335	tlbre
336	mfspr	r15,SPRN_MAS1
337	andis.	r15,r15,MAS1_VALID@h
338	beq	5f
339
340BEGIN_FTR_SECTION_NESTED(532)
341	mfspr	r10,SPRN_MAS8
342	rlwinm	r10,r10,0,0x80000fff  /* tgs,tlpid -> sgs,slpid */
343	mtspr	SPRN_MAS5,r10
344END_FTR_SECTION_NESTED(CPU_FTR_EMB_HV,CPU_FTR_EMB_HV,532)
345
346	mfspr	r10,SPRN_MAS1
347	rlwinm	r15,r10,0,0x3fff0000  /* tid -> spid */
348	rlwimi	r15,r10,20,0x00000003 /* ind,ts -> sind,sas */
349	mfspr	r10,SPRN_MAS6
350	mtspr	SPRN_MAS6,r15
351
352	mfspr	r15,SPRN_MAS2
353	isync
354	PPC_TLBILX_VA(0,R15)
355	isync
356
357	mtspr	SPRN_MAS6,r10
358
3595:
360BEGIN_FTR_SECTION_NESTED(532)
361	li	r10,0
362	mtspr	SPRN_MAS8,r10
363	mtspr	SPRN_MAS5,r10
364END_FTR_SECTION_NESTED(CPU_FTR_EMB_HV,CPU_FTR_EMB_HV,532)
365
366	tlbsx	0,r16
367	mfspr	r10,SPRN_MAS1
368	andis.	r15,r10,MAS1_VALID@h
369	bne	tlb_miss_done_e6500
370FTR_SECTION_ELSE
371	mfspr	r10,SPRN_MAS1
372ALT_FTR_SECTION_END_IFSET(CPU_FTR_SMT)
373
374	oris	r10,r10,MAS1_VALID@h
375	beq	cr2,4f
376	rlwinm	r10,r10,0,16,1		/* Clear TID */
3774:	mtspr	SPRN_MAS1,r10
378
379	/* Now, we need to walk the page tables. First check if we are in
380	 * range.
381	 */
382	rldicl.	r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
383	bne-	tlb_miss_fault_e6500
384
385	rldicl	r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3
386	cmpldi	cr0,r14,0
387	clrrdi	r15,r15,3
388	beq-	tlb_miss_fault_e6500 /* No PGDIR, bail */
389	ldx	r14,r14,r15		/* grab pgd entry */
390
391	rldicl	r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3
392	clrrdi	r15,r15,3
393	cmpdi	cr0,r14,0
394	bge	tlb_miss_huge_e6500	/* Bad pgd entry or hugepage; bail */
395	ldx	r14,r14,r15		/* grab pud entry */
396
397	rldicl	r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3
398	clrrdi	r15,r15,3
399	cmpdi	cr0,r14,0
400	bge	tlb_miss_huge_e6500
401	ldx	r14,r14,r15		/* Grab pmd entry */
402
403	mfspr	r10,SPRN_MAS0
404	cmpdi	cr0,r14,0
405	bge	tlb_miss_huge_e6500
406
407	/* Now we build the MAS for a 2M indirect page:
408	 *
409	 * MAS 0   :	ESEL needs to be filled by software round-robin
410	 * MAS 1   :	Fully set up
411	 *               - PID already updated by caller if necessary
412	 *               - TSIZE for now is base ind page size always
413	 *               - TID already cleared if necessary
414	 * MAS 2   :	Default not 2M-aligned, need to be redone
415	 * MAS 3+7 :	Needs to be done
416	 */
417
418	ori	r14,r14,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT)
419	mtspr	SPRN_MAS7_MAS3,r14
420
421	clrrdi	r15,r16,21		/* make EA 2M-aligned */
422	mtspr	SPRN_MAS2,r15
423
424tlb_miss_huge_done_e6500:
425	lbz	r16,TCD_ESEL_MAX(r11)
426	lbz	r14,TCD_ESEL_FIRST(r11)
427	rlwimi	r10,r7,16,0x00ff0000	/* insert esel_next into MAS0 */
428	addi	r7,r7,1			/* increment esel_next */
429	mtspr	SPRN_MAS0,r10
430	cmpw	r7,r16
431	iseleq	r7,r14,r7		/* if next == last use first */
432	stb	r7,TCD_ESEL_NEXT(r11)
433
434	tlbwe
435
436tlb_miss_done_e6500:
437	.macro	tlb_unlock_e6500
438BEGIN_FTR_SECTION
439	beq	cr1,1f		/* no unlock if lock was recursively grabbed */
440	li	r15,0
441	isync
442	stb	r15,0(r11)
4431:
444END_FTR_SECTION_IFSET(CPU_FTR_SMT)
445	.endm
446
447	tlb_unlock_e6500
448	tlb_epilog_bolted
449	rfi
450
451tlb_miss_huge_e6500:
452	beq	tlb_miss_fault_e6500
453	li	r10,1
454	andi.	r15,r14,HUGEPD_SHIFT_MASK@l /* r15 = psize */
455	rldimi	r14,r10,63,0		/* Set PD_HUGE */
456	xor	r14,r14,r15		/* Clear size bits */
457	ldx	r14,0,r14
458
459	/*
460	 * Now we build the MAS for a huge page.
461	 *
462	 * MAS 0   :	ESEL needs to be filled by software round-robin
463	 *		 - can be handled by indirect code
464	 * MAS 1   :	Need to clear IND and set TSIZE
465	 * MAS 2,3+7:	Needs to be redone similar to non-tablewalk handler
466	 */
467
468	subi	r15,r15,10		/* Convert psize to tsize */
469	mfspr	r10,SPRN_MAS1
470	rlwinm	r10,r10,0,~MAS1_IND
471	rlwimi	r10,r15,MAS1_TSIZE_SHIFT,MAS1_TSIZE_MASK
472	mtspr	SPRN_MAS1,r10
473
474	li	r10,-0x400
475	sld	r15,r10,r15		/* Generate mask based on size */
476	and	r10,r16,r15
477	rldicr	r15,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
478	rlwimi	r10,r14,32-19,27,31	/* Insert WIMGE */
479	clrldi	r15,r15,PAGE_SHIFT	/* Clear crap at the top */
480	rlwimi	r15,r14,32-8,22,25	/* Move in U bits */
481	mtspr	SPRN_MAS2,r10
482	andi.	r10,r14,_PAGE_DIRTY
483	rlwimi	r15,r14,32-2,26,31	/* Move in BAP bits */
484
485	/* Mask out SW and UW if !DIRTY (XXX optimize this !) */
486	bne	1f
487	li	r10,MAS3_SW|MAS3_UW
488	andc	r15,r15,r10
4891:
490	mtspr	SPRN_MAS7_MAS3,r15
491
492	mfspr	r10,SPRN_MAS0
493	b	tlb_miss_huge_done_e6500
494
495tlb_miss_kernel_e6500:
496	ld	r14,PACA_KERNELPGD(r13)
497	srdi	r15,r16,44		/* get kernel region */
498	xoris	r15,r15,0xc		/* Check for vmalloc region */
499	cmplwi	cr1,r15,1
500	beq+	cr1,tlb_miss_common_e6500
501
502tlb_miss_fault_e6500:
503	tlb_unlock_e6500
504	/* We need to check if it was an instruction miss */
505	andi.	r16,r16,1
506	bne	itlb_miss_fault_e6500
507dtlb_miss_fault_e6500:
508	tlb_epilog_bolted
509	b	exc_data_storage_book3e
510itlb_miss_fault_e6500:
511	tlb_epilog_bolted
512	b	exc_instruction_storage_book3e
513
514/*
515 * This is the guts of the second-level TLB miss handler for direct
516 * misses. We are entered with:
517 *
518 * r16 = virtual page table faulting address
519 * r15 = region (top 4 bits of address)
520 * r14 = crap (free to use)
521 * r13 = PACA
522 * r12 = TLB exception frame in PACA
523 * r11 = crap (free to use)
524 * r10 = crap (free to use)
525 *
526 * Note that this should only ever be called as a second level handler
527 * with the current scheme when using SW load.
528 * That means we can always get the original fault DEAR at
529 * EX_TLB_DEAR-EX_TLB_SIZE(r12)
530 *
531 * It can be re-entered by the linear mapping miss handler. However, to
532 * avoid too much complication, it will restart the whole fault at level
533 * 0 so we don't care too much about clobbers
534 *
535 * XXX That code was written back when we couldn't clobber r14. We can now,
536 * so we could probably optimize things a bit
537 */
538virt_page_table_tlb_miss:
539	/* Are we hitting a kernel page table ? */
540	srdi	r15,r16,60
541	andi.	r10,r15,0x8
542
543	/* The cool thing now is that r10 contains 0 for user and 8 for kernel,
544	 * and we happen to have the swapper_pg_dir at offset 8 from the user
545	 * pgdir in the PACA :-).
546	 */
547	add	r11,r10,r13
548
549	/* If kernel, we need to clear MAS1 TID */
550	beq	1f
551	/* XXX replace the RMW cycles with immediate loads + writes */
552	mfspr	r10,SPRN_MAS1
553	rlwinm	r10,r10,0,16,1			/* Clear TID */
554	mtspr	SPRN_MAS1,r10
555#ifdef CONFIG_PPC_KUAP
556	b	2f
5571:
558	mfspr	r10,SPRN_MAS1
559	rlwinm.	r10,r10,0,0x3fff0000
560	beq-	virt_page_table_tlb_miss_fault /* KUAP fault */
5612:
562#else
5631:
564#endif
565
566	/* Now, we need to walk the page tables. First check if we are in
567	 * range.
568	 */
569	rldicl	r10,r16,64-(VPTE_INDEX_SIZE+3),VPTE_INDEX_SIZE+3+4
570	cmpldi	r10,0x80
571	bne-	virt_page_table_tlb_miss_fault
572
573	/* Get the PGD pointer */
574	ld	r15,PACAPGD(r11)
575	cmpldi	cr0,r15,0
576	beq-	virt_page_table_tlb_miss_fault
577
578	/* Get to PGD entry */
579	rldicl	r11,r16,64-VPTE_PGD_SHIFT,64-PGD_INDEX_SIZE-3
580	clrrdi	r10,r11,3
581	ldx	r15,r10,r15
582	cmpdi	cr0,r15,0
583	bge	virt_page_table_tlb_miss_fault
584
585	/* Get to PUD entry */
586	rldicl	r11,r16,64-VPTE_PUD_SHIFT,64-PUD_INDEX_SIZE-3
587	clrrdi	r10,r11,3
588	ldx	r15,r10,r15
589	cmpdi	cr0,r15,0
590	bge	virt_page_table_tlb_miss_fault
591
592	/* Get to PMD entry */
593	rldicl	r11,r16,64-VPTE_PMD_SHIFT,64-PMD_INDEX_SIZE-3
594	clrrdi	r10,r11,3
595	ldx	r15,r10,r15
596	cmpdi	cr0,r15,0
597	bge	virt_page_table_tlb_miss_fault
598
599	/* Ok, we're all right, we can now create a kernel translation for
600	 * a 4K or 64K page from r16 -> r15.
601	 */
602	/* Now we build the MAS:
603	 *
604	 * MAS 0   :	Fully setup with defaults in MAS4 and TLBnCFG
605	 * MAS 1   :	Almost fully setup
606	 *               - PID already updated by caller if necessary
607	 *               - TSIZE for now is base page size always
608	 * MAS 2   :	Use defaults
609	 * MAS 3+7 :	Needs to be done
610	 *
611	 * So we only do MAS 2 and 3 for now...
612	 */
613	clrldi	r11,r15,4		/* remove region ID from RPN */
614	ori	r10,r11,1		/* Or-in SR */
615
616	srdi	r16,r10,32
617	mtspr	SPRN_MAS3,r10
618	mtspr	SPRN_MAS7,r16
619
620	tlbwe
621
622	/* Return to caller, normal case */
623	TLB_MISS_EPILOG_SUCCESS
624	rfi
625
626virt_page_table_tlb_miss_fault:
627	/* If we fault here, things are a little bit tricky. We need to call
628	 * either data or instruction store fault, and we need to retrieve
629	 * the original fault address and ESR (for data).
630	 *
631	 * The thing is, we know that in normal circumstances, this is
632	 * always called as a second level tlb miss for SW load or as a first
633	 * level TLB miss for HW load, so we should be able to peek at the
634	 * relevant information in the first exception frame in the PACA.
635	 *
636	 * However, we do need to double check that, because we may just hit
637	 * a stray kernel pointer or a userland attack trying to hit those
638	 * areas. If that is the case, we do a data fault. (We can't get here
639	 * from an instruction tlb miss anyway).
640	 *
641	 * Note also that when going to a fault, we must unwind the previous
642	 * level as well. Since we are doing that, we don't need to clear or
643	 * restore the TLB reservation neither.
644	 */
645	subf	r10,r13,r12
646	cmpldi	cr0,r10,PACA_EXTLB+EX_TLB_SIZE
647	bne-	virt_page_table_tlb_miss_whacko_fault
648
649	/* We dig the original DEAR and ESR from slot 0 */
650	ld	r15,EX_TLB_DEAR+PACA_EXTLB(r13)
651	ld	r16,EX_TLB_ESR+PACA_EXTLB(r13)
652
653	/* We check for the "special" ESR value for instruction faults */
654	cmpdi	cr0,r16,-1
655	beq	1f
656	mtspr	SPRN_DEAR,r15
657	mtspr	SPRN_ESR,r16
658	TLB_MISS_EPILOG_ERROR
659	b	exc_data_storage_book3e
6601:	TLB_MISS_EPILOG_ERROR
661	b	exc_instruction_storage_book3e
662
663virt_page_table_tlb_miss_whacko_fault:
664	/* The linear fault will restart everything so ESR and DEAR will
665	 * not have been clobbered, let's just fault with what we have
666	 */
667	TLB_MISS_EPILOG_ERROR
668	b	exc_data_storage_book3e
669
670/*
671 * This is the guts of "any" level TLB miss handler for kernel linear
672 * mapping misses. We are entered with:
673 *
674 *
675 * r16 = faulting address
676 * r15 = crap (free to use)
677 * r14 = ESR (data) or -1 (instruction)
678 * r13 = PACA
679 * r12 = TLB exception frame in PACA
680 * r11 = crap (free to use)
681 * r10 = crap (free to use)
682 *
683 * In addition we know that we will not re-enter, so in theory, we could
684 * use a simpler epilog not restoring SRR0/1 etc.. but we'll do that later.
685 *
686 * We also need to be careful about MAS registers here & TLB reservation,
687 * as we know we'll have clobbered them if we interrupt the main TLB miss
688 * handlers in which case we probably want to do a full restart at level
689 * 0 rather than saving / restoring the MAS.
690 *
691 * Note: If we care about performance of that core, we can easily shuffle
692 *       a few things around
693 */
694tlb_load_linear:
695	/* For now, we assume the linear mapping is contiguous and stops at
696	 * linear_map_top. We also assume the size is a multiple of 1G, thus
697	 * we only use 1G pages for now. That might have to be changed in a
698	 * final implementation, especially when dealing with hypervisors
699	 */
700	__LOAD_PACA_TOC(r11)
701	LOAD_REG_ADDR_ALTTOC(r11, r11, linear_map_top)
702	ld	r10,0(r11)
703	tovirt(10,10)
704	cmpld	cr0,r16,r10
705	bge	tlb_load_linear_fault
706
707	/* MAS1 need whole new setup. */
708	li	r15,(BOOK3E_PAGESZ_1GB<<MAS1_TSIZE_SHIFT)
709	oris	r15,r15,MAS1_VALID@h	/* MAS1 needs V and TSIZE */
710	mtspr	SPRN_MAS1,r15
711
712	/* Already somebody there ? */
713	PPC_TLBSRX_DOT(0,R16)
714	beq	tlb_load_linear_done
715
716	/* Now we build the remaining MAS. MAS0 and 2 should be fine
717	 * with their defaults, which leaves us with MAS 3 and 7. The
718	 * mapping is linear, so we just take the address, clear the
719	 * region bits, and or in the permission bits which are currently
720	 * hard wired
721	 */
722	clrrdi	r10,r16,30		/* 1G page index */
723	clrldi	r10,r10,4		/* clear region bits */
724	ori	r10,r10,MAS3_SR|MAS3_SW|MAS3_SX
725
726	srdi	r16,r10,32
727	mtspr	SPRN_MAS3,r10
728	mtspr	SPRN_MAS7,r16
729
730	tlbwe
731
732tlb_load_linear_done:
733	/* We use the "error" epilog for success as we do want to
734	 * restore to the initial faulting context, whatever it was.
735	 * We do that because we can't resume a fault within a TLB
736	 * miss handler, due to MAS and TLB reservation being clobbered.
737	 */
738	TLB_MISS_EPILOG_ERROR
739	rfi
740
741tlb_load_linear_fault:
742	/* We keep the DEAR and ESR around, this shouldn't have happened */
743	cmpdi	cr0,r14,-1
744	beq	1f
745	TLB_MISS_EPILOG_ERROR_SPECIAL
746	b	exc_data_storage_book3e
7471:	TLB_MISS_EPILOG_ERROR_SPECIAL
748	b	exc_instruction_storage_book3e
749