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