Merge branch 'linus' into perfcounters/core-v2
[pandora-kernel.git] / arch / x86 / kernel / cpu / common.c
1 #include <linux/bootmem.h>
2 #include <linux/linkage.h>
3 #include <linux/bitops.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/percpu.h>
7 #include <linux/string.h>
8 #include <linux/delay.h>
9 #include <linux/sched.h>
10 #include <linux/init.h>
11 #include <linux/kgdb.h>
12 #include <linux/smp.h>
13 #include <linux/io.h>
14
15 #include <asm/stackprotector.h>
16 #include <asm/perf_counter.h>
17 #include <asm/mmu_context.h>
18 #include <asm/hypervisor.h>
19 #include <asm/processor.h>
20 #include <asm/sections.h>
21 #include <asm/topology.h>
22 #include <asm/cpumask.h>
23 #include <asm/pgtable.h>
24 #include <asm/atomic.h>
25 #include <asm/proto.h>
26 #include <asm/setup.h>
27 #include <asm/apic.h>
28 #include <asm/desc.h>
29 #include <asm/i387.h>
30 #include <asm/mtrr.h>
31 #include <asm/numa.h>
32 #include <asm/asm.h>
33 #include <asm/cpu.h>
34 #include <asm/mce.h>
35 #include <asm/msr.h>
36 #include <asm/pat.h>
37 #include <asm/smp.h>
38
39 #ifdef CONFIG_X86_LOCAL_APIC
40 #include <asm/uv/uv.h>
41 #endif
42
43 #include "cpu.h"
44
45 /* all of these masks are initialized in setup_cpu_local_masks() */
46 cpumask_var_t cpu_initialized_mask;
47 cpumask_var_t cpu_callout_mask;
48 cpumask_var_t cpu_callin_mask;
49
50 /* representing cpus for which sibling maps can be computed */
51 cpumask_var_t cpu_sibling_setup_mask;
52
53 /* correctly size the local cpu masks */
54 void __init setup_cpu_local_masks(void)
55 {
56         alloc_bootmem_cpumask_var(&cpu_initialized_mask);
57         alloc_bootmem_cpumask_var(&cpu_callin_mask);
58         alloc_bootmem_cpumask_var(&cpu_callout_mask);
59         alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
60 }
61
62 static const struct cpu_dev *this_cpu __cpuinitdata;
63
64 DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
65 #ifdef CONFIG_X86_64
66         /*
67          * We need valid kernel segments for data and code in long mode too
68          * IRET will check the segment types  kkeil 2000/10/28
69          * Also sysret mandates a special GDT layout
70          *
71          * TLS descriptors are currently at a different place compared to i386.
72          * Hopefully nobody expects them at a fixed place (Wine?)
73          */
74         [GDT_ENTRY_KERNEL32_CS]         = { { { 0x0000ffff, 0x00cf9b00 } } },
75         [GDT_ENTRY_KERNEL_CS]           = { { { 0x0000ffff, 0x00af9b00 } } },
76         [GDT_ENTRY_KERNEL_DS]           = { { { 0x0000ffff, 0x00cf9300 } } },
77         [GDT_ENTRY_DEFAULT_USER32_CS]   = { { { 0x0000ffff, 0x00cffb00 } } },
78         [GDT_ENTRY_DEFAULT_USER_DS]     = { { { 0x0000ffff, 0x00cff300 } } },
79         [GDT_ENTRY_DEFAULT_USER_CS]     = { { { 0x0000ffff, 0x00affb00 } } },
80 #else
81         [GDT_ENTRY_KERNEL_CS]           = { { { 0x0000ffff, 0x00cf9a00 } } },
82         [GDT_ENTRY_KERNEL_DS]           = { { { 0x0000ffff, 0x00cf9200 } } },
83         [GDT_ENTRY_DEFAULT_USER_CS]     = { { { 0x0000ffff, 0x00cffa00 } } },
84         [GDT_ENTRY_DEFAULT_USER_DS]     = { { { 0x0000ffff, 0x00cff200 } } },
85         /*
86          * Segments used for calling PnP BIOS have byte granularity.
87          * They code segments and data segments have fixed 64k limits,
88          * the transfer segment sizes are set at run time.
89          */
90         /* 32-bit code */
91         [GDT_ENTRY_PNPBIOS_CS32]        = { { { 0x0000ffff, 0x00409a00 } } },
92         /* 16-bit code */
93         [GDT_ENTRY_PNPBIOS_CS16]        = { { { 0x0000ffff, 0x00009a00 } } },
94         /* 16-bit data */
95         [GDT_ENTRY_PNPBIOS_DS]          = { { { 0x0000ffff, 0x00009200 } } },
96         /* 16-bit data */
97         [GDT_ENTRY_PNPBIOS_TS1]         = { { { 0x00000000, 0x00009200 } } },
98         /* 16-bit data */
99         [GDT_ENTRY_PNPBIOS_TS2]         = { { { 0x00000000, 0x00009200 } } },
100         /*
101          * The APM segments have byte granularity and their bases
102          * are set at run time.  All have 64k limits.
103          */
104         /* 32-bit code */
105         [GDT_ENTRY_APMBIOS_BASE]        = { { { 0x0000ffff, 0x00409a00 } } },
106         /* 16-bit code */
107         [GDT_ENTRY_APMBIOS_BASE+1]      = { { { 0x0000ffff, 0x00009a00 } } },
108         /* data */
109         [GDT_ENTRY_APMBIOS_BASE+2]      = { { { 0x0000ffff, 0x00409200 } } },
110
111         [GDT_ENTRY_ESPFIX_SS]           = { { { 0x00000000, 0x00c09200 } } },
112         [GDT_ENTRY_PERCPU]              = { { { 0x0000ffff, 0x00cf9200 } } },
113         GDT_STACK_CANARY_INIT
114 #endif
115 } };
116 EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
117
118 #ifdef CONFIG_X86_32
119 static int cachesize_override __cpuinitdata = -1;
120 static int disable_x86_serial_nr __cpuinitdata = 1;
121
122 static int __init cachesize_setup(char *str)
123 {
124         get_option(&str, &cachesize_override);
125         return 1;
126 }
127 __setup("cachesize=", cachesize_setup);
128
129 static int __init x86_fxsr_setup(char *s)
130 {
131         setup_clear_cpu_cap(X86_FEATURE_FXSR);
132         setup_clear_cpu_cap(X86_FEATURE_XMM);
133         return 1;
134 }
135 __setup("nofxsr", x86_fxsr_setup);
136
137 static int __init x86_sep_setup(char *s)
138 {
139         setup_clear_cpu_cap(X86_FEATURE_SEP);
140         return 1;
141 }
142 __setup("nosep", x86_sep_setup);
143
144 /* Standard macro to see if a specific flag is changeable */
145 static inline int flag_is_changeable_p(u32 flag)
146 {
147         u32 f1, f2;
148
149         /*
150          * Cyrix and IDT cpus allow disabling of CPUID
151          * so the code below may return different results
152          * when it is executed before and after enabling
153          * the CPUID. Add "volatile" to not allow gcc to
154          * optimize the subsequent calls to this function.
155          */
156         asm volatile ("pushfl           \n\t"
157                       "pushfl           \n\t"
158                       "popl %0          \n\t"
159                       "movl %0, %1      \n\t"
160                       "xorl %2, %0      \n\t"
161                       "pushl %0         \n\t"
162                       "popfl            \n\t"
163                       "pushfl           \n\t"
164                       "popl %0          \n\t"
165                       "popfl            \n\t"
166
167                       : "=&r" (f1), "=&r" (f2)
168                       : "ir" (flag));
169
170         return ((f1^f2) & flag) != 0;
171 }
172
173 /* Probe for the CPUID instruction */
174 static int __cpuinit have_cpuid_p(void)
175 {
176         return flag_is_changeable_p(X86_EFLAGS_ID);
177 }
178
179 static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
180 {
181         unsigned long lo, hi;
182
183         if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr)
184                 return;
185
186         /* Disable processor serial number: */
187
188         rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
189         lo |= 0x200000;
190         wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
191
192         printk(KERN_NOTICE "CPU serial number disabled.\n");
193         clear_cpu_cap(c, X86_FEATURE_PN);
194
195         /* Disabling the serial number may affect the cpuid level */
196         c->cpuid_level = cpuid_eax(0);
197 }
198
199 static int __init x86_serial_nr_setup(char *s)
200 {
201         disable_x86_serial_nr = 0;
202         return 1;
203 }
204 __setup("serialnumber", x86_serial_nr_setup);
205 #else
206 static inline int flag_is_changeable_p(u32 flag)
207 {
208         return 1;
209 }
210 /* Probe for the CPUID instruction */
211 static inline int have_cpuid_p(void)
212 {
213         return 1;
214 }
215 static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
216 {
217 }
218 #endif
219
220 /*
221  * Some CPU features depend on higher CPUID levels, which may not always
222  * be available due to CPUID level capping or broken virtualization
223  * software.  Add those features to this table to auto-disable them.
224  */
225 struct cpuid_dependent_feature {
226         u32 feature;
227         u32 level;
228 };
229
230 static const struct cpuid_dependent_feature __cpuinitconst
231 cpuid_dependent_features[] = {
232         { X86_FEATURE_MWAIT,            0x00000005 },
233         { X86_FEATURE_DCA,              0x00000009 },
234         { X86_FEATURE_XSAVE,            0x0000000d },
235         { 0, 0 }
236 };
237
238 static void __cpuinit filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
239 {
240         const struct cpuid_dependent_feature *df;
241
242         for (df = cpuid_dependent_features; df->feature; df++) {
243
244                 if (!cpu_has(c, df->feature))
245                         continue;
246                 /*
247                  * Note: cpuid_level is set to -1 if unavailable, but
248                  * extended_extended_level is set to 0 if unavailable
249                  * and the legitimate extended levels are all negative
250                  * when signed; hence the weird messing around with
251                  * signs here...
252                  */
253                 if (!((s32)df->level < 0 ?
254                      (u32)df->level > (u32)c->extended_cpuid_level :
255                      (s32)df->level > (s32)c->cpuid_level))
256                         continue;
257
258                 clear_cpu_cap(c, df->feature);
259                 if (!warn)
260                         continue;
261
262                 printk(KERN_WARNING
263                        "CPU: CPU feature %s disabled, no CPUID level 0x%x\n",
264                                 x86_cap_flags[df->feature], df->level);
265         }
266 }
267
268 /*
269  * Naming convention should be: <Name> [(<Codename>)]
270  * This table only is used unless init_<vendor>() below doesn't set it;
271  * in particular, if CPUID levels 0x80000002..4 are supported, this
272  * isn't used
273  */
274
275 /* Look up CPU names by table lookup. */
276 static const char *__cpuinit table_lookup_model(struct cpuinfo_x86 *c)
277 {
278         const struct cpu_model_info *info;
279
280         if (c->x86_model >= 16)
281                 return NULL;    /* Range check */
282
283         if (!this_cpu)
284                 return NULL;
285
286         info = this_cpu->c_models;
287
288         while (info && info->family) {
289                 if (info->family == c->x86)
290                         return info->model_names[c->x86_model];
291                 info++;
292         }
293         return NULL;            /* Not found */
294 }
295
296 __u32 cleared_cpu_caps[NCAPINTS] __cpuinitdata;
297
298 void load_percpu_segment(int cpu)
299 {
300 #ifdef CONFIG_X86_32
301         loadsegment(fs, __KERNEL_PERCPU);
302 #else
303         loadsegment(gs, 0);
304         wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
305 #endif
306         load_stack_canary_segment();
307 }
308
309 /*
310  * Current gdt points %fs at the "master" per-cpu area: after this,
311  * it's on the real one.
312  */
313 void switch_to_new_gdt(int cpu)
314 {
315         struct desc_ptr gdt_descr;
316
317         gdt_descr.address = (long)get_cpu_gdt_table(cpu);
318         gdt_descr.size = GDT_SIZE - 1;
319         load_gdt(&gdt_descr);
320         /* Reload the per-cpu base */
321
322         load_percpu_segment(cpu);
323 }
324
325 static const struct cpu_dev *__cpuinitdata cpu_devs[X86_VENDOR_NUM] = {};
326
327 static void __cpuinit default_init(struct cpuinfo_x86 *c)
328 {
329 #ifdef CONFIG_X86_64
330         display_cacheinfo(c);
331 #else
332         /* Not much we can do here... */
333         /* Check if at least it has cpuid */
334         if (c->cpuid_level == -1) {
335                 /* No cpuid. It must be an ancient CPU */
336                 if (c->x86 == 4)
337                         strcpy(c->x86_model_id, "486");
338                 else if (c->x86 == 3)
339                         strcpy(c->x86_model_id, "386");
340         }
341 #endif
342 }
343
344 static const struct cpu_dev __cpuinitconst default_cpu = {
345         .c_init = default_init,
346         .c_vendor = "Unknown",
347         .c_x86_vendor = X86_VENDOR_UNKNOWN,
348 };
349
350 static void __cpuinit get_model_name(struct cpuinfo_x86 *c)
351 {
352         unsigned int *v;
353         char *p, *q;
354
355         if (c->extended_cpuid_level < 0x80000004)
356                 return;
357
358         v = (unsigned int *)c->x86_model_id;
359         cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
360         cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
361         cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
362         c->x86_model_id[48] = 0;
363
364         /*
365          * Intel chips right-justify this string for some dumb reason;
366          * undo that brain damage:
367          */
368         p = q = &c->x86_model_id[0];
369         while (*p == ' ')
370                 p++;
371         if (p != q) {
372                 while (*p)
373                         *q++ = *p++;
374                 while (q <= &c->x86_model_id[48])
375                         *q++ = '\0';    /* Zero-pad the rest */
376         }
377 }
378
379 void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
380 {
381         unsigned int n, dummy, ebx, ecx, edx, l2size;
382
383         n = c->extended_cpuid_level;
384
385         if (n >= 0x80000005) {
386                 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
387                 printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
388                                 edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
389                 c->x86_cache_size = (ecx>>24) + (edx>>24);
390 #ifdef CONFIG_X86_64
391                 /* On K8 L1 TLB is inclusive, so don't count it */
392                 c->x86_tlbsize = 0;
393 #endif
394         }
395
396         if (n < 0x80000006)     /* Some chips just has a large L1. */
397                 return;
398
399         cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
400         l2size = ecx >> 16;
401
402 #ifdef CONFIG_X86_64
403         c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
404 #else
405         /* do processor-specific cache resizing */
406         if (this_cpu->c_size_cache)
407                 l2size = this_cpu->c_size_cache(c, l2size);
408
409         /* Allow user to override all this if necessary. */
410         if (cachesize_override != -1)
411                 l2size = cachesize_override;
412
413         if (l2size == 0)
414                 return;         /* Again, no L2 cache is possible */
415 #endif
416
417         c->x86_cache_size = l2size;
418
419         printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
420                         l2size, ecx & 0xFF);
421 }
422
423 void __cpuinit detect_ht(struct cpuinfo_x86 *c)
424 {
425 #ifdef CONFIG_X86_HT
426         u32 eax, ebx, ecx, edx;
427         int index_msb, core_bits;
428
429         if (!cpu_has(c, X86_FEATURE_HT))
430                 return;
431
432         if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
433                 goto out;
434
435         if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
436                 return;
437
438         cpuid(1, &eax, &ebx, &ecx, &edx);
439
440         smp_num_siblings = (ebx & 0xff0000) >> 16;
441
442         if (smp_num_siblings == 1) {
443                 printk(KERN_INFO  "CPU: Hyper-Threading is disabled\n");
444                 goto out;
445         }
446
447         if (smp_num_siblings <= 1)
448                 goto out;
449
450         if (smp_num_siblings > nr_cpu_ids) {
451                 pr_warning("CPU: Unsupported number of siblings %d",
452                            smp_num_siblings);
453                 smp_num_siblings = 1;
454                 return;
455         }
456
457         index_msb = get_count_order(smp_num_siblings);
458         c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
459
460         smp_num_siblings = smp_num_siblings / c->x86_max_cores;
461
462         index_msb = get_count_order(smp_num_siblings);
463
464         core_bits = get_count_order(c->x86_max_cores);
465
466         c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
467                                        ((1 << core_bits) - 1);
468
469 out:
470         if ((c->x86_max_cores * smp_num_siblings) > 1) {
471                 printk(KERN_INFO  "CPU: Physical Processor ID: %d\n",
472                        c->phys_proc_id);
473                 printk(KERN_INFO  "CPU: Processor Core ID: %d\n",
474                        c->cpu_core_id);
475         }
476 #endif
477 }
478
479 static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
480 {
481         char *v = c->x86_vendor_id;
482         static int printed;
483         int i;
484
485         for (i = 0; i < X86_VENDOR_NUM; i++) {
486                 if (!cpu_devs[i])
487                         break;
488
489                 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
490                     (cpu_devs[i]->c_ident[1] &&
491                      !strcmp(v, cpu_devs[i]->c_ident[1]))) {
492
493                         this_cpu = cpu_devs[i];
494                         c->x86_vendor = this_cpu->c_x86_vendor;
495                         return;
496                 }
497         }
498
499         if (!printed) {
500                 printed++;
501                 printk(KERN_ERR
502                     "CPU: vendor_id '%s' unknown, using generic init.\n", v);
503
504                 printk(KERN_ERR "CPU: Your system may be unstable.\n");
505         }
506
507         c->x86_vendor = X86_VENDOR_UNKNOWN;
508         this_cpu = &default_cpu;
509 }
510
511 void __cpuinit cpu_detect(struct cpuinfo_x86 *c)
512 {
513         /* Get vendor name */
514         cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
515               (unsigned int *)&c->x86_vendor_id[0],
516               (unsigned int *)&c->x86_vendor_id[8],
517               (unsigned int *)&c->x86_vendor_id[4]);
518
519         c->x86 = 4;
520         /* Intel-defined flags: level 0x00000001 */
521         if (c->cpuid_level >= 0x00000001) {
522                 u32 junk, tfms, cap0, misc;
523
524                 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
525                 c->x86 = (tfms >> 8) & 0xf;
526                 c->x86_model = (tfms >> 4) & 0xf;
527                 c->x86_mask = tfms & 0xf;
528
529                 if (c->x86 == 0xf)
530                         c->x86 += (tfms >> 20) & 0xff;
531                 if (c->x86 >= 0x6)
532                         c->x86_model += ((tfms >> 16) & 0xf) << 4;
533
534                 if (cap0 & (1<<19)) {
535                         c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
536                         c->x86_cache_alignment = c->x86_clflush_size;
537                 }
538         }
539 }
540
541 static void __cpuinit get_cpu_cap(struct cpuinfo_x86 *c)
542 {
543         u32 tfms, xlvl;
544         u32 ebx;
545
546         /* Intel-defined flags: level 0x00000001 */
547         if (c->cpuid_level >= 0x00000001) {
548                 u32 capability, excap;
549
550                 cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
551                 c->x86_capability[0] = capability;
552                 c->x86_capability[4] = excap;
553         }
554
555         /* AMD-defined flags: level 0x80000001 */
556         xlvl = cpuid_eax(0x80000000);
557         c->extended_cpuid_level = xlvl;
558
559         if ((xlvl & 0xffff0000) == 0x80000000) {
560                 if (xlvl >= 0x80000001) {
561                         c->x86_capability[1] = cpuid_edx(0x80000001);
562                         c->x86_capability[6] = cpuid_ecx(0x80000001);
563                 }
564         }
565
566         if (c->extended_cpuid_level >= 0x80000008) {
567                 u32 eax = cpuid_eax(0x80000008);
568
569                 c->x86_virt_bits = (eax >> 8) & 0xff;
570                 c->x86_phys_bits = eax & 0xff;
571         }
572 #ifdef CONFIG_X86_32
573         else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36))
574                 c->x86_phys_bits = 36;
575 #endif
576
577         if (c->extended_cpuid_level >= 0x80000007)
578                 c->x86_power = cpuid_edx(0x80000007);
579
580 }
581
582 static void __cpuinit identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
583 {
584 #ifdef CONFIG_X86_32
585         int i;
586
587         /*
588          * First of all, decide if this is a 486 or higher
589          * It's a 486 if we can modify the AC flag
590          */
591         if (flag_is_changeable_p(X86_EFLAGS_AC))
592                 c->x86 = 4;
593         else
594                 c->x86 = 3;
595
596         for (i = 0; i < X86_VENDOR_NUM; i++)
597                 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
598                         c->x86_vendor_id[0] = 0;
599                         cpu_devs[i]->c_identify(c);
600                         if (c->x86_vendor_id[0]) {
601                                 get_cpu_vendor(c);
602                                 break;
603                         }
604                 }
605 #endif
606 }
607
608 /*
609  * Do minimum CPU detection early.
610  * Fields really needed: vendor, cpuid_level, family, model, mask,
611  * cache alignment.
612  * The others are not touched to avoid unwanted side effects.
613  *
614  * WARNING: this function is only called on the BP.  Don't add code here
615  * that is supposed to run on all CPUs.
616  */
617 static void __init early_identify_cpu(struct cpuinfo_x86 *c)
618 {
619 #ifdef CONFIG_X86_64
620         c->x86_clflush_size = 64;
621         c->x86_phys_bits = 36;
622         c->x86_virt_bits = 48;
623 #else
624         c->x86_clflush_size = 32;
625         c->x86_phys_bits = 32;
626         c->x86_virt_bits = 32;
627 #endif
628         c->x86_cache_alignment = c->x86_clflush_size;
629
630         memset(&c->x86_capability, 0, sizeof c->x86_capability);
631         c->extended_cpuid_level = 0;
632
633         if (!have_cpuid_p())
634                 identify_cpu_without_cpuid(c);
635
636         /* cyrix could have cpuid enabled via c_identify()*/
637         if (!have_cpuid_p())
638                 return;
639
640         cpu_detect(c);
641
642         get_cpu_vendor(c);
643
644         get_cpu_cap(c);
645
646         if (this_cpu->c_early_init)
647                 this_cpu->c_early_init(c);
648
649 #ifdef CONFIG_SMP
650         c->cpu_index = boot_cpu_id;
651 #endif
652         filter_cpuid_features(c, false);
653 }
654
655 void __init early_cpu_init(void)
656 {
657         const struct cpu_dev *const *cdev;
658         int count = 0;
659
660         printk(KERN_INFO "KERNEL supported cpus:\n");
661         for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
662                 const struct cpu_dev *cpudev = *cdev;
663                 unsigned int j;
664
665                 if (count >= X86_VENDOR_NUM)
666                         break;
667                 cpu_devs[count] = cpudev;
668                 count++;
669
670                 for (j = 0; j < 2; j++) {
671                         if (!cpudev->c_ident[j])
672                                 continue;
673                         printk(KERN_INFO "  %s %s\n", cpudev->c_vendor,
674                                 cpudev->c_ident[j]);
675                 }
676         }
677
678         early_identify_cpu(&boot_cpu_data);
679 }
680
681 /*
682  * The NOPL instruction is supposed to exist on all CPUs with
683  * family >= 6; unfortunately, that's not true in practice because
684  * of early VIA chips and (more importantly) broken virtualizers that
685  * are not easy to detect.  In the latter case it doesn't even *fail*
686  * reliably, so probing for it doesn't even work.  Disable it completely
687  * unless we can find a reliable way to detect all the broken cases.
688  */
689 static void __cpuinit detect_nopl(struct cpuinfo_x86 *c)
690 {
691         clear_cpu_cap(c, X86_FEATURE_NOPL);
692 }
693
694 static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
695 {
696         c->extended_cpuid_level = 0;
697
698         if (!have_cpuid_p())
699                 identify_cpu_without_cpuid(c);
700
701         /* cyrix could have cpuid enabled via c_identify()*/
702         if (!have_cpuid_p())
703                 return;
704
705         cpu_detect(c);
706
707         get_cpu_vendor(c);
708
709         get_cpu_cap(c);
710
711         if (c->cpuid_level >= 0x00000001) {
712                 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
713 #ifdef CONFIG_X86_32
714 # ifdef CONFIG_X86_HT
715                 c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
716 # else
717                 c->apicid = c->initial_apicid;
718 # endif
719 #endif
720
721 #ifdef CONFIG_X86_HT
722                 c->phys_proc_id = c->initial_apicid;
723 #endif
724         }
725
726         get_model_name(c); /* Default name */
727
728         init_scattered_cpuid_features(c);
729         detect_nopl(c);
730 }
731
732 /*
733  * This does the hard work of actually picking apart the CPU stuff...
734  */
735 static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
736 {
737         int i;
738
739         c->loops_per_jiffy = loops_per_jiffy;
740         c->x86_cache_size = -1;
741         c->x86_vendor = X86_VENDOR_UNKNOWN;
742         c->x86_model = c->x86_mask = 0; /* So far unknown... */
743         c->x86_vendor_id[0] = '\0'; /* Unset */
744         c->x86_model_id[0] = '\0';  /* Unset */
745         c->x86_max_cores = 1;
746         c->x86_coreid_bits = 0;
747 #ifdef CONFIG_X86_64
748         c->x86_clflush_size = 64;
749         c->x86_phys_bits = 36;
750         c->x86_virt_bits = 48;
751 #else
752         c->cpuid_level = -1;    /* CPUID not detected */
753         c->x86_clflush_size = 32;
754         c->x86_phys_bits = 32;
755         c->x86_virt_bits = 32;
756 #endif
757         c->x86_cache_alignment = c->x86_clflush_size;
758         memset(&c->x86_capability, 0, sizeof c->x86_capability);
759
760         generic_identify(c);
761
762         if (this_cpu->c_identify)
763                 this_cpu->c_identify(c);
764
765 #ifdef CONFIG_X86_64
766         c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
767 #endif
768
769         /*
770          * Vendor-specific initialization.  In this section we
771          * canonicalize the feature flags, meaning if there are
772          * features a certain CPU supports which CPUID doesn't
773          * tell us, CPUID claiming incorrect flags, or other bugs,
774          * we handle them here.
775          *
776          * At the end of this section, c->x86_capability better
777          * indicate the features this CPU genuinely supports!
778          */
779         if (this_cpu->c_init)
780                 this_cpu->c_init(c);
781
782         /* Disable the PN if appropriate */
783         squash_the_stupid_serial_number(c);
784
785         /*
786          * The vendor-specific functions might have changed features.
787          * Now we do "generic changes."
788          */
789
790         /* Filter out anything that depends on CPUID levels we don't have */
791         filter_cpuid_features(c, true);
792
793         /* If the model name is still unset, do table lookup. */
794         if (!c->x86_model_id[0]) {
795                 const char *p;
796                 p = table_lookup_model(c);
797                 if (p)
798                         strcpy(c->x86_model_id, p);
799                 else
800                         /* Last resort... */
801                         sprintf(c->x86_model_id, "%02x/%02x",
802                                 c->x86, c->x86_model);
803         }
804
805 #ifdef CONFIG_X86_64
806         detect_ht(c);
807 #endif
808
809         init_hypervisor(c);
810         /*
811          * On SMP, boot_cpu_data holds the common feature set between
812          * all CPUs; so make sure that we indicate which features are
813          * common between the CPUs.  The first time this routine gets
814          * executed, c == &boot_cpu_data.
815          */
816         if (c != &boot_cpu_data) {
817                 /* AND the already accumulated flags with these */
818                 for (i = 0; i < NCAPINTS; i++)
819                         boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
820         }
821
822         /* Clear all flags overriden by options */
823         for (i = 0; i < NCAPINTS; i++)
824                 c->x86_capability[i] &= ~cleared_cpu_caps[i];
825
826 #ifdef CONFIG_X86_MCE
827         /* Init Machine Check Exception if available. */
828         mcheck_init(c);
829 #endif
830
831         select_idle_routine(c);
832
833 #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
834         numa_add_cpu(smp_processor_id());
835 #endif
836 }
837
838 #ifdef CONFIG_X86_64
839 static void vgetcpu_set_mode(void)
840 {
841         if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
842                 vgetcpu_mode = VGETCPU_RDTSCP;
843         else
844                 vgetcpu_mode = VGETCPU_LSL;
845 }
846 #endif
847
848 void __init identify_boot_cpu(void)
849 {
850         identify_cpu(&boot_cpu_data);
851         init_c1e_mask();
852 #ifdef CONFIG_X86_32
853         sysenter_setup();
854         enable_sep_cpu();
855 #else
856         vgetcpu_set_mode();
857 #endif
858         init_hw_perf_counters();
859 }
860
861 void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
862 {
863         BUG_ON(c == &boot_cpu_data);
864         identify_cpu(c);
865 #ifdef CONFIG_X86_32
866         enable_sep_cpu();
867 #endif
868         mtrr_ap_init();
869 }
870
871 struct msr_range {
872         unsigned        min;
873         unsigned        max;
874 };
875
876 static const struct msr_range msr_range_array[] __cpuinitconst = {
877         { 0x00000000, 0x00000418},
878         { 0xc0000000, 0xc000040b},
879         { 0xc0010000, 0xc0010142},
880         { 0xc0011000, 0xc001103b},
881 };
882
883 static void __cpuinit print_cpu_msr(void)
884 {
885         unsigned index_min, index_max;
886         unsigned index;
887         u64 val;
888         int i;
889
890         for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
891                 index_min = msr_range_array[i].min;
892                 index_max = msr_range_array[i].max;
893
894                 for (index = index_min; index < index_max; index++) {
895                         if (rdmsrl_amd_safe(index, &val))
896                                 continue;
897                         printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
898                 }
899         }
900 }
901
902 static int show_msr __cpuinitdata;
903
904 static __init int setup_show_msr(char *arg)
905 {
906         int num;
907
908         get_option(&arg, &num);
909
910         if (num > 0)
911                 show_msr = num;
912         return 1;
913 }
914 __setup("show_msr=", setup_show_msr);
915
916 static __init int setup_noclflush(char *arg)
917 {
918         setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
919         return 1;
920 }
921 __setup("noclflush", setup_noclflush);
922
923 void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
924 {
925         const char *vendor = NULL;
926
927         if (c->x86_vendor < X86_VENDOR_NUM) {
928                 vendor = this_cpu->c_vendor;
929         } else {
930                 if (c->cpuid_level >= 0)
931                         vendor = c->x86_vendor_id;
932         }
933
934         if (vendor && !strstr(c->x86_model_id, vendor))
935                 printk(KERN_CONT "%s ", vendor);
936
937         if (c->x86_model_id[0])
938                 printk(KERN_CONT "%s", c->x86_model_id);
939         else
940                 printk(KERN_CONT "%d86", c->x86);
941
942         if (c->x86_mask || c->cpuid_level >= 0)
943                 printk(KERN_CONT " stepping %02x\n", c->x86_mask);
944         else
945                 printk(KERN_CONT "\n");
946
947 #ifdef CONFIG_SMP
948         if (c->cpu_index < show_msr)
949                 print_cpu_msr();
950 #else
951         if (show_msr)
952                 print_cpu_msr();
953 #endif
954 }
955
956 static __init int setup_disablecpuid(char *arg)
957 {
958         int bit;
959
960         if (get_option(&arg, &bit) && bit < NCAPINTS*32)
961                 setup_clear_cpu_cap(bit);
962         else
963                 return 0;
964
965         return 1;
966 }
967 __setup("clearcpuid=", setup_disablecpuid);
968
969 #ifdef CONFIG_X86_64
970 struct desc_ptr idt_descr = { 256 * 16 - 1, (unsigned long) idt_table };
971
972 DEFINE_PER_CPU_FIRST(union irq_stack_union,
973                      irq_stack_union) __aligned(PAGE_SIZE);
974
975 DEFINE_PER_CPU(char *, irq_stack_ptr) =
976         init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
977
978 DEFINE_PER_CPU(unsigned long, kernel_stack) =
979         (unsigned long)&init_thread_union - KERNEL_STACK_OFFSET + THREAD_SIZE;
980 EXPORT_PER_CPU_SYMBOL(kernel_stack);
981
982 DEFINE_PER_CPU(unsigned int, irq_count) = -1;
983
984 /*
985  * Special IST stacks which the CPU switches to when it calls
986  * an IST-marked descriptor entry. Up to 7 stacks (hardware
987  * limit), all of them are 4K, except the debug stack which
988  * is 8K.
989  */
990 static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = {
991           [0 ... N_EXCEPTION_STACKS - 1]        = EXCEPTION_STKSZ,
992           [DEBUG_STACK - 1]                     = DEBUG_STKSZ
993 };
994
995 static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
996         [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ])
997         __aligned(PAGE_SIZE);
998
999 /* May not be marked __init: used by software suspend */
1000 void syscall_init(void)
1001 {
1002         /*
1003          * LSTAR and STAR live in a bit strange symbiosis.
1004          * They both write to the same internal register. STAR allows to
1005          * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
1006          */
1007         wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32);
1008         wrmsrl(MSR_LSTAR, system_call);
1009         wrmsrl(MSR_CSTAR, ignore_sysret);
1010
1011 #ifdef CONFIG_IA32_EMULATION
1012         syscall32_cpu_init();
1013 #endif
1014
1015         /* Flags to clear on syscall */
1016         wrmsrl(MSR_SYSCALL_MASK,
1017                X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
1018 }
1019
1020 unsigned long kernel_eflags;
1021
1022 /*
1023  * Copies of the original ist values from the tss are only accessed during
1024  * debugging, no special alignment required.
1025  */
1026 DEFINE_PER_CPU(struct orig_ist, orig_ist);
1027
1028 #else   /* CONFIG_X86_64 */
1029
1030 #ifdef CONFIG_CC_STACKPROTECTOR
1031 DEFINE_PER_CPU(unsigned long, stack_canary);
1032 #endif
1033
1034 /* Make sure %fs and %gs are initialized properly in idle threads */
1035 struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
1036 {
1037         memset(regs, 0, sizeof(struct pt_regs));
1038         regs->fs = __KERNEL_PERCPU;
1039         regs->gs = __KERNEL_STACK_CANARY;
1040
1041         return regs;
1042 }
1043 #endif  /* CONFIG_X86_64 */
1044
1045 /*
1046  * Clear all 6 debug registers:
1047  */
1048 static void clear_all_debug_regs(void)
1049 {
1050         int i;
1051
1052         for (i = 0; i < 8; i++) {
1053                 /* Ignore db4, db5 */
1054                 if ((i == 4) || (i == 5))
1055                         continue;
1056
1057                 set_debugreg(0, i);
1058         }
1059 }
1060
1061 /*
1062  * cpu_init() initializes state that is per-CPU. Some data is already
1063  * initialized (naturally) in the bootstrap process, such as the GDT
1064  * and IDT. We reload them nevertheless, this function acts as a
1065  * 'CPU state barrier', nothing should get across.
1066  * A lot of state is already set up in PDA init for 64 bit
1067  */
1068 #ifdef CONFIG_X86_64
1069
1070 void __cpuinit cpu_init(void)
1071 {
1072         struct orig_ist *orig_ist;
1073         struct task_struct *me;
1074         struct tss_struct *t;
1075         unsigned long v;
1076         int cpu;
1077         int i;
1078
1079         cpu = stack_smp_processor_id();
1080         t = &per_cpu(init_tss, cpu);
1081         orig_ist = &per_cpu(orig_ist, cpu);
1082
1083 #ifdef CONFIG_NUMA
1084         if (cpu != 0 && percpu_read(node_number) == 0 &&
1085             cpu_to_node(cpu) != NUMA_NO_NODE)
1086                 percpu_write(node_number, cpu_to_node(cpu));
1087 #endif
1088
1089         me = current;
1090
1091         if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask))
1092                 panic("CPU#%d already initialized!\n", cpu);
1093
1094         printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1095
1096         clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1097
1098         /*
1099          * Initialize the per-CPU GDT with the boot GDT,
1100          * and set up the GDT descriptor:
1101          */
1102
1103         switch_to_new_gdt(cpu);
1104         loadsegment(fs, 0);
1105
1106         load_idt((const struct desc_ptr *)&idt_descr);
1107
1108         memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
1109         syscall_init();
1110
1111         wrmsrl(MSR_FS_BASE, 0);
1112         wrmsrl(MSR_KERNEL_GS_BASE, 0);
1113         barrier();
1114
1115         check_efer();
1116         if (cpu != 0)
1117                 enable_x2apic();
1118
1119         /*
1120          * set up and load the per-CPU TSS
1121          */
1122         if (!orig_ist->ist[0]) {
1123                 char *estacks = per_cpu(exception_stacks, cpu);
1124
1125                 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
1126                         estacks += exception_stack_sizes[v];
1127                         orig_ist->ist[v] = t->x86_tss.ist[v] =
1128                                         (unsigned long)estacks;
1129                 }
1130         }
1131
1132         t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1133
1134         /*
1135          * <= is required because the CPU will access up to
1136          * 8 bits beyond the end of the IO permission bitmap.
1137          */
1138         for (i = 0; i <= IO_BITMAP_LONGS; i++)
1139                 t->io_bitmap[i] = ~0UL;
1140
1141         atomic_inc(&init_mm.mm_count);
1142         me->active_mm = &init_mm;
1143         BUG_ON(me->mm);
1144         enter_lazy_tlb(&init_mm, me);
1145
1146         load_sp0(t, &current->thread);
1147         set_tss_desc(cpu, t);
1148         load_TR_desc();
1149         load_LDT(&init_mm.context);
1150
1151 #ifdef CONFIG_KGDB
1152         /*
1153          * If the kgdb is connected no debug regs should be altered.  This
1154          * is only applicable when KGDB and a KGDB I/O module are built
1155          * into the kernel and you are using early debugging with
1156          * kgdbwait. KGDB will control the kernel HW breakpoint registers.
1157          */
1158         if (kgdb_connected && arch_kgdb_ops.correct_hw_break)
1159                 arch_kgdb_ops.correct_hw_break();
1160         else
1161 #endif
1162                 clear_all_debug_regs();
1163
1164         fpu_init();
1165
1166         raw_local_save_flags(kernel_eflags);
1167
1168         if (is_uv_system())
1169                 uv_cpu_init();
1170 }
1171
1172 #else
1173
1174 void __cpuinit cpu_init(void)
1175 {
1176         int cpu = smp_processor_id();
1177         struct task_struct *curr = current;
1178         struct tss_struct *t = &per_cpu(init_tss, cpu);
1179         struct thread_struct *thread = &curr->thread;
1180
1181         if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)) {
1182                 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
1183                 for (;;)
1184                         local_irq_enable();
1185         }
1186
1187         printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1188
1189         if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
1190                 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
1191
1192         load_idt(&idt_descr);
1193         switch_to_new_gdt(cpu);
1194
1195         /*
1196          * Set up and load the per-CPU TSS and LDT
1197          */
1198         atomic_inc(&init_mm.mm_count);
1199         curr->active_mm = &init_mm;
1200         BUG_ON(curr->mm);
1201         enter_lazy_tlb(&init_mm, curr);
1202
1203         load_sp0(t, thread);
1204         set_tss_desc(cpu, t);
1205         load_TR_desc();
1206         load_LDT(&init_mm.context);
1207
1208 #ifdef CONFIG_DOUBLEFAULT
1209         /* Set up doublefault TSS pointer in the GDT */
1210         __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
1211 #endif
1212
1213         clear_all_debug_regs();
1214
1215         /*
1216          * Force FPU initialization:
1217          */
1218         if (cpu_has_xsave)
1219                 current_thread_info()->status = TS_XSAVE;
1220         else
1221                 current_thread_info()->status = 0;
1222         clear_used_math();
1223         mxcsr_feature_mask_init();
1224
1225         /*
1226          * Boot processor to setup the FP and extended state context info.
1227          */
1228         if (smp_processor_id() == boot_cpu_id)
1229                 init_thread_xstate();
1230
1231         xsave_init();
1232 }
1233 #endif