This commit was manufactured by cvs2svn to create tag
[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         /* hack: disable SEP unconditionally. */
383         clear_bit(X86_FEATURE_SEP, c->x86_capability);
384
385         /* If the model name is still unset, do table lookup. */
386         if ( !c->x86_model_id[0] ) {
387                 char *p;
388                 p = table_lookup_model(c);
389                 if ( p )
390                         strcpy(c->x86_model_id, p);
391                 else
392                         /* Last resort... */
393                         sprintf(c->x86_model_id, "%02x/%02x",
394                                 c->x86_vendor, c->x86_model);
395         }
396
397         /* Now the feature flags better reflect actual CPU features! */
398
399         printk(KERN_DEBUG "CPU:     After all inits, caps: %08lx %08lx %08lx %08lx\n",
400                c->x86_capability[0],
401                c->x86_capability[1],
402                c->x86_capability[2],
403                c->x86_capability[3]);
404
405         /*
406          * On SMP, boot_cpu_data holds the common feature set between
407          * all CPUs; so make sure that we indicate which features are
408          * common between the CPUs.  The first time this routine gets
409          * executed, c == &boot_cpu_data.
410          */
411         if ( c != &boot_cpu_data ) {
412                 /* AND the already accumulated flags with these */
413                 for ( i = 0 ; i < NCAPINTS ; i++ )
414                         boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
415         }
416
417         /* Init Machine Check Exception if available. */
418 #ifdef CONFIG_X86_MCE
419         mcheck_init(c);
420 #endif
421 }
422 /*
423  *      Perform early boot up checks for a valid TSC. See arch/i386/kernel/time.c
424  */
425  
426 void __init dodgy_tsc(void)
427 {
428         if (( boot_cpu_data.x86_vendor == X86_VENDOR_CYRIX ) ||
429             ( boot_cpu_data.x86_vendor == X86_VENDOR_NSC   ))
430                 cpu_devs[X86_VENDOR_CYRIX]->c_init(&boot_cpu_data);
431 }
432
433 void __init print_cpu_info(struct cpuinfo_x86 *c)
434 {
435         char *vendor = NULL;
436
437         if (c->x86_vendor < X86_VENDOR_NUM)
438                 vendor = this_cpu->c_vendor;
439         else if (c->cpuid_level >= 0)
440                 vendor = c->x86_vendor_id;
441
442         if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
443                 printk("%s ", vendor);
444
445         if (!c->x86_model_id[0])
446                 printk("%d86", c->x86);
447         else
448                 printk("%s", c->x86_model_id);
449
450         if (c->x86_mask || c->cpuid_level >= 0) 
451                 printk(" stepping %02x\n", c->x86_mask);
452         else
453                 printk("\n");
454 }
455
456 unsigned long cpu_initialized __initdata = 0;
457
458 /* This is hacky. :)
459  * We're emulating future behavior.
460  * In the future, the cpu-specific init functions will be called implicitly
461  * via the magic of initcalls.
462  * They will insert themselves into the cpu_devs structure.
463  * Then, when cpu_init() is called, we can just iterate over that array.
464  */
465
466 extern int intel_cpu_init(void);
467 extern int cyrix_init_cpu(void);
468 extern int nsc_init_cpu(void);
469 extern int amd_init_cpu(void);
470 extern int centaur_init_cpu(void);
471 extern int transmeta_init_cpu(void);
472 extern int rise_init_cpu(void);
473 extern int nexgen_init_cpu(void);
474 extern int umc_init_cpu(void);
475 void early_cpu_detect(void);
476
477 void __init early_cpu_init(void)
478 {
479         early_cpu_detect();
480         intel_cpu_init();
481         cyrix_init_cpu();
482         nsc_init_cpu();
483         amd_init_cpu();
484         centaur_init_cpu();
485         transmeta_init_cpu();
486         rise_init_cpu();
487         nexgen_init_cpu();
488         umc_init_cpu();
489
490 #ifdef CONFIG_DEBUG_PAGEALLOC
491         /* pse is not compatible with on-the-fly unmapping,
492          * disable it even if the cpus claim to support it.
493          */
494         clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
495         disable_pse = 1;
496 #endif
497 }
498 /*
499  * cpu_init() initializes state that is per-CPU. Some data is already
500  * initialized (naturally) in the bootstrap process, such as the GDT
501  * and IDT. We reload them nevertheless, this function acts as a
502  * 'CPU state barrier', nothing should get across.
503  */
504 void __init cpu_init (void)
505 {
506         int cpu = smp_processor_id();
507         struct tss_struct * t = init_tss + cpu;
508         struct thread_struct *thread = &current->thread;
509
510         if (test_and_set_bit(cpu, &cpu_initialized)) {
511                 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
512                 for (;;) local_irq_enable();
513         }
514         printk(KERN_INFO "Initializing CPU#%d\n", cpu);
515
516         if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
517                 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
518         if (tsc_disable && cpu_has_tsc) {
519                 printk(KERN_NOTICE "Disabling TSC...\n");
520                 /**** FIX-HPA: DOES THIS REALLY BELONG HERE? ****/
521                 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
522                 set_in_cr4(X86_CR4_TSD);
523         }
524
525         /*
526          * Initialize the per-CPU GDT with the boot GDT,
527          * and set up the GDT descriptor:
528          */
529         if (cpu) {
530                 memcpy(cpu_gdt_table[cpu], cpu_gdt_table[0], GDT_SIZE);
531                 cpu_gdt_descr[cpu].size = GDT_SIZE - 1;
532                 cpu_gdt_descr[cpu].address = (unsigned long)cpu_gdt_table[cpu];
533         }
534         /*
535          * Set up the per-thread TLS descriptor cache:
536          */
537         memcpy(thread->tls_array, cpu_gdt_table[cpu], GDT_ENTRY_TLS_ENTRIES * 8);
538
539         __asm__ __volatile__("lgdt %0" : : "m" (cpu_gdt_descr[cpu]));
540         __asm__ __volatile__("lidt %0" : : "m" (idt_descr));
541
542         /*
543          * Delete NT
544          */
545         __asm__("pushfl ; andl $0xffffbfff,(%esp) ; popfl");
546
547         /*
548          * Set up and load the per-CPU TSS and LDT
549          */
550         atomic_inc(&init_mm.mm_count);
551         current->active_mm = &init_mm;
552         if (current->mm)
553                 BUG();
554         enter_lazy_tlb(&init_mm, current);
555
556         load_esp0(t, thread);
557         set_tss_desc(cpu,t);
558         cpu_gdt_table[cpu][GDT_ENTRY_TSS].b &= 0xfffffdff;
559         load_TR_desc();
560         if (cpu)
561                 load_LDT(&init_mm.context);
562
563         /* Set up doublefault TSS pointer in the GDT */
564         __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
565         cpu_gdt_table[cpu][GDT_ENTRY_DOUBLEFAULT_TSS].b &= 0xfffffdff;
566
567         if (cpu)
568                 trap_init_virtual_GDT();
569
570         /* Clear %fs and %gs. */
571         asm volatile ("xorl %eax, %eax; movl %eax, %fs; movl %eax, %gs");
572
573         /* Clear all 6 debug registers: */
574
575 #define CD(register) __asm__("movl %0,%%db" #register ::"r"(0) );
576
577         CD(0); CD(1); CD(2); CD(3); /* no db4 and db5 */; CD(6); CD(7);
578
579 #undef CD
580
581         /*
582          * Force FPU initialization:
583          */
584         current_thread_info()->status = 0;
585         current->used_math = 0;
586         mxcsr_feature_mask_init();
587 }