1 #include <linux/init.h>
2 #include <linux/string.h>
3 #include <linux/delay.h>
5 #include <linux/module.h>
6 #include <linux/percpu.h>
7 #include <linux/bootmem.h>
8 #include <asm/semaphore.h>
9 #include <asm/processor.h>
13 #include <asm/mmu_context.h>
14 #ifdef CONFIG_X86_LOCAL_APIC
15 #include <asm/mpspec.h>
17 #include <mach_apic.h>
22 DEFINE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
23 EXPORT_PER_CPU_SYMBOL(cpu_gdt_descr);
25 DEFINE_PER_CPU(unsigned char, cpu_16bit_stack[CPU_16BIT_STACK_SIZE]);
26 EXPORT_PER_CPU_SYMBOL(cpu_16bit_stack);
28 static int cachesize_override __cpuinitdata = -1;
29 static int disable_x86_fxsr __cpuinitdata;
30 static int disable_x86_serial_nr __cpuinitdata = 1;
31 static int disable_x86_sep __cpuinitdata;
33 struct cpu_dev * cpu_devs[X86_VENDOR_NUM] = {};
35 extern int disable_pse;
37 static void default_init(struct cpuinfo_x86 * c)
39 /* Not much we can do here... */
40 /* Check if at least it has cpuid */
41 if (c->cpuid_level == -1) {
42 /* No cpuid. It must be an ancient CPU */
44 strcpy(c->x86_model_id, "486");
46 strcpy(c->x86_model_id, "386");
50 static struct cpu_dev default_cpu = {
51 .c_init = default_init,
52 .c_vendor = "Unknown",
54 static struct cpu_dev * this_cpu = &default_cpu;
56 static int __init cachesize_setup(char *str)
58 get_option (&str, &cachesize_override);
61 __setup("cachesize=", cachesize_setup);
63 int __cpuinit get_model_name(struct cpuinfo_x86 *c)
68 if (cpuid_eax(0x80000000) < 0x80000004)
71 v = (unsigned int *) c->x86_model_id;
72 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
73 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
74 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
75 c->x86_model_id[48] = 0;
77 /* Intel chips right-justify this string for some dumb reason;
78 undo that brain damage */
79 p = q = &c->x86_model_id[0];
85 while ( q <= &c->x86_model_id[48] )
86 *q++ = '\0'; /* Zero-pad the rest */
93 void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
95 unsigned int n, dummy, ecx, edx, l2size;
97 n = cpuid_eax(0x80000000);
99 if (n >= 0x80000005) {
100 cpuid(0x80000005, &dummy, &dummy, &ecx, &edx);
101 printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
102 edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
103 c->x86_cache_size=(ecx>>24)+(edx>>24);
106 if (n < 0x80000006) /* Some chips just has a large L1. */
109 ecx = cpuid_ecx(0x80000006);
112 /* do processor-specific cache resizing */
113 if (this_cpu->c_size_cache)
114 l2size = this_cpu->c_size_cache(c,l2size);
116 /* Allow user to override all this if necessary. */
117 if (cachesize_override != -1)
118 l2size = cachesize_override;
121 return; /* Again, no L2 cache is possible */
123 c->x86_cache_size = l2size;
125 printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
129 /* Naming convention should be: <Name> [(<Codename>)] */
130 /* This table only is used unless init_<vendor>() below doesn't set it; */
131 /* in particular, if CPUID levels 0x80000002..4 are supported, this isn't used */
133 /* Look up CPU names by table lookup. */
134 static char __cpuinit *table_lookup_model(struct cpuinfo_x86 *c)
136 struct cpu_model_info *info;
138 if ( c->x86_model >= 16 )
139 return NULL; /* Range check */
144 info = this_cpu->c_models;
146 while (info && info->family) {
147 if (info->family == c->x86)
148 return info->model_names[c->x86_model];
151 return NULL; /* Not found */
155 static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c, int early)
157 char *v = c->x86_vendor_id;
161 for (i = 0; i < X86_VENDOR_NUM; i++) {
163 if (!strcmp(v,cpu_devs[i]->c_ident[0]) ||
164 (cpu_devs[i]->c_ident[1] &&
165 !strcmp(v,cpu_devs[i]->c_ident[1]))) {
168 this_cpu = cpu_devs[i];
175 printk(KERN_ERR "CPU: Vendor unknown, using generic init.\n");
176 printk(KERN_ERR "CPU: Your system may be unstable.\n");
178 c->x86_vendor = X86_VENDOR_UNKNOWN;
179 this_cpu = &default_cpu;
183 static int __init x86_fxsr_setup(char * s)
185 disable_x86_fxsr = 1;
188 __setup("nofxsr", x86_fxsr_setup);
191 static int __init x86_sep_setup(char * s)
196 __setup("nosep", x86_sep_setup);
199 /* Standard macro to see if a specific flag is changeable */
200 static inline int flag_is_changeable_p(u32 flag)
214 : "=&r" (f1), "=&r" (f2)
217 return ((f1^f2) & flag) != 0;
221 /* Probe for the CPUID instruction */
222 static int __cpuinit have_cpuid_p(void)
224 return flag_is_changeable_p(X86_EFLAGS_ID);
227 /* Do minimum CPU detection early.
228 Fields really needed: vendor, cpuid_level, family, model, mask, cache alignment.
229 The others are not touched to avoid unwanted side effects.
231 WARNING: this function is only called on the BP. Don't add code here
232 that is supposed to run on all CPUs. */
233 static void __init early_cpu_detect(void)
235 struct cpuinfo_x86 *c = &boot_cpu_data;
237 c->x86_cache_alignment = 32;
242 /* Get vendor name */
243 cpuid(0x00000000, &c->cpuid_level,
244 (int *)&c->x86_vendor_id[0],
245 (int *)&c->x86_vendor_id[8],
246 (int *)&c->x86_vendor_id[4]);
248 get_cpu_vendor(c, 1);
251 if (c->cpuid_level >= 0x00000001) {
252 u32 junk, tfms, cap0, misc;
253 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
254 c->x86 = (tfms >> 8) & 15;
255 c->x86_model = (tfms >> 4) & 15;
257 c->x86 += (tfms >> 20) & 0xff;
259 c->x86_model += ((tfms >> 16) & 0xF) << 4;
260 c->x86_mask = tfms & 15;
262 c->x86_cache_alignment = ((misc >> 8) & 0xff) * 8;
266 void __cpuinit generic_identify(struct cpuinfo_x86 * c)
271 if (have_cpuid_p()) {
272 /* Get vendor name */
273 cpuid(0x00000000, &c->cpuid_level,
274 (int *)&c->x86_vendor_id[0],
275 (int *)&c->x86_vendor_id[8],
276 (int *)&c->x86_vendor_id[4]);
278 get_cpu_vendor(c, 0);
279 /* Initialize the standard set of capabilities */
280 /* Note that the vendor-specific code below might override */
282 /* Intel-defined flags: level 0x00000001 */
283 if ( c->cpuid_level >= 0x00000001 ) {
284 u32 capability, excap;
285 cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
286 c->x86_capability[0] = capability;
287 c->x86_capability[4] = excap;
288 c->x86 = (tfms >> 8) & 15;
289 c->x86_model = (tfms >> 4) & 15;
291 c->x86 += (tfms >> 20) & 0xff;
293 c->x86_model += ((tfms >> 16) & 0xF) << 4;
294 c->x86_mask = tfms & 15;
296 c->apicid = phys_pkg_id((ebx >> 24) & 0xFF, 0);
298 c->apicid = (ebx >> 24) & 0xFF;
301 /* Have CPUID level 0 only - unheard of */
305 /* AMD-defined flags: level 0x80000001 */
306 xlvl = cpuid_eax(0x80000000);
307 if ( (xlvl & 0xffff0000) == 0x80000000 ) {
308 if ( xlvl >= 0x80000001 ) {
309 c->x86_capability[1] = cpuid_edx(0x80000001);
310 c->x86_capability[6] = cpuid_ecx(0x80000001);
312 if ( xlvl >= 0x80000004 )
313 get_model_name(c); /* Default name */
317 early_intel_workaround(c);
320 phys_proc_id[smp_processor_id()] = (cpuid_ebx(1) >> 24) & 0xff;
324 static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
326 if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr ) {
327 /* Disable processor serial number */
329 rdmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
331 wrmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
332 printk(KERN_NOTICE "CPU serial number disabled.\n");
333 clear_bit(X86_FEATURE_PN, c->x86_capability);
335 /* Disabling the serial number may affect the cpuid level */
336 c->cpuid_level = cpuid_eax(0);
340 static int __init x86_serial_nr_setup(char *s)
342 disable_x86_serial_nr = 0;
345 __setup("serialnumber", x86_serial_nr_setup);
350 * This does the hard work of actually picking apart the CPU stuff...
352 void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
356 c->loops_per_jiffy = loops_per_jiffy;
357 c->x86_cache_size = -1;
358 c->x86_vendor = X86_VENDOR_UNKNOWN;
359 c->cpuid_level = -1; /* CPUID not detected */
360 c->x86_model = c->x86_mask = 0; /* So far unknown... */
361 c->x86_vendor_id[0] = '\0'; /* Unset */
362 c->x86_model_id[0] = '\0'; /* Unset */
363 c->x86_max_cores = 1;
364 memset(&c->x86_capability, 0, sizeof c->x86_capability);
366 if (!have_cpuid_p()) {
367 /* First of all, decide if this is a 486 or higher */
368 /* It's a 486 if we can modify the AC flag */
369 if ( flag_is_changeable_p(X86_EFLAGS_AC) )
377 printk(KERN_DEBUG "CPU: After generic identify, caps:");
378 for (i = 0; i < NCAPINTS; i++)
379 printk(" %08lx", c->x86_capability[i]);
382 if (this_cpu->c_identify) {
383 this_cpu->c_identify(c);
385 printk(KERN_DEBUG "CPU: After vendor identify, caps:");
386 for (i = 0; i < NCAPINTS; i++)
387 printk(" %08lx", c->x86_capability[i]);
392 * Vendor-specific initialization. In this section we
393 * canonicalize the feature flags, meaning if there are
394 * features a certain CPU supports which CPUID doesn't
395 * tell us, CPUID claiming incorrect flags, or other bugs,
396 * we handle them here.
398 * At the end of this section, c->x86_capability better
399 * indicate the features this CPU genuinely supports!
401 if (this_cpu->c_init)
404 /* Disable the PN if appropriate */
405 squash_the_stupid_serial_number(c);
408 * The vendor-specific functions might have changed features. Now
409 * we do "generic changes."
414 clear_bit(X86_FEATURE_TSC, c->x86_capability);
417 if (disable_x86_fxsr) {
418 clear_bit(X86_FEATURE_FXSR, c->x86_capability);
419 clear_bit(X86_FEATURE_XMM, c->x86_capability);
424 clear_bit(X86_FEATURE_SEP, c->x86_capability);
427 clear_bit(X86_FEATURE_PSE, c->x86_capability);
429 /* If the model name is still unset, do table lookup. */
430 if ( !c->x86_model_id[0] ) {
432 p = table_lookup_model(c);
434 strcpy(c->x86_model_id, p);
437 sprintf(c->x86_model_id, "%02x/%02x",
438 c->x86, c->x86_model);
441 /* Now the feature flags better reflect actual CPU features! */
443 printk(KERN_DEBUG "CPU: After all inits, caps:");
444 for (i = 0; i < NCAPINTS; i++)
445 printk(" %08lx", c->x86_capability[i]);
449 * On SMP, boot_cpu_data holds the common feature set between
450 * all CPUs; so make sure that we indicate which features are
451 * common between the CPUs. The first time this routine gets
452 * executed, c == &boot_cpu_data.
454 if ( c != &boot_cpu_data ) {
455 /* AND the already accumulated flags with these */
456 for ( i = 0 ; i < NCAPINTS ; i++ )
457 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
460 /* Init Machine Check Exception if available. */
463 if (c == &boot_cpu_data)
467 if (c == &boot_cpu_data)
474 void __cpuinit detect_ht(struct cpuinfo_x86 *c)
476 u32 eax, ebx, ecx, edx;
477 int index_msb, core_bits;
478 int cpu = smp_processor_id();
480 cpuid(1, &eax, &ebx, &ecx, &edx);
483 if (!cpu_has(c, X86_FEATURE_HT) || cpu_has(c, X86_FEATURE_CMP_LEGACY))
486 smp_num_siblings = (ebx & 0xff0000) >> 16;
488 if (smp_num_siblings == 1) {
489 printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
490 } else if (smp_num_siblings > 1 ) {
492 if (smp_num_siblings > NR_CPUS) {
493 printk(KERN_WARNING "CPU: Unsupported number of the siblings %d", smp_num_siblings);
494 smp_num_siblings = 1;
498 index_msb = get_count_order(smp_num_siblings);
499 phys_proc_id[cpu] = phys_pkg_id((ebx >> 24) & 0xFF, index_msb);
501 printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
504 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
506 index_msb = get_count_order(smp_num_siblings) ;
508 core_bits = get_count_order(c->x86_max_cores);
510 cpu_core_id[cpu] = phys_pkg_id((ebx >> 24) & 0xFF, index_msb) &
511 ((1 << core_bits) - 1);
513 if (c->x86_max_cores > 1)
514 printk(KERN_INFO "CPU: Processor Core ID: %d\n",
520 void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
524 if (c->x86_vendor < X86_VENDOR_NUM)
525 vendor = this_cpu->c_vendor;
526 else if (c->cpuid_level >= 0)
527 vendor = c->x86_vendor_id;
529 if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
530 printk("%s ", vendor);
532 if (!c->x86_model_id[0])
533 printk("%d86", c->x86);
535 printk("%s", c->x86_model_id);
537 if (c->x86_mask || c->cpuid_level >= 0)
538 printk(" stepping %02x\n", c->x86_mask);
543 cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
546 * We're emulating future behavior.
547 * In the future, the cpu-specific init functions will be called implicitly
548 * via the magic of initcalls.
549 * They will insert themselves into the cpu_devs structure.
550 * Then, when cpu_init() is called, we can just iterate over that array.
553 extern int intel_cpu_init(void);
554 extern int cyrix_init_cpu(void);
555 extern int nsc_init_cpu(void);
556 extern int amd_init_cpu(void);
557 extern int centaur_init_cpu(void);
558 extern int transmeta_init_cpu(void);
559 extern int rise_init_cpu(void);
560 extern int nexgen_init_cpu(void);
561 extern int umc_init_cpu(void);
563 void __init early_cpu_init(void)
570 transmeta_init_cpu();
576 #ifdef CONFIG_DEBUG_PAGEALLOC
577 /* pse is not compatible with on-the-fly unmapping,
578 * disable it even if the cpus claim to support it.
580 clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
585 * cpu_init() initializes state that is per-CPU. Some data is already
586 * initialized (naturally) in the bootstrap process, such as the GDT
587 * and IDT. We reload them nevertheless, this function acts as a
588 * 'CPU state barrier', nothing should get across.
590 void __cpuinit cpu_init(void)
592 int cpu = smp_processor_id();
593 struct tss_struct * t = &per_cpu(init_tss, cpu);
594 struct thread_struct *thread = ¤t->thread;
595 struct desc_struct *gdt;
596 __u32 stk16_off = (__u32)&per_cpu(cpu_16bit_stack, cpu);
597 struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
599 if (cpu_test_and_set(cpu, cpu_initialized)) {
600 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
601 for (;;) local_irq_enable();
603 printk(KERN_INFO "Initializing CPU#%d\n", cpu);
605 if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
606 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
607 if (tsc_disable && cpu_has_tsc) {
608 printk(KERN_NOTICE "Disabling TSC...\n");
609 /**** FIX-HPA: DOES THIS REALLY BELONG HERE? ****/
610 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
611 set_in_cr4(X86_CR4_TSD);
615 * This is a horrible hack to allocate the GDT. The problem
616 * is that cpu_init() is called really early for the boot CPU
617 * (and hence needs bootmem) but much later for the secondary
618 * CPUs, when bootmem will have gone away
620 if (NODE_DATA(0)->bdata->node_bootmem_map) {
621 gdt = (struct desc_struct *)alloc_bootmem_pages(PAGE_SIZE);
622 /* alloc_bootmem_pages panics on failure, so no check */
623 memset(gdt, 0, PAGE_SIZE);
625 gdt = (struct desc_struct *)get_zeroed_page(GFP_KERNEL);
626 if (unlikely(!gdt)) {
627 printk(KERN_CRIT "CPU%d failed to allocate GDT\n", cpu);
634 * Initialize the per-CPU GDT with the boot GDT,
635 * and set up the GDT descriptor:
637 memcpy(gdt, cpu_gdt_table, GDT_SIZE);
639 /* Set up GDT entry for 16bit stack */
640 *(__u64 *)(&gdt[GDT_ENTRY_ESPFIX_SS]) |=
641 ((((__u64)stk16_off) << 16) & 0x000000ffffff0000ULL) |
642 ((((__u64)stk16_off) << 32) & 0xff00000000000000ULL) |
643 (CPU_16BIT_STACK_SIZE - 1);
645 cpu_gdt_descr->size = GDT_SIZE - 1;
646 cpu_gdt_descr->address = (unsigned long)gdt;
648 load_gdt(cpu_gdt_descr);
649 load_idt(&idt_descr);
652 * Set up and load the per-CPU TSS and LDT
654 atomic_inc(&init_mm.mm_count);
655 current->active_mm = &init_mm;
658 enter_lazy_tlb(&init_mm, current);
660 load_esp0(t, thread);
663 load_LDT(&init_mm.context);
665 #ifdef CONFIG_DOUBLEFAULT
666 /* Set up doublefault TSS pointer in the GDT */
667 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
670 /* Clear %fs and %gs. */
671 asm volatile ("xorl %eax, %eax; movl %eax, %fs; movl %eax, %gs");
673 /* Clear all 6 debug registers: */
682 * Force FPU initialization:
684 current_thread_info()->status = 0;
686 mxcsr_feature_mask_init();
689 #ifdef CONFIG_HOTPLUG_CPU
690 void __cpuinit cpu_uninit(void)
692 int cpu = raw_smp_processor_id();
693 cpu_clear(cpu, cpu_initialized);
696 per_cpu(cpu_tlbstate, cpu).state = 0;
697 per_cpu(cpu_tlbstate, cpu).active_mm = &init_mm;