init.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * x86 FPU boot time init code
  3. */
  4. #include <asm/fpu/internal.h>
  5. #include <asm/tlbflush.h>
  6. /*
  7. * The earliest FPU detection code.
  8. *
  9. * Set the X86_FEATURE_FPU CPU-capability bit based on
  10. * trying to execute an actual sequence of FPU instructions:
  11. */
  12. static void fpu__init_system_early_generic(struct cpuinfo_x86 *c)
  13. {
  14. unsigned long cr0;
  15. u16 fsw, fcw;
  16. fsw = fcw = 0xffff;
  17. cr0 = read_cr0();
  18. cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
  19. write_cr0(cr0);
  20. asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
  21. : "+m" (fsw), "+m" (fcw));
  22. if (fsw == 0 && (fcw & 0x103f) == 0x003f)
  23. set_cpu_cap(c, X86_FEATURE_FPU);
  24. else
  25. clear_cpu_cap(c, X86_FEATURE_FPU);
  26. #ifndef CONFIG_MATH_EMULATION
  27. if (!cpu_has_fpu) {
  28. pr_emerg("No FPU found and no math emulation present\n");
  29. pr_emerg("Giving up\n");
  30. for (;;)
  31. asm volatile("hlt");
  32. }
  33. #endif
  34. }
  35. /*
  36. * Boot time FPU feature detection code:
  37. */
  38. unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
  39. unsigned int xstate_size;
  40. EXPORT_SYMBOL_GPL(xstate_size);
  41. static void fpu__init_system_mxcsr(void)
  42. {
  43. unsigned int mask = 0;
  44. if (cpu_has_fxsr) {
  45. struct i387_fxsave_struct fx_tmp __aligned(32) = { };
  46. asm volatile("fxsave %0" : "+m" (fx_tmp));
  47. mask = fx_tmp.mxcsr_mask;
  48. /*
  49. * If zero then use the default features mask,
  50. * which has all features set, except the
  51. * denormals-are-zero feature bit:
  52. */
  53. if (mask == 0)
  54. mask = 0x0000ffbf;
  55. }
  56. mxcsr_feature_mask &= mask;
  57. }
  58. /*
  59. * Once per bootup FPU initialization sequences that will run on most x86 CPUs:
  60. */
  61. static void fpu__init_system_generic(void)
  62. {
  63. /*
  64. * Set up the legacy init FPU context. (xstate init might overwrite this
  65. * with a more modern format, if the CPU supports it.)
  66. */
  67. fx_finit(&init_xstate_ctx.i387);
  68. fpu__init_system_mxcsr();
  69. }
  70. static void fpstate_xstate_init_size(void)
  71. {
  72. static bool on_boot_cpu = 1;
  73. if (!on_boot_cpu)
  74. return;
  75. on_boot_cpu = 0;
  76. /*
  77. * Note that xstate_size might be overwriten later during
  78. * fpu__init_system_xstate().
  79. */
  80. if (!cpu_has_fpu) {
  81. /*
  82. * Disable xsave as we do not support it if i387
  83. * emulation is enabled.
  84. */
  85. setup_clear_cpu_cap(X86_FEATURE_XSAVE);
  86. setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
  87. xstate_size = sizeof(struct i387_soft_struct);
  88. } else {
  89. if (cpu_has_fxsr)
  90. xstate_size = sizeof(struct i387_fxsave_struct);
  91. else
  92. xstate_size = sizeof(struct i387_fsave_struct);
  93. }
  94. }
  95. /*
  96. * Initialize the TS bit in CR0 according to the style of context-switches
  97. * we are using:
  98. */
  99. static void fpu__init_cpu_ctx_switch(void)
  100. {
  101. if (!cpu_has_eager_fpu)
  102. stts();
  103. else
  104. clts();
  105. }
  106. /*
  107. * Initialize the registers found in all CPUs, CR0 and CR4:
  108. */
  109. static void fpu__init_cpu_generic(void)
  110. {
  111. unsigned long cr0;
  112. unsigned long cr4_mask = 0;
  113. if (cpu_has_fxsr)
  114. cr4_mask |= X86_CR4_OSFXSR;
  115. if (cpu_has_xmm)
  116. cr4_mask |= X86_CR4_OSXMMEXCPT;
  117. if (cr4_mask)
  118. cr4_set_bits(cr4_mask);
  119. cr0 = read_cr0();
  120. cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
  121. if (!cpu_has_fpu)
  122. cr0 |= X86_CR0_EM;
  123. write_cr0(cr0);
  124. }
  125. /*
  126. * Enable all supported FPU features. Called when a CPU is brought online.
  127. */
  128. void fpu__init_cpu(void)
  129. {
  130. fpu__init_cpu_generic();
  131. fpu__init_cpu_xstate();
  132. fpu__init_cpu_ctx_switch();
  133. }
  134. static enum { AUTO, ENABLE, DISABLE } eagerfpu = AUTO;
  135. static int __init eager_fpu_setup(char *s)
  136. {
  137. if (!strcmp(s, "on"))
  138. eagerfpu = ENABLE;
  139. else if (!strcmp(s, "off"))
  140. eagerfpu = DISABLE;
  141. else if (!strcmp(s, "auto"))
  142. eagerfpu = AUTO;
  143. return 1;
  144. }
  145. __setup("eagerfpu=", eager_fpu_setup);
  146. /*
  147. * setup_init_fpu_buf() is __init and it is OK to call it here because
  148. * init_xstate_ctx will be unset only once during boot.
  149. */
  150. static void fpu__init_system_ctx_switch(void)
  151. {
  152. WARN_ON(current->thread.fpu.fpstate_active);
  153. current_thread_info()->status = 0;
  154. /* Auto enable eagerfpu for xsaveopt */
  155. if (cpu_has_xsaveopt && eagerfpu != DISABLE)
  156. eagerfpu = ENABLE;
  157. if (xfeatures_mask & XSTATE_EAGER) {
  158. if (eagerfpu == DISABLE) {
  159. pr_err("x86/fpu: eagerfpu switching disabled, disabling the following xstate features: 0x%llx.\n",
  160. xfeatures_mask & XSTATE_EAGER);
  161. xfeatures_mask &= ~XSTATE_EAGER;
  162. } else {
  163. eagerfpu = ENABLE;
  164. }
  165. }
  166. if (eagerfpu == ENABLE)
  167. setup_force_cpu_cap(X86_FEATURE_EAGER_FPU);
  168. printk_once(KERN_INFO "x86/fpu: Using '%s' FPU context switches.\n", eagerfpu == ENABLE ? "eager" : "lazy");
  169. }
  170. /*
  171. * Called on the boot CPU once per system bootup, to set up the initial FPU state that
  172. * is later cloned into all processes.
  173. */
  174. void fpu__init_system(struct cpuinfo_x86 *c)
  175. {
  176. fpu__init_system_early_generic(c);
  177. /* The FPU has to be operational for some of the later FPU init activities: */
  178. fpu__init_cpu();
  179. /*
  180. * But don't leave CR0::TS set yet, as some of the FPU setup methods depend
  181. * on being able to execute FPU instructions that will fault on a set TS,
  182. * such as the FXSAVE in fpu__init_system_mxcsr().
  183. */
  184. clts();
  185. fpu__init_system_generic();
  186. fpstate_xstate_init_size();
  187. fpu__init_system_xstate();
  188. fpu__init_system_ctx_switch();
  189. }
  190. static int __init no_387(char *s)
  191. {
  192. setup_clear_cpu_cap(X86_FEATURE_FPU);
  193. return 1;
  194. }
  195. __setup("no387", no_387);
  196. void fpu__detect(struct cpuinfo_x86 *c)
  197. {
  198. fpu__init_system(c);
  199. /* The final cr0 value is set later, in fpu_init() */
  200. }