blob: f2a2b0ac9a18464d4c88d8b216aa843a783843e3 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#ifdef CONFIG_X86_LOCAL_APIC
16#include <asm/mpspec.h>
17#include <asm/apic.h>
18#include <mach_apic.h>
19#endif
20
21#include "cpu.h"
22
James Bottomley2b932f62006-02-24 13:04:14 -080023DEFINE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
24EXPORT_PER_CPU_SYMBOL(cpu_gdt_descr);
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026DEFINE_PER_CPU(unsigned char, cpu_16bit_stack[CPU_16BIT_STACK_SIZE]);
27EXPORT_PER_CPU_SYMBOL(cpu_16bit_stack);
28
Chuck Ebbert3bc9b762006-03-23 02:59:33 -080029static int cachesize_override __cpuinitdata = -1;
Chuck Ebbert4f886512006-03-23 02:59:34 -080030static int disable_x86_fxsr __cpuinitdata;
Chuck Ebbert3bc9b762006-03-23 02:59:33 -080031static int disable_x86_serial_nr __cpuinitdata = 1;
Chuck Ebbert4f886512006-03-23 02:59:34 -080032static int disable_x86_sep __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34struct cpu_dev * cpu_devs[X86_VENDOR_NUM] = {};
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036extern int disable_pse;
37
38static void default_init(struct cpuinfo_x86 * c)
39{
40 /* Not much we can do here... */
41 /* Check if at least it has cpuid */
42 if (c->cpuid_level == -1) {
43 /* No cpuid. It must be an ancient CPU */
44 if (c->x86 == 4)
45 strcpy(c->x86_model_id, "486");
46 else if (c->x86 == 3)
47 strcpy(c->x86_model_id, "386");
48 }
49}
50
51static struct cpu_dev default_cpu = {
52 .c_init = default_init,
Chuck Ebbertfe38d852006-02-04 23:28:03 -080053 .c_vendor = "Unknown",
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55static struct cpu_dev * this_cpu = &default_cpu;
56
57static int __init cachesize_setup(char *str)
58{
59 get_option (&str, &cachesize_override);
60 return 1;
61}
62__setup("cachesize=", cachesize_setup);
63
Chuck Ebbert3bc9b762006-03-23 02:59:33 -080064int __cpuinit get_model_name(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 unsigned int *v;
67 char *p, *q;
68
69 if (cpuid_eax(0x80000000) < 0x80000004)
70 return 0;
71
72 v = (unsigned int *) c->x86_model_id;
73 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
74 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
75 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
76 c->x86_model_id[48] = 0;
77
78 /* Intel chips right-justify this string for some dumb reason;
79 undo that brain damage */
80 p = q = &c->x86_model_id[0];
81 while ( *p == ' ' )
82 p++;
83 if ( p != q ) {
84 while ( *p )
85 *q++ = *p++;
86 while ( q <= &c->x86_model_id[48] )
87 *q++ = '\0'; /* Zero-pad the rest */
88 }
89
90 return 1;
91}
92
93
Chuck Ebbert3bc9b762006-03-23 02:59:33 -080094void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 unsigned int n, dummy, ecx, edx, l2size;
97
98 n = cpuid_eax(0x80000000);
99
100 if (n >= 0x80000005) {
101 cpuid(0x80000005, &dummy, &dummy, &ecx, &edx);
102 printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
103 edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
104 c->x86_cache_size=(ecx>>24)+(edx>>24);
105 }
106
107 if (n < 0x80000006) /* Some chips just has a large L1. */
108 return;
109
110 ecx = cpuid_ecx(0x80000006);
111 l2size = ecx >> 16;
112
113 /* do processor-specific cache resizing */
114 if (this_cpu->c_size_cache)
115 l2size = this_cpu->c_size_cache(c,l2size);
116
117 /* Allow user to override all this if necessary. */
118 if (cachesize_override != -1)
119 l2size = cachesize_override;
120
121 if ( l2size == 0 )
122 return; /* Again, no L2 cache is possible */
123
124 c->x86_cache_size = l2size;
125
126 printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
127 l2size, ecx & 0xFF);
128}
129
130/* Naming convention should be: <Name> [(<Codename>)] */
131/* This table only is used unless init_<vendor>() below doesn't set it; */
132/* in particular, if CPUID levels 0x80000002..4 are supported, this isn't used */
133
134/* Look up CPU names by table lookup. */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800135static char __cpuinit *table_lookup_model(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct cpu_model_info *info;
138
139 if ( c->x86_model >= 16 )
140 return NULL; /* Range check */
141
142 if (!this_cpu)
143 return NULL;
144
145 info = this_cpu->c_models;
146
147 while (info && info->family) {
148 if (info->family == c->x86)
149 return info->model_names[c->x86_model];
150 info++;
151 }
152 return NULL; /* Not found */
153}
154
155
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800156static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c, int early)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 char *v = c->x86_vendor_id;
159 int i;
Chuck Ebbertfe38d852006-02-04 23:28:03 -0800160 static int printed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 for (i = 0; i < X86_VENDOR_NUM; i++) {
163 if (cpu_devs[i]) {
164 if (!strcmp(v,cpu_devs[i]->c_ident[0]) ||
165 (cpu_devs[i]->c_ident[1] &&
166 !strcmp(v,cpu_devs[i]->c_ident[1]))) {
167 c->x86_vendor = i;
168 if (!early)
169 this_cpu = cpu_devs[i];
Chuck Ebbertfe38d852006-02-04 23:28:03 -0800170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 }
173 }
Chuck Ebbertfe38d852006-02-04 23:28:03 -0800174 if (!printed) {
175 printed++;
176 printk(KERN_ERR "CPU: Vendor unknown, using generic init.\n");
177 printk(KERN_ERR "CPU: Your system may be unstable.\n");
178 }
179 c->x86_vendor = X86_VENDOR_UNKNOWN;
180 this_cpu = &default_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183
184static int __init x86_fxsr_setup(char * s)
185{
186 disable_x86_fxsr = 1;
187 return 1;
188}
189__setup("nofxsr", x86_fxsr_setup);
190
191
Chuck Ebbert4f886512006-03-23 02:59:34 -0800192static int __init x86_sep_setup(char * s)
193{
194 disable_x86_sep = 1;
195 return 1;
196}
197__setup("nosep", x86_sep_setup);
198
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* Standard macro to see if a specific flag is changeable */
201static inline int flag_is_changeable_p(u32 flag)
202{
203 u32 f1, f2;
204
205 asm("pushfl\n\t"
206 "pushfl\n\t"
207 "popl %0\n\t"
208 "movl %0,%1\n\t"
209 "xorl %2,%0\n\t"
210 "pushl %0\n\t"
211 "popfl\n\t"
212 "pushfl\n\t"
213 "popl %0\n\t"
214 "popfl\n\t"
215 : "=&r" (f1), "=&r" (f2)
216 : "ir" (flag));
217
218 return ((f1^f2) & flag) != 0;
219}
220
221
222/* Probe for the CPUID instruction */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800223static int __cpuinit have_cpuid_p(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 return flag_is_changeable_p(X86_EFLAGS_ID);
226}
227
228/* Do minimum CPU detection early.
229 Fields really needed: vendor, cpuid_level, family, model, mask, cache alignment.
Andi Kleen2e664aa2006-01-11 22:46:33 +0100230 The others are not touched to avoid unwanted side effects.
231
232 WARNING: this function is only called on the BP. Don't add code here
233 that is supposed to run on all CPUs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static void __init early_cpu_detect(void)
235{
236 struct cpuinfo_x86 *c = &boot_cpu_data;
237
238 c->x86_cache_alignment = 32;
239
240 if (!have_cpuid_p())
241 return;
242
243 /* Get vendor name */
244 cpuid(0x00000000, &c->cpuid_level,
245 (int *)&c->x86_vendor_id[0],
246 (int *)&c->x86_vendor_id[8],
247 (int *)&c->x86_vendor_id[4]);
248
249 get_cpu_vendor(c, 1);
250
251 c->x86 = 4;
252 if (c->cpuid_level >= 0x00000001) {
253 u32 junk, tfms, cap0, misc;
254 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
255 c->x86 = (tfms >> 8) & 15;
256 c->x86_model = (tfms >> 4) & 15;
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100257 if (c->x86 == 0xf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 c->x86 += (tfms >> 20) & 0xff;
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100259 if (c->x86 >= 0x6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 c->x86_model += ((tfms >> 16) & 0xF) << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 c->x86_mask = tfms & 15;
262 if (cap0 & (1<<19))
263 c->x86_cache_alignment = ((misc >> 8) & 0xff) * 8;
264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800267void __cpuinit generic_identify(struct cpuinfo_x86 * c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 u32 tfms, xlvl;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800270 int ebx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (have_cpuid_p()) {
273 /* Get vendor name */
274 cpuid(0x00000000, &c->cpuid_level,
275 (int *)&c->x86_vendor_id[0],
276 (int *)&c->x86_vendor_id[8],
277 (int *)&c->x86_vendor_id[4]);
278
279 get_cpu_vendor(c, 0);
280 /* Initialize the standard set of capabilities */
281 /* Note that the vendor-specific code below might override */
282
283 /* Intel-defined flags: level 0x00000001 */
284 if ( c->cpuid_level >= 0x00000001 ) {
285 u32 capability, excap;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800286 cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 c->x86_capability[0] = capability;
288 c->x86_capability[4] = excap;
289 c->x86 = (tfms >> 8) & 15;
290 c->x86_model = (tfms >> 4) & 15;
Shaohua Lied2da192006-03-07 21:55:40 -0800291 if (c->x86 == 0xf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 c->x86 += (tfms >> 20) & 0xff;
Shaohua Lied2da192006-03-07 21:55:40 -0800293 if (c->x86 >= 0x6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 c->x86_model += ((tfms >> 16) & 0xF) << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 c->x86_mask = tfms & 15;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800296#ifdef CONFIG_SMP
297 c->apicid = phys_pkg_id((ebx >> 24) & 0xFF, 0);
298#else
299 c->apicid = (ebx >> 24) & 0xFF;
300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 } else {
302 /* Have CPUID level 0 only - unheard of */
303 c->x86 = 4;
304 }
305
306 /* AMD-defined flags: level 0x80000001 */
307 xlvl = cpuid_eax(0x80000000);
308 if ( (xlvl & 0xffff0000) == 0x80000000 ) {
309 if ( xlvl >= 0x80000001 ) {
310 c->x86_capability[1] = cpuid_edx(0x80000001);
311 c->x86_capability[6] = cpuid_ecx(0x80000001);
312 }
313 if ( xlvl >= 0x80000004 )
314 get_model_name(c); /* Default name */
315 }
316 }
Andi Kleen2e664aa2006-01-11 22:46:33 +0100317
318 early_intel_workaround(c);
319
320#ifdef CONFIG_X86_HT
321 phys_proc_id[smp_processor_id()] = (cpuid_ebx(1) >> 24) & 0xff;
322#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800325static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr ) {
328 /* Disable processor serial number */
329 unsigned long lo,hi;
330 rdmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
331 lo |= 0x200000;
332 wrmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
333 printk(KERN_NOTICE "CPU serial number disabled.\n");
334 clear_bit(X86_FEATURE_PN, c->x86_capability);
335
336 /* Disabling the serial number may affect the cpuid level */
337 c->cpuid_level = cpuid_eax(0);
338 }
339}
340
341static int __init x86_serial_nr_setup(char *s)
342{
343 disable_x86_serial_nr = 0;
344 return 1;
345}
346__setup("serialnumber", x86_serial_nr_setup);
347
348
349
350/*
351 * This does the hard work of actually picking apart the CPU stuff...
352 */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800353void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 int i;
356
357 c->loops_per_jiffy = loops_per_jiffy;
358 c->x86_cache_size = -1;
359 c->x86_vendor = X86_VENDOR_UNKNOWN;
360 c->cpuid_level = -1; /* CPUID not detected */
361 c->x86_model = c->x86_mask = 0; /* So far unknown... */
362 c->x86_vendor_id[0] = '\0'; /* Unset */
363 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100364 c->x86_max_cores = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 memset(&c->x86_capability, 0, sizeof c->x86_capability);
366
367 if (!have_cpuid_p()) {
368 /* First of all, decide if this is a 486 or higher */
369 /* It's a 486 if we can modify the AC flag */
370 if ( flag_is_changeable_p(X86_EFLAGS_AC) )
371 c->x86 = 4;
372 else
373 c->x86 = 3;
374 }
375
376 generic_identify(c);
377
378 printk(KERN_DEBUG "CPU: After generic identify, caps:");
379 for (i = 0; i < NCAPINTS; i++)
380 printk(" %08lx", c->x86_capability[i]);
381 printk("\n");
382
383 if (this_cpu->c_identify) {
384 this_cpu->c_identify(c);
385
386 printk(KERN_DEBUG "CPU: After vendor identify, caps:");
387 for (i = 0; i < NCAPINTS; i++)
388 printk(" %08lx", c->x86_capability[i]);
389 printk("\n");
390 }
391
392 /*
393 * Vendor-specific initialization. In this section we
394 * canonicalize the feature flags, meaning if there are
395 * features a certain CPU supports which CPUID doesn't
396 * tell us, CPUID claiming incorrect flags, or other bugs,
397 * we handle them here.
398 *
399 * At the end of this section, c->x86_capability better
400 * indicate the features this CPU genuinely supports!
401 */
402 if (this_cpu->c_init)
403 this_cpu->c_init(c);
404
405 /* Disable the PN if appropriate */
406 squash_the_stupid_serial_number(c);
407
408 /*
409 * The vendor-specific functions might have changed features. Now
410 * we do "generic changes."
411 */
412
413 /* TSC disabled? */
414 if ( tsc_disable )
415 clear_bit(X86_FEATURE_TSC, c->x86_capability);
416
417 /* FXSR disabled? */
418 if (disable_x86_fxsr) {
419 clear_bit(X86_FEATURE_FXSR, c->x86_capability);
420 clear_bit(X86_FEATURE_XMM, c->x86_capability);
421 }
422
Chuck Ebbert4f886512006-03-23 02:59:34 -0800423 /* SEP disabled? */
424 if (disable_x86_sep)
425 clear_bit(X86_FEATURE_SEP, c->x86_capability);
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (disable_pse)
428 clear_bit(X86_FEATURE_PSE, c->x86_capability);
429
430 /* If the model name is still unset, do table lookup. */
431 if ( !c->x86_model_id[0] ) {
432 char *p;
433 p = table_lookup_model(c);
434 if ( p )
435 strcpy(c->x86_model_id, p);
436 else
437 /* Last resort... */
438 sprintf(c->x86_model_id, "%02x/%02x",
Chuck Ebbert54a20f82006-03-23 02:59:36 -0800439 c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
442 /* Now the feature flags better reflect actual CPU features! */
443
444 printk(KERN_DEBUG "CPU: After all inits, caps:");
445 for (i = 0; i < NCAPINTS; i++)
446 printk(" %08lx", c->x86_capability[i]);
447 printk("\n");
448
449 /*
450 * On SMP, boot_cpu_data holds the common feature set between
451 * all CPUs; so make sure that we indicate which features are
452 * common between the CPUs. The first time this routine gets
453 * executed, c == &boot_cpu_data.
454 */
455 if ( c != &boot_cpu_data ) {
456 /* AND the already accumulated flags with these */
457 for ( i = 0 ; i < NCAPINTS ; i++ )
458 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
459 }
460
461 /* Init Machine Check Exception if available. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 mcheck_init(c);
Shaohua Li31ab2692005-11-07 00:58:42 -0800463
Li Shaohua6fe940d2005-06-25 14:54:53 -0700464 if (c == &boot_cpu_data)
465 sysenter_setup();
466 enable_sep_cpu();
Shaohua Li3b520b22005-07-07 17:56:38 -0700467
468 if (c == &boot_cpu_data)
469 mtrr_bp_init();
470 else
471 mtrr_ap_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474#ifdef CONFIG_X86_HT
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800475void __cpuinit detect_ht(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 u32 eax, ebx, ecx, edx;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100478 int index_msb, core_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int cpu = smp_processor_id();
480
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100481 cpuid(1, &eax, &ebx, &ecx, &edx);
482
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100483
Andi Kleen63518642005-04-16 15:25:16 -0700484 if (!cpu_has(c, X86_FEATURE_HT) || cpu_has(c, X86_FEATURE_CMP_LEGACY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return;
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 smp_num_siblings = (ebx & 0xff0000) >> 16;
488
489 if (smp_num_siblings == 1) {
490 printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
491 } else if (smp_num_siblings > 1 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 if (smp_num_siblings > NR_CPUS) {
494 printk(KERN_WARNING "CPU: Unsupported number of the siblings %d", smp_num_siblings);
495 smp_num_siblings = 1;
496 return;
497 }
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100498
499 index_msb = get_count_order(smp_num_siblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 phys_proc_id[cpu] = phys_pkg_id((ebx >> 24) & 0xFF, index_msb);
501
502 printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
503 phys_proc_id[cpu]);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700504
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100505 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
Andi Kleen3dd9d512005-04-16 15:25:15 -0700506
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100507 index_msb = get_count_order(smp_num_siblings) ;
Andi Kleen3dd9d512005-04-16 15:25:15 -0700508
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100509 core_bits = get_count_order(c->x86_max_cores);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700510
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100511 cpu_core_id[cpu] = phys_pkg_id((ebx >> 24) & 0xFF, index_msb) &
512 ((1 << core_bits) - 1);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700513
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100514 if (c->x86_max_cores > 1)
Andi Kleen3dd9d512005-04-16 15:25:15 -0700515 printk(KERN_INFO "CPU: Processor Core ID: %d\n",
516 cpu_core_id[cpu]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518}
519#endif
520
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800521void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 char *vendor = NULL;
524
525 if (c->x86_vendor < X86_VENDOR_NUM)
526 vendor = this_cpu->c_vendor;
527 else if (c->cpuid_level >= 0)
528 vendor = c->x86_vendor_id;
529
530 if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
531 printk("%s ", vendor);
532
533 if (!c->x86_model_id[0])
534 printk("%d86", c->x86);
535 else
536 printk("%s", c->x86_model_id);
537
538 if (c->x86_mask || c->cpuid_level >= 0)
539 printk(" stepping %02x\n", c->x86_mask);
540 else
541 printk("\n");
542}
543
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800544cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546/* This is hacky. :)
547 * We're emulating future behavior.
548 * In the future, the cpu-specific init functions will be called implicitly
549 * via the magic of initcalls.
550 * They will insert themselves into the cpu_devs structure.
551 * Then, when cpu_init() is called, we can just iterate over that array.
552 */
553
554extern int intel_cpu_init(void);
555extern int cyrix_init_cpu(void);
556extern int nsc_init_cpu(void);
557extern int amd_init_cpu(void);
558extern int centaur_init_cpu(void);
559extern int transmeta_init_cpu(void);
560extern int rise_init_cpu(void);
561extern int nexgen_init_cpu(void);
562extern int umc_init_cpu(void);
563
564void __init early_cpu_init(void)
565{
566 intel_cpu_init();
567 cyrix_init_cpu();
568 nsc_init_cpu();
569 amd_init_cpu();
570 centaur_init_cpu();
571 transmeta_init_cpu();
572 rise_init_cpu();
573 nexgen_init_cpu();
574 umc_init_cpu();
575 early_cpu_detect();
576
577#ifdef CONFIG_DEBUG_PAGEALLOC
578 /* pse is not compatible with on-the-fly unmapping,
579 * disable it even if the cpus claim to support it.
580 */
581 clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
582 disable_pse = 1;
583#endif
584}
585/*
586 * cpu_init() initializes state that is per-CPU. Some data is already
587 * initialized (naturally) in the bootstrap process, such as the GDT
588 * and IDT. We reload them nevertheless, this function acts as a
589 * 'CPU state barrier', nothing should get across.
590 */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800591void __cpuinit cpu_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 int cpu = smp_processor_id();
594 struct tss_struct * t = &per_cpu(init_tss, cpu);
595 struct thread_struct *thread = &current->thread;
James Bottomley2b932f62006-02-24 13:04:14 -0800596 struct desc_struct *gdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 __u32 stk16_off = (__u32)&per_cpu(cpu_16bit_stack, cpu);
James Bottomley2b932f62006-02-24 13:04:14 -0800598 struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (cpu_test_and_set(cpu, cpu_initialized)) {
601 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
602 for (;;) local_irq_enable();
603 }
604 printk(KERN_INFO "Initializing CPU#%d\n", cpu);
605
606 if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
607 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
608 if (tsc_disable && cpu_has_tsc) {
609 printk(KERN_NOTICE "Disabling TSC...\n");
610 /**** FIX-HPA: DOES THIS REALLY BELONG HERE? ****/
611 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
612 set_in_cr4(X86_CR4_TSD);
613 }
614
615 /*
James Bottomley2b932f62006-02-24 13:04:14 -0800616 * This is a horrible hack to allocate the GDT. The problem
617 * is that cpu_init() is called really early for the boot CPU
618 * (and hence needs bootmem) but much later for the secondary
619 * CPUs, when bootmem will have gone away
620 */
621 if (NODE_DATA(0)->bdata->node_bootmem_map) {
622 gdt = (struct desc_struct *)alloc_bootmem_pages(PAGE_SIZE);
623 /* alloc_bootmem_pages panics on failure, so no check */
624 memset(gdt, 0, PAGE_SIZE);
625 } else {
626 gdt = (struct desc_struct *)get_zeroed_page(GFP_KERNEL);
627 if (unlikely(!gdt)) {
628 printk(KERN_CRIT "CPU%d failed to allocate GDT\n", cpu);
629 for (;;)
630 local_irq_enable();
631 }
632 }
633
634 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * Initialize the per-CPU GDT with the boot GDT,
636 * and set up the GDT descriptor:
637 */
Zachary Amsden251e6912005-10-30 14:59:34 -0800638 memcpy(gdt, cpu_gdt_table, GDT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 /* Set up GDT entry for 16bit stack */
Zachary Amsden251e6912005-10-30 14:59:34 -0800641 *(__u64 *)(&gdt[GDT_ENTRY_ESPFIX_SS]) |=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 ((((__u64)stk16_off) << 16) & 0x000000ffffff0000ULL) |
643 ((((__u64)stk16_off) << 32) & 0xff00000000000000ULL) |
644 (CPU_16BIT_STACK_SIZE - 1);
645
James Bottomley2b932f62006-02-24 13:04:14 -0800646 cpu_gdt_descr->size = GDT_SIZE - 1;
647 cpu_gdt_descr->address = (unsigned long)gdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
James Bottomley2b932f62006-02-24 13:04:14 -0800649 load_gdt(cpu_gdt_descr);
Zachary Amsden4d37e7e2005-09-03 15:56:38 -0700650 load_idt(&idt_descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * Set up and load the per-CPU TSS and LDT
654 */
655 atomic_inc(&init_mm.mm_count);
656 current->active_mm = &init_mm;
657 if (current->mm)
658 BUG();
659 enter_lazy_tlb(&init_mm, current);
660
661 load_esp0(t, thread);
662 set_tss_desc(cpu,t);
663 load_TR_desc();
664 load_LDT(&init_mm.context);
665
Matt Mackall22c4e302006-01-08 01:05:24 -0800666#ifdef CONFIG_DOUBLEFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Set up doublefault TSS pointer in the GDT */
668 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
Matt Mackall22c4e302006-01-08 01:05:24 -0800669#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /* Clear %fs and %gs. */
672 asm volatile ("xorl %eax, %eax; movl %eax, %fs; movl %eax, %gs");
673
674 /* Clear all 6 debug registers: */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700675 set_debugreg(0, 0);
676 set_debugreg(0, 1);
677 set_debugreg(0, 2);
678 set_debugreg(0, 3);
679 set_debugreg(0, 6);
680 set_debugreg(0, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /*
683 * Force FPU initialization:
684 */
685 current_thread_info()->status = 0;
686 clear_used_math();
687 mxcsr_feature_mask_init();
688}
Li Shaohuae1367da2005-06-25 14:54:56 -0700689
690#ifdef CONFIG_HOTPLUG_CPU
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800691void __cpuinit cpu_uninit(void)
Li Shaohuae1367da2005-06-25 14:54:56 -0700692{
693 int cpu = raw_smp_processor_id();
694 cpu_clear(cpu, cpu_initialized);
695
696 /* lazy TLB state */
697 per_cpu(cpu_tlbstate, cpu).state = 0;
698 per_cpu(cpu_tlbstate, cpu).active_mm = &init_mm;
699}
700#endif