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