x86/cpu: Merge bugs.c and bugs_64.c
[pandora-kernel.git] / arch / x86 / kernel / cpu / bugs.c
1 /*
2  *  Copyright (C) 1994  Linus Torvalds
3  *
4  *  Cyrix stuff, June 1998 by:
5  *      - Rafael R. Reilova (moved everything from head.S),
6  *        <rreilova@ececs.uc.edu>
7  *      - Channing Corn (tests & fixes),
8  *      - Andrew D. Balsa (code cleanup).
9  */
10 #include <linux/init.h>
11 #include <linux/utsname.h>
12 #include <asm/bugs.h>
13 #include <asm/processor.h>
14 #include <asm/processor-flags.h>
15 #include <asm/i387.h>
16 #include <asm/msr.h>
17 #include <asm/paravirt.h>
18 #include <asm/alternative.h>
19 #include <asm/pgtable.h>
20 #include <asm/cacheflush.h>
21
22 #ifdef CONFIG_X86_32
23
24 static int __init no_halt(char *s)
25 {
26         WARN_ONCE(1, "\"no-hlt\" is deprecated, please use \"idle=poll\"\n");
27         boot_cpu_data.hlt_works_ok = 0;
28         return 1;
29 }
30
31 __setup("no-hlt", no_halt);
32
33 static int __init no_387(char *s)
34 {
35         boot_cpu_data.hard_math = 0;
36         write_cr0(X86_CR0_TS | X86_CR0_EM | X86_CR0_MP | read_cr0());
37         return 1;
38 }
39
40 __setup("no387", no_387);
41
42 static double __initdata x = 4195835.0;
43 static double __initdata y = 3145727.0;
44
45 /*
46  * This used to check for exceptions..
47  * However, it turns out that to support that,
48  * the XMM trap handlers basically had to
49  * be buggy. So let's have a correct XMM trap
50  * handler, and forget about printing out
51  * some status at boot.
52  *
53  * We should really only care about bugs here
54  * anyway. Not features.
55  */
56 static void __init check_fpu(void)
57 {
58         s32 fdiv_bug;
59
60         if (!boot_cpu_data.hard_math) {
61 #ifndef CONFIG_MATH_EMULATION
62                 printk(KERN_EMERG "No coprocessor found and no math emulation present.\n");
63                 printk(KERN_EMERG "Giving up.\n");
64                 for (;;) ;
65 #endif
66                 return;
67         }
68
69         kernel_fpu_begin();
70
71         /*
72          * trap_init() enabled FXSR and company _before_ testing for FP
73          * problems here.
74          *
75          * Test for the divl bug..
76          */
77         __asm__("fninit\n\t"
78                 "fldl %1\n\t"
79                 "fdivl %2\n\t"
80                 "fmull %2\n\t"
81                 "fldl %1\n\t"
82                 "fsubp %%st,%%st(1)\n\t"
83                 "fistpl %0\n\t"
84                 "fwait\n\t"
85                 "fninit"
86                 : "=m" (*&fdiv_bug)
87                 : "m" (*&x), "m" (*&y));
88
89         kernel_fpu_end();
90
91         boot_cpu_data.fdiv_bug = fdiv_bug;
92         if (boot_cpu_data.fdiv_bug)
93                 printk(KERN_WARNING "Hmm, FPU with FDIV bug.\n");
94 }
95
96 static void __init check_hlt(void)
97 {
98         if (boot_cpu_data.x86 >= 5 || paravirt_enabled())
99                 return;
100
101         printk(KERN_INFO "Checking 'hlt' instruction... ");
102         if (!boot_cpu_data.hlt_works_ok) {
103                 printk("disabled\n");
104                 return;
105         }
106         halt();
107         halt();
108         halt();
109         halt();
110         printk(KERN_CONT "OK.\n");
111 }
112
113 /*
114  *      Most 386 processors have a bug where a POPAD can lock the
115  *      machine even from user space.
116  */
117
118 static void __init check_popad(void)
119 {
120 #ifndef CONFIG_X86_POPAD_OK
121         int res, inp = (int) &res;
122
123         printk(KERN_INFO "Checking for popad bug... ");
124         __asm__ __volatile__(
125           "movl $12345678,%%eax; movl $0,%%edi; pusha; popa; movl (%%edx,%%edi),%%ecx "
126           : "=&a" (res)
127           : "d" (inp)
128           : "ecx", "edi");
129         /*
130          * If this fails, it means that any user program may lock the
131          * CPU hard. Too bad.
132          */
133         if (res != 12345678)
134                 printk(KERN_CONT "Buggy.\n");
135         else
136                 printk(KERN_CONT "OK.\n");
137 #endif
138 }
139
140 /*
141  * Check whether we are able to run this kernel safely on SMP.
142  *
143  * - In order to run on a i386, we need to be compiled for i386
144  *   (for due to lack of "invlpg" and working WP on a i386)
145  * - In order to run on anything without a TSC, we need to be
146  *   compiled for a i486.
147  */
148
149 static void __init check_config(void)
150 {
151 /*
152  * We'd better not be a i386 if we're configured to use some
153  * i486+ only features! (WP works in supervisor mode and the
154  * new "invlpg" and "bswap" instructions)
155  */
156 #if defined(CONFIG_X86_WP_WORKS_OK) || defined(CONFIG_X86_INVLPG) || \
157         defined(CONFIG_X86_BSWAP)
158         if (boot_cpu_data.x86 == 3)
159                 panic("Kernel requires i486+ for 'invlpg' and other features");
160 #endif
161 }
162
163 #endif /* CONFIG_X86_32 */
164
165 void __init check_bugs(void)
166 {
167 #ifdef CONFIG_X86_32
168         /*
169          * Regardless of whether PCID is enumerated, the SDM says
170          * that it can't be enabled in 32-bit mode.
171          */
172         setup_clear_cpu_cap(X86_FEATURE_PCID);
173 #endif
174
175         identify_boot_cpu();
176
177         if (!IS_ENABLED(CONFIG_SMP)) {
178                 pr_info("CPU: ");
179                 print_cpu_info(&boot_cpu_data);
180         }
181
182 #ifdef CONFIG_X86_32
183         check_config();
184         check_fpu();
185         check_hlt();
186         check_popad();
187         init_utsname()->machine[1] =
188                 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
189         alternative_instructions();
190 #else /* CONFIG_X86_64 */
191         alternative_instructions();
192
193         /*
194          * Make sure the first 2MB area is not mapped by huge pages
195          * There are typically fixed size MTRRs in there and overlapping
196          * MTRRs into large pages causes slow downs.
197          *
198          * Right now we don't do that with gbpages because there seems
199          * very little benefit for that case.
200          */
201         if (!direct_gbpages)
202                 set_memory_4k((unsigned long)__va(0), 1);
203 #endif
204 }