ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / kernel / r4k_switch.S
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1994, 1995, 1996, 1998, 1999, 2002, 2003 Ralf Baechle
7  * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
8  * Copyright (C) 1994, 1995, 1996, by Andreas Busse
9  * Copyright (C) 1999 Silicon Graphics, Inc.
10  * Copyright (C) 2000 MIPS Technologies, Inc.
11  *    written by Carsten Langgaard, carstenl@mips.com
12  */
13 #include <linux/config.h>
14 #include <asm/asm.h>
15 #include <asm/cachectl.h>
16 #include <asm/fpregdef.h>
17 #include <asm/mipsregs.h>
18 #include <asm/offset.h>
19 #include <asm/page.h>
20 #include <asm/pgtable-bits.h>
21 #include <asm/regdef.h>
22 #include <asm/stackframe.h>
23 #include <asm/thread_info.h>
24
25 #include <asm/asmmacro.h>
26
27 /*
28  * Offset to the current process status flags, the first 32 bytes of the
29  * stack are not used.
30  */
31 #define ST_OFF (_THREAD_SIZE - 32 - PT_SIZE + PT_STATUS)
32
33 /*
34  * FPU context is saved iff the process has used it's FPU in the current
35  * time slice as indicated by _TIF_USEDFPU.  In any case, the CU1 bit for user
36  * space STATUS register should be 0, so that a process *always* starts its 
37  * userland with FPU disabled after each context switch.
38  *
39  * FPU will be enabled as soon as the process accesses FPU again, through
40  * do_cpu() trap.
41  */
42
43 /*
44  * task_struct *resume(task_struct *prev, task_struct *next,
45  *                     struct thread_info *next_ti)
46  */
47         .align  5
48         LEAF(resume)
49 #ifndef CONFIG_CPU_HAS_LLSC
50         sw      zero, ll_bit
51 #endif
52         mfc0    t1, CP0_STATUS
53         LONG_S  t1, THREAD_STATUS(a0)
54         cpu_save_nonscratch a0
55         LONG_S  ra, THREAD_REG31(a0)
56
57         /*
58          * check if we need to save FPU registers
59          */
60         PTR_L   t3, TASK_THREAD_INFO(a0)
61         LONG_L  t0, TI_FLAGS(t3)
62         li      t1, _TIF_USEDFPU
63         and     t2, t0, t1
64         beqz    t2, 1f
65         nor     t1, zero, t1
66
67         and     t0, t0, t1
68         LONG_S  t0, TI_FLAGS(t3)
69
70         /*
71          * clear saved user stack CU1 bit
72          */
73         LONG_L  t0, ST_OFF(t3)
74         li      t1, ~ST0_CU1
75         and     t0, t0, t1
76         LONG_S  t0, ST_OFF(t3)
77
78 #ifdef CONFIG_MIPS32
79         fpu_save_double a0 t0                   # clobbers t0
80 #endif
81 #ifdef CONFIG_MIPS64
82         sll     t2, t0, 5
83         bgez    t2, 2f
84         sdc1    $f0, (THREAD_FPU + 0x00)(a0)
85         fpu_save_16odd a0
86 2:
87         fpu_save_16even a0 t1                   # clobbers t1
88 #endif
89 1:
90
91         /*
92          * The order of restoring the registers takes care of the race
93          * updating $28, $29 and kernelsp without disabling ints.
94          */
95         move    $28, a2
96         cpu_restore_nonscratch a1
97
98         PTR_ADDIU       t0, $28, _THREAD_SIZE - 32
99         set_saved_sp    t0, t1, t2
100
101         mfc0    t1, CP0_STATUS          /* Do we really need this? */
102         li      a3, 0xff01
103         and     t1, a3
104         LONG_L  a2, THREAD_STATUS(a1)
105         nor     a3, $0, a3
106         and     a2, a3
107         or      a2, t1
108         mtc0    a2, CP0_STATUS
109         move    v0, a0
110         jr      ra
111         END(resume)
112
113 /*
114  * Save a thread's fp context.
115  */
116 LEAF(_save_fp)
117 #ifdef CONFIG_MIPS32
118         fpu_save_double a0 t1                   # clobbers t1
119 #endif
120 #ifdef CONFIG_MIPS64
121         mfc0    t0, CP0_STATUS
122         sll     t1, t0, 5
123         bgez    t1, 1f                          # 16 register mode?
124         fpu_save_16odd a0
125 1:
126         fpu_save_16even a0 t1                   # clobbers t1
127         sdc1    $f0, (THREAD_FPU + 0x00)(a0)
128 #endif
129         jr      ra
130         END(_save_fp)
131
132 /*
133  * Restore a thread's fp context.
134  */
135 LEAF(_restore_fp)
136 #ifdef CONFIG_MIPS32
137         fpu_restore_double a0, t1               # clobbers t1
138 #endif
139 #ifdef CONFIG_MIPS64
140         mfc0    t0, CP0_STATUS
141         sll     t1, t0, 5
142         bgez    t1, 1f                          # 16 register mode?
143
144         fpu_restore_16odd a0
145 1:      fpu_restore_16even a0, t0               # clobbers t0
146          ldc1   $f0, (THREAD_FPU + 0x00)(a0)
147 #endif
148
149         jr      ra
150         END(_restore_fp)
151
152 /*
153  * Load the FPU with signalling NANS.  This bit pattern we're using has
154  * the property that no matter whether considered as single or as double
155  * precision represents signaling NANS.
156  *
157  * We initialize fcr31 to rounding to nearest, no exceptions.
158  */
159
160 #define FPU_DEFAULT  0x00000000
161
162 LEAF(_init_fpu)
163         mfc0    t0, CP0_STATUS
164         li      t1, ST0_CU1
165         or      t0, t1
166         mtc0    t0, CP0_STATUS
167         fpu_enable_hazard
168
169         li      t1, FPU_DEFAULT
170         ctc1    t1, fcr31
171
172         li      t1, -1                          # SNaN
173
174 #ifdef CONFIG_MIPS64
175         sll     t0, t0, 5
176         bgez    t0, 1f                          # 16 / 32 register mode?
177
178         dmtc1   t1, $f1
179         dmtc1   t1, $f3
180         dmtc1   t1, $f5
181         dmtc1   t1, $f7
182         dmtc1   t1, $f9
183         dmtc1   t1, $f11
184         dmtc1   t1, $f13
185         dmtc1   t1, $f15
186         dmtc1   t1, $f17
187         dmtc1   t1, $f19
188         dmtc1   t1, $f21
189         dmtc1   t1, $f23
190         dmtc1   t1, $f25
191         dmtc1   t1, $f27
192         dmtc1   t1, $f29
193         dmtc1   t1, $f31
194 1:
195 #endif
196         
197 #ifdef CONFIG_CPU_MIPS32
198         mtc1    t1, $f0
199         mtc1    t1, $f1
200         mtc1    t1, $f2
201         mtc1    t1, $f3
202         mtc1    t1, $f4
203         mtc1    t1, $f5
204         mtc1    t1, $f6
205         mtc1    t1, $f7
206         mtc1    t1, $f8
207         mtc1    t1, $f9
208         mtc1    t1, $f10
209         mtc1    t1, $f11
210         mtc1    t1, $f12
211         mtc1    t1, $f13
212         mtc1    t1, $f14
213         mtc1    t1, $f15
214         mtc1    t1, $f16
215         mtc1    t1, $f17
216         mtc1    t1, $f18
217         mtc1    t1, $f19
218         mtc1    t1, $f20
219         mtc1    t1, $f21
220         mtc1    t1, $f22
221         mtc1    t1, $f23
222         mtc1    t1, $f24
223         mtc1    t1, $f25
224         mtc1    t1, $f26
225         mtc1    t1, $f27
226         mtc1    t1, $f28
227         mtc1    t1, $f29
228         mtc1    t1, $f30
229         mtc1    t1, $f31
230 #else
231         .set    mips3
232         dmtc1   t1, $f0
233         dmtc1   t1, $f2
234         dmtc1   t1, $f4
235         dmtc1   t1, $f6
236         dmtc1   t1, $f8
237         dmtc1   t1, $f10
238         dmtc1   t1, $f12
239         dmtc1   t1, $f14
240         dmtc1   t1, $f16
241         dmtc1   t1, $f18
242         dmtc1   t1, $f20
243         dmtc1   t1, $f22
244         dmtc1   t1, $f24
245         dmtc1   t1, $f26
246         dmtc1   t1, $f28
247         dmtc1   t1, $f30
248 #endif
249         jr      ra
250         END(_init_fpu)