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