blob: a2813fe381cf5442ca5a4fb2a3f22d619d7ef557 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 MontaVista Software Inc.
3 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 */
10#ifndef _ASM_FPU_H
11#define _ASM_FPU_H
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010014#include <linux/sched/task_stack.h>
Arnd Bergmannfc699102017-03-08 08:29:31 +010015#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/thread_info.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070017#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/mipsregs.h>
20#include <asm/cpu.h>
21#include <asm/cpu-features.h>
Ralf Baechlee0cc3a42014-04-28 22:34:01 +020022#include <asm/fpu_emulator.h>
Chris Dearman0b624952007-05-08 16:09:13 +010023#include <asm/hazards.h>
James Hogan0c7e2bc2017-03-04 00:32:03 +000024#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/processor.h>
26#include <asm/current.h>
Paul Burton33c771b2014-07-11 16:44:30 +010027#include <asm/msa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Ralf Baechlef088fc82006-04-05 09:45:47 +010029#ifdef CONFIG_MIPS_MT_FPAFF
30#include <asm/mips_mt.h>
31#endif
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033struct sigcontext;
34struct sigcontext32;
35
Maciej W. Rozycki9b266162015-04-03 23:27:48 +010036extern void _init_fpu(unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037extern void _save_fp(struct task_struct *);
38extern void _restore_fp(struct task_struct *);
39
Paul Burton597ce172013-11-22 13:12:07 +000040/*
41 * This enum specifies a mode in which we want the FPU to operate, for cores
Paul Burton4227a2d2014-09-11 08:30:20 +010042 * which implement the Status.FR bit. Note that the bottom bit of the value
43 * purposefully matches the desired value of the Status.FR bit.
Paul Burton597ce172013-11-22 13:12:07 +000044 */
45enum fpu_mode {
46 FPU_32BIT = 0, /* FR = 0 */
Paul Burton4227a2d2014-09-11 08:30:20 +010047 FPU_64BIT, /* FR = 1, FRE = 0 */
Paul Burton597ce172013-11-22 13:12:07 +000048 FPU_AS_IS,
Paul Burton4227a2d2014-09-11 08:30:20 +010049 FPU_HYBRID, /* FR = 1, FRE = 1 */
50
51#define FPU_FR_MASK 0x1
Paul Burton597ce172013-11-22 13:12:07 +000052};
53
Paul Burton84ab45b2015-01-30 12:09:37 +000054#define __disable_fpu() \
55do { \
56 clear_c0_status(ST0_CU1); \
57 disable_fpu_hazard(); \
58} while (0)
59
Paul Burton597ce172013-11-22 13:12:07 +000060static inline int __enable_fpu(enum fpu_mode mode)
61{
62 int fr;
63
64 switch (mode) {
65 case FPU_AS_IS:
66 /* just enable the FPU in its current mode */
67 set_c0_status(ST0_CU1);
68 enable_fpu_hazard();
69 return 0;
70
Paul Burton4227a2d2014-09-11 08:30:20 +010071 case FPU_HYBRID:
72 if (!cpu_has_fre)
73 return SIGFPE;
74
75 /* set FRE */
Ralf Baechled33e6fe2014-12-17 11:46:40 +010076 set_c0_config5(MIPS_CONF5_FRE);
Paul Burton4227a2d2014-09-11 08:30:20 +010077 goto fr_common;
78
Paul Burton597ce172013-11-22 13:12:07 +000079 case FPU_64BIT:
Markos Chandrasfcc53b52015-07-16 15:30:04 +010080#if !(defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6) \
Markos Chandras6134d942015-01-30 10:20:28 +000081 || defined(CONFIG_64BIT))
Paul Burton597ce172013-11-22 13:12:07 +000082 /* we only have a 32-bit FPU */
83 return SIGFPE;
84#endif
85 /* fall through */
86 case FPU_32BIT:
Ralf Baechleb0c34f62014-12-17 11:39:30 +010087 if (cpu_has_fre) {
88 /* clear FRE */
Ralf Baechled33e6fe2014-12-17 11:46:40 +010089 clear_c0_config5(MIPS_CONF5_FRE);
Ralf Baechleb0c34f62014-12-17 11:39:30 +010090 }
Paul Burton4227a2d2014-09-11 08:30:20 +010091fr_common:
Paul Burton597ce172013-11-22 13:12:07 +000092 /* set CU1 & change FR appropriately */
Paul Burton4227a2d2014-09-11 08:30:20 +010093 fr = (int)mode & FPU_FR_MASK;
Paul Burton597ce172013-11-22 13:12:07 +000094 change_c0_status(ST0_CU1 | ST0_FR, ST0_CU1 | (fr ? ST0_FR : 0));
95 enable_fpu_hazard();
96
97 /* check FR has the desired value */
Paul Burton84ab45b2015-01-30 12:09:37 +000098 if (!!(read_c0_status() & ST0_FR) == !!fr)
99 return 0;
100
101 /* unsupported FR value */
102 __disable_fpu();
103 return SIGFPE;
Paul Burton597ce172013-11-22 13:12:07 +0000104
105 default:
106 BUG();
107 }
Aaro Koskinen97b8b16b2014-02-05 22:05:44 +0200108
109 return SIGFPE;
Paul Burton597ce172013-11-22 13:12:07 +0000110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define clear_fpu_owner() clear_thread_flag(TIF_USEDFPU)
113
Ralf Baechle1d74f6b2005-05-09 13:16:07 +0000114static inline int __is_fpu_owner(void)
115{
116 return test_thread_flag(TIF_USEDFPU);
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static inline int is_fpu_owner(void)
120{
Ralf Baechle1d74f6b2005-05-09 13:16:07 +0000121 return cpu_has_fpu && __is_fpu_owner();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Paul Burton597ce172013-11-22 13:12:07 +0000124static inline int __own_fpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Paul Burton597ce172013-11-22 13:12:07 +0000126 enum fpu_mode mode;
127 int ret;
128
Paul Burton4227a2d2014-09-11 08:30:20 +0100129 if (test_thread_flag(TIF_HYBRID_FPREGS))
130 mode = FPU_HYBRID;
131 else
132 mode = !test_thread_flag(TIF_32BIT_FPREGS);
133
Paul Burton597ce172013-11-22 13:12:07 +0000134 ret = __enable_fpu(mode);
135 if (ret)
136 return ret;
137
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900138 KSTK_STATUS(current) |= ST0_CU1;
Paul Burton4227a2d2014-09-11 08:30:20 +0100139 if (mode == FPU_64BIT || mode == FPU_HYBRID)
Paul Burton597ce172013-11-22 13:12:07 +0000140 KSTK_STATUS(current) |= ST0_FR;
141 else /* mode == FPU_32BIT */
142 KSTK_STATUS(current) &= ~ST0_FR;
143
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900144 set_thread_flag(TIF_USEDFPU);
Paul Burton597ce172013-11-22 13:12:07 +0000145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Paul Burton597ce172013-11-22 13:12:07 +0000148static inline int own_fpu_inatomic(int restore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Paul Burton597ce172013-11-22 13:12:07 +0000150 int ret = 0;
151
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900152 if (cpu_has_fpu && !__is_fpu_owner()) {
Paul Burton597ce172013-11-22 13:12:07 +0000153 ret = __own_fpu();
154 if (restore && !ret)
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900155 _restore_fp(current);
156 }
Paul Burton597ce172013-11-22 13:12:07 +0000157 return ret;
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900158}
159
Paul Burton597ce172013-11-22 13:12:07 +0000160static inline int own_fpu(int restore)
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900161{
Paul Burton597ce172013-11-22 13:12:07 +0000162 int ret;
163
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900164 preempt_disable();
Paul Burton597ce172013-11-22 13:12:07 +0000165 ret = own_fpu_inatomic(restore);
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900166 preempt_enable();
Paul Burton597ce172013-11-22 13:12:07 +0000167 return ret;
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900168}
169
Paul Burton1a3d5952015-08-03 08:49:30 -0700170static inline void lose_fpu_inatomic(int save, struct task_struct *tsk)
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900171{
Paul Burton33c771b2014-07-11 16:44:30 +0100172 if (is_msa_enabled()) {
173 if (save) {
Paul Burton1a3d5952015-08-03 08:49:30 -0700174 save_msa(tsk);
175 tsk->thread.fpu.fcr31 =
Manuel Lauss842dfc12014-11-07 14:13:54 +0100176 read_32bit_cp1_register(CP1_STATUS);
Paul Burton33c771b2014-07-11 16:44:30 +0100177 }
178 disable_msa();
Paul Burton1a3d5952015-08-03 08:49:30 -0700179 clear_tsk_thread_flag(tsk, TIF_USEDMSA);
James Hoganacaf6a92015-02-25 13:08:05 +0000180 __disable_fpu();
Paul Burton33c771b2014-07-11 16:44:30 +0100181 } else if (is_fpu_owner()) {
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900182 if (save)
Paul Burton1a3d5952015-08-03 08:49:30 -0700183 _save_fp(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 __disable_fpu();
James Hogan00fe56d2016-02-01 13:50:37 +0000185 } else {
186 /* FPU should not have been left enabled with no owner */
187 WARN(read_c0_status() & ST0_CU1,
188 "Orphaned FPU left enabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Paul Burton1a3d5952015-08-03 08:49:30 -0700190 KSTK_STATUS(tsk) &= ~ST0_CU1;
191 clear_tsk_thread_flag(tsk, TIF_USEDFPU);
192}
193
194static inline void lose_fpu(int save)
195{
196 preempt_disable();
197 lose_fpu_inatomic(save, current);
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900198 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Paul Burton597ce172013-11-22 13:12:07 +0000201static inline int init_fpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100203 unsigned int fcr31 = current->thread.fpu.fcr31;
Paul Burton597ce172013-11-22 13:12:07 +0000204 int ret = 0;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (cpu_has_fpu) {
Ralf Baechleb0c34f62014-12-17 11:39:30 +0100207 unsigned int config5;
208
Paul Burton597ce172013-11-22 13:12:07 +0000209 ret = __own_fpu();
Ralf Baechleb0c34f62014-12-17 11:39:30 +0100210 if (ret)
211 return ret;
Paul Burton4227a2d2014-09-11 08:30:20 +0100212
Ralf Baechleb0c34f62014-12-17 11:39:30 +0100213 if (!cpu_has_fre) {
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100214 _init_fpu(fcr31);
Paul Burton4227a2d2014-09-11 08:30:20 +0100215
Ralf Baechleb0c34f62014-12-17 11:39:30 +0100216 return 0;
Paul Burton4227a2d2014-09-11 08:30:20 +0100217 }
Ralf Baechleb0c34f62014-12-17 11:39:30 +0100218
Ralf Baechleb0c34f62014-12-17 11:39:30 +0100219 /*
220 * Ensure FRE is clear whilst running _init_fpu, since
221 * single precision FP instructions are used. If FRE
222 * was set then we'll just end up initialising all 32
223 * 64b registers.
224 */
Ralf Baechled33e6fe2014-12-17 11:46:40 +0100225 config5 = clear_c0_config5(MIPS_CONF5_FRE);
Ralf Baechleb0c34f62014-12-17 11:39:30 +0100226 enable_fpu_hazard();
227
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100228 _init_fpu(fcr31);
Ralf Baechleb0c34f62014-12-17 11:39:30 +0100229
230 /* Restore FRE */
231 write_c0_config5(config5);
232 enable_fpu_hazard();
Ralf Baechlee0cc3a42014-04-28 22:34:01 +0200233 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 fpu_emulator_init_fpu();
Paul Burton597ce172013-11-22 13:12:07 +0000235
Paul Burton597ce172013-11-22 13:12:07 +0000236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239static inline void save_fp(struct task_struct *tsk)
240{
241 if (cpu_has_fpu)
242 _save_fp(tsk);
243}
244
245static inline void restore_fp(struct task_struct *tsk)
246{
247 if (cpu_has_fpu)
248 _restore_fp(tsk);
249}
250
Paul Burtonbbd426f2014-02-13 11:26:41 +0000251static inline union fpureg *get_fpu_regs(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900253 if (tsk == current) {
254 preempt_disable();
255 if (is_fpu_owner())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 _save_fp(current);
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900257 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900260 return tsk->thread.fpu.fpr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#endif /* _ASM_FPU_H */