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