blob: 2a26956fce428d76275bc676ccd7952b8f7d5854 [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
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +010021#include <asm/pda.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "cpu.h"
24
James Bottomley2b932f62006-02-24 13:04:14 -080025DEFINE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
26EXPORT_PER_CPU_SYMBOL(cpu_gdt_descr);
27
Rusty Russellbf5046722007-05-02 19:27:10 +020028DEFINE_PER_CPU(struct desc_struct, cpu_gdt[GDT_ENTRIES]) = {
29 [GDT_ENTRY_KERNEL_CS] = { 0x0000ffff, 0x00cf9a00 },
30 [GDT_ENTRY_KERNEL_DS] = { 0x0000ffff, 0x00cf9200 },
31 [GDT_ENTRY_DEFAULT_USER_CS] = { 0x0000ffff, 0x00cffa00 },
32 [GDT_ENTRY_DEFAULT_USER_DS] = { 0x0000ffff, 0x00cff200 },
33 /*
34 * Segments used for calling PnP BIOS have byte granularity.
35 * They code segments and data segments have fixed 64k limits,
36 * the transfer segment sizes are set at run time.
37 */
38 [GDT_ENTRY_PNPBIOS_CS32] = { 0x0000ffff, 0x00409a00 },/* 32-bit code */
39 [GDT_ENTRY_PNPBIOS_CS16] = { 0x0000ffff, 0x00009a00 },/* 16-bit code */
40 [GDT_ENTRY_PNPBIOS_DS] = { 0x0000ffff, 0x00009200 }, /* 16-bit data */
41 [GDT_ENTRY_PNPBIOS_TS1] = { 0x00000000, 0x00009200 },/* 16-bit data */
42 [GDT_ENTRY_PNPBIOS_TS2] = { 0x00000000, 0x00009200 },/* 16-bit data */
43 /*
44 * The APM segments have byte granularity and their bases
45 * are set at run time. All have 64k limits.
46 */
47 [GDT_ENTRY_APMBIOS_BASE] = { 0x0000ffff, 0x00409a00 },/* 32-bit code */
48 /* 16-bit code */
49 [GDT_ENTRY_APMBIOS_BASE+1] = { 0x0000ffff, 0x00009a00 },
50 [GDT_ENTRY_APMBIOS_BASE+2] = { 0x0000ffff, 0x00409200 }, /* data */
51
52 [GDT_ENTRY_ESPFIX_SS] = { 0x00000000, 0x00c09200 },
53 [GDT_ENTRY_PDA] = { 0x00000000, 0x00c09200 }, /* set in setup_pda */
54};
Rusty Russellae1ee112007-05-02 19:27:10 +020055
56DEFINE_PER_CPU(struct i386_pda, _cpu_pda);
57EXPORT_PER_CPU_SYMBOL(_cpu_pda);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +010058
Chuck Ebbert3bc9b762006-03-23 02:59:33 -080059static int cachesize_override __cpuinitdata = -1;
Chuck Ebbert4f886512006-03-23 02:59:34 -080060static int disable_x86_fxsr __cpuinitdata;
Chuck Ebbert3bc9b762006-03-23 02:59:33 -080061static int disable_x86_serial_nr __cpuinitdata = 1;
Chuck Ebbert4f886512006-03-23 02:59:34 -080062static int disable_x86_sep __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64struct cpu_dev * cpu_devs[X86_VENDOR_NUM] = {};
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066extern int disable_pse;
67
Magnus Dammb4af3f72006-09-26 10:52:36 +020068static void __cpuinit default_init(struct cpuinfo_x86 * c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 /* Not much we can do here... */
71 /* Check if at least it has cpuid */
72 if (c->cpuid_level == -1) {
73 /* No cpuid. It must be an ancient CPU */
74 if (c->x86 == 4)
75 strcpy(c->x86_model_id, "486");
76 else if (c->x86 == 3)
77 strcpy(c->x86_model_id, "386");
78 }
79}
80
Magnus Damm95414932006-09-26 10:52:36 +020081static struct cpu_dev __cpuinitdata default_cpu = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 .c_init = default_init,
Chuck Ebbertfe38d852006-02-04 23:28:03 -080083 .c_vendor = "Unknown",
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
Vivek Goyal9dbeeec2007-01-05 16:36:34 -080085static struct cpu_dev * this_cpu __cpuinitdata = &default_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87static int __init cachesize_setup(char *str)
88{
89 get_option (&str, &cachesize_override);
90 return 1;
91}
92__setup("cachesize=", cachesize_setup);
93
Chuck Ebbert3bc9b762006-03-23 02:59:33 -080094int __cpuinit get_model_name(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 unsigned int *v;
97 char *p, *q;
98
99 if (cpuid_eax(0x80000000) < 0x80000004)
100 return 0;
101
102 v = (unsigned int *) c->x86_model_id;
103 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
104 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
105 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
106 c->x86_model_id[48] = 0;
107
108 /* Intel chips right-justify this string for some dumb reason;
109 undo that brain damage */
110 p = q = &c->x86_model_id[0];
111 while ( *p == ' ' )
112 p++;
113 if ( p != q ) {
114 while ( *p )
115 *q++ = *p++;
116 while ( q <= &c->x86_model_id[48] )
117 *q++ = '\0'; /* Zero-pad the rest */
118 }
119
120 return 1;
121}
122
123
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800124void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 unsigned int n, dummy, ecx, edx, l2size;
127
128 n = cpuid_eax(0x80000000);
129
130 if (n >= 0x80000005) {
131 cpuid(0x80000005, &dummy, &dummy, &ecx, &edx);
132 printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
133 edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
134 c->x86_cache_size=(ecx>>24)+(edx>>24);
135 }
136
137 if (n < 0x80000006) /* Some chips just has a large L1. */
138 return;
139
140 ecx = cpuid_ecx(0x80000006);
141 l2size = ecx >> 16;
142
143 /* do processor-specific cache resizing */
144 if (this_cpu->c_size_cache)
145 l2size = this_cpu->c_size_cache(c,l2size);
146
147 /* Allow user to override all this if necessary. */
148 if (cachesize_override != -1)
149 l2size = cachesize_override;
150
151 if ( l2size == 0 )
152 return; /* Again, no L2 cache is possible */
153
154 c->x86_cache_size = l2size;
155
156 printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
157 l2size, ecx & 0xFF);
158}
159
160/* Naming convention should be: <Name> [(<Codename>)] */
161/* This table only is used unless init_<vendor>() below doesn't set it; */
162/* in particular, if CPUID levels 0x80000002..4 are supported, this isn't used */
163
164/* Look up CPU names by table lookup. */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800165static char __cpuinit *table_lookup_model(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct cpu_model_info *info;
168
169 if ( c->x86_model >= 16 )
170 return NULL; /* Range check */
171
172 if (!this_cpu)
173 return NULL;
174
175 info = this_cpu->c_models;
176
177 while (info && info->family) {
178 if (info->family == c->x86)
179 return info->model_names[c->x86_model];
180 info++;
181 }
182 return NULL; /* Not found */
183}
184
185
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800186static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c, int early)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 char *v = c->x86_vendor_id;
189 int i;
Chuck Ebbertfe38d852006-02-04 23:28:03 -0800190 static int printed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 for (i = 0; i < X86_VENDOR_NUM; i++) {
193 if (cpu_devs[i]) {
194 if (!strcmp(v,cpu_devs[i]->c_ident[0]) ||
195 (cpu_devs[i]->c_ident[1] &&
196 !strcmp(v,cpu_devs[i]->c_ident[1]))) {
197 c->x86_vendor = i;
198 if (!early)
199 this_cpu = cpu_devs[i];
Chuck Ebbertfe38d852006-02-04 23:28:03 -0800200 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 }
203 }
Chuck Ebbertfe38d852006-02-04 23:28:03 -0800204 if (!printed) {
205 printed++;
206 printk(KERN_ERR "CPU: Vendor unknown, using generic init.\n");
207 printk(KERN_ERR "CPU: Your system may be unstable.\n");
208 }
209 c->x86_vendor = X86_VENDOR_UNKNOWN;
210 this_cpu = &default_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213
214static int __init x86_fxsr_setup(char * s)
215{
Linus Torvalds8ccb3dc2006-10-03 09:45:46 -0700216 /* Tell all the other CPU's to not use it... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 disable_x86_fxsr = 1;
Linus Torvalds8ccb3dc2006-10-03 09:45:46 -0700218
219 /*
220 * ... and clear the bits early in the boot_cpu_data
221 * so that the bootup process doesn't try to do this
222 * either.
223 */
224 clear_bit(X86_FEATURE_FXSR, boot_cpu_data.x86_capability);
225 clear_bit(X86_FEATURE_XMM, boot_cpu_data.x86_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return 1;
227}
228__setup("nofxsr", x86_fxsr_setup);
229
230
Chuck Ebbert4f886512006-03-23 02:59:34 -0800231static int __init x86_sep_setup(char * s)
232{
233 disable_x86_sep = 1;
234 return 1;
235}
236__setup("nosep", x86_sep_setup);
237
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/* Standard macro to see if a specific flag is changeable */
240static inline int flag_is_changeable_p(u32 flag)
241{
242 u32 f1, f2;
243
244 asm("pushfl\n\t"
245 "pushfl\n\t"
246 "popl %0\n\t"
247 "movl %0,%1\n\t"
248 "xorl %2,%0\n\t"
249 "pushl %0\n\t"
250 "popfl\n\t"
251 "pushfl\n\t"
252 "popl %0\n\t"
253 "popfl\n\t"
254 : "=&r" (f1), "=&r" (f2)
255 : "ir" (flag));
256
257 return ((f1^f2) & flag) != 0;
258}
259
260
261/* Probe for the CPUID instruction */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800262static int __cpuinit have_cpuid_p(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 return flag_is_changeable_p(X86_EFLAGS_ID);
265}
266
Rusty Russelld7cd5612006-12-07 02:14:08 +0100267void __init cpu_detect(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* Get vendor name */
270 cpuid(0x00000000, &c->cpuid_level,
271 (int *)&c->x86_vendor_id[0],
272 (int *)&c->x86_vendor_id[8],
273 (int *)&c->x86_vendor_id[4]);
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 c->x86 = 4;
276 if (c->cpuid_level >= 0x00000001) {
277 u32 junk, tfms, cap0, misc;
278 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
279 c->x86 = (tfms >> 8) & 15;
280 c->x86_model = (tfms >> 4) & 15;
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100281 if (c->x86 == 0xf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 c->x86 += (tfms >> 20) & 0xff;
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100283 if (c->x86 >= 0x6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 c->x86_model += ((tfms >> 16) & 0xF) << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 c->x86_mask = tfms & 15;
286 if (cap0 & (1<<19))
287 c->x86_cache_alignment = ((misc >> 8) & 0xff) * 8;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Rusty Russelld7cd5612006-12-07 02:14:08 +0100291/* Do minimum CPU detection early.
292 Fields really needed: vendor, cpuid_level, family, model, mask, cache alignment.
293 The others are not touched to avoid unwanted side effects.
294
295 WARNING: this function is only called on the BP. Don't add code here
296 that is supposed to run on all CPUs. */
297static void __init early_cpu_detect(void)
298{
299 struct cpuinfo_x86 *c = &boot_cpu_data;
300
301 c->x86_cache_alignment = 32;
302
303 if (!have_cpuid_p())
304 return;
305
306 cpu_detect(c);
307
308 get_cpu_vendor(c, 1);
309}
310
Magnus Damm68bbc172006-09-26 10:52:36 +0200311static void __cpuinit generic_identify(struct cpuinfo_x86 * c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 u32 tfms, xlvl;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800314 int ebx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if (have_cpuid_p()) {
317 /* Get vendor name */
318 cpuid(0x00000000, &c->cpuid_level,
319 (int *)&c->x86_vendor_id[0],
320 (int *)&c->x86_vendor_id[8],
321 (int *)&c->x86_vendor_id[4]);
322
323 get_cpu_vendor(c, 0);
324 /* Initialize the standard set of capabilities */
325 /* Note that the vendor-specific code below might override */
326
327 /* Intel-defined flags: level 0x00000001 */
328 if ( c->cpuid_level >= 0x00000001 ) {
329 u32 capability, excap;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800330 cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 c->x86_capability[0] = capability;
332 c->x86_capability[4] = excap;
333 c->x86 = (tfms >> 8) & 15;
334 c->x86_model = (tfms >> 4) & 15;
Shaohua Lied2da192006-03-07 21:55:40 -0800335 if (c->x86 == 0xf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 c->x86 += (tfms >> 20) & 0xff;
Shaohua Lied2da192006-03-07 21:55:40 -0800337 if (c->x86 >= 0x6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 c->x86_model += ((tfms >> 16) & 0xF) << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 c->x86_mask = tfms & 15;
James Bottomley96c52742006-06-27 02:53:49 -0700340#ifdef CONFIG_X86_HT
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800341 c->apicid = phys_pkg_id((ebx >> 24) & 0xFF, 0);
342#else
343 c->apicid = (ebx >> 24) & 0xFF;
344#endif
Andi Kleen770d1322006-12-07 02:14:05 +0100345 if (c->x86_capability[0] & (1<<19))
346 c->x86_clflush_size = ((ebx >> 8) & 0xff) * 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 } else {
348 /* Have CPUID level 0 only - unheard of */
349 c->x86 = 4;
350 }
351
352 /* AMD-defined flags: level 0x80000001 */
353 xlvl = cpuid_eax(0x80000000);
354 if ( (xlvl & 0xffff0000) == 0x80000000 ) {
355 if ( xlvl >= 0x80000001 ) {
356 c->x86_capability[1] = cpuid_edx(0x80000001);
357 c->x86_capability[6] = cpuid_ecx(0x80000001);
358 }
359 if ( xlvl >= 0x80000004 )
360 get_model_name(c); /* Default name */
361 }
362 }
Andi Kleen2e664aa2006-01-11 22:46:33 +0100363
364 early_intel_workaround(c);
365
366#ifdef CONFIG_X86_HT
Rohit Seth4b89aff2006-06-27 02:53:46 -0700367 c->phys_proc_id = (cpuid_ebx(1) >> 24) & 0xff;
Andi Kleen2e664aa2006-01-11 22:46:33 +0100368#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800371static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr ) {
374 /* Disable processor serial number */
375 unsigned long lo,hi;
376 rdmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
377 lo |= 0x200000;
378 wrmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
379 printk(KERN_NOTICE "CPU serial number disabled.\n");
380 clear_bit(X86_FEATURE_PN, c->x86_capability);
381
382 /* Disabling the serial number may affect the cpuid level */
383 c->cpuid_level = cpuid_eax(0);
384 }
385}
386
387static int __init x86_serial_nr_setup(char *s)
388{
389 disable_x86_serial_nr = 0;
390 return 1;
391}
392__setup("serialnumber", x86_serial_nr_setup);
393
394
395
396/*
397 * This does the hard work of actually picking apart the CPU stuff...
398 */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800399void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 int i;
402
403 c->loops_per_jiffy = loops_per_jiffy;
404 c->x86_cache_size = -1;
405 c->x86_vendor = X86_VENDOR_UNKNOWN;
406 c->cpuid_level = -1; /* CPUID not detected */
407 c->x86_model = c->x86_mask = 0; /* So far unknown... */
408 c->x86_vendor_id[0] = '\0'; /* Unset */
409 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100410 c->x86_max_cores = 1;
Andi Kleen770d1322006-12-07 02:14:05 +0100411 c->x86_clflush_size = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 memset(&c->x86_capability, 0, sizeof c->x86_capability);
413
414 if (!have_cpuid_p()) {
415 /* First of all, decide if this is a 486 or higher */
416 /* It's a 486 if we can modify the AC flag */
417 if ( flag_is_changeable_p(X86_EFLAGS_AC) )
418 c->x86 = 4;
419 else
420 c->x86 = 3;
421 }
422
423 generic_identify(c);
424
425 printk(KERN_DEBUG "CPU: After generic identify, caps:");
426 for (i = 0; i < NCAPINTS; i++)
427 printk(" %08lx", c->x86_capability[i]);
428 printk("\n");
429
430 if (this_cpu->c_identify) {
431 this_cpu->c_identify(c);
432
433 printk(KERN_DEBUG "CPU: After vendor identify, caps:");
434 for (i = 0; i < NCAPINTS; i++)
435 printk(" %08lx", c->x86_capability[i]);
436 printk("\n");
437 }
438
439 /*
440 * Vendor-specific initialization. In this section we
441 * canonicalize the feature flags, meaning if there are
442 * features a certain CPU supports which CPUID doesn't
443 * tell us, CPUID claiming incorrect flags, or other bugs,
444 * we handle them here.
445 *
446 * At the end of this section, c->x86_capability better
447 * indicate the features this CPU genuinely supports!
448 */
449 if (this_cpu->c_init)
450 this_cpu->c_init(c);
451
452 /* Disable the PN if appropriate */
453 squash_the_stupid_serial_number(c);
454
455 /*
456 * The vendor-specific functions might have changed features. Now
457 * we do "generic changes."
458 */
459
460 /* TSC disabled? */
461 if ( tsc_disable )
462 clear_bit(X86_FEATURE_TSC, c->x86_capability);
463
464 /* FXSR disabled? */
465 if (disable_x86_fxsr) {
466 clear_bit(X86_FEATURE_FXSR, c->x86_capability);
467 clear_bit(X86_FEATURE_XMM, c->x86_capability);
468 }
469
Chuck Ebbert4f886512006-03-23 02:59:34 -0800470 /* SEP disabled? */
471 if (disable_x86_sep)
472 clear_bit(X86_FEATURE_SEP, c->x86_capability);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (disable_pse)
475 clear_bit(X86_FEATURE_PSE, c->x86_capability);
476
477 /* If the model name is still unset, do table lookup. */
478 if ( !c->x86_model_id[0] ) {
479 char *p;
480 p = table_lookup_model(c);
481 if ( p )
482 strcpy(c->x86_model_id, p);
483 else
484 /* Last resort... */
485 sprintf(c->x86_model_id, "%02x/%02x",
Chuck Ebbert54a20f82006-03-23 02:59:36 -0800486 c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
489 /* Now the feature flags better reflect actual CPU features! */
490
491 printk(KERN_DEBUG "CPU: After all inits, caps:");
492 for (i = 0; i < NCAPINTS; i++)
493 printk(" %08lx", c->x86_capability[i]);
494 printk("\n");
495
496 /*
497 * On SMP, boot_cpu_data holds the common feature set between
498 * all CPUs; so make sure that we indicate which features are
499 * common between the CPUs. The first time this routine gets
500 * executed, c == &boot_cpu_data.
501 */
502 if ( c != &boot_cpu_data ) {
503 /* AND the already accumulated flags with these */
504 for ( i = 0 ; i < NCAPINTS ; i++ )
505 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
506 }
507
508 /* Init Machine Check Exception if available. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 mcheck_init(c);
Shaohua Li31ab2692005-11-07 00:58:42 -0800510
Li Shaohua6fe940d2005-06-25 14:54:53 -0700511 if (c == &boot_cpu_data)
512 sysenter_setup();
513 enable_sep_cpu();
Shaohua Li3b520b22005-07-07 17:56:38 -0700514
515 if (c == &boot_cpu_data)
516 mtrr_bp_init();
517 else
518 mtrr_ap_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521#ifdef CONFIG_X86_HT
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800522void __cpuinit detect_ht(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 u32 eax, ebx, ecx, edx;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100525 int index_msb, core_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100527 cpuid(1, &eax, &ebx, &ecx, &edx);
528
Andi Kleen63518642005-04-16 15:25:16 -0700529 if (!cpu_has(c, X86_FEATURE_HT) || cpu_has(c, X86_FEATURE_CMP_LEGACY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 smp_num_siblings = (ebx & 0xff0000) >> 16;
533
534 if (smp_num_siblings == 1) {
535 printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
536 } else if (smp_num_siblings > 1 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (smp_num_siblings > NR_CPUS) {
Rohit Seth4b89aff2006-06-27 02:53:46 -0700539 printk(KERN_WARNING "CPU: Unsupported number of the "
540 "siblings %d", smp_num_siblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 smp_num_siblings = 1;
542 return;
543 }
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100544
545 index_msb = get_count_order(smp_num_siblings);
Rohit Seth4b89aff2006-06-27 02:53:46 -0700546 c->phys_proc_id = phys_pkg_id((ebx >> 24) & 0xFF, index_msb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
Rohit Seth4b89aff2006-06-27 02:53:46 -0700549 c->phys_proc_id);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700550
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100551 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
Andi Kleen3dd9d512005-04-16 15:25:15 -0700552
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100553 index_msb = get_count_order(smp_num_siblings) ;
Andi Kleen3dd9d512005-04-16 15:25:15 -0700554
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100555 core_bits = get_count_order(c->x86_max_cores);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700556
Rohit Seth4b89aff2006-06-27 02:53:46 -0700557 c->cpu_core_id = phys_pkg_id((ebx >> 24) & 0xFF, index_msb) &
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100558 ((1 << core_bits) - 1);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700559
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100560 if (c->x86_max_cores > 1)
Andi Kleen3dd9d512005-04-16 15:25:15 -0700561 printk(KERN_INFO "CPU: Processor Core ID: %d\n",
Rohit Seth4b89aff2006-06-27 02:53:46 -0700562 c->cpu_core_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564}
565#endif
566
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800567void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 char *vendor = NULL;
570
571 if (c->x86_vendor < X86_VENDOR_NUM)
572 vendor = this_cpu->c_vendor;
573 else if (c->cpuid_level >= 0)
574 vendor = c->x86_vendor_id;
575
576 if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
577 printk("%s ", vendor);
578
579 if (!c->x86_model_id[0])
580 printk("%d86", c->x86);
581 else
582 printk("%s", c->x86_model_id);
583
584 if (c->x86_mask || c->cpuid_level >= 0)
585 printk(" stepping %02x\n", c->x86_mask);
586 else
587 printk("\n");
588}
589
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800590cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592/* This is hacky. :)
593 * We're emulating future behavior.
594 * In the future, the cpu-specific init functions will be called implicitly
595 * via the magic of initcalls.
596 * They will insert themselves into the cpu_devs structure.
597 * Then, when cpu_init() is called, we can just iterate over that array.
598 */
599
600extern int intel_cpu_init(void);
601extern int cyrix_init_cpu(void);
602extern int nsc_init_cpu(void);
603extern int amd_init_cpu(void);
604extern int centaur_init_cpu(void);
605extern int transmeta_init_cpu(void);
606extern int rise_init_cpu(void);
607extern int nexgen_init_cpu(void);
608extern int umc_init_cpu(void);
609
610void __init early_cpu_init(void)
611{
612 intel_cpu_init();
613 cyrix_init_cpu();
614 nsc_init_cpu();
615 amd_init_cpu();
616 centaur_init_cpu();
617 transmeta_init_cpu();
618 rise_init_cpu();
619 nexgen_init_cpu();
620 umc_init_cpu();
621 early_cpu_detect();
622
623#ifdef CONFIG_DEBUG_PAGEALLOC
624 /* pse is not compatible with on-the-fly unmapping,
625 * disable it even if the cpus claim to support it.
626 */
627 clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
628 disable_pse = 1;
629#endif
630}
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100631
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100632/* Make sure %gs is initialized properly in idle threads */
633struct pt_regs * __devinit idle_regs(struct pt_regs *regs)
634{
635 memset(regs, 0, sizeof(struct pt_regs));
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100636 regs->xfs = __KERNEL_PDA;
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100637 return regs;
638}
639
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100640/* Initial PDA used by boot CPU */
641struct i386_pda boot_pda = {
642 ._pda = &boot_pda,
Jeremy Fitzhardingeb2938f82006-12-07 02:14:03 +0100643 .cpu_number = 0,
Jeremy Fitzhardingeec7fcaa2006-12-07 02:14:03 +0100644 .pcurrent = &init_task,
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100645};
646
Rusty Russelld2cbcc42007-05-02 19:27:10 +0200647/*
648 * cpu_init() initializes state that is per-CPU. Some data is already
649 * initialized (naturally) in the bootstrap process, such as the GDT
650 * and IDT. We reload them nevertheless, this function acts as a
651 * 'CPU state barrier', nothing should get across.
652 */
653void __cpuinit cpu_init(void)
James Bottomley9ee79a32007-01-22 09:18:31 -0600654{
Rusty Russelld2cbcc42007-05-02 19:27:10 +0200655 int cpu = smp_processor_id();
656 struct task_struct *curr = current;
James Bottomley9ee79a32007-01-22 09:18:31 -0600657 struct tss_struct * t = &per_cpu(init_tss, cpu);
658 struct thread_struct *thread = &curr->thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (cpu_test_and_set(cpu, cpu_initialized)) {
661 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
662 for (;;) local_irq_enable();
663 }
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 printk(KERN_INFO "Initializing CPU#%d\n", cpu);
666
667 if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
668 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
669 if (tsc_disable && cpu_has_tsc) {
670 printk(KERN_NOTICE "Disabling TSC...\n");
671 /**** FIX-HPA: DOES THIS REALLY BELONG HERE? ****/
672 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
673 set_in_cr4(X86_CR4_TSD);
674 }
675
Zachary Amsden4d37e7e2005-09-03 15:56:38 -0700676 load_idt(&idt_descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 * Set up and load the per-CPU TSS and LDT
680 */
681 atomic_inc(&init_mm.mm_count);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100682 curr->active_mm = &init_mm;
683 if (curr->mm)
684 BUG();
685 enter_lazy_tlb(&init_mm, curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 load_esp0(t, thread);
688 set_tss_desc(cpu,t);
689 load_TR_desc();
690 load_LDT(&init_mm.context);
691
Matt Mackall22c4e302006-01-08 01:05:24 -0800692#ifdef CONFIG_DOUBLEFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* Set up doublefault TSS pointer in the GDT */
694 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
Matt Mackall22c4e302006-01-08 01:05:24 -0800695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100697 /* Clear %gs. */
698 asm volatile ("mov %0, %%gs" : : "r" (0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /* Clear all 6 debug registers: */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700701 set_debugreg(0, 0);
702 set_debugreg(0, 1);
703 set_debugreg(0, 2);
704 set_debugreg(0, 3);
705 set_debugreg(0, 6);
706 set_debugreg(0, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /*
709 * Force FPU initialization:
710 */
711 current_thread_info()->status = 0;
712 clear_used_math();
713 mxcsr_feature_mask_init();
714}
Li Shaohuae1367da2005-06-25 14:54:56 -0700715
716#ifdef CONFIG_HOTPLUG_CPU
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800717void __cpuinit cpu_uninit(void)
Li Shaohuae1367da2005-06-25 14:54:56 -0700718{
719 int cpu = raw_smp_processor_id();
720 cpu_clear(cpu, cpu_initialized);
721
722 /* lazy TLB state */
723 per_cpu(cpu_tlbstate, cpu).state = 0;
724 per_cpu(cpu_tlbstate, cpu).active_mm = &init_mm;
725}
726#endif