x86: Implement support to synchronize RDTSC with LFENCE on Intel CPUs
[pandora-kernel.git] / arch / x86 / kernel / cpu / intel.c
1 #include <linux/init.h>
2 #include <linux/kernel.h>
3
4 #include <linux/string.h>
5 #include <linux/bitops.h>
6 #include <linux/smp.h>
7 #include <linux/thread_info.h>
8 #include <linux/module.h>
9
10 #include <asm/processor.h>
11 #include <asm/pgtable.h>
12 #include <asm/msr.h>
13 #include <asm/uaccess.h>
14 #include <asm/ptrace.h>
15 #include <asm/ds.h>
16
17 #include "cpu.h"
18
19 #ifdef CONFIG_X86_LOCAL_APIC
20 #include <asm/mpspec.h>
21 #include <asm/apic.h>
22 #include <mach_apic.h>
23 #endif
24
25 #ifdef CONFIG_X86_INTEL_USERCOPY
26 /*
27  * Alignment at which movsl is preferred for bulk memory copies.
28  */
29 struct movsl_mask movsl_mask __read_mostly;
30 #endif
31
32 void __cpuinit early_intel_workaround(struct cpuinfo_x86 *c)
33 {
34         if (c->x86_vendor != X86_VENDOR_INTEL)
35                 return;
36         /* Netburst reports 64 bytes clflush size, but does IO in 128 bytes */
37         if (c->x86 == 15 && c->x86_cache_alignment == 64)
38                 c->x86_cache_alignment = 128;
39 }
40
41 /*
42  *      Early probe support logic for ppro memory erratum #50
43  *
44  *      This is called before we do cpu ident work
45  */
46  
47 int __cpuinit ppro_with_ram_bug(void)
48 {
49         /* Uses data from early_cpu_detect now */
50         if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
51             boot_cpu_data.x86 == 6 &&
52             boot_cpu_data.x86_model == 1 &&
53             boot_cpu_data.x86_mask < 8) {
54                 printk(KERN_INFO "Pentium Pro with Errata#50 detected. Taking evasive action.\n");
55                 return 1;
56         }
57         return 0;
58 }
59         
60
61 /*
62  * P4 Xeon errata 037 workaround.
63  * Hardware prefetcher may cause stale data to be loaded into the cache.
64  */
65 static void __cpuinit Intel_errata_workarounds(struct cpuinfo_x86 *c)
66 {
67         unsigned long lo, hi;
68
69         if ((c->x86 == 15) && (c->x86_model == 1) && (c->x86_mask == 1)) {
70                 rdmsr (MSR_IA32_MISC_ENABLE, lo, hi);
71                 if ((lo & (1<<9)) == 0) {
72                         printk (KERN_INFO "CPU: C0 stepping P4 Xeon detected.\n");
73                         printk (KERN_INFO "CPU: Disabling hardware prefetching (Errata 037)\n");
74                         lo |= (1<<9);   /* Disable hw prefetching */
75                         wrmsr (MSR_IA32_MISC_ENABLE, lo, hi);
76                 }
77         }
78 }
79
80
81 /*
82  * find out the number of processor cores on the die
83  */
84 static int __cpuinit num_cpu_cores(struct cpuinfo_x86 *c)
85 {
86         unsigned int eax, ebx, ecx, edx;
87
88         if (c->cpuid_level < 4)
89                 return 1;
90
91         /* Intel has a non-standard dependency on %ecx for this CPUID level. */
92         cpuid_count(4, 0, &eax, &ebx, &ecx, &edx);
93         if (eax & 0x1f)
94                 return ((eax >> 26) + 1);
95         else
96                 return 1;
97 }
98
99 #ifdef CONFIG_X86_F00F_BUG
100 static void __cpuinit trap_init_f00f_bug(void)
101 {
102         __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
103
104         /*
105          * Update the IDT descriptor and reload the IDT so that
106          * it uses the read-only mapped virtual address.
107          */
108         idt_descr.address = fix_to_virt(FIX_F00F_IDT);
109         load_idt(&idt_descr);
110 }
111 #endif
112
113 static void __cpuinit init_intel(struct cpuinfo_x86 *c)
114 {
115         unsigned int l2 = 0;
116         char *p = NULL;
117
118 #ifdef CONFIG_X86_F00F_BUG
119         /*
120          * All current models of Pentium and Pentium with MMX technology CPUs
121          * have the F0 0F bug, which lets nonprivileged users lock up the system.
122          * Note that the workaround only should be initialized once...
123          */
124         c->f00f_bug = 0;
125         if (!paravirt_enabled() && c->x86 == 5) {
126                 static int f00f_workaround_enabled = 0;
127
128                 c->f00f_bug = 1;
129                 if ( !f00f_workaround_enabled ) {
130                         trap_init_f00f_bug();
131                         printk(KERN_NOTICE "Intel Pentium with F0 0F bug - workaround enabled.\n");
132                         f00f_workaround_enabled = 1;
133                 }
134         }
135 #endif
136
137         select_idle_routine(c);
138         l2 = init_intel_cacheinfo(c);
139         if (c->cpuid_level > 9 ) {
140                 unsigned eax = cpuid_eax(10);
141                 /* Check for version and the number of counters */
142                 if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
143                         set_bit(X86_FEATURE_ARCH_PERFMON, c->x86_capability);
144         }
145
146         /* SEP CPUID bug: Pentium Pro reports SEP but doesn't have it until model 3 mask 3 */
147         if ((c->x86<<8 | c->x86_model<<4 | c->x86_mask) < 0x633)
148                 clear_bit(X86_FEATURE_SEP, c->x86_capability);
149
150         /* Names for the Pentium II/Celeron processors 
151            detectable only by also checking the cache size.
152            Dixon is NOT a Celeron. */
153         if (c->x86 == 6) {
154                 switch (c->x86_model) {
155                 case 5:
156                         if (c->x86_mask == 0) {
157                                 if (l2 == 0)
158                                         p = "Celeron (Covington)";
159                                 else if (l2 == 256)
160                                         p = "Mobile Pentium II (Dixon)";
161                         }
162                         break;
163                         
164                 case 6:
165                         if (l2 == 128)
166                                 p = "Celeron (Mendocino)";
167                         else if (c->x86_mask == 0 || c->x86_mask == 5)
168                                 p = "Celeron-A";
169                         break;
170                         
171                 case 8:
172                         if (l2 == 128)
173                                 p = "Celeron (Coppermine)";
174                         break;
175                 }
176         }
177
178         if ( p )
179                 strcpy(c->x86_model_id, p);
180         
181         c->x86_max_cores = num_cpu_cores(c);
182
183         detect_ht(c);
184
185         /* Work around errata */
186         Intel_errata_workarounds(c);
187
188 #ifdef CONFIG_X86_INTEL_USERCOPY
189         /*
190          * Set up the preferred alignment for movsl bulk memory moves
191          */
192         switch (c->x86) {
193         case 4:         /* 486: untested */
194                 break;
195         case 5:         /* Old Pentia: untested */
196                 break;
197         case 6:         /* PII/PIII only like movsl with 8-byte alignment */
198                 movsl_mask.mask = 7;
199                 break;
200         case 15:        /* P4 is OK down to 8-byte alignment */
201                 movsl_mask.mask = 7;
202                 break;
203         }
204 #endif
205
206         if (cpu_has_xmm)
207                 set_bit(X86_FEATURE_LFENCE_RDTSC, c->x86_capability);
208         if (c->x86 == 15) {
209                 set_bit(X86_FEATURE_P4, c->x86_capability);
210         }
211         if (c->x86 == 6) 
212                 set_bit(X86_FEATURE_P3, c->x86_capability);
213         if ((c->x86 == 0xf && c->x86_model >= 0x03) ||
214                 (c->x86 == 0x6 && c->x86_model >= 0x0e))
215                 set_bit(X86_FEATURE_CONSTANT_TSC, c->x86_capability);
216
217         if (cpu_has_ds) {
218                 unsigned int l1;
219                 rdmsr(MSR_IA32_MISC_ENABLE, l1, l2);
220                 if (!(l1 & (1<<11)))
221                         set_bit(X86_FEATURE_BTS, c->x86_capability);
222                 if (!(l1 & (1<<12)))
223                         set_bit(X86_FEATURE_PEBS, c->x86_capability);
224         }
225
226         if (cpu_has_bts)
227                 ds_init_intel(c);
228 }
229
230 static unsigned int __cpuinit intel_size_cache(struct cpuinfo_x86 * c, unsigned int size)
231 {
232         /* Intel PIII Tualatin. This comes in two flavours.
233          * One has 256kb of cache, the other 512. We have no way
234          * to determine which, so we use a boottime override
235          * for the 512kb model, and assume 256 otherwise.
236          */
237         if ((c->x86 == 6) && (c->x86_model == 11) && (size == 0))
238                 size = 256;
239         return size;
240 }
241
242 static struct cpu_dev intel_cpu_dev __cpuinitdata = {
243         .c_vendor       = "Intel",
244         .c_ident        = { "GenuineIntel" },
245         .c_models = {
246                 { .vendor = X86_VENDOR_INTEL, .family = 4, .model_names = 
247                   { 
248                           [0] = "486 DX-25/33", 
249                           [1] = "486 DX-50", 
250                           [2] = "486 SX", 
251                           [3] = "486 DX/2", 
252                           [4] = "486 SL", 
253                           [5] = "486 SX/2", 
254                           [7] = "486 DX/2-WB", 
255                           [8] = "486 DX/4", 
256                           [9] = "486 DX/4-WB"
257                   }
258                 },
259                 { .vendor = X86_VENDOR_INTEL, .family = 5, .model_names =
260                   { 
261                           [0] = "Pentium 60/66 A-step", 
262                           [1] = "Pentium 60/66", 
263                           [2] = "Pentium 75 - 200",
264                           [3] = "OverDrive PODP5V83", 
265                           [4] = "Pentium MMX",
266                           [7] = "Mobile Pentium 75 - 200", 
267                           [8] = "Mobile Pentium MMX"
268                   }
269                 },
270                 { .vendor = X86_VENDOR_INTEL, .family = 6, .model_names =
271                   { 
272                           [0] = "Pentium Pro A-step",
273                           [1] = "Pentium Pro", 
274                           [3] = "Pentium II (Klamath)", 
275                           [4] = "Pentium II (Deschutes)", 
276                           [5] = "Pentium II (Deschutes)", 
277                           [6] = "Mobile Pentium II",
278                           [7] = "Pentium III (Katmai)", 
279                           [8] = "Pentium III (Coppermine)", 
280                           [10] = "Pentium III (Cascades)",
281                           [11] = "Pentium III (Tualatin)",
282                   }
283                 },
284                 { .vendor = X86_VENDOR_INTEL, .family = 15, .model_names =
285                   {
286                           [0] = "Pentium 4 (Unknown)",
287                           [1] = "Pentium 4 (Willamette)",
288                           [2] = "Pentium 4 (Northwood)",
289                           [4] = "Pentium 4 (Foster)",
290                           [5] = "Pentium 4 (Foster)",
291                   }
292                 },
293         },
294         .c_init         = init_intel,
295         .c_size_cache   = intel_size_cache,
296 };
297
298 __init int intel_cpu_init(void)
299 {
300         cpu_devs[X86_VENDOR_INTEL] = &intel_cpu_dev;
301         return 0;
302 }
303
304 #ifndef CONFIG_X86_CMPXCHG
305 unsigned long cmpxchg_386_u8(volatile void *ptr, u8 old, u8 new)
306 {
307         u8 prev;
308         unsigned long flags;
309
310         /* Poor man's cmpxchg for 386. Unsuitable for SMP */
311         local_irq_save(flags);
312         prev = *(u8 *)ptr;
313         if (prev == old)
314                 *(u8 *)ptr = new;
315         local_irq_restore(flags);
316         return prev;
317 }
318 EXPORT_SYMBOL(cmpxchg_386_u8);
319
320 unsigned long cmpxchg_386_u16(volatile void *ptr, u16 old, u16 new)
321 {
322         u16 prev;
323         unsigned long flags;
324
325         /* Poor man's cmpxchg for 386. Unsuitable for SMP */
326         local_irq_save(flags);
327         prev = *(u16 *)ptr;
328         if (prev == old)
329                 *(u16 *)ptr = new;
330         local_irq_restore(flags);
331         return prev;
332 }
333 EXPORT_SYMBOL(cmpxchg_386_u16);
334
335 unsigned long cmpxchg_386_u32(volatile void *ptr, u32 old, u32 new)
336 {
337         u32 prev;
338         unsigned long flags;
339
340         /* Poor man's cmpxchg for 386. Unsuitable for SMP */
341         local_irq_save(flags);
342         prev = *(u32 *)ptr;
343         if (prev == old)
344                 *(u32 *)ptr = new;
345         local_irq_restore(flags);
346         return prev;
347 }
348 EXPORT_SYMBOL(cmpxchg_386_u32);
349 #endif
350
351 #ifndef CONFIG_X86_CMPXCHG64
352 unsigned long long cmpxchg_486_u64(volatile void *ptr, u64 old, u64 new)
353 {
354         u64 prev;
355         unsigned long flags;
356
357         /* Poor man's cmpxchg8b for 386 and 486. Unsuitable for SMP */
358         local_irq_save(flags);
359         prev = *(u64 *)ptr;
360         if (prev == old)
361                 *(u64 *)ptr = new;
362         local_irq_restore(flags);
363         return prev;
364 }
365 EXPORT_SYMBOL(cmpxchg_486_u64);
366 #endif
367
368 // arch_initcall(intel_cpu_init);
369