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