blob: 31e344b26bae824f255baa1446980486d8aff52a [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>
7#include <asm/semaphore.h>
8#include <asm/processor.h>
9#include <asm/i387.h>
10#include <asm/msr.h>
11#include <asm/io.h>
12#include <asm/mmu_context.h>
13#ifdef CONFIG_X86_LOCAL_APIC
14#include <asm/mpspec.h>
15#include <asm/apic.h>
16#include <mach_apic.h>
17#endif
18
19#include "cpu.h"
20
21DEFINE_PER_CPU(struct desc_struct, cpu_gdt_table[GDT_ENTRIES]);
22EXPORT_PER_CPU_SYMBOL(cpu_gdt_table);
23
24DEFINE_PER_CPU(unsigned char, cpu_16bit_stack[CPU_16BIT_STACK_SIZE]);
25EXPORT_PER_CPU_SYMBOL(cpu_16bit_stack);
26
Li Shaohua0bb31842005-06-25 14:54:55 -070027static int cachesize_override __devinitdata = -1;
28static int disable_x86_fxsr __devinitdata = 0;
29static int disable_x86_serial_nr __devinitdata = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31struct cpu_dev * cpu_devs[X86_VENDOR_NUM] = {};
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033extern int disable_pse;
34
35static void default_init(struct cpuinfo_x86 * c)
36{
37 /* Not much we can do here... */
38 /* Check if at least it has cpuid */
39 if (c->cpuid_level == -1) {
40 /* No cpuid. It must be an ancient CPU */
41 if (c->x86 == 4)
42 strcpy(c->x86_model_id, "486");
43 else if (c->x86 == 3)
44 strcpy(c->x86_model_id, "386");
45 }
46}
47
48static struct cpu_dev default_cpu = {
49 .c_init = default_init,
50};
51static struct cpu_dev * this_cpu = &default_cpu;
52
53static int __init cachesize_setup(char *str)
54{
55 get_option (&str, &cachesize_override);
56 return 1;
57}
58__setup("cachesize=", cachesize_setup);
59
Li Shaohua0bb31842005-06-25 14:54:55 -070060int __devinit get_model_name(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 unsigned int *v;
63 char *p, *q;
64
65 if (cpuid_eax(0x80000000) < 0x80000004)
66 return 0;
67
68 v = (unsigned int *) c->x86_model_id;
69 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
70 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
71 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
72 c->x86_model_id[48] = 0;
73
74 /* Intel chips right-justify this string for some dumb reason;
75 undo that brain damage */
76 p = q = &c->x86_model_id[0];
77 while ( *p == ' ' )
78 p++;
79 if ( p != q ) {
80 while ( *p )
81 *q++ = *p++;
82 while ( q <= &c->x86_model_id[48] )
83 *q++ = '\0'; /* Zero-pad the rest */
84 }
85
86 return 1;
87}
88
89
Li Shaohua0bb31842005-06-25 14:54:55 -070090void __devinit display_cacheinfo(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 unsigned int n, dummy, ecx, edx, l2size;
93
94 n = cpuid_eax(0x80000000);
95
96 if (n >= 0x80000005) {
97 cpuid(0x80000005, &dummy, &dummy, &ecx, &edx);
98 printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
99 edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
100 c->x86_cache_size=(ecx>>24)+(edx>>24);
101 }
102
103 if (n < 0x80000006) /* Some chips just has a large L1. */
104 return;
105
106 ecx = cpuid_ecx(0x80000006);
107 l2size = ecx >> 16;
108
109 /* do processor-specific cache resizing */
110 if (this_cpu->c_size_cache)
111 l2size = this_cpu->c_size_cache(c,l2size);
112
113 /* Allow user to override all this if necessary. */
114 if (cachesize_override != -1)
115 l2size = cachesize_override;
116
117 if ( l2size == 0 )
118 return; /* Again, no L2 cache is possible */
119
120 c->x86_cache_size = l2size;
121
122 printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
123 l2size, ecx & 0xFF);
124}
125
126/* Naming convention should be: <Name> [(<Codename>)] */
127/* This table only is used unless init_<vendor>() below doesn't set it; */
128/* in particular, if CPUID levels 0x80000002..4 are supported, this isn't used */
129
130/* Look up CPU names by table lookup. */
Li Shaohua0bb31842005-06-25 14:54:55 -0700131static char __devinit *table_lookup_model(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 struct cpu_model_info *info;
134
135 if ( c->x86_model >= 16 )
136 return NULL; /* Range check */
137
138 if (!this_cpu)
139 return NULL;
140
141 info = this_cpu->c_models;
142
143 while (info && info->family) {
144 if (info->family == c->x86)
145 return info->model_names[c->x86_model];
146 info++;
147 }
148 return NULL; /* Not found */
149}
150
151
Adrian Bunk672289e2005-09-10 00:27:21 -0700152static void __devinit get_cpu_vendor(struct cpuinfo_x86 *c, int early)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 char *v = c->x86_vendor_id;
155 int i;
156
157 for (i = 0; i < X86_VENDOR_NUM; i++) {
158 if (cpu_devs[i]) {
159 if (!strcmp(v,cpu_devs[i]->c_ident[0]) ||
160 (cpu_devs[i]->c_ident[1] &&
161 !strcmp(v,cpu_devs[i]->c_ident[1]))) {
162 c->x86_vendor = i;
163 if (!early)
164 this_cpu = cpu_devs[i];
165 break;
166 }
167 }
168 }
169}
170
171
172static int __init x86_fxsr_setup(char * s)
173{
174 disable_x86_fxsr = 1;
175 return 1;
176}
177__setup("nofxsr", x86_fxsr_setup);
178
179
180/* Standard macro to see if a specific flag is changeable */
181static inline int flag_is_changeable_p(u32 flag)
182{
183 u32 f1, f2;
184
185 asm("pushfl\n\t"
186 "pushfl\n\t"
187 "popl %0\n\t"
188 "movl %0,%1\n\t"
189 "xorl %2,%0\n\t"
190 "pushl %0\n\t"
191 "popfl\n\t"
192 "pushfl\n\t"
193 "popl %0\n\t"
194 "popfl\n\t"
195 : "=&r" (f1), "=&r" (f2)
196 : "ir" (flag));
197
198 return ((f1^f2) & flag) != 0;
199}
200
201
202/* Probe for the CPUID instruction */
Li Shaohua0bb31842005-06-25 14:54:55 -0700203static int __devinit have_cpuid_p(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 return flag_is_changeable_p(X86_EFLAGS_ID);
206}
207
208/* Do minimum CPU detection early.
209 Fields really needed: vendor, cpuid_level, family, model, mask, cache alignment.
210 The others are not touched to avoid unwanted side effects. */
211static void __init early_cpu_detect(void)
212{
213 struct cpuinfo_x86 *c = &boot_cpu_data;
214
215 c->x86_cache_alignment = 32;
216
217 if (!have_cpuid_p())
218 return;
219
220 /* Get vendor name */
221 cpuid(0x00000000, &c->cpuid_level,
222 (int *)&c->x86_vendor_id[0],
223 (int *)&c->x86_vendor_id[8],
224 (int *)&c->x86_vendor_id[4]);
225
226 get_cpu_vendor(c, 1);
227
228 c->x86 = 4;
229 if (c->cpuid_level >= 0x00000001) {
230 u32 junk, tfms, cap0, misc;
231 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
232 c->x86 = (tfms >> 8) & 15;
233 c->x86_model = (tfms >> 4) & 15;
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100234 if (c->x86 == 0xf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 c->x86 += (tfms >> 20) & 0xff;
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100236 if (c->x86 >= 0x6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 c->x86_model += ((tfms >> 16) & 0xF) << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 c->x86_mask = tfms & 15;
239 if (cap0 & (1<<19))
240 c->x86_cache_alignment = ((misc >> 8) & 0xff) * 8;
241 }
242
243 early_intel_workaround(c);
Andi Kleena1586082005-05-16 21:53:21 -0700244
Andi Kleena1586082005-05-16 21:53:21 -0700245#ifdef CONFIG_X86_HT
Andi Kleenb41e2932005-05-20 14:27:55 -0700246 phys_proc_id[smp_processor_id()] = (cpuid_ebx(1) >> 24) & 0xff;
Andi Kleena1586082005-05-16 21:53:21 -0700247#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Li Shaohua0bb31842005-06-25 14:54:55 -0700250void __devinit generic_identify(struct cpuinfo_x86 * c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 u32 tfms, xlvl;
253 int junk;
254
255 if (have_cpuid_p()) {
256 /* Get vendor name */
257 cpuid(0x00000000, &c->cpuid_level,
258 (int *)&c->x86_vendor_id[0],
259 (int *)&c->x86_vendor_id[8],
260 (int *)&c->x86_vendor_id[4]);
261
262 get_cpu_vendor(c, 0);
263 /* Initialize the standard set of capabilities */
264 /* Note that the vendor-specific code below might override */
265
266 /* Intel-defined flags: level 0x00000001 */
267 if ( c->cpuid_level >= 0x00000001 ) {
268 u32 capability, excap;
269 cpuid(0x00000001, &tfms, &junk, &excap, &capability);
270 c->x86_capability[0] = capability;
271 c->x86_capability[4] = excap;
272 c->x86 = (tfms >> 8) & 15;
273 c->x86_model = (tfms >> 4) & 15;
274 if (c->x86 == 0xf) {
275 c->x86 += (tfms >> 20) & 0xff;
276 c->x86_model += ((tfms >> 16) & 0xF) << 4;
277 }
278 c->x86_mask = tfms & 15;
279 } else {
280 /* Have CPUID level 0 only - unheard of */
281 c->x86 = 4;
282 }
283
284 /* AMD-defined flags: level 0x80000001 */
285 xlvl = cpuid_eax(0x80000000);
286 if ( (xlvl & 0xffff0000) == 0x80000000 ) {
287 if ( xlvl >= 0x80000001 ) {
288 c->x86_capability[1] = cpuid_edx(0x80000001);
289 c->x86_capability[6] = cpuid_ecx(0x80000001);
290 }
291 if ( xlvl >= 0x80000004 )
292 get_model_name(c); /* Default name */
293 }
294 }
295}
296
Li Shaohua0bb31842005-06-25 14:54:55 -0700297static void __devinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr ) {
300 /* Disable processor serial number */
301 unsigned long lo,hi;
302 rdmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
303 lo |= 0x200000;
304 wrmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
305 printk(KERN_NOTICE "CPU serial number disabled.\n");
306 clear_bit(X86_FEATURE_PN, c->x86_capability);
307
308 /* Disabling the serial number may affect the cpuid level */
309 c->cpuid_level = cpuid_eax(0);
310 }
311}
312
313static int __init x86_serial_nr_setup(char *s)
314{
315 disable_x86_serial_nr = 0;
316 return 1;
317}
318__setup("serialnumber", x86_serial_nr_setup);
319
320
321
322/*
323 * This does the hard work of actually picking apart the CPU stuff...
324 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700325void __devinit identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 int i;
328
329 c->loops_per_jiffy = loops_per_jiffy;
330 c->x86_cache_size = -1;
331 c->x86_vendor = X86_VENDOR_UNKNOWN;
332 c->cpuid_level = -1; /* CPUID not detected */
333 c->x86_model = c->x86_mask = 0; /* So far unknown... */
334 c->x86_vendor_id[0] = '\0'; /* Unset */
335 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100336 c->x86_max_cores = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 memset(&c->x86_capability, 0, sizeof c->x86_capability);
338
339 if (!have_cpuid_p()) {
340 /* First of all, decide if this is a 486 or higher */
341 /* It's a 486 if we can modify the AC flag */
342 if ( flag_is_changeable_p(X86_EFLAGS_AC) )
343 c->x86 = 4;
344 else
345 c->x86 = 3;
346 }
347
348 generic_identify(c);
349
350 printk(KERN_DEBUG "CPU: After generic identify, caps:");
351 for (i = 0; i < NCAPINTS; i++)
352 printk(" %08lx", c->x86_capability[i]);
353 printk("\n");
354
355 if (this_cpu->c_identify) {
356 this_cpu->c_identify(c);
357
358 printk(KERN_DEBUG "CPU: After vendor identify, caps:");
359 for (i = 0; i < NCAPINTS; i++)
360 printk(" %08lx", c->x86_capability[i]);
361 printk("\n");
362 }
363
364 /*
365 * Vendor-specific initialization. In this section we
366 * canonicalize the feature flags, meaning if there are
367 * features a certain CPU supports which CPUID doesn't
368 * tell us, CPUID claiming incorrect flags, or other bugs,
369 * we handle them here.
370 *
371 * At the end of this section, c->x86_capability better
372 * indicate the features this CPU genuinely supports!
373 */
374 if (this_cpu->c_init)
375 this_cpu->c_init(c);
376
377 /* Disable the PN if appropriate */
378 squash_the_stupid_serial_number(c);
379
380 /*
381 * The vendor-specific functions might have changed features. Now
382 * we do "generic changes."
383 */
384
385 /* TSC disabled? */
386 if ( tsc_disable )
387 clear_bit(X86_FEATURE_TSC, c->x86_capability);
388
389 /* FXSR disabled? */
390 if (disable_x86_fxsr) {
391 clear_bit(X86_FEATURE_FXSR, c->x86_capability);
392 clear_bit(X86_FEATURE_XMM, c->x86_capability);
393 }
394
395 if (disable_pse)
396 clear_bit(X86_FEATURE_PSE, c->x86_capability);
397
398 /* If the model name is still unset, do table lookup. */
399 if ( !c->x86_model_id[0] ) {
400 char *p;
401 p = table_lookup_model(c);
402 if ( p )
403 strcpy(c->x86_model_id, p);
404 else
405 /* Last resort... */
406 sprintf(c->x86_model_id, "%02x/%02x",
407 c->x86_vendor, c->x86_model);
408 }
409
410 /* Now the feature flags better reflect actual CPU features! */
411
412 printk(KERN_DEBUG "CPU: After all inits, caps:");
413 for (i = 0; i < NCAPINTS; i++)
414 printk(" %08lx", c->x86_capability[i]);
415 printk("\n");
416
417 /*
418 * On SMP, boot_cpu_data holds the common feature set between
419 * all CPUs; so make sure that we indicate which features are
420 * common between the CPUs. The first time this routine gets
421 * executed, c == &boot_cpu_data.
422 */
423 if ( c != &boot_cpu_data ) {
424 /* AND the already accumulated flags with these */
425 for ( i = 0 ; i < NCAPINTS ; i++ )
426 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
427 }
428
429 /* Init Machine Check Exception if available. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 mcheck_init(c);
Shaohua Li31ab2692005-11-07 00:58:42 -0800431
Li Shaohua6fe940d2005-06-25 14:54:53 -0700432 if (c == &boot_cpu_data)
433 sysenter_setup();
434 enable_sep_cpu();
Shaohua Li3b520b22005-07-07 17:56:38 -0700435
436 if (c == &boot_cpu_data)
437 mtrr_bp_init();
438 else
439 mtrr_ap_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442#ifdef CONFIG_X86_HT
Li Shaohua0bb31842005-06-25 14:54:55 -0700443void __devinit detect_ht(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 u32 eax, ebx, ecx, edx;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100446 int index_msb, core_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int cpu = smp_processor_id();
448
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100449 cpuid(1, &eax, &ebx, &ecx, &edx);
450
451 c->apicid = phys_pkg_id((ebx >> 24) & 0xFF, 0);
452
Andi Kleen63518642005-04-16 15:25:16 -0700453 if (!cpu_has(c, X86_FEATURE_HT) || cpu_has(c, X86_FEATURE_CMP_LEGACY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return;
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 smp_num_siblings = (ebx & 0xff0000) >> 16;
457
458 if (smp_num_siblings == 1) {
459 printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
460 } else if (smp_num_siblings > 1 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (smp_num_siblings > NR_CPUS) {
463 printk(KERN_WARNING "CPU: Unsupported number of the siblings %d", smp_num_siblings);
464 smp_num_siblings = 1;
465 return;
466 }
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100467
468 index_msb = get_count_order(smp_num_siblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 phys_proc_id[cpu] = phys_pkg_id((ebx >> 24) & 0xFF, index_msb);
470
471 printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
472 phys_proc_id[cpu]);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700473
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100474 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
Andi Kleen3dd9d512005-04-16 15:25:15 -0700475
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100476 index_msb = get_count_order(smp_num_siblings) ;
Andi Kleen3dd9d512005-04-16 15:25:15 -0700477
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100478 core_bits = get_count_order(c->x86_max_cores);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700479
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100480 cpu_core_id[cpu] = phys_pkg_id((ebx >> 24) & 0xFF, index_msb) &
481 ((1 << core_bits) - 1);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700482
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100483 if (c->x86_max_cores > 1)
Andi Kleen3dd9d512005-04-16 15:25:15 -0700484 printk(KERN_INFO "CPU: Processor Core ID: %d\n",
485 cpu_core_id[cpu]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487}
488#endif
489
Li Shaohua0bb31842005-06-25 14:54:55 -0700490void __devinit print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 char *vendor = NULL;
493
494 if (c->x86_vendor < X86_VENDOR_NUM)
495 vendor = this_cpu->c_vendor;
496 else if (c->cpuid_level >= 0)
497 vendor = c->x86_vendor_id;
498
499 if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
500 printk("%s ", vendor);
501
502 if (!c->x86_model_id[0])
503 printk("%d86", c->x86);
504 else
505 printk("%s", c->x86_model_id);
506
507 if (c->x86_mask || c->cpuid_level >= 0)
508 printk(" stepping %02x\n", c->x86_mask);
509 else
510 printk("\n");
511}
512
Li Shaohua0bb31842005-06-25 14:54:55 -0700513cpumask_t cpu_initialized __devinitdata = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515/* This is hacky. :)
516 * We're emulating future behavior.
517 * In the future, the cpu-specific init functions will be called implicitly
518 * via the magic of initcalls.
519 * They will insert themselves into the cpu_devs structure.
520 * Then, when cpu_init() is called, we can just iterate over that array.
521 */
522
523extern int intel_cpu_init(void);
524extern int cyrix_init_cpu(void);
525extern int nsc_init_cpu(void);
526extern int amd_init_cpu(void);
527extern int centaur_init_cpu(void);
528extern int transmeta_init_cpu(void);
529extern int rise_init_cpu(void);
530extern int nexgen_init_cpu(void);
531extern int umc_init_cpu(void);
532
533void __init early_cpu_init(void)
534{
535 intel_cpu_init();
536 cyrix_init_cpu();
537 nsc_init_cpu();
538 amd_init_cpu();
539 centaur_init_cpu();
540 transmeta_init_cpu();
541 rise_init_cpu();
542 nexgen_init_cpu();
543 umc_init_cpu();
544 early_cpu_detect();
545
546#ifdef CONFIG_DEBUG_PAGEALLOC
547 /* pse is not compatible with on-the-fly unmapping,
548 * disable it even if the cpus claim to support it.
549 */
550 clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
551 disable_pse = 1;
552#endif
553}
554/*
555 * cpu_init() initializes state that is per-CPU. Some data is already
556 * initialized (naturally) in the bootstrap process, such as the GDT
557 * and IDT. We reload them nevertheless, this function acts as a
558 * 'CPU state barrier', nothing should get across.
559 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700560void __devinit cpu_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 int cpu = smp_processor_id();
563 struct tss_struct * t = &per_cpu(init_tss, cpu);
564 struct thread_struct *thread = &current->thread;
Zachary Amsden251e6912005-10-30 14:59:34 -0800565 struct desc_struct *gdt = get_cpu_gdt_table(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 __u32 stk16_off = (__u32)&per_cpu(cpu_16bit_stack, cpu);
567
568 if (cpu_test_and_set(cpu, cpu_initialized)) {
569 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
570 for (;;) local_irq_enable();
571 }
572 printk(KERN_INFO "Initializing CPU#%d\n", cpu);
573
574 if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
575 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
576 if (tsc_disable && cpu_has_tsc) {
577 printk(KERN_NOTICE "Disabling TSC...\n");
578 /**** FIX-HPA: DOES THIS REALLY BELONG HERE? ****/
579 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
580 set_in_cr4(X86_CR4_TSD);
581 }
582
583 /*
584 * Initialize the per-CPU GDT with the boot GDT,
585 * and set up the GDT descriptor:
586 */
Zachary Amsden251e6912005-10-30 14:59:34 -0800587 memcpy(gdt, cpu_gdt_table, GDT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /* Set up GDT entry for 16bit stack */
Zachary Amsden251e6912005-10-30 14:59:34 -0800590 *(__u64 *)(&gdt[GDT_ENTRY_ESPFIX_SS]) |=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 ((((__u64)stk16_off) << 16) & 0x000000ffffff0000ULL) |
592 ((((__u64)stk16_off) << 32) & 0xff00000000000000ULL) |
593 (CPU_16BIT_STACK_SIZE - 1);
594
595 cpu_gdt_descr[cpu].size = GDT_SIZE - 1;
Zachary Amsden251e6912005-10-30 14:59:34 -0800596 cpu_gdt_descr[cpu].address = (unsigned long)gdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Zachary Amsden4d37e7e2005-09-03 15:56:38 -0700598 load_gdt(&cpu_gdt_descr[cpu]);
599 load_idt(&idt_descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /*
602 * Delete NT
603 */
604 __asm__("pushfl ; andl $0xffffbfff,(%esp) ; popfl");
605
606 /*
607 * Set up and load the per-CPU TSS and LDT
608 */
609 atomic_inc(&init_mm.mm_count);
610 current->active_mm = &init_mm;
611 if (current->mm)
612 BUG();
613 enter_lazy_tlb(&init_mm, current);
614
615 load_esp0(t, thread);
616 set_tss_desc(cpu,t);
617 load_TR_desc();
618 load_LDT(&init_mm.context);
619
620 /* Set up doublefault TSS pointer in the GDT */
621 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
622
623 /* Clear %fs and %gs. */
624 asm volatile ("xorl %eax, %eax; movl %eax, %fs; movl %eax, %gs");
625
626 /* Clear all 6 debug registers: */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700627 set_debugreg(0, 0);
628 set_debugreg(0, 1);
629 set_debugreg(0, 2);
630 set_debugreg(0, 3);
631 set_debugreg(0, 6);
632 set_debugreg(0, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /*
635 * Force FPU initialization:
636 */
637 current_thread_info()->status = 0;
638 clear_used_math();
639 mxcsr_feature_mask_init();
640}
Li Shaohuae1367da2005-06-25 14:54:56 -0700641
642#ifdef CONFIG_HOTPLUG_CPU
643void __devinit cpu_uninit(void)
644{
645 int cpu = raw_smp_processor_id();
646 cpu_clear(cpu, cpu_initialized);
647
648 /* lazy TLB state */
649 per_cpu(cpu_tlbstate, cpu).state = 0;
650 per_cpu(cpu_tlbstate, cpu).active_mm = &init_mm;
651}
652#endif