blob: c95eb323e9ae5ac8e07feb28c024127266e405a8 [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
287 * r10 = crap (free to use)
288 */
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 */
296 addi r10,r11,TCD_LOCK
2971: lbarx r15,0,r10
298 cmpdi r15,0
299 bne 2f
300 li r15,1
301 stbcx. r15,0,r10
302 bne 1b
303 .subsection 1
3042: lbz r15,0(r10)
305 cmpdi r15,0
306 bne 2b
307 b 1b
308 .previous
309
310 mfspr r15,SPRN_MAS2
311
312 tlbsx 0,r16
313 mfspr r10,SPRN_MAS1
314 andis. r10,r10,MAS1_VALID@h
315 bne tlb_miss_done_e6500
316
317 /* Undo MAS-damage from the tlbsx */
318 mfspr r10,SPRN_MAS1
319 oris r10,r10,MAS1_VALID@h
320 mtspr SPRN_MAS1,r10
321 mtspr SPRN_MAS2,r15
322
323 /* Now, we need to walk the page tables. First check if we are in
324 * range.
325 */
326 rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
327 bne- tlb_miss_fault_e6500
328
329 rldicl r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3
330 cmpldi cr0,r14,0
331 clrrdi r15,r15,3
332 beq- tlb_miss_fault_e6500 /* No PGDIR, bail */
333 ldx r14,r14,r15 /* grab pgd entry */
334
335 rldicl r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3
336 clrrdi r15,r15,3
337 cmpdi cr0,r14,0
338 bge tlb_miss_fault_e6500 /* Bad pgd entry or hugepage; bail */
339 ldx r14,r14,r15 /* grab pud entry */
340
341 rldicl r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3
342 clrrdi r15,r15,3
343 cmpdi cr0,r14,0
344 bge tlb_miss_fault_e6500
345 ldx r14,r14,r15 /* Grab pmd entry */
346
347 mfspr r10,SPRN_MAS0
348 cmpdi cr0,r14,0
349 bge tlb_miss_fault_e6500
350
351 /* Now we build the MAS for a 2M indirect page:
352 *
353 * MAS 0 : ESEL needs to be filled by software round-robin
354 * MAS 1 : Fully set up
355 * - PID already updated by caller if necessary
356 * - TSIZE for now is base ind page size always
357 * - TID already cleared if necessary
358 * MAS 2 : Default not 2M-aligned, need to be redone
359 * MAS 3+7 : Needs to be done
360 */
361
362 ori r14,r14,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT)
363 mtspr SPRN_MAS7_MAS3,r14
364
365 clrrdi r15,r16,21 /* make EA 2M-aligned */
366 mtspr SPRN_MAS2,r15
367
368 lbz r15,TCD_ESEL_NEXT(r11)
369 lbz r16,TCD_ESEL_MAX(r11)
370 lbz r14,TCD_ESEL_FIRST(r11)
371 rlwimi r10,r15,16,0x00ff0000 /* insert esel_next into MAS0 */
372 addi r15,r15,1 /* increment esel_next */
373 mtspr SPRN_MAS0,r10
374 cmpw r15,r16
375 iseleq r15,r14,r15 /* if next == last use first */
376 stb r15,TCD_ESEL_NEXT(r11)
377
378 tlbwe
379
380tlb_miss_done_e6500:
381 .macro tlb_unlock_e6500
382 li r15,0
383 isync
384 stb r15,TCD_LOCK(r11)
385 .endm
386
387 tlb_unlock_e6500
388 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
389 tlb_epilog_bolted
390 rfi
391
392tlb_miss_kernel_e6500:
393 mfspr r10,SPRN_MAS1
394 ld r14,PACA_KERNELPGD(r13)
395 cmpldi cr0,r15,8 /* Check for vmalloc region */
396 rlwinm r10,r10,0,16,1 /* Clear TID */
397 mtspr SPRN_MAS1,r10
398 beq+ tlb_miss_common_e6500
399
400tlb_miss_fault_e6500:
401 tlb_unlock_e6500
402 /* We need to check if it was an instruction miss */
403 andi. r16,r16,1
404 bne itlb_miss_fault_e6500
405dtlb_miss_fault_e6500:
406 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
407 tlb_epilog_bolted
408 b exc_data_storage_book3e
409itlb_miss_fault_e6500:
410 TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
411 tlb_epilog_bolted
412 b exc_instruction_storage_book3e
Scott Wood9841c792014-01-17 18:36:38 -0600413#endif /* CONFIG_PPC_FSL_BOOK3E */
Scott Wood28efc352013-10-11 19:22:38 -0500414
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000415/**********************************************************************
416 * *
417 * TLB miss handling for Book3E with TLB reservation and HES support *
418 * *
419 **********************************************************************/
420
421
422/* Data TLB miss */
423 START_EXCEPTION(data_tlb_miss)
424 TLB_MISS_PROLOG
425
426 /* Now we handle the fault proper. We only save DEAR in normal
427 * fault case since that's the only interesting values here.
428 * We could probably also optimize by not saving SRR0/1 in the
429 * linear mapping case but I'll leave that for later
430 */
431 mfspr r14,SPRN_ESR
432 mfspr r16,SPRN_DEAR /* get faulting address */
433 srdi r15,r16,60 /* get region */
434 cmpldi cr0,r15,0xc /* linear mapping ? */
435 TLB_MISS_STATS_SAVE_INFO
436 beq tlb_load_linear /* yes -> go to linear map load */
437
438 /* The page tables are mapped virtually linear. At this point, though,
439 * we don't know whether we are trying to fault in a first level
440 * virtual address or a virtual page table address. We can get that
441 * from bit 0x1 of the region ID which we have set for a page table
442 */
443 andi. r10,r15,0x1
444 bne- virt_page_table_tlb_miss
445
446 std r14,EX_TLB_ESR(r12); /* save ESR */
447 std r16,EX_TLB_DEAR(r12); /* save DEAR */
448
449 /* We need _PAGE_PRESENT and _PAGE_ACCESSED set */
450 li r11,_PAGE_PRESENT
451 oris r11,r11,_PAGE_ACCESSED@h
452
453 /* We do the user/kernel test for the PID here along with the RW test
454 */
455 cmpldi cr0,r15,0 /* Check for user region */
456
457 /* We pre-test some combination of permissions to avoid double
458 * faults:
459 *
460 * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE
461 * ESR_ST is 0x00800000
462 * _PAGE_BAP_SW is 0x00000010
463 * So the shift is >> 19. This tests for supervisor writeability.
464 * If the page happens to be supervisor writeable and not user
465 * writeable, we will take a new fault later, but that should be
466 * a rare enough case.
467 *
468 * We also move ESR_ST in _PAGE_DIRTY position
469 * _PAGE_DIRTY is 0x00001000 so the shift is >> 11
470 *
471 * MAS1 is preset for all we need except for TID that needs to
472 * be cleared for kernel translations
473 */
474 rlwimi r11,r14,32-19,27,27
475 rlwimi r11,r14,32-16,19,19
476 beq normal_tlb_miss
477 /* XXX replace the RMW cycles with immediate loads + writes */
4781: mfspr r10,SPRN_MAS1
479 cmpldi cr0,r15,8 /* Check for vmalloc region */
480 rlwinm r10,r10,0,16,1 /* Clear TID */
481 mtspr SPRN_MAS1,r10
482 beq+ normal_tlb_miss
483
484 /* We got a crappy address, just fault with whatever DEAR and ESR
485 * are here
486 */
487 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
488 TLB_MISS_EPILOG_ERROR
489 b exc_data_storage_book3e
490
491/* Instruction TLB miss */
492 START_EXCEPTION(instruction_tlb_miss)
493 TLB_MISS_PROLOG
494
495 /* If we take a recursive fault, the second level handler may need
496 * to know whether we are handling a data or instruction fault in
497 * order to get to the right store fault handler. We provide that
498 * info by writing a crazy value in ESR in our exception frame
499 */
500 li r14,-1 /* store to exception frame is done later */
501
502 /* Now we handle the fault proper. We only save DEAR in the non
503 * linear mapping case since we know the linear mapping case will
504 * not re-enter. We could indeed optimize and also not save SRR0/1
505 * in the linear mapping case but I'll leave that for later
506 *
507 * Faulting address is SRR0 which is already in r16
508 */
509 srdi r15,r16,60 /* get region */
510 cmpldi cr0,r15,0xc /* linear mapping ? */
511 TLB_MISS_STATS_SAVE_INFO
512 beq tlb_load_linear /* yes -> go to linear map load */
513
514 /* We do the user/kernel test for the PID here along with the RW test
515 */
Benjamin Herrenschmidtea3cc332009-08-18 19:00:34 +0000516 li r11,_PAGE_PRESENT|_PAGE_EXEC /* Base perm */
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000517 oris r11,r11,_PAGE_ACCESSED@h
518
519 cmpldi cr0,r15,0 /* Check for user region */
520 std r14,EX_TLB_ESR(r12) /* write crazy -1 to frame */
521 beq normal_tlb_miss
Kumar Gala82ae5ea2010-11-17 07:20:32 +0000522
523 li r11,_PAGE_PRESENT|_PAGE_BAP_SX /* Base perm */
524 oris r11,r11,_PAGE_ACCESSED@h
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000525 /* XXX replace the RMW cycles with immediate loads + writes */
Kumar Gala82ae5ea2010-11-17 07:20:32 +0000526 mfspr r10,SPRN_MAS1
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000527 cmpldi cr0,r15,8 /* Check for vmalloc region */
528 rlwinm r10,r10,0,16,1 /* Clear TID */
529 mtspr SPRN_MAS1,r10
530 beq+ normal_tlb_miss
531
532 /* We got a crappy address, just fault */
533 TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
534 TLB_MISS_EPILOG_ERROR
535 b exc_instruction_storage_book3e
536
537/*
538 * This is the guts of the first-level TLB miss handler for direct
539 * misses. We are entered with:
540 *
541 * r16 = faulting address
542 * r15 = region ID
543 * r14 = crap (free to use)
544 * r13 = PACA
545 * r12 = TLB exception frame in PACA
546 * r11 = PTE permission mask
547 * r10 = crap (free to use)
548 */
549normal_tlb_miss:
550 /* So we first construct the page table address. We do that by
551 * shifting the bottom of the address (not the region ID) by
552 * PAGE_SHIFT-3, clearing the bottom 3 bits (get a PTE ptr) and
553 * or'ing the fourth high bit.
554 *
555 * NOTE: For 64K pages, we do things slightly differently in
556 * order to handle the weird page table format used by linux
557 */
558 ori r10,r15,0x1
559#ifdef CONFIG_PPC_64K_PAGES
560 /* For the top bits, 16 bytes per PTE */
561 rldicl r14,r16,64-(PAGE_SHIFT-4),PAGE_SHIFT-4+4
562 /* Now create the bottom bits as 0 in position 0x8000 and
563 * the rest calculated for 8 bytes per PTE
564 */
565 rldicl r15,r16,64-(PAGE_SHIFT-3),64-15
566 /* Insert the bottom bits in */
567 rlwimi r14,r15,0,16,31
568#else
569 rldicl r14,r16,64-(PAGE_SHIFT-3),PAGE_SHIFT-3+4
570#endif
571 sldi r15,r10,60
572 clrrdi r14,r14,3
573 or r10,r15,r14
574
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000575BEGIN_MMU_FTR_SECTION
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300576 /* Set the TLB reservation and search for existing entry. Then load
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000577 * the entry.
578 */
Michael Neuling962cffb2012-06-25 13:33:25 +0000579 PPC_TLBSRX_DOT(0,R16)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000580 ld r14,0(r10)
581 beq normal_tlb_miss_done
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000582MMU_FTR_SECTION_ELSE
583 ld r14,0(r10)
584ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_USE_TLBRSRV)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000585
586finish_normal_tlb_miss:
587 /* Check if required permissions are met */
588 andc. r15,r11,r14
589 bne- normal_tlb_miss_access_fault
590
591 /* Now we build the MAS:
592 *
593 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
594 * MAS 1 : Almost fully setup
595 * - PID already updated by caller if necessary
596 * - TSIZE need change if !base page size, not
597 * yet implemented for now
598 * MAS 2 : Defaults not useful, need to be redone
599 * MAS 3+7 : Needs to be done
600 *
601 * TODO: mix up code below for better scheduling
602 */
603 clrrdi r11,r16,12 /* Clear low crap in EA */
604 rlwimi r11,r14,32-19,27,31 /* Insert WIMGE */
605 mtspr SPRN_MAS2,r11
606
607 /* Check page size, if not standard, update MAS1 */
608 rldicl r11,r14,64-8,64-8
609#ifdef CONFIG_PPC_64K_PAGES
610 cmpldi cr0,r11,BOOK3E_PAGESZ_64K
611#else
612 cmpldi cr0,r11,BOOK3E_PAGESZ_4K
613#endif
614 beq- 1f
615 mfspr r11,SPRN_MAS1
616 rlwimi r11,r14,31,21,24
617 rlwinm r11,r11,0,21,19
618 mtspr SPRN_MAS1,r11
6191:
620 /* Move RPN in position */
621 rldicr r11,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
622 clrldi r15,r11,12 /* Clear crap at the top */
623 rlwimi r15,r14,32-8,22,25 /* Move in U bits */
624 rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */
625
626 /* Mask out SW and UW if !DIRTY (XXX optimize this !) */
627 andi. r11,r14,_PAGE_DIRTY
628 bne 1f
629 li r11,MAS3_SW|MAS3_UW
630 andc r15,r15,r11
Kumar Galadf5d6ec2009-08-24 15:52:48 +00006311:
632BEGIN_MMU_FTR_SECTION
633 srdi r16,r15,32
634 mtspr SPRN_MAS3,r15
635 mtspr SPRN_MAS7,r16
636MMU_FTR_SECTION_ELSE
637 mtspr SPRN_MAS7_MAS3,r15
638ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000639
640 tlbwe
641
642normal_tlb_miss_done:
643 /* We don't bother with restoring DEAR or ESR since we know we are
644 * level 0 and just going back to userland. They are only needed
645 * if you are going to take an access fault
646 */
647 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
648 TLB_MISS_EPILOG_SUCCESS
649 rfi
650
651normal_tlb_miss_access_fault:
652 /* We need to check if it was an instruction miss */
Benjamin Herrenschmidtea3cc332009-08-18 19:00:34 +0000653 andi. r10,r11,_PAGE_EXEC
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000654 bne 1f
655 ld r14,EX_TLB_DEAR(r12)
656 ld r15,EX_TLB_ESR(r12)
657 mtspr SPRN_DEAR,r14
658 mtspr SPRN_ESR,r15
659 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
660 TLB_MISS_EPILOG_ERROR
661 b exc_data_storage_book3e
6621: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
663 TLB_MISS_EPILOG_ERROR
664 b exc_instruction_storage_book3e
665
666
667/*
668 * This is the guts of the second-level TLB miss handler for direct
669 * misses. We are entered with:
670 *
671 * r16 = virtual page table faulting address
672 * r15 = region (top 4 bits of address)
673 * r14 = crap (free to use)
674 * r13 = PACA
675 * r12 = TLB exception frame in PACA
676 * r11 = crap (free to use)
677 * r10 = crap (free to use)
678 *
679 * Note that this should only ever be called as a second level handler
680 * with the current scheme when using SW load.
681 * That means we can always get the original fault DEAR at
682 * EX_TLB_DEAR-EX_TLB_SIZE(r12)
683 *
684 * It can be re-entered by the linear mapping miss handler. However, to
685 * avoid too much complication, it will restart the whole fault at level
686 * 0 so we don't care too much about clobbers
687 *
688 * XXX That code was written back when we couldn't clobber r14. We can now,
689 * so we could probably optimize things a bit
690 */
691virt_page_table_tlb_miss:
692 /* Are we hitting a kernel page table ? */
693 andi. r10,r15,0x8
694
695 /* The cool thing now is that r10 contains 0 for user and 8 for kernel,
696 * and we happen to have the swapper_pg_dir at offset 8 from the user
697 * pgdir in the PACA :-).
698 */
699 add r11,r10,r13
700
701 /* If kernel, we need to clear MAS1 TID */
702 beq 1f
703 /* XXX replace the RMW cycles with immediate loads + writes */
704 mfspr r10,SPRN_MAS1
705 rlwinm r10,r10,0,16,1 /* Clear TID */
706 mtspr SPRN_MAS1,r10
7071:
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000708BEGIN_MMU_FTR_SECTION
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000709 /* Search if we already have a TLB entry for that virtual address, and
710 * if we do, bail out.
711 */
Michael Neuling962cffb2012-06-25 13:33:25 +0000712 PPC_TLBSRX_DOT(0,R16)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000713 beq virt_page_table_tlb_miss_done
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000714END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000715
716 /* Now, we need to walk the page tables. First check if we are in
717 * range.
718 */
719 rldicl. r10,r16,64-(VPTE_INDEX_SIZE+3),VPTE_INDEX_SIZE+3+4
720 bne- virt_page_table_tlb_miss_fault
721
722 /* Get the PGD pointer */
723 ld r15,PACAPGD(r11)
724 cmpldi cr0,r15,0
725 beq- virt_page_table_tlb_miss_fault
726
727 /* Get to PGD entry */
728 rldicl r11,r16,64-VPTE_PGD_SHIFT,64-PGD_INDEX_SIZE-3
729 clrrdi r10,r11,3
730 ldx r15,r10,r15
Becky Bruce41151e72011-06-28 09:54:48 +0000731 cmpdi cr0,r15,0
732 bge virt_page_table_tlb_miss_fault
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000733
734#ifndef CONFIG_PPC_64K_PAGES
735 /* Get to PUD entry */
736 rldicl r11,r16,64-VPTE_PUD_SHIFT,64-PUD_INDEX_SIZE-3
737 clrrdi r10,r11,3
738 ldx r15,r10,r15
Becky Bruce41151e72011-06-28 09:54:48 +0000739 cmpdi cr0,r15,0
740 bge virt_page_table_tlb_miss_fault
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000741#endif /* CONFIG_PPC_64K_PAGES */
742
743 /* Get to PMD entry */
744 rldicl r11,r16,64-VPTE_PMD_SHIFT,64-PMD_INDEX_SIZE-3
745 clrrdi r10,r11,3
746 ldx r15,r10,r15
Becky Bruce41151e72011-06-28 09:54:48 +0000747 cmpdi cr0,r15,0
748 bge virt_page_table_tlb_miss_fault
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000749
750 /* Ok, we're all right, we can now create a kernel translation for
751 * a 4K or 64K page from r16 -> r15.
752 */
753 /* Now we build the MAS:
754 *
755 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
756 * MAS 1 : Almost fully setup
757 * - PID already updated by caller if necessary
758 * - TSIZE for now is base page size always
759 * MAS 2 : Use defaults
760 * MAS 3+7 : Needs to be done
761 *
762 * So we only do MAS 2 and 3 for now...
763 */
764 clrldi r11,r15,4 /* remove region ID from RPN */
765 ori r10,r11,1 /* Or-in SR */
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000766
767BEGIN_MMU_FTR_SECTION
768 srdi r16,r10,32
769 mtspr SPRN_MAS3,r10
770 mtspr SPRN_MAS7,r16
771MMU_FTR_SECTION_ELSE
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000772 mtspr SPRN_MAS7_MAS3,r10
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000773ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000774
775 tlbwe
776
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000777BEGIN_MMU_FTR_SECTION
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000778virt_page_table_tlb_miss_done:
779
780 /* We have overriden MAS2:EPN but currently our primary TLB miss
781 * handler will always restore it so that should not be an issue,
782 * if we ever optimize the primary handler to not write MAS2 on
783 * some cases, we'll have to restore MAS2:EPN here based on the
784 * original fault's DEAR. If we do that we have to modify the
785 * ITLB miss handler to also store SRR0 in the exception frame
786 * as DEAR.
787 *
788 * However, one nasty thing we did is we cleared the reservation
789 * (well, potentially we did). We do a trick here thus if we
790 * are not a level 0 exception (we interrupted the TLB miss) we
791 * offset the return address by -4 in order to replay the tlbsrx
792 * instruction there
793 */
794 subf r10,r13,r12
795 cmpldi cr0,r10,PACA_EXTLB+EX_TLB_SIZE
796 bne- 1f
797 ld r11,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
798 addi r10,r11,-4
799 std r10,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
8001:
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000801END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000802 /* Return to caller, normal case */
803 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK);
804 TLB_MISS_EPILOG_SUCCESS
805 rfi
806
807virt_page_table_tlb_miss_fault:
808 /* If we fault here, things are a little bit tricky. We need to call
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300809 * either data or instruction store fault, and we need to retrieve
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000810 * the original fault address and ESR (for data).
811 *
812 * The thing is, we know that in normal circumstances, this is
813 * always called as a second level tlb miss for SW load or as a first
814 * level TLB miss for HW load, so we should be able to peek at the
Sylvestre Ledruf65e51d2011-04-04 15:04:46 -0700815 * relevant information in the first exception frame in the PACA.
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000816 *
817 * However, we do need to double check that, because we may just hit
818 * a stray kernel pointer or a userland attack trying to hit those
819 * areas. If that is the case, we do a data fault. (We can't get here
820 * from an instruction tlb miss anyway).
821 *
822 * Note also that when going to a fault, we must unwind the previous
823 * level as well. Since we are doing that, we don't need to clear or
824 * restore the TLB reservation neither.
825 */
826 subf r10,r13,r12
827 cmpldi cr0,r10,PACA_EXTLB+EX_TLB_SIZE
828 bne- virt_page_table_tlb_miss_whacko_fault
829
830 /* We dig the original DEAR and ESR from slot 0 */
831 ld r15,EX_TLB_DEAR+PACA_EXTLB(r13)
832 ld r16,EX_TLB_ESR+PACA_EXTLB(r13)
833
834 /* We check for the "special" ESR value for instruction faults */
835 cmpdi cr0,r16,-1
836 beq 1f
837 mtspr SPRN_DEAR,r15
838 mtspr SPRN_ESR,r16
839 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT);
840 TLB_MISS_EPILOG_ERROR
841 b exc_data_storage_book3e
8421: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT);
843 TLB_MISS_EPILOG_ERROR
844 b exc_instruction_storage_book3e
845
846virt_page_table_tlb_miss_whacko_fault:
847 /* The linear fault will restart everything so ESR and DEAR will
848 * not have been clobbered, let's just fault with what we have
849 */
850 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_FAULT);
851 TLB_MISS_EPILOG_ERROR
852 b exc_data_storage_book3e
853
854
855/**************************************************************
856 * *
857 * TLB miss handling for Book3E with hw page table support *
858 * *
859 **************************************************************/
860
861
862/* Data TLB miss */
863 START_EXCEPTION(data_tlb_miss_htw)
864 TLB_MISS_PROLOG
865
866 /* Now we handle the fault proper. We only save DEAR in normal
867 * fault case since that's the only interesting values here.
868 * We could probably also optimize by not saving SRR0/1 in the
869 * linear mapping case but I'll leave that for later
870 */
871 mfspr r14,SPRN_ESR
872 mfspr r16,SPRN_DEAR /* get faulting address */
873 srdi r11,r16,60 /* get region */
874 cmpldi cr0,r11,0xc /* linear mapping ? */
875 TLB_MISS_STATS_SAVE_INFO
876 beq tlb_load_linear /* yes -> go to linear map load */
877
878 /* We do the user/kernel test for the PID here along with the RW test
879 */
880 cmpldi cr0,r11,0 /* Check for user region */
881 ld r15,PACAPGD(r13) /* Load user pgdir */
882 beq htw_tlb_miss
883
884 /* XXX replace the RMW cycles with immediate loads + writes */
8851: mfspr r10,SPRN_MAS1
886 cmpldi cr0,r11,8 /* Check for vmalloc region */
887 rlwinm r10,r10,0,16,1 /* Clear TID */
888 mtspr SPRN_MAS1,r10
889 ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */
890 beq+ htw_tlb_miss
891
892 /* We got a crappy address, just fault with whatever DEAR and ESR
893 * are here
894 */
895 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
896 TLB_MISS_EPILOG_ERROR
897 b exc_data_storage_book3e
898
899/* Instruction TLB miss */
900 START_EXCEPTION(instruction_tlb_miss_htw)
901 TLB_MISS_PROLOG
902
903 /* If we take a recursive fault, the second level handler may need
904 * to know whether we are handling a data or instruction fault in
905 * order to get to the right store fault handler. We provide that
906 * info by keeping a crazy value for ESR in r14
907 */
908 li r14,-1 /* store to exception frame is done later */
909
910 /* Now we handle the fault proper. We only save DEAR in the non
911 * linear mapping case since we know the linear mapping case will
912 * not re-enter. We could indeed optimize and also not save SRR0/1
913 * in the linear mapping case but I'll leave that for later
914 *
915 * Faulting address is SRR0 which is already in r16
916 */
917 srdi r11,r16,60 /* get region */
918 cmpldi cr0,r11,0xc /* linear mapping ? */
919 TLB_MISS_STATS_SAVE_INFO
920 beq tlb_load_linear /* yes -> go to linear map load */
921
922 /* We do the user/kernel test for the PID here along with the RW test
923 */
924 cmpldi cr0,r11,0 /* Check for user region */
925 ld r15,PACAPGD(r13) /* Load user pgdir */
926 beq htw_tlb_miss
927
928 /* XXX replace the RMW cycles with immediate loads + writes */
9291: mfspr r10,SPRN_MAS1
930 cmpldi cr0,r11,8 /* Check for vmalloc region */
931 rlwinm r10,r10,0,16,1 /* Clear TID */
932 mtspr SPRN_MAS1,r10
933 ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */
934 beq+ htw_tlb_miss
935
936 /* We got a crappy address, just fault */
937 TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
938 TLB_MISS_EPILOG_ERROR
939 b exc_instruction_storage_book3e
940
941
942/*
943 * This is the guts of the second-level TLB miss handler for direct
944 * misses. We are entered with:
945 *
946 * r16 = virtual page table faulting address
947 * r15 = PGD pointer
948 * r14 = ESR
949 * r13 = PACA
950 * r12 = TLB exception frame in PACA
951 * r11 = crap (free to use)
952 * r10 = crap (free to use)
953 *
954 * It can be re-entered by the linear mapping miss handler. However, to
955 * avoid too much complication, it will save/restore things for us
956 */
957htw_tlb_miss:
958 /* Search if we already have a TLB entry for that virtual address, and
959 * if we do, bail out.
960 *
961 * MAS1:IND should be already set based on MAS4
962 */
Michael Neuling962cffb2012-06-25 13:33:25 +0000963 PPC_TLBSRX_DOT(0,R16)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000964 beq htw_tlb_miss_done
965
966 /* Now, we need to walk the page tables. First check if we are in
967 * range.
968 */
969 rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
970 bne- htw_tlb_miss_fault
971
972 /* Get the PGD pointer */
973 cmpldi cr0,r15,0
974 beq- htw_tlb_miss_fault
975
976 /* Get to PGD entry */
977 rldicl r11,r16,64-(PGDIR_SHIFT-3),64-PGD_INDEX_SIZE-3
978 clrrdi r10,r11,3
979 ldx r15,r10,r15
Becky Bruce41151e72011-06-28 09:54:48 +0000980 cmpdi cr0,r15,0
981 bge htw_tlb_miss_fault
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000982
983#ifndef CONFIG_PPC_64K_PAGES
984 /* Get to PUD entry */
985 rldicl r11,r16,64-(PUD_SHIFT-3),64-PUD_INDEX_SIZE-3
986 clrrdi r10,r11,3
987 ldx r15,r10,r15
Becky Bruce41151e72011-06-28 09:54:48 +0000988 cmpdi cr0,r15,0
989 bge htw_tlb_miss_fault
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000990#endif /* CONFIG_PPC_64K_PAGES */
991
992 /* Get to PMD entry */
993 rldicl r11,r16,64-(PMD_SHIFT-3),64-PMD_INDEX_SIZE-3
994 clrrdi r10,r11,3
995 ldx r15,r10,r15
Becky Bruce41151e72011-06-28 09:54:48 +0000996 cmpdi cr0,r15,0
997 bge htw_tlb_miss_fault
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000998
999 /* Ok, we're all right, we can now create an indirect entry for
1000 * a 1M or 256M page.
1001 *
1002 * The last trick is now that because we use "half" pages for
1003 * the HTW (1M IND is 2K and 256M IND is 32K) we need to account
1004 * for an added LSB bit to the RPN. For 64K pages, there is no
1005 * problem as we already use 32K arrays (half PTE pages), but for
1006 * 4K page we need to extract a bit from the virtual address and
1007 * insert it into the "PA52" bit of the RPN.
1008 */
1009#ifndef CONFIG_PPC_64K_PAGES
1010 rlwimi r15,r16,32-9,20,20
1011#endif
1012 /* Now we build the MAS:
1013 *
1014 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
1015 * MAS 1 : Almost fully setup
1016 * - PID already updated by caller if necessary
1017 * - TSIZE for now is base ind page size always
1018 * MAS 2 : Use defaults
1019 * MAS 3+7 : Needs to be done
1020 */
1021#ifdef CONFIG_PPC_64K_PAGES
1022 ori r10,r15,(BOOK3E_PAGESZ_64K << MAS3_SPSIZE_SHIFT)
1023#else
1024 ori r10,r15,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT)
1025#endif
Kumar Galadf5d6ec2009-08-24 15:52:48 +00001026
1027BEGIN_MMU_FTR_SECTION
1028 srdi r16,r10,32
1029 mtspr SPRN_MAS3,r10
1030 mtspr SPRN_MAS7,r16
1031MMU_FTR_SECTION_ELSE
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +00001032 mtspr SPRN_MAS7_MAS3,r10
Kumar Galadf5d6ec2009-08-24 15:52:48 +00001033ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +00001034
1035 tlbwe
1036
1037htw_tlb_miss_done:
1038 /* We don't bother with restoring DEAR or ESR since we know we are
1039 * level 0 and just going back to userland. They are only needed
1040 * if you are going to take an access fault
1041 */
1042 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK)
1043 TLB_MISS_EPILOG_SUCCESS
1044 rfi
1045
1046htw_tlb_miss_fault:
1047 /* We need to check if it was an instruction miss. We know this
1048 * though because r14 would contain -1
1049 */
1050 cmpdi cr0,r14,-1
1051 beq 1f
1052 mtspr SPRN_DEAR,r16
1053 mtspr SPRN_ESR,r14
1054 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT)
1055 TLB_MISS_EPILOG_ERROR
1056 b exc_data_storage_book3e
10571: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT)
1058 TLB_MISS_EPILOG_ERROR
1059 b exc_instruction_storage_book3e
1060
1061/*
1062 * This is the guts of "any" level TLB miss handler for kernel linear
1063 * mapping misses. We are entered with:
1064 *
1065 *
1066 * r16 = faulting address
1067 * r15 = crap (free to use)
1068 * r14 = ESR (data) or -1 (instruction)
1069 * r13 = PACA
1070 * r12 = TLB exception frame in PACA
1071 * r11 = crap (free to use)
1072 * r10 = crap (free to use)
1073 *
1074 * In addition we know that we will not re-enter, so in theory, we could
1075 * use a simpler epilog not restoring SRR0/1 etc.. but we'll do that later.
1076 *
1077 * We also need to be careful about MAS registers here & TLB reservation,
1078 * as we know we'll have clobbered them if we interrupt the main TLB miss
1079 * handlers in which case we probably want to do a full restart at level
1080 * 0 rather than saving / restoring the MAS.
1081 *
1082 * Note: If we care about performance of that core, we can easily shuffle
1083 * a few things around
1084 */
1085tlb_load_linear:
1086 /* For now, we assume the linear mapping is contiguous and stops at
1087 * linear_map_top. We also assume the size is a multiple of 1G, thus
1088 * we only use 1G pages for now. That might have to be changed in a
1089 * final implementation, especially when dealing with hypervisors
1090 */
1091 ld r11,PACATOC(r13)
1092 ld r11,linear_map_top@got(r11)
1093 ld r10,0(r11)
1094 cmpld cr0,r10,r16
1095 bge tlb_load_linear_fault
1096
1097 /* MAS1 need whole new setup. */
1098 li r15,(BOOK3E_PAGESZ_1GB<<MAS1_TSIZE_SHIFT)
1099 oris r15,r15,MAS1_VALID@h /* MAS1 needs V and TSIZE */
1100 mtspr SPRN_MAS1,r15
1101
1102 /* Already somebody there ? */
Michael Neuling962cffb2012-06-25 13:33:25 +00001103 PPC_TLBSRX_DOT(0,R16)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +00001104 beq tlb_load_linear_done
1105
1106 /* Now we build the remaining MAS. MAS0 and 2 should be fine
1107 * with their defaults, which leaves us with MAS 3 and 7. The
1108 * mapping is linear, so we just take the address, clear the
1109 * region bits, and or in the permission bits which are currently
1110 * hard wired
1111 */
1112 clrrdi r10,r16,30 /* 1G page index */
1113 clrldi r10,r10,4 /* clear region bits */
1114 ori r10,r10,MAS3_SR|MAS3_SW|MAS3_SX
Kumar Galadf5d6ec2009-08-24 15:52:48 +00001115
1116BEGIN_MMU_FTR_SECTION
1117 srdi r16,r10,32
1118 mtspr SPRN_MAS3,r10
1119 mtspr SPRN_MAS7,r16
1120MMU_FTR_SECTION_ELSE
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +00001121 mtspr SPRN_MAS7_MAS3,r10
Kumar Galadf5d6ec2009-08-24 15:52:48 +00001122ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +00001123
1124 tlbwe
1125
1126tlb_load_linear_done:
1127 /* We use the "error" epilog for success as we do want to
1128 * restore to the initial faulting context, whatever it was.
1129 * We do that because we can't resume a fault within a TLB
1130 * miss handler, due to MAS and TLB reservation being clobbered.
1131 */
1132 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_LINEAR)
1133 TLB_MISS_EPILOG_ERROR
1134 rfi
1135
1136tlb_load_linear_fault:
1137 /* We keep the DEAR and ESR around, this shouldn't have happened */
1138 cmpdi cr0,r14,-1
1139 beq 1f
1140 TLB_MISS_EPILOG_ERROR_SPECIAL
1141 b exc_data_storage_book3e
11421: TLB_MISS_EPILOG_ERROR_SPECIAL
1143 b exc_instruction_storage_book3e
1144
1145
1146#ifdef CONFIG_BOOK3E_MMU_TLB_STATS
1147.tlb_stat_inc:
11481: ldarx r8,0,r9
1149 addi r8,r8,1
1150 stdcx. r8,0,r9
1151 bne- 1b
1152 blr
1153#endif