blob: b2f24d57fddd25254eab6f832ffb3304cab80c84 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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>
James Bottomley2b932f62006-02-24 13:04:14 -08007#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <asm/semaphore.h>
9#include <asm/processor.h>
10#include <asm/i387.h>
11#include <asm/msr.h>
12#include <asm/io.h>
13#include <asm/mmu_context.h>
Alexey Dobriyan27b07da2006-06-23 02:04:18 -070014#include <asm/mtrr.h>
Alexey Dobriyana03a3e22006-06-23 02:04:20 -070015#include <asm/mce.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#ifdef CONFIG_X86_LOCAL_APIC
17#include <asm/mpspec.h>
18#include <asm/apic.h>
19#include <mach_apic.h>
20#endif
21
22#include "cpu.h"
23
James Bottomley2b932f62006-02-24 13:04:14 -080024DEFINE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
25EXPORT_PER_CPU_SYMBOL(cpu_gdt_descr);
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027DEFINE_PER_CPU(unsigned char, cpu_16bit_stack[CPU_16BIT_STACK_SIZE]);
28EXPORT_PER_CPU_SYMBOL(cpu_16bit_stack);
29
Chuck Ebbert3bc9b762006-03-23 02:59:33 -080030static int cachesize_override __cpuinitdata = -1;
Chuck Ebbert4f886512006-03-23 02:59:34 -080031static int disable_x86_fxsr __cpuinitdata;
Chuck Ebbert3bc9b762006-03-23 02:59:33 -080032static int disable_x86_serial_nr __cpuinitdata = 1;
Chuck Ebbert4f886512006-03-23 02:59:34 -080033static int disable_x86_sep __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35struct cpu_dev * cpu_devs[X86_VENDOR_NUM] = {};
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037extern int disable_pse;
38
Magnus Dammb4af3f72006-09-26 10:52:36 +020039static void __cpuinit default_init(struct cpuinfo_x86 * c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 /* Not much we can do here... */
42 /* Check if at least it has cpuid */
43 if (c->cpuid_level == -1) {
44 /* No cpuid. It must be an ancient CPU */
45 if (c->x86 == 4)
46 strcpy(c->x86_model_id, "486");
47 else if (c->x86 == 3)
48 strcpy(c->x86_model_id, "386");
49 }
50}
51
Magnus Damm95414932006-09-26 10:52:36 +020052static struct cpu_dev __cpuinitdata default_cpu = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .c_init = default_init,
Chuck Ebbertfe38d852006-02-04 23:28:03 -080054 .c_vendor = "Unknown",
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56static struct cpu_dev * this_cpu = &default_cpu;
57
58static int __init cachesize_setup(char *str)
59{
60 get_option (&str, &cachesize_override);
61 return 1;
62}
63__setup("cachesize=", cachesize_setup);
64
Chuck Ebbert3bc9b762006-03-23 02:59:33 -080065int __cpuinit get_model_name(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 unsigned int *v;
68 char *p, *q;
69
70 if (cpuid_eax(0x80000000) < 0x80000004)
71 return 0;
72
73 v = (unsigned int *) c->x86_model_id;
74 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
75 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
76 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
77 c->x86_model_id[48] = 0;
78
79 /* Intel chips right-justify this string for some dumb reason;
80 undo that brain damage */
81 p = q = &c->x86_model_id[0];
82 while ( *p == ' ' )
83 p++;
84 if ( p != q ) {
85 while ( *p )
86 *q++ = *p++;
87 while ( q <= &c->x86_model_id[48] )
88 *q++ = '\0'; /* Zero-pad the rest */
89 }
90
91 return 1;
92}
93
94
Chuck Ebbert3bc9b762006-03-23 02:59:33 -080095void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 unsigned int n, dummy, ecx, edx, l2size;
98
99 n = cpuid_eax(0x80000000);
100
101 if (n >= 0x80000005) {
102 cpuid(0x80000005, &dummy, &dummy, &ecx, &edx);
103 printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
104 edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
105 c->x86_cache_size=(ecx>>24)+(edx>>24);
106 }
107
108 if (n < 0x80000006) /* Some chips just has a large L1. */
109 return;
110
111 ecx = cpuid_ecx(0x80000006);
112 l2size = ecx >> 16;
113
114 /* do processor-specific cache resizing */
115 if (this_cpu->c_size_cache)
116 l2size = this_cpu->c_size_cache(c,l2size);
117
118 /* Allow user to override all this if necessary. */
119 if (cachesize_override != -1)
120 l2size = cachesize_override;
121
122 if ( l2size == 0 )
123 return; /* Again, no L2 cache is possible */
124
125 c->x86_cache_size = l2size;
126
127 printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
128 l2size, ecx & 0xFF);
129}
130
131/* Naming convention should be: <Name> [(<Codename>)] */
132/* This table only is used unless init_<vendor>() below doesn't set it; */
133/* in particular, if CPUID levels 0x80000002..4 are supported, this isn't used */
134
135/* Look up CPU names by table lookup. */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800136static char __cpuinit *table_lookup_model(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 struct cpu_model_info *info;
139
140 if ( c->x86_model >= 16 )
141 return NULL; /* Range check */
142
143 if (!this_cpu)
144 return NULL;
145
146 info = this_cpu->c_models;
147
148 while (info && info->family) {
149 if (info->family == c->x86)
150 return info->model_names[c->x86_model];
151 info++;
152 }
153 return NULL; /* Not found */
154}
155
156
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800157static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c, int early)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 char *v = c->x86_vendor_id;
160 int i;
Chuck Ebbertfe38d852006-02-04 23:28:03 -0800161 static int printed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 for (i = 0; i < X86_VENDOR_NUM; i++) {
164 if (cpu_devs[i]) {
165 if (!strcmp(v,cpu_devs[i]->c_ident[0]) ||
166 (cpu_devs[i]->c_ident[1] &&
167 !strcmp(v,cpu_devs[i]->c_ident[1]))) {
168 c->x86_vendor = i;
169 if (!early)
170 this_cpu = cpu_devs[i];
Chuck Ebbertfe38d852006-02-04 23:28:03 -0800171 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 }
174 }
Chuck Ebbertfe38d852006-02-04 23:28:03 -0800175 if (!printed) {
176 printed++;
177 printk(KERN_ERR "CPU: Vendor unknown, using generic init.\n");
178 printk(KERN_ERR "CPU: Your system may be unstable.\n");
179 }
180 c->x86_vendor = X86_VENDOR_UNKNOWN;
181 this_cpu = &default_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184
185static int __init x86_fxsr_setup(char * s)
186{
Linus Torvalds8ccb3dc2006-10-03 09:45:46 -0700187 /* Tell all the other CPU's to not use it... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 disable_x86_fxsr = 1;
Linus Torvalds8ccb3dc2006-10-03 09:45:46 -0700189
190 /*
191 * ... and clear the bits early in the boot_cpu_data
192 * so that the bootup process doesn't try to do this
193 * either.
194 */
195 clear_bit(X86_FEATURE_FXSR, boot_cpu_data.x86_capability);
196 clear_bit(X86_FEATURE_XMM, boot_cpu_data.x86_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 1;
198}
199__setup("nofxsr", x86_fxsr_setup);
200
201
Chuck Ebbert4f886512006-03-23 02:59:34 -0800202static int __init x86_sep_setup(char * s)
203{
204 disable_x86_sep = 1;
205 return 1;
206}
207__setup("nosep", x86_sep_setup);
208
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/* Standard macro to see if a specific flag is changeable */
211static inline int flag_is_changeable_p(u32 flag)
212{
213 u32 f1, f2;
214
215 asm("pushfl\n\t"
216 "pushfl\n\t"
217 "popl %0\n\t"
218 "movl %0,%1\n\t"
219 "xorl %2,%0\n\t"
220 "pushl %0\n\t"
221 "popfl\n\t"
222 "pushfl\n\t"
223 "popl %0\n\t"
224 "popfl\n\t"
225 : "=&r" (f1), "=&r" (f2)
226 : "ir" (flag));
227
228 return ((f1^f2) & flag) != 0;
229}
230
231
232/* Probe for the CPUID instruction */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800233static int __cpuinit have_cpuid_p(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 return flag_is_changeable_p(X86_EFLAGS_ID);
236}
237
238/* Do minimum CPU detection early.
239 Fields really needed: vendor, cpuid_level, family, model, mask, cache alignment.
Andi Kleen2e664aa2006-01-11 22:46:33 +0100240 The others are not touched to avoid unwanted side effects.
241
242 WARNING: this function is only called on the BP. Don't add code here
243 that is supposed to run on all CPUs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static void __init early_cpu_detect(void)
245{
246 struct cpuinfo_x86 *c = &boot_cpu_data;
247
248 c->x86_cache_alignment = 32;
249
250 if (!have_cpuid_p())
251 return;
252
253 /* Get vendor name */
254 cpuid(0x00000000, &c->cpuid_level,
255 (int *)&c->x86_vendor_id[0],
256 (int *)&c->x86_vendor_id[8],
257 (int *)&c->x86_vendor_id[4]);
258
259 get_cpu_vendor(c, 1);
260
261 c->x86 = 4;
262 if (c->cpuid_level >= 0x00000001) {
263 u32 junk, tfms, cap0, misc;
264 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
265 c->x86 = (tfms >> 8) & 15;
266 c->x86_model = (tfms >> 4) & 15;
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100267 if (c->x86 == 0xf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 c->x86 += (tfms >> 20) & 0xff;
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100269 if (c->x86 >= 0x6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 c->x86_model += ((tfms >> 16) & 0xF) << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 c->x86_mask = tfms & 15;
272 if (cap0 & (1<<19))
273 c->x86_cache_alignment = ((misc >> 8) & 0xff) * 8;
274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Magnus Damm68bbc172006-09-26 10:52:36 +0200277static void __cpuinit generic_identify(struct cpuinfo_x86 * c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 u32 tfms, xlvl;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800280 int ebx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (have_cpuid_p()) {
283 /* Get vendor name */
284 cpuid(0x00000000, &c->cpuid_level,
285 (int *)&c->x86_vendor_id[0],
286 (int *)&c->x86_vendor_id[8],
287 (int *)&c->x86_vendor_id[4]);
288
289 get_cpu_vendor(c, 0);
290 /* Initialize the standard set of capabilities */
291 /* Note that the vendor-specific code below might override */
292
293 /* Intel-defined flags: level 0x00000001 */
294 if ( c->cpuid_level >= 0x00000001 ) {
295 u32 capability, excap;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800296 cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 c->x86_capability[0] = capability;
298 c->x86_capability[4] = excap;
299 c->x86 = (tfms >> 8) & 15;
300 c->x86_model = (tfms >> 4) & 15;
Shaohua Lied2da192006-03-07 21:55:40 -0800301 if (c->x86 == 0xf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 c->x86 += (tfms >> 20) & 0xff;
Shaohua Lied2da192006-03-07 21:55:40 -0800303 if (c->x86 >= 0x6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 c->x86_model += ((tfms >> 16) & 0xF) << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 c->x86_mask = tfms & 15;
James Bottomley96c52742006-06-27 02:53:49 -0700306#ifdef CONFIG_X86_HT
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800307 c->apicid = phys_pkg_id((ebx >> 24) & 0xFF, 0);
308#else
309 c->apicid = (ebx >> 24) & 0xFF;
310#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 } else {
312 /* Have CPUID level 0 only - unheard of */
313 c->x86 = 4;
314 }
315
316 /* AMD-defined flags: level 0x80000001 */
317 xlvl = cpuid_eax(0x80000000);
318 if ( (xlvl & 0xffff0000) == 0x80000000 ) {
319 if ( xlvl >= 0x80000001 ) {
320 c->x86_capability[1] = cpuid_edx(0x80000001);
321 c->x86_capability[6] = cpuid_ecx(0x80000001);
322 }
323 if ( xlvl >= 0x80000004 )
324 get_model_name(c); /* Default name */
325 }
326 }
Andi Kleen2e664aa2006-01-11 22:46:33 +0100327
328 early_intel_workaround(c);
329
330#ifdef CONFIG_X86_HT
Rohit Seth4b89aff2006-06-27 02:53:46 -0700331 c->phys_proc_id = (cpuid_ebx(1) >> 24) & 0xff;
Andi Kleen2e664aa2006-01-11 22:46:33 +0100332#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800335static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr ) {
338 /* Disable processor serial number */
339 unsigned long lo,hi;
340 rdmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
341 lo |= 0x200000;
342 wrmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
343 printk(KERN_NOTICE "CPU serial number disabled.\n");
344 clear_bit(X86_FEATURE_PN, c->x86_capability);
345
346 /* Disabling the serial number may affect the cpuid level */
347 c->cpuid_level = cpuid_eax(0);
348 }
349}
350
351static int __init x86_serial_nr_setup(char *s)
352{
353 disable_x86_serial_nr = 0;
354 return 1;
355}
356__setup("serialnumber", x86_serial_nr_setup);
357
358
359
360/*
361 * This does the hard work of actually picking apart the CPU stuff...
362 */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800363void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 int i;
366
367 c->loops_per_jiffy = loops_per_jiffy;
368 c->x86_cache_size = -1;
369 c->x86_vendor = X86_VENDOR_UNKNOWN;
370 c->cpuid_level = -1; /* CPUID not detected */
371 c->x86_model = c->x86_mask = 0; /* So far unknown... */
372 c->x86_vendor_id[0] = '\0'; /* Unset */
373 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100374 c->x86_max_cores = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 memset(&c->x86_capability, 0, sizeof c->x86_capability);
376
377 if (!have_cpuid_p()) {
378 /* First of all, decide if this is a 486 or higher */
379 /* It's a 486 if we can modify the AC flag */
380 if ( flag_is_changeable_p(X86_EFLAGS_AC) )
381 c->x86 = 4;
382 else
383 c->x86 = 3;
384 }
385
386 generic_identify(c);
387
388 printk(KERN_DEBUG "CPU: After generic identify, caps:");
389 for (i = 0; i < NCAPINTS; i++)
390 printk(" %08lx", c->x86_capability[i]);
391 printk("\n");
392
393 if (this_cpu->c_identify) {
394 this_cpu->c_identify(c);
395
396 printk(KERN_DEBUG "CPU: After vendor identify, caps:");
397 for (i = 0; i < NCAPINTS; i++)
398 printk(" %08lx", c->x86_capability[i]);
399 printk("\n");
400 }
401
402 /*
403 * Vendor-specific initialization. In this section we
404 * canonicalize the feature flags, meaning if there are
405 * features a certain CPU supports which CPUID doesn't
406 * tell us, CPUID claiming incorrect flags, or other bugs,
407 * we handle them here.
408 *
409 * At the end of this section, c->x86_capability better
410 * indicate the features this CPU genuinely supports!
411 */
412 if (this_cpu->c_init)
413 this_cpu->c_init(c);
414
415 /* Disable the PN if appropriate */
416 squash_the_stupid_serial_number(c);
417
418 /*
419 * The vendor-specific functions might have changed features. Now
420 * we do "generic changes."
421 */
422
423 /* TSC disabled? */
424 if ( tsc_disable )
425 clear_bit(X86_FEATURE_TSC, c->x86_capability);
426
427 /* FXSR disabled? */
428 if (disable_x86_fxsr) {
429 clear_bit(X86_FEATURE_FXSR, c->x86_capability);
430 clear_bit(X86_FEATURE_XMM, c->x86_capability);
431 }
432
Chuck Ebbert4f886512006-03-23 02:59:34 -0800433 /* SEP disabled? */
434 if (disable_x86_sep)
435 clear_bit(X86_FEATURE_SEP, c->x86_capability);
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (disable_pse)
438 clear_bit(X86_FEATURE_PSE, c->x86_capability);
439
440 /* If the model name is still unset, do table lookup. */
441 if ( !c->x86_model_id[0] ) {
442 char *p;
443 p = table_lookup_model(c);
444 if ( p )
445 strcpy(c->x86_model_id, p);
446 else
447 /* Last resort... */
448 sprintf(c->x86_model_id, "%02x/%02x",
Chuck Ebbert54a20f82006-03-23 02:59:36 -0800449 c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 /* Now the feature flags better reflect actual CPU features! */
453
454 printk(KERN_DEBUG "CPU: After all inits, caps:");
455 for (i = 0; i < NCAPINTS; i++)
456 printk(" %08lx", c->x86_capability[i]);
457 printk("\n");
458
459 /*
460 * On SMP, boot_cpu_data holds the common feature set between
461 * all CPUs; so make sure that we indicate which features are
462 * common between the CPUs. The first time this routine gets
463 * executed, c == &boot_cpu_data.
464 */
465 if ( c != &boot_cpu_data ) {
466 /* AND the already accumulated flags with these */
467 for ( i = 0 ; i < NCAPINTS ; i++ )
468 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
469 }
470
471 /* Init Machine Check Exception if available. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 mcheck_init(c);
Shaohua Li31ab2692005-11-07 00:58:42 -0800473
Li Shaohua6fe940d2005-06-25 14:54:53 -0700474 if (c == &boot_cpu_data)
475 sysenter_setup();
476 enable_sep_cpu();
Shaohua Li3b520b22005-07-07 17:56:38 -0700477
478 if (c == &boot_cpu_data)
479 mtrr_bp_init();
480 else
481 mtrr_ap_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484#ifdef CONFIG_X86_HT
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800485void __cpuinit detect_ht(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 u32 eax, ebx, ecx, edx;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100488 int index_msb, core_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100490 cpuid(1, &eax, &ebx, &ecx, &edx);
491
Andi Kleen63518642005-04-16 15:25:16 -0700492 if (!cpu_has(c, X86_FEATURE_HT) || cpu_has(c, X86_FEATURE_CMP_LEGACY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return;
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 smp_num_siblings = (ebx & 0xff0000) >> 16;
496
497 if (smp_num_siblings == 1) {
498 printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
499 } else if (smp_num_siblings > 1 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (smp_num_siblings > NR_CPUS) {
Rohit Seth4b89aff2006-06-27 02:53:46 -0700502 printk(KERN_WARNING "CPU: Unsupported number of the "
503 "siblings %d", smp_num_siblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 smp_num_siblings = 1;
505 return;
506 }
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100507
508 index_msb = get_count_order(smp_num_siblings);
Rohit Seth4b89aff2006-06-27 02:53:46 -0700509 c->phys_proc_id = phys_pkg_id((ebx >> 24) & 0xFF, index_msb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
Rohit Seth4b89aff2006-06-27 02:53:46 -0700512 c->phys_proc_id);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700513
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100514 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
Andi Kleen3dd9d512005-04-16 15:25:15 -0700515
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100516 index_msb = get_count_order(smp_num_siblings) ;
Andi Kleen3dd9d512005-04-16 15:25:15 -0700517
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100518 core_bits = get_count_order(c->x86_max_cores);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700519
Rohit Seth4b89aff2006-06-27 02:53:46 -0700520 c->cpu_core_id = phys_pkg_id((ebx >> 24) & 0xFF, index_msb) &
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100521 ((1 << core_bits) - 1);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700522
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100523 if (c->x86_max_cores > 1)
Andi Kleen3dd9d512005-04-16 15:25:15 -0700524 printk(KERN_INFO "CPU: Processor Core ID: %d\n",
Rohit Seth4b89aff2006-06-27 02:53:46 -0700525 c->cpu_core_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527}
528#endif
529
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800530void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 char *vendor = NULL;
533
534 if (c->x86_vendor < X86_VENDOR_NUM)
535 vendor = this_cpu->c_vendor;
536 else if (c->cpuid_level >= 0)
537 vendor = c->x86_vendor_id;
538
539 if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
540 printk("%s ", vendor);
541
542 if (!c->x86_model_id[0])
543 printk("%d86", c->x86);
544 else
545 printk("%s", c->x86_model_id);
546
547 if (c->x86_mask || c->cpuid_level >= 0)
548 printk(" stepping %02x\n", c->x86_mask);
549 else
550 printk("\n");
551}
552
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800553cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555/* This is hacky. :)
556 * We're emulating future behavior.
557 * In the future, the cpu-specific init functions will be called implicitly
558 * via the magic of initcalls.
559 * They will insert themselves into the cpu_devs structure.
560 * Then, when cpu_init() is called, we can just iterate over that array.
561 */
562
563extern int intel_cpu_init(void);
564extern int cyrix_init_cpu(void);
565extern int nsc_init_cpu(void);
566extern int amd_init_cpu(void);
567extern int centaur_init_cpu(void);
568extern int transmeta_init_cpu(void);
569extern int rise_init_cpu(void);
570extern int nexgen_init_cpu(void);
571extern int umc_init_cpu(void);
572
573void __init early_cpu_init(void)
574{
575 intel_cpu_init();
576 cyrix_init_cpu();
577 nsc_init_cpu();
578 amd_init_cpu();
579 centaur_init_cpu();
580 transmeta_init_cpu();
581 rise_init_cpu();
582 nexgen_init_cpu();
583 umc_init_cpu();
584 early_cpu_detect();
585
586#ifdef CONFIG_DEBUG_PAGEALLOC
587 /* pse is not compatible with on-the-fly unmapping,
588 * disable it even if the cpus claim to support it.
589 */
590 clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
591 disable_pse = 1;
592#endif
593}
594/*
595 * cpu_init() initializes state that is per-CPU. Some data is already
596 * initialized (naturally) in the bootstrap process, such as the GDT
597 * and IDT. We reload them nevertheless, this function acts as a
598 * 'CPU state barrier', nothing should get across.
599 */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800600void __cpuinit cpu_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 int cpu = smp_processor_id();
603 struct tss_struct * t = &per_cpu(init_tss, cpu);
604 struct thread_struct *thread = &current->thread;
James Bottomley2b932f62006-02-24 13:04:14 -0800605 struct desc_struct *gdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 __u32 stk16_off = (__u32)&per_cpu(cpu_16bit_stack, cpu);
James Bottomley2b932f62006-02-24 13:04:14 -0800607 struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if (cpu_test_and_set(cpu, cpu_initialized)) {
610 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
611 for (;;) local_irq_enable();
612 }
613 printk(KERN_INFO "Initializing CPU#%d\n", cpu);
614
615 if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
616 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
617 if (tsc_disable && cpu_has_tsc) {
618 printk(KERN_NOTICE "Disabling TSC...\n");
619 /**** FIX-HPA: DOES THIS REALLY BELONG HERE? ****/
620 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
621 set_in_cr4(X86_CR4_TSD);
622 }
623
Shaohua Libd9e0b72006-06-27 02:53:43 -0700624 /* The CPU hotplug case */
625 if (cpu_gdt_descr->address) {
626 gdt = (struct desc_struct *)cpu_gdt_descr->address;
627 memset(gdt, 0, PAGE_SIZE);
628 goto old_gdt;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
James Bottomley2b932f62006-02-24 13:04:14 -0800631 * This is a horrible hack to allocate the GDT. The problem
632 * is that cpu_init() is called really early for the boot CPU
633 * (and hence needs bootmem) but much later for the secondary
634 * CPUs, when bootmem will have gone away
635 */
636 if (NODE_DATA(0)->bdata->node_bootmem_map) {
637 gdt = (struct desc_struct *)alloc_bootmem_pages(PAGE_SIZE);
638 /* alloc_bootmem_pages panics on failure, so no check */
639 memset(gdt, 0, PAGE_SIZE);
640 } else {
641 gdt = (struct desc_struct *)get_zeroed_page(GFP_KERNEL);
642 if (unlikely(!gdt)) {
643 printk(KERN_CRIT "CPU%d failed to allocate GDT\n", cpu);
644 for (;;)
645 local_irq_enable();
646 }
647 }
Shaohua Libd9e0b72006-06-27 02:53:43 -0700648old_gdt:
James Bottomley2b932f62006-02-24 13:04:14 -0800649 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 * Initialize the per-CPU GDT with the boot GDT,
651 * and set up the GDT descriptor:
652 */
Zachary Amsden251e6912005-10-30 14:59:34 -0800653 memcpy(gdt, cpu_gdt_table, GDT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /* Set up GDT entry for 16bit stack */
Zachary Amsden251e6912005-10-30 14:59:34 -0800656 *(__u64 *)(&gdt[GDT_ENTRY_ESPFIX_SS]) |=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ((((__u64)stk16_off) << 16) & 0x000000ffffff0000ULL) |
658 ((((__u64)stk16_off) << 32) & 0xff00000000000000ULL) |
659 (CPU_16BIT_STACK_SIZE - 1);
660
James Bottomley2b932f62006-02-24 13:04:14 -0800661 cpu_gdt_descr->size = GDT_SIZE - 1;
662 cpu_gdt_descr->address = (unsigned long)gdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
James Bottomley2b932f62006-02-24 13:04:14 -0800664 load_gdt(cpu_gdt_descr);
Zachary Amsden4d37e7e2005-09-03 15:56:38 -0700665 load_idt(&idt_descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * Set up and load the per-CPU TSS and LDT
669 */
670 atomic_inc(&init_mm.mm_count);
671 current->active_mm = &init_mm;
672 if (current->mm)
673 BUG();
674 enter_lazy_tlb(&init_mm, current);
675
676 load_esp0(t, thread);
677 set_tss_desc(cpu,t);
678 load_TR_desc();
679 load_LDT(&init_mm.context);
680
Matt Mackall22c4e302006-01-08 01:05:24 -0800681#ifdef CONFIG_DOUBLEFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* Set up doublefault TSS pointer in the GDT */
683 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
Matt Mackall22c4e302006-01-08 01:05:24 -0800684#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* Clear %fs and %gs. */
Jeremy Fitzhardinge5758d5d2006-09-26 10:52:34 +0200687 asm volatile ("movl %0, %%fs; movl %0, %%gs" : : "r" (0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 /* Clear all 6 debug registers: */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700690 set_debugreg(0, 0);
691 set_debugreg(0, 1);
692 set_debugreg(0, 2);
693 set_debugreg(0, 3);
694 set_debugreg(0, 6);
695 set_debugreg(0, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /*
698 * Force FPU initialization:
699 */
700 current_thread_info()->status = 0;
701 clear_used_math();
702 mxcsr_feature_mask_init();
703}
Li Shaohuae1367da2005-06-25 14:54:56 -0700704
705#ifdef CONFIG_HOTPLUG_CPU
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800706void __cpuinit cpu_uninit(void)
Li Shaohuae1367da2005-06-25 14:54:56 -0700707{
708 int cpu = raw_smp_processor_id();
709 cpu_clear(cpu, cpu_initialized);
710
711 /* lazy TLB state */
712 per_cpu(cpu_tlbstate, cpu).state = 0;
713 per_cpu(cpu_tlbstate, cpu).active_mm = &init_mm;
714}
715#endif