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