ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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
142 void __init get_cpu_vendor(struct cpuinfo_x86 *c)
143 {
144         char *v = c->x86_vendor_id;
145         int i;
146
147         for (i = 0; i < X86_VENDOR_NUM; i++) {
148                 if (cpu_devs[i]) {
149                         if (!strcmp(v,cpu_devs[i]->c_ident[0]) ||
150                             (cpu_devs[i]->c_ident[1] && 
151                              !strcmp(v,cpu_devs[i]->c_ident[1]))) {
152                                 c->x86_vendor = i;
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 void __init generic_identify(struct cpuinfo_x86 * c)
198 {
199         u32 tfms, xlvl;
200         int junk;
201
202         if (have_cpuid_p()) {
203                 /* Get vendor name */
204                 cpuid(0x00000000, &c->cpuid_level,
205                       (int *)&c->x86_vendor_id[0],
206                       (int *)&c->x86_vendor_id[8],
207                       (int *)&c->x86_vendor_id[4]);
208                 
209                 get_cpu_vendor(c);
210                 /* Initialize the standard set of capabilities */
211                 /* Note that the vendor-specific code below might override */
212         
213                 /* Intel-defined flags: level 0x00000001 */
214                 if ( c->cpuid_level >= 0x00000001 ) {
215                         u32 capability, excap;
216                         cpuid(0x00000001, &tfms, &junk, &excap, &capability);
217                         c->x86_capability[0] = capability;
218                         c->x86_capability[4] = excap;
219                         c->x86 = (tfms >> 8) & 15;
220                         c->x86_model = (tfms >> 4) & 15;
221                         if (c->x86 == 0xf) {
222                                 c->x86 += (tfms >> 20) & 0xff;
223                                 c->x86_model += ((tfms >> 16) & 0xF) << 4;
224                         } 
225                         c->x86_mask = tfms & 15;
226                 } else {
227                         /* Have CPUID level 0 only - unheard of */
228                         c->x86 = 4;
229                 }
230
231                 /* AMD-defined flags: level 0x80000001 */
232                 xlvl = cpuid_eax(0x80000000);
233                 if ( (xlvl & 0xffff0000) == 0x80000000 ) {
234                         if ( xlvl >= 0x80000001 )
235                                 c->x86_capability[1] = cpuid_edx(0x80000001);
236                         if ( xlvl >= 0x80000004 )
237                                 get_model_name(c); /* Default name */
238                 }
239         }
240 }
241
242 static void __init squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
243 {
244         if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr ) {
245                 /* Disable processor serial number */
246                 unsigned long lo,hi;
247                 rdmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
248                 lo |= 0x200000;
249                 wrmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
250                 printk(KERN_NOTICE "CPU serial number disabled.\n");
251                 clear_bit(X86_FEATURE_PN, c->x86_capability);
252
253                 /* Disabling the serial number may affect the cpuid level */
254                 c->cpuid_level = cpuid_eax(0);
255         }
256 }
257
258 static int __init x86_serial_nr_setup(char *s)
259 {
260         disable_x86_serial_nr = 0;
261         return 1;
262 }
263 __setup("serialnumber", x86_serial_nr_setup);
264
265
266
267 /*
268  * This does the hard work of actually picking apart the CPU stuff...
269  */
270 void __init identify_cpu(struct cpuinfo_x86 *c)
271 {
272         int i;
273
274         c->loops_per_jiffy = loops_per_jiffy;
275         c->x86_cache_size = -1;
276         c->x86_vendor = X86_VENDOR_UNKNOWN;
277         c->cpuid_level = -1;    /* CPUID not detected */
278         c->x86_model = c->x86_mask = 0; /* So far unknown... */
279         c->x86_vendor_id[0] = '\0'; /* Unset */
280         c->x86_model_id[0] = '\0';  /* Unset */
281         memset(&c->x86_capability, 0, sizeof c->x86_capability);
282
283         if (!have_cpuid_p()) {
284                 /* First of all, decide if this is a 486 or higher */
285                 /* It's a 486 if we can modify the AC flag */
286                 if ( flag_is_changeable_p(X86_EFLAGS_AC) )
287                         c->x86 = 4;
288                 else
289                         c->x86 = 3;
290         }
291
292         generic_identify(c);
293
294         printk(KERN_DEBUG "CPU:     After generic identify, caps: %08lx %08lx %08lx %08lx\n",
295                 c->x86_capability[0],
296                 c->x86_capability[1],
297                 c->x86_capability[2],
298                 c->x86_capability[3]);
299
300         if (this_cpu->c_identify) {
301                 this_cpu->c_identify(c);
302
303         printk(KERN_DEBUG "CPU:     After vendor identify, caps: %08lx %08lx %08lx %08lx\n",
304                 c->x86_capability[0],
305                 c->x86_capability[1],
306                 c->x86_capability[2],
307                 c->x86_capability[3]);
308 }
309
310         /*
311          * Vendor-specific initialization.  In this section we
312          * canonicalize the feature flags, meaning if there are
313          * features a certain CPU supports which CPUID doesn't
314          * tell us, CPUID claiming incorrect flags, or other bugs,
315          * we handle them here.
316          *
317          * At the end of this section, c->x86_capability better
318          * indicate the features this CPU genuinely supports!
319          */
320         if (this_cpu->c_init)
321                 this_cpu->c_init(c);
322
323         /* Disable the PN if appropriate */
324         squash_the_stupid_serial_number(c);
325
326         /*
327          * The vendor-specific functions might have changed features.  Now
328          * we do "generic changes."
329          */
330
331         /* TSC disabled? */
332         if ( tsc_disable )
333                 clear_bit(X86_FEATURE_TSC, c->x86_capability);
334
335         /* FXSR disabled? */
336         if (disable_x86_fxsr) {
337                 clear_bit(X86_FEATURE_FXSR, c->x86_capability);
338                 clear_bit(X86_FEATURE_XMM, c->x86_capability);
339         }
340
341         if (disable_pse)
342                 clear_bit(X86_FEATURE_PSE, c->x86_capability);
343
344         /* If the model name is still unset, do table lookup. */
345         if ( !c->x86_model_id[0] ) {
346                 char *p;
347                 p = table_lookup_model(c);
348                 if ( p )
349                         strcpy(c->x86_model_id, p);
350                 else
351                         /* Last resort... */
352                         sprintf(c->x86_model_id, "%02x/%02x",
353                                 c->x86_vendor, c->x86_model);
354         }
355
356         /* Now the feature flags better reflect actual CPU features! */
357
358         printk(KERN_DEBUG "CPU:     After all inits, caps: %08lx %08lx %08lx %08lx\n",
359                c->x86_capability[0],
360                c->x86_capability[1],
361                c->x86_capability[2],
362                c->x86_capability[3]);
363
364         /*
365          * On SMP, boot_cpu_data holds the common feature set between
366          * all CPUs; so make sure that we indicate which features are
367          * common between the CPUs.  The first time this routine gets
368          * executed, c == &boot_cpu_data.
369          */
370         if ( c != &boot_cpu_data ) {
371                 /* AND the already accumulated flags with these */
372                 for ( i = 0 ; i < NCAPINTS ; i++ )
373                         boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
374         }
375
376         /* Init Machine Check Exception if available. */
377 #ifdef CONFIG_X86_MCE
378         mcheck_init(c);
379 #endif
380 }
381 /*
382  *      Perform early boot up checks for a valid TSC. See arch/i386/kernel/time.c
383  */
384  
385 void __init dodgy_tsc(void)
386 {
387         get_cpu_vendor(&boot_cpu_data);
388         if (( boot_cpu_data.x86_vendor == X86_VENDOR_CYRIX ) ||
389             ( boot_cpu_data.x86_vendor == X86_VENDOR_NSC   ))
390                 cpu_devs[X86_VENDOR_CYRIX]->c_init(&boot_cpu_data);
391 }
392
393 void __init print_cpu_info(struct cpuinfo_x86 *c)
394 {
395         char *vendor = NULL;
396
397         if (c->x86_vendor < X86_VENDOR_NUM)
398                 vendor = this_cpu->c_vendor;
399         else if (c->cpuid_level >= 0)
400                 vendor = c->x86_vendor_id;
401
402         if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
403                 printk("%s ", vendor);
404
405         if (!c->x86_model_id[0])
406                 printk("%d86", c->x86);
407         else
408                 printk("%s", c->x86_model_id);
409
410         if (c->x86_mask || c->cpuid_level >= 0) 
411                 printk(" stepping %02x\n", c->x86_mask);
412         else
413                 printk("\n");
414 }
415
416 unsigned long cpu_initialized __initdata = 0;
417
418 /* This is hacky. :)
419  * We're emulating future behavior.
420  * In the future, the cpu-specific init functions will be called implicitly
421  * via the magic of initcalls.
422  * They will insert themselves into the cpu_devs structure.
423  * Then, when cpu_init() is called, we can just iterate over that array.
424  */
425
426 extern int intel_cpu_init(void);
427 extern int cyrix_init_cpu(void);
428 extern int nsc_init_cpu(void);
429 extern int amd_init_cpu(void);
430 extern int centaur_init_cpu(void);
431 extern int transmeta_init_cpu(void);
432 extern int rise_init_cpu(void);
433 extern int nexgen_init_cpu(void);
434 extern int umc_init_cpu(void);
435
436 void __init early_cpu_init(void)
437 {
438         intel_cpu_init();
439         cyrix_init_cpu();
440         nsc_init_cpu();
441         amd_init_cpu();
442         centaur_init_cpu();
443         transmeta_init_cpu();
444         rise_init_cpu();
445         nexgen_init_cpu();
446         umc_init_cpu();
447
448 #ifdef CONFIG_DEBUG_PAGEALLOC
449         /* pse is not compatible with on-the-fly unmapping,
450          * disable it even if the cpus claim to support it.
451          */
452         clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
453         disable_pse = 1;
454 #endif
455 }
456 /*
457  * cpu_init() initializes state that is per-CPU. Some data is already
458  * initialized (naturally) in the bootstrap process, such as the GDT
459  * and IDT. We reload them nevertheless, this function acts as a
460  * 'CPU state barrier', nothing should get across.
461  */
462 void __init cpu_init (void)
463 {
464         int cpu = smp_processor_id();
465         struct tss_struct * t = init_tss + cpu;
466         struct thread_struct *thread = &current->thread;
467
468         if (test_and_set_bit(cpu, &cpu_initialized)) {
469                 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
470                 for (;;) local_irq_enable();
471         }
472         printk(KERN_INFO "Initializing CPU#%d\n", cpu);
473
474         if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
475                 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
476         if (tsc_disable && cpu_has_tsc) {
477                 printk(KERN_NOTICE "Disabling TSC...\n");
478                 /**** FIX-HPA: DOES THIS REALLY BELONG HERE? ****/
479                 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
480                 set_in_cr4(X86_CR4_TSD);
481         }
482
483         /*
484          * Initialize the per-CPU GDT with the boot GDT,
485          * and set up the GDT descriptor:
486          */
487         if (cpu) {
488                 memcpy(cpu_gdt_table[cpu], cpu_gdt_table[0], GDT_SIZE);
489                 cpu_gdt_descr[cpu].size = GDT_SIZE - 1;
490                 cpu_gdt_descr[cpu].address = (unsigned long)cpu_gdt_table[cpu];
491         }
492         /*
493          * Set up the per-thread TLS descriptor cache:
494          */
495         memcpy(thread->tls_array, cpu_gdt_table[cpu], GDT_ENTRY_TLS_ENTRIES * 8);
496
497         __asm__ __volatile__("lgdt %0" : : "m" (cpu_gdt_descr[cpu]));
498         __asm__ __volatile__("lidt %0" : : "m" (idt_descr));
499
500         /*
501          * Delete NT
502          */
503         __asm__("pushfl ; andl $0xffffbfff,(%esp) ; popfl");
504
505         /*
506          * Set up and load the per-CPU TSS and LDT
507          */
508         atomic_inc(&init_mm.mm_count);
509         current->active_mm = &init_mm;
510         if (current->mm)
511                 BUG();
512         enter_lazy_tlb(&init_mm, current);
513
514         load_esp0(t, thread);
515         set_tss_desc(cpu,t);
516         cpu_gdt_table[cpu][GDT_ENTRY_TSS].b &= 0xfffffdff;
517         load_TR_desc();
518         load_LDT(&init_mm.context);
519
520         /* Set up doublefault TSS pointer in the GDT */
521         __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
522         cpu_gdt_table[cpu][GDT_ENTRY_DOUBLEFAULT_TSS].b &= 0xfffffdff;
523
524         /* Clear %fs and %gs. */
525         asm volatile ("xorl %eax, %eax; movl %eax, %fs; movl %eax, %gs");
526
527         /* Clear all 6 debug registers: */
528
529 #define CD(register) __asm__("movl %0,%%db" #register ::"r"(0) );
530
531         CD(0); CD(1); CD(2); CD(3); /* no db4 and db5 */; CD(6); CD(7);
532
533 #undef CD
534
535         /*
536          * Force FPU initialization:
537          */
538         current_thread_info()->status = 0;
539         current->used_math = 0;
540         mxcsr_feature_mask_init();
541 }