fpu-probe.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Processor capabilities determination functions.
  4. *
  5. * Copyright (C) xxxx the Anonymous
  6. * Copyright (C) 1994 - 2006 Ralf Baechle
  7. * Copyright (C) 2003, 2004 Maciej W. Rozycki
  8. * Copyright (C) 2001, 2004, 2011, 2012 MIPS Technologies, Inc.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <asm/bugs.h>
  13. #include <asm/cpu.h>
  14. #include <asm/cpu-features.h>
  15. #include <asm/cpu-type.h>
  16. #include <asm/elf.h>
  17. #include <asm/fpu.h>
  18. #include <asm/mipsregs.h>
  19. #include "fpu-probe.h"
  20. /*
  21. * Get the FPU Implementation/Revision.
  22. */
  23. static inline unsigned long cpu_get_fpu_id(void)
  24. {
  25. unsigned long tmp, fpu_id;
  26. tmp = read_c0_status();
  27. __enable_fpu(FPU_AS_IS);
  28. fpu_id = read_32bit_cp1_register(CP1_REVISION);
  29. write_c0_status(tmp);
  30. return fpu_id;
  31. }
  32. /*
  33. * Check if the CPU has an external FPU.
  34. */
  35. int __cpu_has_fpu(void)
  36. {
  37. return (cpu_get_fpu_id() & FPIR_IMP_MASK) != FPIR_IMP_NONE;
  38. }
  39. /*
  40. * Determine the FCSR mask for FPU hardware.
  41. */
  42. static inline void cpu_set_fpu_fcsr_mask(struct cpuinfo_mips *c)
  43. {
  44. unsigned long sr, mask, fcsr, fcsr0, fcsr1;
  45. fcsr = c->fpu_csr31;
  46. mask = FPU_CSR_ALL_X | FPU_CSR_ALL_E | FPU_CSR_ALL_S | FPU_CSR_RM;
  47. sr = read_c0_status();
  48. __enable_fpu(FPU_AS_IS);
  49. fcsr0 = fcsr & mask;
  50. write_32bit_cp1_register(CP1_STATUS, fcsr0);
  51. fcsr0 = read_32bit_cp1_register(CP1_STATUS);
  52. fcsr1 = fcsr | ~mask;
  53. write_32bit_cp1_register(CP1_STATUS, fcsr1);
  54. fcsr1 = read_32bit_cp1_register(CP1_STATUS);
  55. write_32bit_cp1_register(CP1_STATUS, fcsr);
  56. write_c0_status(sr);
  57. c->fpu_msk31 = ~(fcsr0 ^ fcsr1) & ~mask;
  58. }
  59. /*
  60. * Determine the IEEE 754 NaN encodings and ABS.fmt/NEG.fmt execution modes
  61. * supported by FPU hardware.
  62. */
  63. static void cpu_set_fpu_2008(struct cpuinfo_mips *c)
  64. {
  65. if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M64R1 |
  66. MIPS_CPU_ISA_M32R2 | MIPS_CPU_ISA_M64R2 |
  67. MIPS_CPU_ISA_M32R5 | MIPS_CPU_ISA_M64R5 |
  68. MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R6)) {
  69. unsigned long sr, fir, fcsr, fcsr0, fcsr1;
  70. sr = read_c0_status();
  71. __enable_fpu(FPU_AS_IS);
  72. fir = read_32bit_cp1_register(CP1_REVISION);
  73. if (fir & MIPS_FPIR_HAS2008) {
  74. fcsr = read_32bit_cp1_register(CP1_STATUS);
  75. /*
  76. * MAC2008 toolchain never landed in real world, so
  77. * we're only testing whether it can be disabled and
  78. * don't try to enabled it.
  79. */
  80. fcsr0 = fcsr & ~(FPU_CSR_ABS2008 | FPU_CSR_NAN2008 |
  81. FPU_CSR_MAC2008);
  82. write_32bit_cp1_register(CP1_STATUS, fcsr0);
  83. fcsr0 = read_32bit_cp1_register(CP1_STATUS);
  84. fcsr1 = fcsr | FPU_CSR_ABS2008 | FPU_CSR_NAN2008;
  85. write_32bit_cp1_register(CP1_STATUS, fcsr1);
  86. fcsr1 = read_32bit_cp1_register(CP1_STATUS);
  87. write_32bit_cp1_register(CP1_STATUS, fcsr);
  88. if (c->isa_level & (MIPS_CPU_ISA_M32R2 |
  89. MIPS_CPU_ISA_M64R2)) {
  90. /*
  91. * The bit for MAC2008 might be reused by R6
  92. * in future, so we only test for R2-R5.
  93. */
  94. if (fcsr0 & FPU_CSR_MAC2008)
  95. c->options |= MIPS_CPU_MAC_2008_ONLY;
  96. }
  97. if (!(fcsr0 & FPU_CSR_NAN2008))
  98. c->options |= MIPS_CPU_NAN_LEGACY;
  99. if (fcsr1 & FPU_CSR_NAN2008)
  100. c->options |= MIPS_CPU_NAN_2008;
  101. if ((fcsr0 ^ fcsr1) & FPU_CSR_ABS2008)
  102. c->fpu_msk31 &= ~FPU_CSR_ABS2008;
  103. else
  104. c->fpu_csr31 |= fcsr & FPU_CSR_ABS2008;
  105. if ((fcsr0 ^ fcsr1) & FPU_CSR_NAN2008)
  106. c->fpu_msk31 &= ~FPU_CSR_NAN2008;
  107. else
  108. c->fpu_csr31 |= fcsr & FPU_CSR_NAN2008;
  109. } else {
  110. c->options |= MIPS_CPU_NAN_LEGACY;
  111. }
  112. write_c0_status(sr);
  113. } else {
  114. c->options |= MIPS_CPU_NAN_LEGACY;
  115. }
  116. }
  117. /*
  118. * IEEE 754 conformance mode to use. Affects the NaN encoding and the
  119. * ABS.fmt/NEG.fmt execution mode.
  120. */
  121. static enum { STRICT, EMULATED, LEGACY, STD2008, RELAXED } ieee754 = STRICT;
  122. /*
  123. * Set the IEEE 754 NaN encodings and the ABS.fmt/NEG.fmt execution modes
  124. * to support by the FPU emulator according to the IEEE 754 conformance
  125. * mode selected. Note that "relaxed" straps the emulator so that it
  126. * allows 2008-NaN binaries even for legacy processors.
  127. */
  128. static void cpu_set_nofpu_2008(struct cpuinfo_mips *c)
  129. {
  130. c->options &= ~(MIPS_CPU_NAN_2008 | MIPS_CPU_NAN_LEGACY);
  131. c->fpu_csr31 &= ~(FPU_CSR_ABS2008 | FPU_CSR_NAN2008);
  132. c->fpu_msk31 &= ~(FPU_CSR_ABS2008 | FPU_CSR_NAN2008);
  133. switch (ieee754) {
  134. case STRICT:
  135. case EMULATED:
  136. if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M64R1 |
  137. MIPS_CPU_ISA_M32R2 | MIPS_CPU_ISA_M64R2 |
  138. MIPS_CPU_ISA_M32R5 | MIPS_CPU_ISA_M64R5 |
  139. MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R6)) {
  140. c->options |= MIPS_CPU_NAN_2008 | MIPS_CPU_NAN_LEGACY;
  141. } else {
  142. c->options |= MIPS_CPU_NAN_LEGACY;
  143. c->fpu_msk31 |= FPU_CSR_ABS2008 | FPU_CSR_NAN2008;
  144. }
  145. break;
  146. case LEGACY:
  147. c->options |= MIPS_CPU_NAN_LEGACY;
  148. c->fpu_msk31 |= FPU_CSR_ABS2008 | FPU_CSR_NAN2008;
  149. break;
  150. case STD2008:
  151. c->options |= MIPS_CPU_NAN_2008;
  152. c->fpu_csr31 |= FPU_CSR_ABS2008 | FPU_CSR_NAN2008;
  153. c->fpu_msk31 |= FPU_CSR_ABS2008 | FPU_CSR_NAN2008;
  154. break;
  155. case RELAXED:
  156. c->options |= MIPS_CPU_NAN_2008 | MIPS_CPU_NAN_LEGACY;
  157. break;
  158. }
  159. }
  160. /*
  161. * Override the IEEE 754 NaN encoding and ABS.fmt/NEG.fmt execution mode
  162. * according to the "ieee754=" parameter.
  163. */
  164. static void cpu_set_nan_2008(struct cpuinfo_mips *c)
  165. {
  166. switch (ieee754) {
  167. case STRICT:
  168. mips_use_nan_legacy = !!cpu_has_nan_legacy;
  169. mips_use_nan_2008 = !!cpu_has_nan_2008;
  170. break;
  171. case LEGACY:
  172. mips_use_nan_legacy = !!cpu_has_nan_legacy;
  173. mips_use_nan_2008 = !cpu_has_nan_legacy;
  174. break;
  175. case STD2008:
  176. mips_use_nan_legacy = !cpu_has_nan_2008;
  177. mips_use_nan_2008 = !!cpu_has_nan_2008;
  178. break;
  179. case EMULATED:
  180. /* Pretend ABS2008/NAN2008 options are dynamic */
  181. c->fpu_msk31 &= ~(FPU_CSR_NAN2008 | FPU_CSR_ABS2008);
  182. fallthrough;
  183. case RELAXED:
  184. mips_use_nan_legacy = true;
  185. mips_use_nan_2008 = true;
  186. break;
  187. }
  188. }
  189. /*
  190. * IEEE 754 NaN encoding and ABS.fmt/NEG.fmt execution mode override
  191. * settings:
  192. *
  193. * strict: accept binaries that request a NaN encoding supported by the FPU
  194. * legacy: only accept legacy-NaN binaries
  195. * 2008: only accept 2008-NaN binaries
  196. * relaxed: accept any binaries regardless of whether supported by the FPU
  197. */
  198. static int __init ieee754_setup(char *s)
  199. {
  200. if (!s)
  201. return -1;
  202. else if (!strcmp(s, "strict"))
  203. ieee754 = STRICT;
  204. else if (!strcmp(s, "emulated"))
  205. ieee754 = EMULATED;
  206. else if (!strcmp(s, "legacy"))
  207. ieee754 = LEGACY;
  208. else if (!strcmp(s, "2008"))
  209. ieee754 = STD2008;
  210. else if (!strcmp(s, "relaxed"))
  211. ieee754 = RELAXED;
  212. else
  213. return -1;
  214. if (!(boot_cpu_data.options & MIPS_CPU_FPU))
  215. cpu_set_nofpu_2008(&boot_cpu_data);
  216. cpu_set_nan_2008(&boot_cpu_data);
  217. return 0;
  218. }
  219. early_param("ieee754", ieee754_setup);
  220. /*
  221. * Set the FIR feature flags for the FPU emulator.
  222. */
  223. static void cpu_set_nofpu_id(struct cpuinfo_mips *c)
  224. {
  225. u32 value;
  226. value = 0;
  227. if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M64R1 |
  228. MIPS_CPU_ISA_M32R2 | MIPS_CPU_ISA_M64R2 |
  229. MIPS_CPU_ISA_M32R5 | MIPS_CPU_ISA_M64R5 |
  230. MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R6))
  231. value |= MIPS_FPIR_D | MIPS_FPIR_S;
  232. if (c->isa_level & (MIPS_CPU_ISA_M32R2 | MIPS_CPU_ISA_M64R2 |
  233. MIPS_CPU_ISA_M32R5 | MIPS_CPU_ISA_M64R5 |
  234. MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R6))
  235. value |= MIPS_FPIR_F64 | MIPS_FPIR_L | MIPS_FPIR_W;
  236. if (c->options & MIPS_CPU_NAN_2008)
  237. value |= MIPS_FPIR_HAS2008;
  238. c->fpu_id = value;
  239. }
  240. /* Determined FPU emulator mask to use for the boot CPU with "nofpu". */
  241. static unsigned int mips_nofpu_msk31;
  242. /*
  243. * Set options for FPU hardware.
  244. */
  245. void cpu_set_fpu_opts(struct cpuinfo_mips *c)
  246. {
  247. c->fpu_id = cpu_get_fpu_id();
  248. mips_nofpu_msk31 = c->fpu_msk31;
  249. if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M64R1 |
  250. MIPS_CPU_ISA_M32R2 | MIPS_CPU_ISA_M64R2 |
  251. MIPS_CPU_ISA_M32R5 | MIPS_CPU_ISA_M64R5 |
  252. MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R6)) {
  253. if (c->fpu_id & MIPS_FPIR_3D)
  254. c->ases |= MIPS_ASE_MIPS3D;
  255. if (c->fpu_id & MIPS_FPIR_UFRP)
  256. c->options |= MIPS_CPU_UFR;
  257. if (c->fpu_id & MIPS_FPIR_FREP)
  258. c->options |= MIPS_CPU_FRE;
  259. }
  260. cpu_set_fpu_fcsr_mask(c);
  261. cpu_set_fpu_2008(c);
  262. cpu_set_nan_2008(c);
  263. }
  264. /*
  265. * Set options for the FPU emulator.
  266. */
  267. void cpu_set_nofpu_opts(struct cpuinfo_mips *c)
  268. {
  269. c->options &= ~MIPS_CPU_FPU;
  270. c->fpu_msk31 = mips_nofpu_msk31;
  271. cpu_set_nofpu_2008(c);
  272. cpu_set_nan_2008(c);
  273. cpu_set_nofpu_id(c);
  274. }
  275. int mips_fpu_disabled;
  276. static int __init fpu_disable(char *s)
  277. {
  278. cpu_set_nofpu_opts(&boot_cpu_data);
  279. mips_fpu_disabled = 1;
  280. return 1;
  281. }
  282. __setup("nofpu", fpu_disable);