upgrade to linux 2.6.9-1.11_FC2
[linux-2.6.git] / arch / i386 / kernel / cpu / common.c
1 #include <linux/init.h>
2 #include <linux/string.h>
3 #include <linux/delay.h>
4 #include <linux/smp.h>
5 #include <linux/module.h>
6 #include <linux/percpu.h>
7 #include <asm/semaphore.h>
8 #include <asm/processor.h>
9 #include <asm/i387.h>
10 #include <asm/msr.h>
11 #include <asm/io.h>
12 #include <asm/mmu_context.h>
13
14 #include "cpu.h"
15
16 DEFINE_PER_CPU(struct desc_struct, cpu_gdt_table[GDT_ENTRIES]);
17 EXPORT_PER_CPU_SYMBOL(cpu_gdt_table);
18
19 static int cachesize_override __initdata = -1;
20 static int disable_x86_fxsr __initdata = 0;
21 static int disable_x86_serial_nr __initdata = 1;
22
23 struct cpu_dev * cpu_devs[X86_VENDOR_NUM] = {};
24
25 extern void mcheck_init(struct cpuinfo_x86 *c);
26
27 extern int disable_pse;
28
29 static void default_init(struct cpuinfo_x86 * c)
30 {
31         /* Not much we can do here... */
32         /* Check if at least it has cpuid */
33         if (c->cpuid_level == -1) {
34                 /* No cpuid. It must be an ancient CPU */
35                 if (c->x86 == 4)
36                         strcpy(c->x86_model_id, "486");
37                 else if (c->x86 == 3)
38                         strcpy(c->x86_model_id, "386");
39         }
40 }
41
42 static struct cpu_dev default_cpu = {
43         .c_init = default_init,
44 };
45 static struct cpu_dev * this_cpu = &default_cpu;
46
47 static int __init cachesize_setup(char *str)
48 {
49         get_option (&str, &cachesize_override);
50         return 1;
51 }
52 __setup("cachesize=", cachesize_setup);
53
54 int __init get_model_name(struct cpuinfo_x86 *c)
55 {
56         unsigned int *v;
57         char *p, *q;
58
59         if (cpuid_eax(0x80000000) < 0x80000004)
60                 return 0;
61
62         v = (unsigned int *) c->x86_model_id;
63         cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
64         cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
65         cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
66         c->x86_model_id[48] = 0;
67
68         /* Intel chips right-justify this string for some dumb reason;
69            undo that brain damage */
70         p = q = &c->x86_model_id[0];
71         while ( *p == ' ' )
72              p++;
73         if ( p != q ) {
74              while ( *p )
75                   *q++ = *p++;
76              while ( q <= &c->x86_model_id[48] )
77                   *q++ = '\0';  /* Zero-pad the rest */
78         }
79
80         return 1;
81 }
82
83
84 void __init display_cacheinfo(struct cpuinfo_x86 *c)
85 {
86         unsigned int n, dummy, ecx, edx, l2size;
87
88         n = cpuid_eax(0x80000000);
89
90         if (n >= 0x80000005) {
91                 cpuid(0x80000005, &dummy, &dummy, &ecx, &edx);
92                 printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
93                         edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
94                 c->x86_cache_size=(ecx>>24)+(edx>>24);  
95         }
96
97         if (n < 0x80000006)     /* Some chips just has a large L1. */
98                 return;
99
100         ecx = cpuid_ecx(0x80000006);
101         l2size = ecx >> 16;
102         
103         /* do processor-specific cache resizing */
104         if (this_cpu->c_size_cache)
105                 l2size = this_cpu->c_size_cache(c,l2size);
106
107         /* Allow user to override all this if necessary. */
108         if (cachesize_override != -1)
109                 l2size = cachesize_override;
110
111         if ( l2size == 0 )
112                 return;         /* Again, no L2 cache is possible */
113
114         c->x86_cache_size = l2size;
115
116         printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
117                l2size, ecx & 0xFF);
118 }
119
120 /* Naming convention should be: <Name> [(<Codename>)] */
121 /* This table only is used unless init_<vendor>() below doesn't set it; */
122 /* in particular, if CPUID levels 0x80000002..4 are supported, this isn't used */
123
124 /* Look up CPU names by table lookup. */
125 static char __init *table_lookup_model(struct cpuinfo_x86 *c)
126 {
127         struct cpu_model_info *info;
128
129         if ( c->x86_model >= 16 )
130                 return NULL;    /* Range check */
131
132         if (!this_cpu)
133                 return NULL;
134
135         info = this_cpu->c_models;
136
137         while (info && info->family) {
138                 if (info->family == c->x86)
139                         return info->model_names[c->x86_model];
140                 info++;
141         }
142         return NULL;            /* Not found */
143 }
144
145
146 void __init get_cpu_vendor(struct cpuinfo_x86 *c, int early)
147 {
148         char *v = c->x86_vendor_id;
149         int i;
150
151         for (i = 0; i < X86_VENDOR_NUM; i++) {
152                 if (cpu_devs[i]) {
153                         if (!strcmp(v,cpu_devs[i]->c_ident[0]) ||
154                             (cpu_devs[i]->c_ident[1] && 
155                              !strcmp(v,cpu_devs[i]->c_ident[1]))) {
156                                 c->x86_vendor = i;
157                                 if (!early)
158                                         this_cpu = cpu_devs[i];
159                                 break;
160                         }
161                 }
162         }
163 }
164
165
166 static int __init x86_fxsr_setup(char * s)
167 {
168         disable_x86_fxsr = 1;
169         return 1;
170 }
171 __setup("nofxsr", x86_fxsr_setup);
172
173
174 /* Standard macro to see if a specific flag is changeable */
175 static inline int flag_is_changeable_p(u32 flag)
176 {
177         u32 f1, f2;
178
179         asm("pushfl\n\t"
180             "pushfl\n\t"
181             "popl %0\n\t"
182             "movl %0,%1\n\t"
183             "xorl %2,%0\n\t"
184             "pushl %0\n\t"
185             "popfl\n\t"
186             "pushfl\n\t"
187             "popl %0\n\t"
188             "popfl\n\t"
189             : "=&r" (f1), "=&r" (f2)
190             : "ir" (flag));
191
192         return ((f1^f2) & flag) != 0;
193 }
194
195
196 /* Probe for the CPUID instruction */
197 int __init have_cpuid_p(void)
198 {
199         return flag_is_changeable_p(X86_EFLAGS_ID);
200 }
201
202 /* Do minimum CPU detection early.
203    Fields really needed: vendor, cpuid_level, family, model, mask, cache alignment.
204    The others are not touched to avoid unwanted side effects. */
205 void __init early_cpu_detect(void)
206 {
207         struct cpuinfo_x86 *c = &boot_cpu_data;
208
209         c->x86_cache_alignment = 32;
210
211         if (!have_cpuid_p())
212                 return;
213
214         /* Get vendor name */
215         cpuid(0x00000000, &c->cpuid_level,
216               (int *)&c->x86_vendor_id[0],
217               (int *)&c->x86_vendor_id[8],
218               (int *)&c->x86_vendor_id[4]);
219
220         get_cpu_vendor(c, 1);
221
222         c->x86 = 4;
223         if (c->cpuid_level >= 0x00000001) {
224                 u32 junk, tfms, cap0, misc;
225                 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
226                 c->x86 = (tfms >> 8) & 15;
227                 c->x86_model = (tfms >> 4) & 15;
228                 if (c->x86 == 0xf) {
229                         c->x86 += (tfms >> 20) & 0xff;
230                         c->x86_model += ((tfms >> 16) & 0xF) << 4;
231                 }
232                 c->x86_mask = tfms & 15;
233                 if (cap0 & (1<<19))
234                         c->x86_cache_alignment = ((misc >> 8) & 0xff) * 8;
235         }
236
237         early_intel_workaround(c);
238 }
239
240 void __init generic_identify(struct cpuinfo_x86 * c)
241 {
242         u32 tfms, xlvl;
243         int junk;
244
245         if (have_cpuid_p()) {
246                 /* Get vendor name */
247                 cpuid(0x00000000, &c->cpuid_level,
248                       (int *)&c->x86_vendor_id[0],
249                       (int *)&c->x86_vendor_id[8],
250                       (int *)&c->x86_vendor_id[4]);
251                 
252                 get_cpu_vendor(c, 0);
253                 /* Initialize the standard set of capabilities */
254                 /* Note that the vendor-specific code below might override */
255         
256                 /* Intel-defined flags: level 0x00000001 */
257                 if ( c->cpuid_level >= 0x00000001 ) {
258                         u32 capability, excap;
259                         cpuid(0x00000001, &tfms, &junk, &excap, &capability);
260                         c->x86_capability[0] = capability;
261                         c->x86_capability[4] = excap;
262                         c->x86 = (tfms >> 8) & 15;
263                         c->x86_model = (tfms >> 4) & 15;
264                         if (c->x86 == 0xf) {
265                                 c->x86 += (tfms >> 20) & 0xff;
266                                 c->x86_model += ((tfms >> 16) & 0xF) << 4;
267                         } 
268                         c->x86_mask = tfms & 15;
269                 } else {
270                         /* Have CPUID level 0 only - unheard of */
271                         c->x86 = 4;
272                 }
273
274                 /* AMD-defined flags: level 0x80000001 */
275                 xlvl = cpuid_eax(0x80000000);
276                 if ( (xlvl & 0xffff0000) == 0x80000000 ) {
277                         if ( xlvl >= 0x80000001 )
278                                 c->x86_capability[1] = cpuid_edx(0x80000001);
279                         if ( xlvl >= 0x80000004 )
280                                 get_model_name(c); /* Default name */
281                 }
282         }
283 }
284
285 static void __init squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
286 {
287         if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr ) {
288                 /* Disable processor serial number */
289                 unsigned long lo,hi;
290                 rdmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
291                 lo |= 0x200000;
292                 wrmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
293                 printk(KERN_NOTICE "CPU serial number disabled.\n");
294                 clear_bit(X86_FEATURE_PN, c->x86_capability);
295
296                 /* Disabling the serial number may affect the cpuid level */
297                 c->cpuid_level = cpuid_eax(0);
298         }
299 }
300
301 static int __init x86_serial_nr_setup(char *s)
302 {
303         disable_x86_serial_nr = 0;
304         return 1;
305 }
306 __setup("serialnumber", x86_serial_nr_setup);
307
308
309
310 /*
311  * This does the hard work of actually picking apart the CPU stuff...
312  */
313 void __init identify_cpu(struct cpuinfo_x86 *c)
314 {
315         int i;
316
317         c->loops_per_jiffy = loops_per_jiffy;
318         c->x86_cache_size = -1;
319         c->x86_vendor = X86_VENDOR_UNKNOWN;
320         c->cpuid_level = -1;    /* CPUID not detected */
321         c->x86_model = c->x86_mask = 0; /* So far unknown... */
322         c->x86_vendor_id[0] = '\0'; /* Unset */
323         c->x86_model_id[0] = '\0';  /* Unset */
324         memset(&c->x86_capability, 0, sizeof c->x86_capability);
325
326         if (!have_cpuid_p()) {
327                 /* First of all, decide if this is a 486 or higher */
328                 /* It's a 486 if we can modify the AC flag */
329                 if ( flag_is_changeable_p(X86_EFLAGS_AC) )
330                         c->x86 = 4;
331                 else
332                         c->x86 = 3;
333         }
334
335         generic_identify(c);
336
337         printk(KERN_DEBUG "CPU: After generic identify, caps: %08lx %08lx %08lx %08lx\n",
338                 c->x86_capability[0],
339                 c->x86_capability[1],
340                 c->x86_capability[2],
341                 c->x86_capability[3]);
342
343         if (this_cpu->c_identify) {
344                 this_cpu->c_identify(c);
345
346         printk(KERN_DEBUG "CPU: After vendor identify, caps:  %08lx %08lx %08lx %08lx\n",
347                 c->x86_capability[0],
348                 c->x86_capability[1],
349                 c->x86_capability[2],
350                 c->x86_capability[3]);
351 }
352
353         /*
354          * Vendor-specific initialization.  In this section we
355          * canonicalize the feature flags, meaning if there are
356          * features a certain CPU supports which CPUID doesn't
357          * tell us, CPUID claiming incorrect flags, or other bugs,
358          * we handle them here.
359          *
360          * At the end of this section, c->x86_capability better
361          * indicate the features this CPU genuinely supports!
362          */
363         if (this_cpu->c_init)
364                 this_cpu->c_init(c);
365
366         /* Disable the PN if appropriate */
367         squash_the_stupid_serial_number(c);
368
369         /*
370          * The vendor-specific functions might have changed features.  Now
371          * we do "generic changes."
372          */
373
374         /* TSC disabled? */
375         if ( tsc_disable )
376                 clear_bit(X86_FEATURE_TSC, c->x86_capability);
377
378         /* FXSR disabled? */
379         if (disable_x86_fxsr) {
380                 clear_bit(X86_FEATURE_FXSR, c->x86_capability);
381                 clear_bit(X86_FEATURE_XMM, c->x86_capability);
382         }
383
384         if (disable_pse)
385                 clear_bit(X86_FEATURE_PSE, c->x86_capability);
386
387         /* hack: disable SEP for non-NX cpus; SEP breaks Execshield. */
388         #ifdef CONFIG_HIGHMEM64G
389         if (!test_bit(X86_FEATURE_NX, c->x86_capability)) 
390         #endif
391                 clear_bit(X86_FEATURE_SEP, c->x86_capability);
392
393         /* If the model name is still unset, do table lookup. */
394         if ( !c->x86_model_id[0] ) {
395                 char *p;
396                 p = table_lookup_model(c);
397                 if ( p )
398                         strcpy(c->x86_model_id, p);
399                 else
400                         /* Last resort... */
401                         sprintf(c->x86_model_id, "%02x/%02x",
402                                 c->x86_vendor, c->x86_model);
403         }
404
405         /* Now the feature flags better reflect actual CPU features! */
406
407         printk(KERN_DEBUG "CPU: After all inits, caps:        %08lx %08lx %08lx %08lx\n",
408                c->x86_capability[0],
409                c->x86_capability[1],
410                c->x86_capability[2],
411                c->x86_capability[3]);
412
413         /*
414          * On SMP, boot_cpu_data holds the common feature set between
415          * all CPUs; so make sure that we indicate which features are
416          * common between the CPUs.  The first time this routine gets
417          * executed, c == &boot_cpu_data.
418          */
419         if ( c != &boot_cpu_data ) {
420                 /* AND the already accumulated flags with these */
421                 for ( i = 0 ; i < NCAPINTS ; i++ )
422                         boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
423         }
424
425         /* Init Machine Check Exception if available. */
426 #ifdef CONFIG_X86_MCE
427         mcheck_init(c);
428 #endif
429 }
430 /*
431  *      Perform early boot up checks for a valid TSC. See arch/i386/kernel/time.c
432  */
433  
434 void __init dodgy_tsc(void)
435 {
436         if (( boot_cpu_data.x86_vendor == X86_VENDOR_CYRIX ) ||
437             ( boot_cpu_data.x86_vendor == X86_VENDOR_NSC   ))
438                 cpu_devs[X86_VENDOR_CYRIX]->c_init(&boot_cpu_data);
439 }
440
441 void __init print_cpu_info(struct cpuinfo_x86 *c)
442 {
443         char *vendor = NULL;
444
445         if (c->x86_vendor < X86_VENDOR_NUM)
446                 vendor = this_cpu->c_vendor;
447         else if (c->cpuid_level >= 0)
448                 vendor = c->x86_vendor_id;
449
450         if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
451                 printk("%s ", vendor);
452
453         if (!c->x86_model_id[0])
454                 printk("%d86", c->x86);
455         else
456                 printk("%s", c->x86_model_id);
457
458         if (c->x86_mask || c->cpuid_level >= 0) 
459                 printk(" stepping %02x\n", c->x86_mask);
460         else
461                 printk("\n");
462 }
463
464 unsigned long cpu_initialized __initdata = 0;
465
466 /* This is hacky. :)
467  * We're emulating future behavior.
468  * In the future, the cpu-specific init functions will be called implicitly
469  * via the magic of initcalls.
470  * They will insert themselves into the cpu_devs structure.
471  * Then, when cpu_init() is called, we can just iterate over that array.
472  */
473
474 extern int intel_cpu_init(void);
475 extern int cyrix_init_cpu(void);
476 extern int nsc_init_cpu(void);
477 extern int amd_init_cpu(void);
478 extern int centaur_init_cpu(void);
479 extern int transmeta_init_cpu(void);
480 extern int rise_init_cpu(void);
481 extern int nexgen_init_cpu(void);
482 extern int umc_init_cpu(void);
483 void early_cpu_detect(void);
484
485 void __init early_cpu_init(void)
486 {
487         intel_cpu_init();
488         cyrix_init_cpu();
489         nsc_init_cpu();
490         amd_init_cpu();
491         centaur_init_cpu();
492         transmeta_init_cpu();
493         rise_init_cpu();
494         nexgen_init_cpu();
495         umc_init_cpu();
496         early_cpu_detect();
497
498 #ifdef CONFIG_DEBUG_PAGEALLOC
499         /* pse is not compatible with on-the-fly unmapping,
500          * disable it even if the cpus claim to support it.
501          */
502         clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
503         disable_pse = 1;
504 #endif
505 }
506 /*
507  * cpu_init() initializes state that is per-CPU. Some data is already
508  * initialized (naturally) in the bootstrap process, such as the GDT
509  * and IDT. We reload them nevertheless, this function acts as a
510  * 'CPU state barrier', nothing should get across.
511  */
512 void __init cpu_init (void)
513 {
514         int cpu = smp_processor_id();
515         struct tss_struct * t = &per_cpu(init_tss, cpu);
516         struct thread_struct *thread = &current->thread;
517
518         if (test_and_set_bit(cpu, &cpu_initialized)) {
519                 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
520                 for (;;) local_irq_enable();
521         }
522         printk(KERN_INFO "Initializing CPU#%d\n", cpu);
523
524         if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
525                 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
526         if (tsc_disable && cpu_has_tsc) {
527                 printk(KERN_NOTICE "Disabling TSC...\n");
528                 /**** FIX-HPA: DOES THIS REALLY BELONG HERE? ****/
529                 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
530                 set_in_cr4(X86_CR4_TSD);
531         }
532
533         /*
534          * Initialize the per-CPU GDT with the boot GDT,
535          * and set up the GDT descriptor:
536          */
537         memcpy(&per_cpu(cpu_gdt_table, cpu), cpu_gdt_table,
538                GDT_SIZE);
539         cpu_gdt_descr[cpu].size = GDT_SIZE - 1;
540         cpu_gdt_descr[cpu].address =
541             (unsigned long)&per_cpu(cpu_gdt_table, cpu);
542
543         /*
544          * Set up the per-thread TLS descriptor cache:
545          */
546         memcpy(thread->tls_array, &per_cpu(cpu_gdt_table, cpu),
547                 GDT_ENTRY_TLS_ENTRIES * 8);
548
549         __asm__ __volatile__("lgdt %0" : : "m" (cpu_gdt_descr[cpu]));
550         __asm__ __volatile__("lidt %0" : : "m" (idt_descr));
551
552         /*
553          * Delete NT
554          */
555         __asm__("pushfl ; andl $0xffffbfff,(%esp) ; popfl");
556
557         /*
558          * Set up and load the per-CPU TSS and LDT
559          */
560         atomic_inc(&init_mm.mm_count);
561         current->active_mm = &init_mm;
562         if (current->mm)
563                 BUG();
564         enter_lazy_tlb(&init_mm, current);
565
566         load_esp0(t, thread);
567         set_tss_desc(cpu,t);
568         load_TR_desc();
569         load_LDT(&init_mm.context);
570
571         /* Set up doublefault TSS pointer in the GDT */
572         __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
573
574         /* Clear %fs and %gs. */
575         asm volatile ("xorl %eax, %eax; movl %eax, %fs; movl %eax, %gs");
576
577         /* Clear all 6 debug registers: */
578
579 #define CD(register) __asm__("movl %0,%%db" #register ::"r"(0) );
580
581         CD(0); CD(1); CD(2); CD(3); /* no db4 and db5 */; CD(6); CD(7);
582
583 #undef CD
584
585         /*
586          * Force FPU initialization:
587          */
588         current_thread_info()->status = 0;
589         current->used_math = 0;
590         mxcsr_feature_mask_init();
591 }