[SPARC64]: Hypervisor TSB context switching.
[safe/jmp/linux-2.6] / arch / sparc64 / kernel / tsb.S
1 /* tsb.S: Sparc64 TSB table handling.
2  *
3  * Copyright (C) 2006 David S. Miller <davem@davemloft.net>
4  */
5
6 #include <asm/tsb.h>
7 #include <asm/hypervisor.h>
8
9         .text
10         .align  32
11
12         /* Invoked from TLB miss handler, we are in the
13          * MMU global registers and they are setup like
14          * this:
15          *
16          * %g1: TSB entry pointer
17          * %g2: available temporary
18          * %g3: FAULT_CODE_{D,I}TLB
19          * %g4: available temporary
20          * %g5: available temporary
21          * %g6: TAG TARGET
22          * %g7: available temporary, will be loaded by us with
23          *      the physical address base of the linux page
24          *      tables for the current address space
25          */
26 tsb_miss_dtlb:
27         mov             TLB_TAG_ACCESS, %g4
28         ldxa            [%g4] ASI_DMMU, %g4
29         ba,pt           %xcc, tsb_miss_page_table_walk
30          nop
31
32 tsb_miss_itlb:
33         mov             TLB_TAG_ACCESS, %g4
34         ldxa            [%g4] ASI_IMMU, %g4
35         ba,pt           %xcc, tsb_miss_page_table_walk
36          nop
37
38         /* The sun4v TLB miss handlers jump directly here instead
39          * of tsb_miss_{d,i}tlb with registers setup as follows:
40          *
41          * %g4: missing virtual address
42          * %g1: TSB entry address loaded
43          * %g6: TAG TARGET ((vaddr >> 22) | (ctx << 48))
44          */
45 tsb_miss_page_table_walk:
46         TRAP_LOAD_PGD_PHYS(%g7, %g5)
47
48         USER_PGTABLE_WALK_TL1(%g4, %g7, %g5, %g2, tsb_do_fault)
49
50 tsb_reload:
51         TSB_LOCK_TAG(%g1, %g2, %g7)
52
53         /* Load and check PTE.  */
54         ldxa            [%g5] ASI_PHYS_USE_EC, %g5
55         brgez,a,pn      %g5, tsb_do_fault
56          TSB_STORE(%g1, %g0)
57
58         /* If it is larger than the base page size, don't
59          * bother putting it into the TSB.
60          */
61         srlx            %g5, 32, %g2
62         sethi           %hi(_PAGE_ALL_SZ_BITS >> 32), %g7
63         and             %g2, %g7, %g2
64         sethi           %hi(_PAGE_SZBITS >> 32), %g7
65         cmp             %g2, %g7
66         bne,a,pn        %xcc, tsb_tlb_reload
67          TSB_STORE(%g1, %g0)
68
69         TSB_WRITE(%g1, %g5, %g6)
70
71         /* Finally, load TLB and return from trap.  */
72 tsb_tlb_reload:
73         cmp             %g3, FAULT_CODE_DTLB
74         bne,pn          %xcc, tsb_itlb_load
75          nop
76
77 tsb_dtlb_load:
78
79 661:    stxa            %g5, [%g0] ASI_DTLB_DATA_IN
80         retry
81         .section        .sun4v_2insn_patch, "ax"
82         .word           661b
83         nop
84         nop
85         .previous
86
87         /* For sun4v the ASI_DTLB_DATA_IN store and the retry
88          * instruction get nop'd out and we get here to branch
89          * to the sun4v tlb load code.  The registers are setup
90          * as follows:
91          *
92          * %g4: vaddr
93          * %g5: PTE
94          * %g6: TAG
95          *
96          * The sun4v TLB load wants the PTE in %g3 so we fix that
97          * up here.
98          */
99         ba,pt           %xcc, sun4v_dtlb_load
100          mov            %g5, %g3
101
102 tsb_itlb_load:
103
104 661:    stxa            %g5, [%g0] ASI_ITLB_DATA_IN
105         retry
106         .section        .sun4v_2insn_patch, "ax"
107         .word           661b
108         nop
109         nop
110         .previous
111
112         /* For sun4v the ASI_ITLB_DATA_IN store and the retry
113          * instruction get nop'd out and we get here to branch
114          * to the sun4v tlb load code.  The registers are setup
115          * as follows:
116          *
117          * %g4: vaddr
118          * %g5: PTE
119          * %g6: TAG
120          *
121          * The sun4v TLB load wants the PTE in %g3 so we fix that
122          * up here.
123          */
124         ba,pt           %xcc, sun4v_itlb_load
125          mov            %g5, %g3
126
127         /* No valid entry in the page tables, do full fault
128          * processing.
129          */
130
131         .globl          tsb_do_fault
132 tsb_do_fault:
133         cmp             %g3, FAULT_CODE_DTLB
134
135 661:    rdpr            %pstate, %g5
136         wrpr            %g5, PSTATE_AG | PSTATE_MG, %pstate
137         .section        .sun4v_2insn_patch, "ax"
138         .word           661b
139         nop
140         nop
141         .previous
142
143         bne,pn          %xcc, tsb_do_itlb_fault
144          nop
145
146 tsb_do_dtlb_fault:
147         rdpr    %tl, %g3
148         cmp     %g3, 1
149
150 661:    mov     TLB_TAG_ACCESS, %g4
151         ldxa    [%g4] ASI_DMMU, %g5
152         .section .sun4v_2insn_patch, "ax"
153         .word   661b
154         mov     %g4, %g5
155         nop
156         .previous
157
158         be,pt   %xcc, sparc64_realfault_common
159          mov    FAULT_CODE_DTLB, %g4
160         ba,pt   %xcc, winfix_trampoline
161          nop
162
163 tsb_do_itlb_fault:
164         rdpr    %tpc, %g5
165         ba,pt   %xcc, sparc64_realfault_common
166          mov    FAULT_CODE_ITLB, %g4
167
168         .globl  sparc64_realfault_common
169 sparc64_realfault_common:
170         /* fault code in %g4, fault address in %g5, etrap will
171          * preserve these two values in %l4 and %l5 respectively
172          */
173         ba,pt   %xcc, etrap                     ! Save trap state
174 1:       rd     %pc, %g7                        ! ...
175         stb     %l4, [%g6 + TI_FAULT_CODE]      ! Save fault code
176         stx     %l5, [%g6 + TI_FAULT_ADDR]      ! Save fault address
177         call    do_sparc64_fault                ! Call fault handler
178          add    %sp, PTREGS_OFF, %o0            ! Compute pt_regs arg
179         ba,pt   %xcc, rtrap_clr_l6              ! Restore cpu state
180          nop                                    ! Delay slot (fill me)
181
182 winfix_trampoline:
183         rdpr    %tpc, %g3                       ! Prepare winfixup TNPC
184         or      %g3, 0x7c, %g3                  ! Compute branch offset
185         wrpr    %g3, %tnpc                      ! Write it into TNPC
186         done                                    ! Trap return
187
188         /* Insert an entry into the TSB.
189          *
190          * %o0: TSB entry pointer (virt or phys address)
191          * %o1: tag
192          * %o2: pte
193          */
194         .align  32
195         .globl  __tsb_insert
196 __tsb_insert:
197         rdpr    %pstate, %o5
198         wrpr    %o5, PSTATE_IE, %pstate
199         TSB_LOCK_TAG(%o0, %g2, %g3)
200         TSB_WRITE(%o0, %o2, %o1)
201         wrpr    %o5, %pstate
202         retl
203          nop
204
205         /* Flush the given TSB entry if it has the matching
206          * tag.
207          *
208          * %o0: TSB entry pointer (virt or phys address)
209          * %o1: tag
210          */
211         .align  32
212         .globl  tsb_flush
213 tsb_flush:
214         sethi   %hi(TSB_TAG_LOCK_HIGH), %g2
215 1:      TSB_LOAD_TAG(%o0, %g1)
216         srlx    %g1, 32, %o3
217         andcc   %o3, %g2, %g0
218         bne,pn  %icc, 1b
219          membar #LoadLoad
220         cmp     %g1, %o1
221         bne,pt  %xcc, 2f
222          clr    %o3
223         TSB_CAS_TAG(%o0, %g1, %o3)
224         cmp     %g1, %o3
225         bne,pn  %xcc, 1b
226          nop
227 2:      retl
228          TSB_MEMBAR
229
230         /* Reload MMU related context switch state at
231          * schedule() time.
232          *
233          * %o0: page table physical address
234          * %o1: TSB register value
235          * %o2: TSB virtual address
236          * %o3: TSB mapping locked PTE
237          * %o4: Hypervisor TSB descriptor physical address
238          *
239          * We have to run this whole thing with interrupts
240          * disabled so that the current cpu doesn't change
241          * due to preemption.
242          */
243         .align  32
244         .globl  __tsb_context_switch
245 __tsb_context_switch:
246         rdpr    %pstate, %o5
247         wrpr    %o5, PSTATE_IE, %pstate
248
249         ldub    [%g6 + TI_CPU], %g1
250         sethi   %hi(trap_block), %g2
251         sllx    %g1, TRAP_BLOCK_SZ_SHIFT, %g1
252         or      %g2, %lo(trap_block), %g2
253         add     %g2, %g1, %g2
254         stx     %o0, [%g2 + TRAP_PER_CPU_PGD_PADDR]
255
256         sethi   %hi(tlb_type), %g1
257         lduw    [%g1 + %lo(tlb_type)], %g1
258         cmp     %g1, 3
259         bne,pt  %icc, 1f
260          nop
261
262         /* Hypervisor TSB switch. */
263         mov     SCRATCHPAD_UTSBREG1, %g1
264         stxa    %o1, [%g1] ASI_SCRATCHPAD
265         mov     -1, %g2
266         mov     SCRATCHPAD_UTSBREG2, %g1
267         stxa    %g2, [%g1] ASI_SCRATCHPAD
268
269         mov     HV_FAST_MMU_TSB_CTXNON0, %o0
270         mov     1, %o1
271         mov     %o4, %o2
272         ta      HV_FAST_TRAP
273
274         ba,pt   %xcc, 9f
275          nop
276
277         /* SUN4U TSB switch.  */
278 1:      mov     TSB_REG, %g1
279         stxa    %o1, [%g1] ASI_DMMU
280         membar  #Sync
281         stxa    %o1, [%g1] ASI_IMMU
282         membar  #Sync
283
284 2:      brz     %o2, 9f
285          nop
286
287         sethi   %hi(sparc64_highest_unlocked_tlb_ent), %g2
288         mov     TLB_TAG_ACCESS, %g1
289         lduw    [%g2 + %lo(sparc64_highest_unlocked_tlb_ent)], %g2
290         stxa    %o2, [%g1] ASI_DMMU
291         membar  #Sync
292         sllx    %g2, 3, %g2
293         stxa    %o3, [%g2] ASI_DTLB_DATA_ACCESS
294         membar  #Sync
295 9:
296         wrpr    %o5, %pstate
297
298         retl
299          nop