blob: ef1cccf71173760e202ccfd881f726ce7201ae31 [file] [log] [blame]
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +00001/*
2 * Low leve TLB miss handlers for Book3E
3 *
4 * Copyright (C) 2008-2009
5 * Ben. Herrenschmidt (benh@kernel.crashing.org), IBM Corp.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <asm/processor.h>
14#include <asm/reg.h>
15#include <asm/page.h>
16#include <asm/mmu.h>
17#include <asm/ppc_asm.h>
18#include <asm/asm-offsets.h>
19#include <asm/cputable.h>
20#include <asm/pgtable.h>
21#include <asm/reg.h>
22#include <asm/exception-64e.h>
23#include <asm/ppc-opcode.h>
24
25#ifdef CONFIG_PPC_64K_PAGES
26#define VPTE_PMD_SHIFT (PTE_INDEX_SIZE+1)
27#else
28#define VPTE_PMD_SHIFT (PTE_INDEX_SIZE)
29#endif
30#define VPTE_PUD_SHIFT (VPTE_PMD_SHIFT + PMD_INDEX_SIZE)
31#define VPTE_PGD_SHIFT (VPTE_PUD_SHIFT + PUD_INDEX_SIZE)
32#define VPTE_INDEX_SIZE (VPTE_PGD_SHIFT + PGD_INDEX_SIZE)
33
34
35/**********************************************************************
36 * *
37 * TLB miss handling for Book3E with TLB reservation and HES support *
38 * *
39 **********************************************************************/
40
41
42/* Data TLB miss */
43 START_EXCEPTION(data_tlb_miss)
44 TLB_MISS_PROLOG
45
46 /* Now we handle the fault proper. We only save DEAR in normal
47 * fault case since that's the only interesting values here.
48 * We could probably also optimize by not saving SRR0/1 in the
49 * linear mapping case but I'll leave that for later
50 */
51 mfspr r14,SPRN_ESR
52 mfspr r16,SPRN_DEAR /* get faulting address */
53 srdi r15,r16,60 /* get region */
54 cmpldi cr0,r15,0xc /* linear mapping ? */
55 TLB_MISS_STATS_SAVE_INFO
56 beq tlb_load_linear /* yes -> go to linear map load */
57
58 /* The page tables are mapped virtually linear. At this point, though,
59 * we don't know whether we are trying to fault in a first level
60 * virtual address or a virtual page table address. We can get that
61 * from bit 0x1 of the region ID which we have set for a page table
62 */
63 andi. r10,r15,0x1
64 bne- virt_page_table_tlb_miss
65
66 std r14,EX_TLB_ESR(r12); /* save ESR */
67 std r16,EX_TLB_DEAR(r12); /* save DEAR */
68
69 /* We need _PAGE_PRESENT and _PAGE_ACCESSED set */
70 li r11,_PAGE_PRESENT
71 oris r11,r11,_PAGE_ACCESSED@h
72
73 /* We do the user/kernel test for the PID here along with the RW test
74 */
75 cmpldi cr0,r15,0 /* Check for user region */
76
77 /* We pre-test some combination of permissions to avoid double
78 * faults:
79 *
80 * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE
81 * ESR_ST is 0x00800000
82 * _PAGE_BAP_SW is 0x00000010
83 * So the shift is >> 19. This tests for supervisor writeability.
84 * If the page happens to be supervisor writeable and not user
85 * writeable, we will take a new fault later, but that should be
86 * a rare enough case.
87 *
88 * We also move ESR_ST in _PAGE_DIRTY position
89 * _PAGE_DIRTY is 0x00001000 so the shift is >> 11
90 *
91 * MAS1 is preset for all we need except for TID that needs to
92 * be cleared for kernel translations
93 */
94 rlwimi r11,r14,32-19,27,27
95 rlwimi r11,r14,32-16,19,19
96 beq normal_tlb_miss
97 /* XXX replace the RMW cycles with immediate loads + writes */
981: mfspr r10,SPRN_MAS1
99 cmpldi cr0,r15,8 /* Check for vmalloc region */
100 rlwinm r10,r10,0,16,1 /* Clear TID */
101 mtspr SPRN_MAS1,r10
102 beq+ normal_tlb_miss
103
104 /* We got a crappy address, just fault with whatever DEAR and ESR
105 * are here
106 */
107 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
108 TLB_MISS_EPILOG_ERROR
109 b exc_data_storage_book3e
110
111/* Instruction TLB miss */
112 START_EXCEPTION(instruction_tlb_miss)
113 TLB_MISS_PROLOG
114
115 /* If we take a recursive fault, the second level handler may need
116 * to know whether we are handling a data or instruction fault in
117 * order to get to the right store fault handler. We provide that
118 * info by writing a crazy value in ESR in our exception frame
119 */
120 li r14,-1 /* store to exception frame is done later */
121
122 /* Now we handle the fault proper. We only save DEAR in the non
123 * linear mapping case since we know the linear mapping case will
124 * not re-enter. We could indeed optimize and also not save SRR0/1
125 * in the linear mapping case but I'll leave that for later
126 *
127 * Faulting address is SRR0 which is already in r16
128 */
129 srdi r15,r16,60 /* get region */
130 cmpldi cr0,r15,0xc /* linear mapping ? */
131 TLB_MISS_STATS_SAVE_INFO
132 beq tlb_load_linear /* yes -> go to linear map load */
133
134 /* We do the user/kernel test for the PID here along with the RW test
135 */
Benjamin Herrenschmidtea3cc332009-08-18 19:00:34 +0000136 li r11,_PAGE_PRESENT|_PAGE_EXEC /* Base perm */
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000137 oris r11,r11,_PAGE_ACCESSED@h
138
139 cmpldi cr0,r15,0 /* Check for user region */
140 std r14,EX_TLB_ESR(r12) /* write crazy -1 to frame */
141 beq normal_tlb_miss
142 /* XXX replace the RMW cycles with immediate loads + writes */
1431: mfspr r10,SPRN_MAS1
144 cmpldi cr0,r15,8 /* Check for vmalloc region */
145 rlwinm r10,r10,0,16,1 /* Clear TID */
146 mtspr SPRN_MAS1,r10
147 beq+ normal_tlb_miss
148
149 /* We got a crappy address, just fault */
150 TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
151 TLB_MISS_EPILOG_ERROR
152 b exc_instruction_storage_book3e
153
154/*
155 * This is the guts of the first-level TLB miss handler for direct
156 * misses. We are entered with:
157 *
158 * r16 = faulting address
159 * r15 = region ID
160 * r14 = crap (free to use)
161 * r13 = PACA
162 * r12 = TLB exception frame in PACA
163 * r11 = PTE permission mask
164 * r10 = crap (free to use)
165 */
166normal_tlb_miss:
167 /* So we first construct the page table address. We do that by
168 * shifting the bottom of the address (not the region ID) by
169 * PAGE_SHIFT-3, clearing the bottom 3 bits (get a PTE ptr) and
170 * or'ing the fourth high bit.
171 *
172 * NOTE: For 64K pages, we do things slightly differently in
173 * order to handle the weird page table format used by linux
174 */
175 ori r10,r15,0x1
176#ifdef CONFIG_PPC_64K_PAGES
177 /* For the top bits, 16 bytes per PTE */
178 rldicl r14,r16,64-(PAGE_SHIFT-4),PAGE_SHIFT-4+4
179 /* Now create the bottom bits as 0 in position 0x8000 and
180 * the rest calculated for 8 bytes per PTE
181 */
182 rldicl r15,r16,64-(PAGE_SHIFT-3),64-15
183 /* Insert the bottom bits in */
184 rlwimi r14,r15,0,16,31
185#else
186 rldicl r14,r16,64-(PAGE_SHIFT-3),PAGE_SHIFT-3+4
187#endif
188 sldi r15,r10,60
189 clrrdi r14,r14,3
190 or r10,r15,r14
191
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000192BEGIN_MMU_FTR_SECTION
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000193 /* Set the TLB reservation and seach for existing entry. Then load
194 * the entry.
195 */
196 PPC_TLBSRX_DOT(0,r16)
197 ld r14,0(r10)
198 beq normal_tlb_miss_done
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000199MMU_FTR_SECTION_ELSE
200 ld r14,0(r10)
201ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_USE_TLBRSRV)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000202
203finish_normal_tlb_miss:
204 /* Check if required permissions are met */
205 andc. r15,r11,r14
206 bne- normal_tlb_miss_access_fault
207
208 /* Now we build the MAS:
209 *
210 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
211 * MAS 1 : Almost fully setup
212 * - PID already updated by caller if necessary
213 * - TSIZE need change if !base page size, not
214 * yet implemented for now
215 * MAS 2 : Defaults not useful, need to be redone
216 * MAS 3+7 : Needs to be done
217 *
218 * TODO: mix up code below for better scheduling
219 */
220 clrrdi r11,r16,12 /* Clear low crap in EA */
221 rlwimi r11,r14,32-19,27,31 /* Insert WIMGE */
222 mtspr SPRN_MAS2,r11
223
224 /* Check page size, if not standard, update MAS1 */
225 rldicl r11,r14,64-8,64-8
226#ifdef CONFIG_PPC_64K_PAGES
227 cmpldi cr0,r11,BOOK3E_PAGESZ_64K
228#else
229 cmpldi cr0,r11,BOOK3E_PAGESZ_4K
230#endif
231 beq- 1f
232 mfspr r11,SPRN_MAS1
233 rlwimi r11,r14,31,21,24
234 rlwinm r11,r11,0,21,19
235 mtspr SPRN_MAS1,r11
2361:
237 /* Move RPN in position */
238 rldicr r11,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
239 clrldi r15,r11,12 /* Clear crap at the top */
240 rlwimi r15,r14,32-8,22,25 /* Move in U bits */
241 rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */
242
243 /* Mask out SW and UW if !DIRTY (XXX optimize this !) */
244 andi. r11,r14,_PAGE_DIRTY
245 bne 1f
246 li r11,MAS3_SW|MAS3_UW
247 andc r15,r15,r11
Kumar Galadf5d6ec2009-08-24 15:52:48 +00002481:
249BEGIN_MMU_FTR_SECTION
250 srdi r16,r15,32
251 mtspr SPRN_MAS3,r15
252 mtspr SPRN_MAS7,r16
253MMU_FTR_SECTION_ELSE
254 mtspr SPRN_MAS7_MAS3,r15
255ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000256
257 tlbwe
258
259normal_tlb_miss_done:
260 /* We don't bother with restoring DEAR or ESR since we know we are
261 * level 0 and just going back to userland. They are only needed
262 * if you are going to take an access fault
263 */
264 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
265 TLB_MISS_EPILOG_SUCCESS
266 rfi
267
268normal_tlb_miss_access_fault:
269 /* We need to check if it was an instruction miss */
Benjamin Herrenschmidtea3cc332009-08-18 19:00:34 +0000270 andi. r10,r11,_PAGE_EXEC
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000271 bne 1f
272 ld r14,EX_TLB_DEAR(r12)
273 ld r15,EX_TLB_ESR(r12)
274 mtspr SPRN_DEAR,r14
275 mtspr SPRN_ESR,r15
276 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
277 TLB_MISS_EPILOG_ERROR
278 b exc_data_storage_book3e
2791: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
280 TLB_MISS_EPILOG_ERROR
281 b exc_instruction_storage_book3e
282
283
284/*
285 * This is the guts of the second-level TLB miss handler for direct
286 * misses. We are entered with:
287 *
288 * r16 = virtual page table faulting address
289 * r15 = region (top 4 bits of address)
290 * r14 = crap (free to use)
291 * r13 = PACA
292 * r12 = TLB exception frame in PACA
293 * r11 = crap (free to use)
294 * r10 = crap (free to use)
295 *
296 * Note that this should only ever be called as a second level handler
297 * with the current scheme when using SW load.
298 * That means we can always get the original fault DEAR at
299 * EX_TLB_DEAR-EX_TLB_SIZE(r12)
300 *
301 * It can be re-entered by the linear mapping miss handler. However, to
302 * avoid too much complication, it will restart the whole fault at level
303 * 0 so we don't care too much about clobbers
304 *
305 * XXX That code was written back when we couldn't clobber r14. We can now,
306 * so we could probably optimize things a bit
307 */
308virt_page_table_tlb_miss:
309 /* Are we hitting a kernel page table ? */
310 andi. r10,r15,0x8
311
312 /* The cool thing now is that r10 contains 0 for user and 8 for kernel,
313 * and we happen to have the swapper_pg_dir at offset 8 from the user
314 * pgdir in the PACA :-).
315 */
316 add r11,r10,r13
317
318 /* If kernel, we need to clear MAS1 TID */
319 beq 1f
320 /* XXX replace the RMW cycles with immediate loads + writes */
321 mfspr r10,SPRN_MAS1
322 rlwinm r10,r10,0,16,1 /* Clear TID */
323 mtspr SPRN_MAS1,r10
3241:
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000325BEGIN_MMU_FTR_SECTION
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000326 /* Search if we already have a TLB entry for that virtual address, and
327 * if we do, bail out.
328 */
329 PPC_TLBSRX_DOT(0,r16)
330 beq virt_page_table_tlb_miss_done
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000331END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000332
333 /* Now, we need to walk the page tables. First check if we are in
334 * range.
335 */
336 rldicl. r10,r16,64-(VPTE_INDEX_SIZE+3),VPTE_INDEX_SIZE+3+4
337 bne- virt_page_table_tlb_miss_fault
338
339 /* Get the PGD pointer */
340 ld r15,PACAPGD(r11)
341 cmpldi cr0,r15,0
342 beq- virt_page_table_tlb_miss_fault
343
344 /* Get to PGD entry */
345 rldicl r11,r16,64-VPTE_PGD_SHIFT,64-PGD_INDEX_SIZE-3
346 clrrdi r10,r11,3
347 ldx r15,r10,r15
348 cmpldi cr0,r15,0
349 beq virt_page_table_tlb_miss_fault
350
351#ifndef CONFIG_PPC_64K_PAGES
352 /* Get to PUD entry */
353 rldicl r11,r16,64-VPTE_PUD_SHIFT,64-PUD_INDEX_SIZE-3
354 clrrdi r10,r11,3
355 ldx r15,r10,r15
356 cmpldi cr0,r15,0
357 beq virt_page_table_tlb_miss_fault
358#endif /* CONFIG_PPC_64K_PAGES */
359
360 /* Get to PMD entry */
361 rldicl r11,r16,64-VPTE_PMD_SHIFT,64-PMD_INDEX_SIZE-3
362 clrrdi r10,r11,3
363 ldx r15,r10,r15
364 cmpldi cr0,r15,0
365 beq virt_page_table_tlb_miss_fault
366
367 /* Ok, we're all right, we can now create a kernel translation for
368 * a 4K or 64K page from r16 -> r15.
369 */
370 /* Now we build the MAS:
371 *
372 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
373 * MAS 1 : Almost fully setup
374 * - PID already updated by caller if necessary
375 * - TSIZE for now is base page size always
376 * MAS 2 : Use defaults
377 * MAS 3+7 : Needs to be done
378 *
379 * So we only do MAS 2 and 3 for now...
380 */
381 clrldi r11,r15,4 /* remove region ID from RPN */
382 ori r10,r11,1 /* Or-in SR */
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000383
384BEGIN_MMU_FTR_SECTION
385 srdi r16,r10,32
386 mtspr SPRN_MAS3,r10
387 mtspr SPRN_MAS7,r16
388MMU_FTR_SECTION_ELSE
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000389 mtspr SPRN_MAS7_MAS3,r10
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000390ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000391
392 tlbwe
393
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000394BEGIN_MMU_FTR_SECTION
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000395virt_page_table_tlb_miss_done:
396
397 /* We have overriden MAS2:EPN but currently our primary TLB miss
398 * handler will always restore it so that should not be an issue,
399 * if we ever optimize the primary handler to not write MAS2 on
400 * some cases, we'll have to restore MAS2:EPN here based on the
401 * original fault's DEAR. If we do that we have to modify the
402 * ITLB miss handler to also store SRR0 in the exception frame
403 * as DEAR.
404 *
405 * However, one nasty thing we did is we cleared the reservation
406 * (well, potentially we did). We do a trick here thus if we
407 * are not a level 0 exception (we interrupted the TLB miss) we
408 * offset the return address by -4 in order to replay the tlbsrx
409 * instruction there
410 */
411 subf r10,r13,r12
412 cmpldi cr0,r10,PACA_EXTLB+EX_TLB_SIZE
413 bne- 1f
414 ld r11,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
415 addi r10,r11,-4
416 std r10,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
4171:
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000418END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000419 /* Return to caller, normal case */
420 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK);
421 TLB_MISS_EPILOG_SUCCESS
422 rfi
423
424virt_page_table_tlb_miss_fault:
425 /* If we fault here, things are a little bit tricky. We need to call
426 * either data or instruction store fault, and we need to retreive
427 * the original fault address and ESR (for data).
428 *
429 * The thing is, we know that in normal circumstances, this is
430 * always called as a second level tlb miss for SW load or as a first
431 * level TLB miss for HW load, so we should be able to peek at the
432 * relevant informations in the first exception frame in the PACA.
433 *
434 * However, we do need to double check that, because we may just hit
435 * a stray kernel pointer or a userland attack trying to hit those
436 * areas. If that is the case, we do a data fault. (We can't get here
437 * from an instruction tlb miss anyway).
438 *
439 * Note also that when going to a fault, we must unwind the previous
440 * level as well. Since we are doing that, we don't need to clear or
441 * restore the TLB reservation neither.
442 */
443 subf r10,r13,r12
444 cmpldi cr0,r10,PACA_EXTLB+EX_TLB_SIZE
445 bne- virt_page_table_tlb_miss_whacko_fault
446
447 /* We dig the original DEAR and ESR from slot 0 */
448 ld r15,EX_TLB_DEAR+PACA_EXTLB(r13)
449 ld r16,EX_TLB_ESR+PACA_EXTLB(r13)
450
451 /* We check for the "special" ESR value for instruction faults */
452 cmpdi cr0,r16,-1
453 beq 1f
454 mtspr SPRN_DEAR,r15
455 mtspr SPRN_ESR,r16
456 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT);
457 TLB_MISS_EPILOG_ERROR
458 b exc_data_storage_book3e
4591: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT);
460 TLB_MISS_EPILOG_ERROR
461 b exc_instruction_storage_book3e
462
463virt_page_table_tlb_miss_whacko_fault:
464 /* The linear fault will restart everything so ESR and DEAR will
465 * not have been clobbered, let's just fault with what we have
466 */
467 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_FAULT);
468 TLB_MISS_EPILOG_ERROR
469 b exc_data_storage_book3e
470
471
472/**************************************************************
473 * *
474 * TLB miss handling for Book3E with hw page table support *
475 * *
476 **************************************************************/
477
478
479/* Data TLB miss */
480 START_EXCEPTION(data_tlb_miss_htw)
481 TLB_MISS_PROLOG
482
483 /* Now we handle the fault proper. We only save DEAR in normal
484 * fault case since that's the only interesting values here.
485 * We could probably also optimize by not saving SRR0/1 in the
486 * linear mapping case but I'll leave that for later
487 */
488 mfspr r14,SPRN_ESR
489 mfspr r16,SPRN_DEAR /* get faulting address */
490 srdi r11,r16,60 /* get region */
491 cmpldi cr0,r11,0xc /* linear mapping ? */
492 TLB_MISS_STATS_SAVE_INFO
493 beq tlb_load_linear /* yes -> go to linear map load */
494
495 /* We do the user/kernel test for the PID here along with the RW test
496 */
497 cmpldi cr0,r11,0 /* Check for user region */
498 ld r15,PACAPGD(r13) /* Load user pgdir */
499 beq htw_tlb_miss
500
501 /* XXX replace the RMW cycles with immediate loads + writes */
5021: mfspr r10,SPRN_MAS1
503 cmpldi cr0,r11,8 /* Check for vmalloc region */
504 rlwinm r10,r10,0,16,1 /* Clear TID */
505 mtspr SPRN_MAS1,r10
506 ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */
507 beq+ htw_tlb_miss
508
509 /* We got a crappy address, just fault with whatever DEAR and ESR
510 * are here
511 */
512 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
513 TLB_MISS_EPILOG_ERROR
514 b exc_data_storage_book3e
515
516/* Instruction TLB miss */
517 START_EXCEPTION(instruction_tlb_miss_htw)
518 TLB_MISS_PROLOG
519
520 /* If we take a recursive fault, the second level handler may need
521 * to know whether we are handling a data or instruction fault in
522 * order to get to the right store fault handler. We provide that
523 * info by keeping a crazy value for ESR in r14
524 */
525 li r14,-1 /* store to exception frame is done later */
526
527 /* Now we handle the fault proper. We only save DEAR in the non
528 * linear mapping case since we know the linear mapping case will
529 * not re-enter. We could indeed optimize and also not save SRR0/1
530 * in the linear mapping case but I'll leave that for later
531 *
532 * Faulting address is SRR0 which is already in r16
533 */
534 srdi r11,r16,60 /* get region */
535 cmpldi cr0,r11,0xc /* linear mapping ? */
536 TLB_MISS_STATS_SAVE_INFO
537 beq tlb_load_linear /* yes -> go to linear map load */
538
539 /* We do the user/kernel test for the PID here along with the RW test
540 */
541 cmpldi cr0,r11,0 /* Check for user region */
542 ld r15,PACAPGD(r13) /* Load user pgdir */
543 beq htw_tlb_miss
544
545 /* XXX replace the RMW cycles with immediate loads + writes */
5461: mfspr r10,SPRN_MAS1
547 cmpldi cr0,r11,8 /* Check for vmalloc region */
548 rlwinm r10,r10,0,16,1 /* Clear TID */
549 mtspr SPRN_MAS1,r10
550 ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */
551 beq+ htw_tlb_miss
552
553 /* We got a crappy address, just fault */
554 TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
555 TLB_MISS_EPILOG_ERROR
556 b exc_instruction_storage_book3e
557
558
559/*
560 * This is the guts of the second-level TLB miss handler for direct
561 * misses. We are entered with:
562 *
563 * r16 = virtual page table faulting address
564 * r15 = PGD pointer
565 * r14 = ESR
566 * r13 = PACA
567 * r12 = TLB exception frame in PACA
568 * r11 = crap (free to use)
569 * r10 = crap (free to use)
570 *
571 * It can be re-entered by the linear mapping miss handler. However, to
572 * avoid too much complication, it will save/restore things for us
573 */
574htw_tlb_miss:
575 /* Search if we already have a TLB entry for that virtual address, and
576 * if we do, bail out.
577 *
578 * MAS1:IND should be already set based on MAS4
579 */
580 PPC_TLBSRX_DOT(0,r16)
581 beq htw_tlb_miss_done
582
583 /* Now, we need to walk the page tables. First check if we are in
584 * range.
585 */
586 rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
587 bne- htw_tlb_miss_fault
588
589 /* Get the PGD pointer */
590 cmpldi cr0,r15,0
591 beq- htw_tlb_miss_fault
592
593 /* Get to PGD entry */
594 rldicl r11,r16,64-(PGDIR_SHIFT-3),64-PGD_INDEX_SIZE-3
595 clrrdi r10,r11,3
596 ldx r15,r10,r15
597 cmpldi cr0,r15,0
598 beq htw_tlb_miss_fault
599
600#ifndef CONFIG_PPC_64K_PAGES
601 /* Get to PUD entry */
602 rldicl r11,r16,64-(PUD_SHIFT-3),64-PUD_INDEX_SIZE-3
603 clrrdi r10,r11,3
604 ldx r15,r10,r15
605 cmpldi cr0,r15,0
606 beq htw_tlb_miss_fault
607#endif /* CONFIG_PPC_64K_PAGES */
608
609 /* Get to PMD entry */
610 rldicl r11,r16,64-(PMD_SHIFT-3),64-PMD_INDEX_SIZE-3
611 clrrdi r10,r11,3
612 ldx r15,r10,r15
613 cmpldi cr0,r15,0
614 beq htw_tlb_miss_fault
615
616 /* Ok, we're all right, we can now create an indirect entry for
617 * a 1M or 256M page.
618 *
619 * The last trick is now that because we use "half" pages for
620 * the HTW (1M IND is 2K and 256M IND is 32K) we need to account
621 * for an added LSB bit to the RPN. For 64K pages, there is no
622 * problem as we already use 32K arrays (half PTE pages), but for
623 * 4K page we need to extract a bit from the virtual address and
624 * insert it into the "PA52" bit of the RPN.
625 */
626#ifndef CONFIG_PPC_64K_PAGES
627 rlwimi r15,r16,32-9,20,20
628#endif
629 /* Now we build the MAS:
630 *
631 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
632 * MAS 1 : Almost fully setup
633 * - PID already updated by caller if necessary
634 * - TSIZE for now is base ind page size always
635 * MAS 2 : Use defaults
636 * MAS 3+7 : Needs to be done
637 */
638#ifdef CONFIG_PPC_64K_PAGES
639 ori r10,r15,(BOOK3E_PAGESZ_64K << MAS3_SPSIZE_SHIFT)
640#else
641 ori r10,r15,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT)
642#endif
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000643
644BEGIN_MMU_FTR_SECTION
645 srdi r16,r10,32
646 mtspr SPRN_MAS3,r10
647 mtspr SPRN_MAS7,r16
648MMU_FTR_SECTION_ELSE
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000649 mtspr SPRN_MAS7_MAS3,r10
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000650ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000651
652 tlbwe
653
654htw_tlb_miss_done:
655 /* We don't bother with restoring DEAR or ESR since we know we are
656 * level 0 and just going back to userland. They are only needed
657 * if you are going to take an access fault
658 */
659 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK)
660 TLB_MISS_EPILOG_SUCCESS
661 rfi
662
663htw_tlb_miss_fault:
664 /* We need to check if it was an instruction miss. We know this
665 * though because r14 would contain -1
666 */
667 cmpdi cr0,r14,-1
668 beq 1f
669 mtspr SPRN_DEAR,r16
670 mtspr SPRN_ESR,r14
671 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT)
672 TLB_MISS_EPILOG_ERROR
673 b exc_data_storage_book3e
6741: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT)
675 TLB_MISS_EPILOG_ERROR
676 b exc_instruction_storage_book3e
677
678/*
679 * This is the guts of "any" level TLB miss handler for kernel linear
680 * mapping misses. We are entered with:
681 *
682 *
683 * r16 = faulting address
684 * r15 = crap (free to use)
685 * r14 = ESR (data) or -1 (instruction)
686 * r13 = PACA
687 * r12 = TLB exception frame in PACA
688 * r11 = crap (free to use)
689 * r10 = crap (free to use)
690 *
691 * In addition we know that we will not re-enter, so in theory, we could
692 * use a simpler epilog not restoring SRR0/1 etc.. but we'll do that later.
693 *
694 * We also need to be careful about MAS registers here & TLB reservation,
695 * as we know we'll have clobbered them if we interrupt the main TLB miss
696 * handlers in which case we probably want to do a full restart at level
697 * 0 rather than saving / restoring the MAS.
698 *
699 * Note: If we care about performance of that core, we can easily shuffle
700 * a few things around
701 */
702tlb_load_linear:
703 /* For now, we assume the linear mapping is contiguous and stops at
704 * linear_map_top. We also assume the size is a multiple of 1G, thus
705 * we only use 1G pages for now. That might have to be changed in a
706 * final implementation, especially when dealing with hypervisors
707 */
708 ld r11,PACATOC(r13)
709 ld r11,linear_map_top@got(r11)
710 ld r10,0(r11)
711 cmpld cr0,r10,r16
712 bge tlb_load_linear_fault
713
714 /* MAS1 need whole new setup. */
715 li r15,(BOOK3E_PAGESZ_1GB<<MAS1_TSIZE_SHIFT)
716 oris r15,r15,MAS1_VALID@h /* MAS1 needs V and TSIZE */
717 mtspr SPRN_MAS1,r15
718
719 /* Already somebody there ? */
720 PPC_TLBSRX_DOT(0,r16)
721 beq tlb_load_linear_done
722
723 /* Now we build the remaining MAS. MAS0 and 2 should be fine
724 * with their defaults, which leaves us with MAS 3 and 7. The
725 * mapping is linear, so we just take the address, clear the
726 * region bits, and or in the permission bits which are currently
727 * hard wired
728 */
729 clrrdi r10,r16,30 /* 1G page index */
730 clrldi r10,r10,4 /* clear region bits */
731 ori r10,r10,MAS3_SR|MAS3_SW|MAS3_SX
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000732
733BEGIN_MMU_FTR_SECTION
734 srdi r16,r10,32
735 mtspr SPRN_MAS3,r10
736 mtspr SPRN_MAS7,r16
737MMU_FTR_SECTION_ELSE
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000738 mtspr SPRN_MAS7_MAS3,r10
Kumar Galadf5d6ec2009-08-24 15:52:48 +0000739ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000740
741 tlbwe
742
743tlb_load_linear_done:
744 /* We use the "error" epilog for success as we do want to
745 * restore to the initial faulting context, whatever it was.
746 * We do that because we can't resume a fault within a TLB
747 * miss handler, due to MAS and TLB reservation being clobbered.
748 */
749 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_LINEAR)
750 TLB_MISS_EPILOG_ERROR
751 rfi
752
753tlb_load_linear_fault:
754 /* We keep the DEAR and ESR around, this shouldn't have happened */
755 cmpdi cr0,r14,-1
756 beq 1f
757 TLB_MISS_EPILOG_ERROR_SPECIAL
758 b exc_data_storage_book3e
7591: TLB_MISS_EPILOG_ERROR_SPECIAL
760 b exc_instruction_storage_book3e
761
762
763#ifdef CONFIG_BOOK3E_MMU_TLB_STATS
764.tlb_stat_inc:
7651: ldarx r8,0,r9
766 addi r8,r8,1
767 stdcx. r8,0,r9
768 bne- 1b
769 blr
770#endif