Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/sched.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 14 | #include <linux/sched/task_stack.h> |
Arnd Bergmann | fc69910 | 2017-03-08 08:29:31 +0100 | [diff] [blame] | 15 | #include <linux/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/thread_info.h> |
Jiri Slaby | 1977f03 | 2007-10-18 23:40:25 -0700 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include <asm/mipsregs.h> |
| 20 | #include <asm/cpu.h> |
| 21 | #include <asm/cpu-features.h> |
Ralf Baechle | e0cc3a4 | 2014-04-28 22:34:01 +0200 | [diff] [blame] | 22 | #include <asm/fpu_emulator.h> |
Chris Dearman | 0b62495 | 2007-05-08 16:09:13 +0100 | [diff] [blame] | 23 | #include <asm/hazards.h> |
James Hogan | 0c7e2bc | 2017-03-04 00:32:03 +0000 | [diff] [blame] | 24 | #include <asm/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/processor.h> |
| 26 | #include <asm/current.h> |
Paul Burton | 33c771b | 2014-07-11 16:44:30 +0100 | [diff] [blame] | 27 | #include <asm/msa.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Ralf Baechle | f088fc8 | 2006-04-05 09:45:47 +0100 | [diff] [blame] | 29 | #ifdef CONFIG_MIPS_MT_FPAFF |
| 30 | #include <asm/mips_mt.h> |
| 31 | #endif |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | struct sigcontext; |
| 34 | struct sigcontext32; |
| 35 | |
Maciej W. Rozycki | 9b26616 | 2015-04-03 23:27:48 +0100 | [diff] [blame] | 36 | extern void _init_fpu(unsigned int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | extern void _save_fp(struct task_struct *); |
| 38 | extern void _restore_fp(struct task_struct *); |
| 39 | |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 40 | /* |
| 41 | * This enum specifies a mode in which we want the FPU to operate, for cores |
Paul Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 42 | * 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 Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 44 | */ |
| 45 | enum fpu_mode { |
| 46 | FPU_32BIT = 0, /* FR = 0 */ |
Paul Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 47 | FPU_64BIT, /* FR = 1, FRE = 0 */ |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 48 | FPU_AS_IS, |
Paul Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 49 | FPU_HYBRID, /* FR = 1, FRE = 1 */ |
| 50 | |
| 51 | #define FPU_FR_MASK 0x1 |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Paul Burton | 84ab45b | 2015-01-30 12:09:37 +0000 | [diff] [blame] | 54 | #define __disable_fpu() \ |
| 55 | do { \ |
| 56 | clear_c0_status(ST0_CU1); \ |
| 57 | disable_fpu_hazard(); \ |
| 58 | } while (0) |
| 59 | |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 60 | static 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 Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 71 | case FPU_HYBRID: |
| 72 | if (!cpu_has_fre) |
| 73 | return SIGFPE; |
| 74 | |
| 75 | /* set FRE */ |
Ralf Baechle | d33e6fe | 2014-12-17 11:46:40 +0100 | [diff] [blame] | 76 | set_c0_config5(MIPS_CONF5_FRE); |
Paul Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 77 | goto fr_common; |
| 78 | |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 79 | case FPU_64BIT: |
Markos Chandras | fcc53b5 | 2015-07-16 15:30:04 +0100 | [diff] [blame] | 80 | #if !(defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6) \ |
Markos Chandras | 6134d94 | 2015-01-30 10:20:28 +0000 | [diff] [blame] | 81 | || defined(CONFIG_64BIT)) |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 82 | /* we only have a 32-bit FPU */ |
| 83 | return SIGFPE; |
| 84 | #endif |
| 85 | /* fall through */ |
| 86 | case FPU_32BIT: |
Ralf Baechle | b0c34f6 | 2014-12-17 11:39:30 +0100 | [diff] [blame] | 87 | if (cpu_has_fre) { |
| 88 | /* clear FRE */ |
Ralf Baechle | d33e6fe | 2014-12-17 11:46:40 +0100 | [diff] [blame] | 89 | clear_c0_config5(MIPS_CONF5_FRE); |
Ralf Baechle | b0c34f6 | 2014-12-17 11:39:30 +0100 | [diff] [blame] | 90 | } |
Paul Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 91 | fr_common: |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 92 | /* set CU1 & change FR appropriately */ |
Paul Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 93 | fr = (int)mode & FPU_FR_MASK; |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 94 | 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 Burton | 84ab45b | 2015-01-30 12:09:37 +0000 | [diff] [blame] | 98 | if (!!(read_c0_status() & ST0_FR) == !!fr) |
| 99 | return 0; |
| 100 | |
| 101 | /* unsupported FR value */ |
| 102 | __disable_fpu(); |
| 103 | return SIGFPE; |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 104 | |
| 105 | default: |
| 106 | BUG(); |
| 107 | } |
Aaro Koskinen | 97b8b16b | 2014-02-05 22:05:44 +0200 | [diff] [blame] | 108 | |
| 109 | return SIGFPE; |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | #define clear_fpu_owner() clear_thread_flag(TIF_USEDFPU) |
| 113 | |
Ralf Baechle | 1d74f6b | 2005-05-09 13:16:07 +0000 | [diff] [blame] | 114 | static inline int __is_fpu_owner(void) |
| 115 | { |
| 116 | return test_thread_flag(TIF_USEDFPU); |
| 117 | } |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | static inline int is_fpu_owner(void) |
| 120 | { |
Ralf Baechle | 1d74f6b | 2005-05-09 13:16:07 +0000 | [diff] [blame] | 121 | return cpu_has_fpu && __is_fpu_owner(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 124 | static inline int __own_fpu(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 126 | enum fpu_mode mode; |
| 127 | int ret; |
| 128 | |
Paul Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 129 | if (test_thread_flag(TIF_HYBRID_FPREGS)) |
| 130 | mode = FPU_HYBRID; |
| 131 | else |
| 132 | mode = !test_thread_flag(TIF_32BIT_FPREGS); |
| 133 | |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 134 | ret = __enable_fpu(mode); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | |
Atsushi Nemoto | 53dc802 | 2007-03-10 01:07:45 +0900 | [diff] [blame] | 138 | KSTK_STATUS(current) |= ST0_CU1; |
Paul Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 139 | if (mode == FPU_64BIT || mode == FPU_HYBRID) |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 140 | KSTK_STATUS(current) |= ST0_FR; |
| 141 | else /* mode == FPU_32BIT */ |
| 142 | KSTK_STATUS(current) &= ~ST0_FR; |
| 143 | |
Atsushi Nemoto | 53dc802 | 2007-03-10 01:07:45 +0900 | [diff] [blame] | 144 | set_thread_flag(TIF_USEDFPU); |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 145 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 148 | static inline int own_fpu_inatomic(int restore) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 150 | int ret = 0; |
| 151 | |
Atsushi Nemoto | 53dc802 | 2007-03-10 01:07:45 +0900 | [diff] [blame] | 152 | if (cpu_has_fpu && !__is_fpu_owner()) { |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 153 | ret = __own_fpu(); |
| 154 | if (restore && !ret) |
Atsushi Nemoto | 53dc802 | 2007-03-10 01:07:45 +0900 | [diff] [blame] | 155 | _restore_fp(current); |
| 156 | } |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 157 | return ret; |
Atsushi Nemoto | faea623 | 2007-04-16 23:19:44 +0900 | [diff] [blame] | 158 | } |
| 159 | |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 160 | static inline int own_fpu(int restore) |
Atsushi Nemoto | faea623 | 2007-04-16 23:19:44 +0900 | [diff] [blame] | 161 | { |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 162 | int ret; |
| 163 | |
Atsushi Nemoto | faea623 | 2007-04-16 23:19:44 +0900 | [diff] [blame] | 164 | preempt_disable(); |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 165 | ret = own_fpu_inatomic(restore); |
Atsushi Nemoto | 53dc802 | 2007-03-10 01:07:45 +0900 | [diff] [blame] | 166 | preempt_enable(); |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 167 | return ret; |
Atsushi Nemoto | 53dc802 | 2007-03-10 01:07:45 +0900 | [diff] [blame] | 168 | } |
| 169 | |
Paul Burton | 1a3d595 | 2015-08-03 08:49:30 -0700 | [diff] [blame] | 170 | static inline void lose_fpu_inatomic(int save, struct task_struct *tsk) |
Atsushi Nemoto | 53dc802 | 2007-03-10 01:07:45 +0900 | [diff] [blame] | 171 | { |
Paul Burton | 33c771b | 2014-07-11 16:44:30 +0100 | [diff] [blame] | 172 | if (is_msa_enabled()) { |
| 173 | if (save) { |
Paul Burton | 1a3d595 | 2015-08-03 08:49:30 -0700 | [diff] [blame] | 174 | save_msa(tsk); |
| 175 | tsk->thread.fpu.fcr31 = |
Manuel Lauss | 842dfc1 | 2014-11-07 14:13:54 +0100 | [diff] [blame] | 176 | read_32bit_cp1_register(CP1_STATUS); |
Paul Burton | 33c771b | 2014-07-11 16:44:30 +0100 | [diff] [blame] | 177 | } |
| 178 | disable_msa(); |
Paul Burton | 1a3d595 | 2015-08-03 08:49:30 -0700 | [diff] [blame] | 179 | clear_tsk_thread_flag(tsk, TIF_USEDMSA); |
James Hogan | acaf6a9 | 2015-02-25 13:08:05 +0000 | [diff] [blame] | 180 | __disable_fpu(); |
Paul Burton | 33c771b | 2014-07-11 16:44:30 +0100 | [diff] [blame] | 181 | } else if (is_fpu_owner()) { |
Atsushi Nemoto | 53dc802 | 2007-03-10 01:07:45 +0900 | [diff] [blame] | 182 | if (save) |
Paul Burton | 1a3d595 | 2015-08-03 08:49:30 -0700 | [diff] [blame] | 183 | _save_fp(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | __disable_fpu(); |
James Hogan | 00fe56d | 2016-02-01 13:50:37 +0000 | [diff] [blame] | 185 | } 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
Paul Burton | 1a3d595 | 2015-08-03 08:49:30 -0700 | [diff] [blame] | 190 | KSTK_STATUS(tsk) &= ~ST0_CU1; |
| 191 | clear_tsk_thread_flag(tsk, TIF_USEDFPU); |
| 192 | } |
| 193 | |
| 194 | static inline void lose_fpu(int save) |
| 195 | { |
| 196 | preempt_disable(); |
| 197 | lose_fpu_inatomic(save, current); |
Atsushi Nemoto | 53dc802 | 2007-03-10 01:07:45 +0900 | [diff] [blame] | 198 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 201 | static inline int init_fpu(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
Maciej W. Rozycki | 9b26616 | 2015-04-03 23:27:48 +0100 | [diff] [blame] | 203 | unsigned int fcr31 = current->thread.fpu.fcr31; |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 204 | int ret = 0; |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (cpu_has_fpu) { |
Ralf Baechle | b0c34f6 | 2014-12-17 11:39:30 +0100 | [diff] [blame] | 207 | unsigned int config5; |
| 208 | |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 209 | ret = __own_fpu(); |
Ralf Baechle | b0c34f6 | 2014-12-17 11:39:30 +0100 | [diff] [blame] | 210 | if (ret) |
| 211 | return ret; |
Paul Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 212 | |
Ralf Baechle | b0c34f6 | 2014-12-17 11:39:30 +0100 | [diff] [blame] | 213 | if (!cpu_has_fre) { |
Maciej W. Rozycki | 9b26616 | 2015-04-03 23:27:48 +0100 | [diff] [blame] | 214 | _init_fpu(fcr31); |
Paul Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 215 | |
Ralf Baechle | b0c34f6 | 2014-12-17 11:39:30 +0100 | [diff] [blame] | 216 | return 0; |
Paul Burton | 4227a2d | 2014-09-11 08:30:20 +0100 | [diff] [blame] | 217 | } |
Ralf Baechle | b0c34f6 | 2014-12-17 11:39:30 +0100 | [diff] [blame] | 218 | |
Ralf Baechle | b0c34f6 | 2014-12-17 11:39:30 +0100 | [diff] [blame] | 219 | /* |
| 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 Baechle | d33e6fe | 2014-12-17 11:46:40 +0100 | [diff] [blame] | 225 | config5 = clear_c0_config5(MIPS_CONF5_FRE); |
Ralf Baechle | b0c34f6 | 2014-12-17 11:39:30 +0100 | [diff] [blame] | 226 | enable_fpu_hazard(); |
| 227 | |
Maciej W. Rozycki | 9b26616 | 2015-04-03 23:27:48 +0100 | [diff] [blame] | 228 | _init_fpu(fcr31); |
Ralf Baechle | b0c34f6 | 2014-12-17 11:39:30 +0100 | [diff] [blame] | 229 | |
| 230 | /* Restore FRE */ |
| 231 | write_c0_config5(config5); |
| 232 | enable_fpu_hazard(); |
Ralf Baechle | e0cc3a4 | 2014-04-28 22:34:01 +0200 | [diff] [blame] | 233 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | fpu_emulator_init_fpu(); |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 235 | |
Paul Burton | 597ce17 | 2013-11-22 13:12:07 +0000 | [diff] [blame] | 236 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static inline void save_fp(struct task_struct *tsk) |
| 240 | { |
| 241 | if (cpu_has_fpu) |
| 242 | _save_fp(tsk); |
| 243 | } |
| 244 | |
| 245 | static inline void restore_fp(struct task_struct *tsk) |
| 246 | { |
| 247 | if (cpu_has_fpu) |
| 248 | _restore_fp(tsk); |
| 249 | } |
| 250 | |
Paul Burton | bbd426f | 2014-02-13 11:26:41 +0000 | [diff] [blame] | 251 | static inline union fpureg *get_fpu_regs(struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Atsushi Nemoto | e04582b | 2006-10-09 00:10:01 +0900 | [diff] [blame] | 253 | if (tsk == current) { |
| 254 | preempt_disable(); |
| 255 | if (is_fpu_owner()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | _save_fp(current); |
Atsushi Nemoto | e04582b | 2006-10-09 00:10:01 +0900 | [diff] [blame] | 257 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Atsushi Nemoto | eae8907 | 2006-05-16 01:26:03 +0900 | [diff] [blame] | 260 | return tsk->thread.fpu.fpr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | #endif /* _ASM_FPU_H */ |