blob: f7619a2eaffe86f708f4dca58fd27f7d0cf6a297 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/init.h>
Yinghai Luf0fc4af2008-09-04 20:09:00 -07002#include <linux/kernel.h>
3#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/string.h>
Yinghai Luf0fc4af2008-09-04 20:09:00 -07005#include <linux/bootmem.h>
6#include <linux/bitops.h>
7#include <linux/module.h>
8#include <linux/kgdb.h>
9#include <linux/topology.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/delay.h>
11#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/i387.h>
14#include <asm/msr.h>
15#include <asm/io.h>
Yinghai Luf0fc4af2008-09-04 20:09:00 -070016#include <asm/linkage.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/mmu_context.h>
Alexey Dobriyan27b07da2006-06-23 02:04:18 -070018#include <asm/mtrr.h>
Alexey Dobriyana03a3e22006-06-23 02:04:20 -070019#include <asm/mce.h>
Thomas Gleixner8d4a4302008-05-08 09:18:43 +020020#include <asm/pat.h>
H. Peter Anvinb6734c32008-08-18 17:39:32 -070021#include <asm/asm.h>
Yinghai Luf0fc4af2008-09-04 20:09:00 -070022#include <asm/numa.h>
Ingo Molnarb3427972008-10-31 09:31:38 +010023#include <asm/smp.h>
Jaswinder Singh Rajputf472cdb2009-01-07 21:34:25 +053024#include <asm/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#ifdef CONFIG_X86_LOCAL_APIC
26#include <asm/mpspec.h>
27#include <asm/apic.h>
28#include <mach_apic.h>
Yinghai Luf0fc4af2008-09-04 20:09:00 -070029#include <asm/genapic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#endif
31
Yinghai Luf0fc4af2008-09-04 20:09:00 -070032#include <asm/pda.h>
33#include <asm/pgtable.h>
34#include <asm/processor.h>
35#include <asm/desc.h>
36#include <asm/atomic.h>
37#include <asm/proto.h>
38#include <asm/sections.h>
39#include <asm/setup.h>
Alok Kataria88b094f2008-10-27 10:41:46 -070040#include <asm/hypervisor.h>
Yinghai Luf0fc4af2008-09-04 20:09:00 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "cpu.h"
43
Yinghai Lu0a488a52008-09-04 21:09:47 +020044static struct cpu_dev *this_cpu __cpuinitdata;
45
Yinghai Lu950ad7f2008-09-04 20:09:01 -070046#ifdef CONFIG_X86_64
47/* We need valid kernel segments for data and code in long mode too
48 * IRET will check the segment types kkeil 2000/10/28
49 * Also sysret mandates a special GDT layout
50 */
51/* The TLS descriptors are currently at a different place compared to i386.
52 Hopefully nobody expects them at a fixed place (Wine?) */
Jeremy Fitzhardinge7a61d352007-05-02 19:27:15 +020053DEFINE_PER_CPU(struct gdt_page, gdt_page) = { .gdt = {
Yinghai Lu950ad7f2008-09-04 20:09:01 -070054 [GDT_ENTRY_KERNEL32_CS] = { { { 0x0000ffff, 0x00cf9b00 } } },
55 [GDT_ENTRY_KERNEL_CS] = { { { 0x0000ffff, 0x00af9b00 } } },
56 [GDT_ENTRY_KERNEL_DS] = { { { 0x0000ffff, 0x00cf9300 } } },
57 [GDT_ENTRY_DEFAULT_USER32_CS] = { { { 0x0000ffff, 0x00cffb00 } } },
58 [GDT_ENTRY_DEFAULT_USER_DS] = { { { 0x0000ffff, 0x00cff300 } } },
59 [GDT_ENTRY_DEFAULT_USER_CS] = { { { 0x0000ffff, 0x00affb00 } } },
60} };
61#else
Eric Dumazet63cc8c72008-05-12 15:44:40 +020062DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +010063 [GDT_ENTRY_KERNEL_CS] = { { { 0x0000ffff, 0x00cf9a00 } } },
64 [GDT_ENTRY_KERNEL_DS] = { { { 0x0000ffff, 0x00cf9200 } } },
65 [GDT_ENTRY_DEFAULT_USER_CS] = { { { 0x0000ffff, 0x00cffa00 } } },
66 [GDT_ENTRY_DEFAULT_USER_DS] = { { { 0x0000ffff, 0x00cff200 } } },
Rusty Russellbf5046722007-05-02 19:27:10 +020067 /*
68 * Segments used for calling PnP BIOS have byte granularity.
69 * They code segments and data segments have fixed 64k limits,
70 * the transfer segment sizes are set at run time.
71 */
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +010072 /* 32-bit code */
73 [GDT_ENTRY_PNPBIOS_CS32] = { { { 0x0000ffff, 0x00409a00 } } },
74 /* 16-bit code */
75 [GDT_ENTRY_PNPBIOS_CS16] = { { { 0x0000ffff, 0x00009a00 } } },
76 /* 16-bit data */
77 [GDT_ENTRY_PNPBIOS_DS] = { { { 0x0000ffff, 0x00009200 } } },
78 /* 16-bit data */
79 [GDT_ENTRY_PNPBIOS_TS1] = { { { 0x00000000, 0x00009200 } } },
80 /* 16-bit data */
81 [GDT_ENTRY_PNPBIOS_TS2] = { { { 0x00000000, 0x00009200 } } },
Rusty Russellbf5046722007-05-02 19:27:10 +020082 /*
83 * The APM segments have byte granularity and their bases
84 * are set at run time. All have 64k limits.
85 */
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +010086 /* 32-bit code */
87 [GDT_ENTRY_APMBIOS_BASE] = { { { 0x0000ffff, 0x00409a00 } } },
Rusty Russellbf5046722007-05-02 19:27:10 +020088 /* 16-bit code */
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +010089 [GDT_ENTRY_APMBIOS_BASE+1] = { { { 0x0000ffff, 0x00009a00 } } },
90 /* data */
91 [GDT_ENTRY_APMBIOS_BASE+2] = { { { 0x0000ffff, 0x00409200 } } },
Rusty Russellbf5046722007-05-02 19:27:10 +020092
Glauber de Oliveira Costa6842ef02008-01-30 13:31:11 +010093 [GDT_ENTRY_ESPFIX_SS] = { { { 0x00000000, 0x00c09200 } } },
94 [GDT_ENTRY_PERCPU] = { { { 0x00000000, 0x00000000 } } },
Jeremy Fitzhardinge7a61d352007-05-02 19:27:15 +020095} };
Yinghai Lu950ad7f2008-09-04 20:09:01 -070096#endif
Jeremy Fitzhardinge7a61d352007-05-02 19:27:15 +020097EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
Rusty Russellae1ee112007-05-02 19:27:10 +020098
Yinghai Luba51dce2008-09-04 20:09:02 -070099#ifdef CONFIG_X86_32
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800100static int cachesize_override __cpuinitdata = -1;
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800101static int disable_x86_serial_nr __cpuinitdata = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int __init cachesize_setup(char *str)
104{
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100105 get_option(&str, &cachesize_override);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return 1;
107}
108__setup("cachesize=", cachesize_setup);
109
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100110static int __init x86_fxsr_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Andi Kleen13530252008-01-30 13:33:20 +0100112 setup_clear_cpu_cap(X86_FEATURE_FXSR);
113 setup_clear_cpu_cap(X86_FEATURE_XMM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 1;
115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116__setup("nofxsr", x86_fxsr_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100118static int __init x86_sep_setup(char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Andi Kleen13530252008-01-30 13:33:20 +0100120 setup_clear_cpu_cap(X86_FEATURE_SEP);
Chuck Ebbert4f886512006-03-23 02:59:34 -0800121 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
Chuck Ebbert4f886512006-03-23 02:59:34 -0800123__setup("nosep", x86_sep_setup);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* Standard macro to see if a specific flag is changeable */
126static inline int flag_is_changeable_p(u32 flag)
127{
128 u32 f1, f2;
129
Krzysztof Helt94f6bac2008-09-30 23:17:51 +0200130 /*
131 * Cyrix and IDT cpus allow disabling of CPUID
132 * so the code below may return different results
133 * when it is executed before and after enabling
134 * the CPUID. Add "volatile" to not allow gcc to
135 * optimize the subsequent calls to this function.
136 */
137 asm volatile ("pushfl\n\t"
138 "pushfl\n\t"
139 "popl %0\n\t"
140 "movl %0,%1\n\t"
141 "xorl %2,%0\n\t"
142 "pushl %0\n\t"
143 "popfl\n\t"
144 "pushfl\n\t"
145 "popl %0\n\t"
146 "popfl\n\t"
147 : "=&r" (f1), "=&r" (f2)
148 : "ir" (flag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return ((f1^f2) & flag) != 0;
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* Probe for the CPUID instruction */
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800154static int __cpuinit have_cpuid_p(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 return flag_is_changeable_p(X86_EFLAGS_ID);
157}
158
Yinghai Lu0a488a52008-09-04 21:09:47 +0200159static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
160{
161 if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr) {
162 /* Disable processor serial number */
163 unsigned long lo, hi;
164 rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
165 lo |= 0x200000;
166 wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
167 printk(KERN_NOTICE "CPU serial number disabled.\n");
168 clear_cpu_cap(c, X86_FEATURE_PN);
169
170 /* Disabling the serial number may affect the cpuid level */
171 c->cpuid_level = cpuid_eax(0);
172 }
173}
174
175static int __init x86_serial_nr_setup(char *s)
176{
177 disable_x86_serial_nr = 0;
178 return 1;
179}
180__setup("serialnumber", x86_serial_nr_setup);
Yinghai Luba51dce2008-09-04 20:09:02 -0700181#else
Yinghai Lu102bbe32008-09-04 20:09:13 -0700182static inline int flag_is_changeable_p(u32 flag)
183{
184 return 1;
185}
Yinghai Luba51dce2008-09-04 20:09:02 -0700186/* Probe for the CPUID instruction */
187static inline int have_cpuid_p(void)
188{
189 return 1;
190}
Yinghai Lu102bbe32008-09-04 20:09:13 -0700191static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
192{
193}
Yinghai Luba51dce2008-09-04 20:09:02 -0700194#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/*
197 * Naming convention should be: <Name> [(<Codename>)]
198 * This table only is used unless init_<vendor>() below doesn't set it;
199 * in particular, if CPUID levels 0x80000002..4 are supported, this isn't used
200 *
201 */
202
203/* Look up CPU names by table lookup. */
204static char __cpuinit *table_lookup_model(struct cpuinfo_x86 *c)
205{
206 struct cpu_model_info *info;
207
208 if (c->x86_model >= 16)
209 return NULL; /* Range check */
210
211 if (!this_cpu)
212 return NULL;
213
214 info = this_cpu->c_models;
215
216 while (info && info->family) {
217 if (info->family == c->x86)
218 return info->model_names[c->x86_model];
219 info++;
220 }
221 return NULL; /* Not found */
222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224__u32 cleared_cpu_caps[NCAPINTS] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Yinghai Lu9d31d352008-09-04 21:09:44 +0200226/* Current gdt points %fs at the "master" per-cpu area: after this,
227 * it's on the real one. */
228void switch_to_new_gdt(void)
229{
230 struct desc_ptr gdt_descr;
231
232 gdt_descr.address = (long)get_cpu_gdt_table(smp_processor_id());
233 gdt_descr.size = GDT_SIZE - 1;
234 load_gdt(&gdt_descr);
Yinghai Lufab334c2008-09-04 20:09:05 -0700235#ifdef CONFIG_X86_32
Yinghai Lu9d31d352008-09-04 21:09:44 +0200236 asm("mov %0, %%fs" : : "r" (__KERNEL_PERCPU) : "memory");
Yinghai Lufab334c2008-09-04 20:09:05 -0700237#endif
Yinghai Lu9d31d352008-09-04 21:09:44 +0200238}
239
Yinghai Lu10a434f2008-09-04 21:09:45 +0200240static struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242static void __cpuinit default_init(struct cpuinfo_x86 *c)
243{
Yinghai Lub9e67f02008-09-04 20:09:06 -0700244#ifdef CONFIG_X86_64
245 display_cacheinfo(c);
246#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Not much we can do here... */
248 /* Check if at least it has cpuid */
249 if (c->cpuid_level == -1) {
250 /* No cpuid. It must be an ancient CPU */
251 if (c->x86 == 4)
252 strcpy(c->x86_model_id, "486");
253 else if (c->x86 == 3)
254 strcpy(c->x86_model_id, "386");
255 }
Yinghai Lub9e67f02008-09-04 20:09:06 -0700256#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259static struct cpu_dev __cpuinitdata default_cpu = {
260 .c_init = default_init,
261 .c_vendor = "Unknown",
Yinghai Lu10a434f2008-09-04 21:09:45 +0200262 .c_x86_vendor = X86_VENDOR_UNKNOWN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Yinghai Lu1b05d602008-09-06 01:52:27 -0700265static void __cpuinit get_model_name(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 unsigned int *v;
268 char *p, *q;
269
Yinghai Lu3da99c92008-09-04 21:09:44 +0200270 if (c->extended_cpuid_level < 0x80000004)
Yinghai Lu1b05d602008-09-06 01:52:27 -0700271 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 v = (unsigned int *) c->x86_model_id;
274 cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
275 cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
276 cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
277 c->x86_model_id[48] = 0;
278
279 /* Intel chips right-justify this string for some dumb reason;
280 undo that brain damage */
281 p = q = &c->x86_model_id[0];
282 while (*p == ' ')
283 p++;
284 if (p != q) {
285 while (*p)
286 *q++ = *p++;
287 while (q <= &c->x86_model_id[48])
288 *q++ = '\0'; /* Zero-pad the rest */
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
293{
Yinghai Lu9d31d352008-09-04 21:09:44 +0200294 unsigned int n, dummy, ebx, ecx, edx, l2size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Yinghai Lu3da99c92008-09-04 21:09:44 +0200296 n = c->extended_cpuid_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (n >= 0x80000005) {
Yinghai Lu9d31d352008-09-04 21:09:44 +0200299 cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
Yinghai Lu9d31d352008-09-04 21:09:44 +0200301 edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
302 c->x86_cache_size = (ecx>>24) + (edx>>24);
Yinghai Lu140fc722008-09-04 20:09:07 -0700303#ifdef CONFIG_X86_64
304 /* On K8 L1 TLB is inclusive, so don't count it */
305 c->x86_tlbsize = 0;
306#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
309 if (n < 0x80000006) /* Some chips just has a large L1. */
310 return;
311
Yinghai Lu0a488a52008-09-04 21:09:47 +0200312 cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 l2size = ecx >> 16;
314
Yinghai Lu140fc722008-09-04 20:09:07 -0700315#ifdef CONFIG_X86_64
316 c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
317#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* do processor-specific cache resizing */
319 if (this_cpu->c_size_cache)
320 l2size = this_cpu->c_size_cache(c, l2size);
321
322 /* Allow user to override all this if necessary. */
323 if (cachesize_override != -1)
324 l2size = cachesize_override;
325
326 if (l2size == 0)
327 return; /* Again, no L2 cache is possible */
Yinghai Lu140fc722008-09-04 20:09:07 -0700328#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 c->x86_cache_size = l2size;
331
332 printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
Yinghai Lu0a488a52008-09-04 21:09:47 +0200333 l2size, ecx & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Yinghai Lu9d31d352008-09-04 21:09:44 +0200336void __cpuinit detect_ht(struct cpuinfo_x86 *c)
337{
Yinghai Lu97e4db72008-09-04 20:08:59 -0700338#ifdef CONFIG_X86_HT
Yinghai Lu0a488a52008-09-04 21:09:47 +0200339 u32 eax, ebx, ecx, edx;
340 int index_msb, core_bits;
341
342 if (!cpu_has(c, X86_FEATURE_HT))
343 return;
344
345 if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
346 goto out;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200347
Yinghai Lu1cd78772008-09-04 20:09:08 -0700348 if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
349 return;
350
Yinghai Lu9d31d352008-09-04 21:09:44 +0200351 cpuid(1, &eax, &ebx, &ecx, &edx);
352
Yinghai Lu9d31d352008-09-04 21:09:44 +0200353 smp_num_siblings = (ebx & 0xff0000) >> 16;
354
355 if (smp_num_siblings == 1) {
356 printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
357 } else if (smp_num_siblings > 1) {
358
Mike Travis96289372008-12-31 18:08:46 -0800359 if (smp_num_siblings > nr_cpu_ids) {
Yinghai Lu9d31d352008-09-04 21:09:44 +0200360 printk(KERN_WARNING "CPU: Unsupported number of siblings %d",
361 smp_num_siblings);
362 smp_num_siblings = 1;
363 return;
364 }
365
366 index_msb = get_count_order(smp_num_siblings);
Yinghai Lu1cd78772008-09-04 20:09:08 -0700367#ifdef CONFIG_X86_64
368 c->phys_proc_id = phys_pkg_id(index_msb);
369#else
Yinghai Lu9d31d352008-09-04 21:09:44 +0200370 c->phys_proc_id = phys_pkg_id(c->initial_apicid, index_msb);
Yinghai Lu1cd78772008-09-04 20:09:08 -0700371#endif
Yinghai Lu9d31d352008-09-04 21:09:44 +0200372
373 smp_num_siblings = smp_num_siblings / c->x86_max_cores;
374
375 index_msb = get_count_order(smp_num_siblings);
376
377 core_bits = get_count_order(c->x86_max_cores);
378
Yinghai Lu1cd78772008-09-04 20:09:08 -0700379#ifdef CONFIG_X86_64
380 c->cpu_core_id = phys_pkg_id(index_msb) &
381 ((1 << core_bits) - 1);
382#else
Yinghai Lu9d31d352008-09-04 21:09:44 +0200383 c->cpu_core_id = phys_pkg_id(c->initial_apicid, index_msb) &
384 ((1 << core_bits) - 1);
Yinghai Lu1cd78772008-09-04 20:09:08 -0700385#endif
Yinghai Lu0a488a52008-09-04 21:09:47 +0200386 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200387
Yinghai Lu0a488a52008-09-04 21:09:47 +0200388out:
389 if ((c->x86_max_cores * smp_num_siblings) > 1) {
390 printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
391 c->phys_proc_id);
392 printk(KERN_INFO "CPU: Processor Core ID: %d\n",
393 c->cpu_core_id);
Yinghai Lu9d31d352008-09-04 21:09:44 +0200394 }
Yinghai Lu9d31d352008-09-04 21:09:44 +0200395#endif
Yinghai Lu97e4db72008-09-04 20:08:59 -0700396}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Yinghai Lu3da99c92008-09-04 21:09:44 +0200398static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 char *v = c->x86_vendor_id;
401 int i;
402 static int printed;
403
404 for (i = 0; i < X86_VENDOR_NUM; i++) {
Yinghai Lu10a434f2008-09-04 21:09:45 +0200405 if (!cpu_devs[i])
406 break;
407
408 if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
409 (cpu_devs[i]->c_ident[1] &&
410 !strcmp(v, cpu_devs[i]->c_ident[1]))) {
411 this_cpu = cpu_devs[i];
412 c->x86_vendor = this_cpu->c_x86_vendor;
413 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 }
Yinghai Lu10a434f2008-09-04 21:09:45 +0200416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!printed) {
418 printed++;
Hans Schou43603c82008-10-09 20:47:24 +0200419 printk(KERN_ERR "CPU: vendor_id '%s' unknown, using generic init.\n", v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 printk(KERN_ERR "CPU: Your system may be unstable.\n");
421 }
Yinghai Lu10a434f2008-09-04 21:09:45 +0200422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 c->x86_vendor = X86_VENDOR_UNKNOWN;
424 this_cpu = &default_cpu;
425}
426
Yinghai Lu9d31d352008-09-04 21:09:44 +0200427void __cpuinit cpu_detect(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* Get vendor name */
Harvey Harrison4a148512008-02-01 17:49:43 +0100430 cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
431 (unsigned int *)&c->x86_vendor_id[0],
432 (unsigned int *)&c->x86_vendor_id[8],
433 (unsigned int *)&c->x86_vendor_id[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 c->x86 = 4;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200436 /* Intel-defined flags: level 0x00000001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (c->cpuid_level >= 0x00000001) {
438 u32 junk, tfms, cap0, misc;
439 cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
Yinghai Lu9d31d352008-09-04 21:09:44 +0200440 c->x86 = (tfms >> 8) & 0xf;
441 c->x86_model = (tfms >> 4) & 0xf;
442 c->x86_mask = tfms & 0xf;
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100443 if (c->x86 == 0xf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 c->x86 += (tfms >> 20) & 0xff;
Suresh Siddhaf5f786d2005-11-05 17:25:53 +0100445 if (c->x86 >= 0x6)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200446 c->x86_model += ((tfms >> 16) & 0xf) << 4;
Huang, Yingd4387bd2008-01-31 22:05:45 +0100447 if (cap0 & (1<<19)) {
Huang, Yingd4387bd2008-01-31 22:05:45 +0100448 c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200449 c->x86_cache_alignment = c->x86_clflush_size;
Huang, Yingd4387bd2008-01-31 22:05:45 +0100450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
Yinghai Lu3da99c92008-09-04 21:09:44 +0200453
454static void __cpuinit get_cpu_cap(struct cpuinfo_x86 *c)
Yinghai Lu093af8d2008-01-30 13:33:32 +0100455{
456 u32 tfms, xlvl;
Yinghai Lu3da99c92008-09-04 21:09:44 +0200457 u32 ebx;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100458
Yinghai Lu3da99c92008-09-04 21:09:44 +0200459 /* Intel-defined flags: level 0x00000001 */
460 if (c->cpuid_level >= 0x00000001) {
461 u32 capability, excap;
462 cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
463 c->x86_capability[0] = capability;
464 c->x86_capability[4] = excap;
Yinghai Lu093af8d2008-01-30 13:33:32 +0100465 }
466
Yinghai Lu3da99c92008-09-04 21:09:44 +0200467 /* AMD-defined flags: level 0x80000001 */
468 xlvl = cpuid_eax(0x80000000);
469 c->extended_cpuid_level = xlvl;
470 if ((xlvl & 0xffff0000) == 0x80000000) {
471 if (xlvl >= 0x80000001) {
472 c->x86_capability[1] = cpuid_edx(0x80000001);
473 c->x86_capability[6] = cpuid_ecx(0x80000001);
474 }
475 }
Yinghai Lu5122c892008-09-04 20:09:09 -0700476
477#ifdef CONFIG_X86_64
Yinghai Lu5122c892008-09-04 20:09:09 -0700478 if (c->extended_cpuid_level >= 0x80000008) {
479 u32 eax = cpuid_eax(0x80000008);
480
481 c->x86_virt_bits = (eax >> 8) & 0xff;
482 c->x86_phys_bits = eax & 0xff;
483 }
484#endif
Yinghai Lue3224232008-09-06 01:52:28 -0700485
486 if (c->extended_cpuid_level >= 0x80000007)
487 c->x86_power = cpuid_edx(0x80000007);
488
Yinghai Lu093af8d2008-01-30 13:33:32 +0100489}
Yinghai Luaef93c82008-09-14 02:33:15 -0700490
491static void __cpuinit identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
492{
493#ifdef CONFIG_X86_32
494 int i;
495
496 /*
497 * First of all, decide if this is a 486 or higher
498 * It's a 486 if we can modify the AC flag
499 */
500 if (flag_is_changeable_p(X86_EFLAGS_AC))
501 c->x86 = 4;
502 else
503 c->x86 = 3;
504
505 for (i = 0; i < X86_VENDOR_NUM; i++)
506 if (cpu_devs[i] && cpu_devs[i]->c_identify) {
507 c->x86_vendor_id[0] = 0;
508 cpu_devs[i]->c_identify(c);
509 if (c->x86_vendor_id[0]) {
510 get_cpu_vendor(c);
511 break;
512 }
513 }
514#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100517/*
518 * Do minimum CPU detection early.
519 * Fields really needed: vendor, cpuid_level, family, model, mask,
520 * cache alignment.
521 * The others are not touched to avoid unwanted side effects.
522 *
523 * WARNING: this function is only called on the BP. Don't add code here
524 * that is supposed to run on all CPUs.
525 */
Yinghai Lu3da99c92008-09-04 21:09:44 +0200526static void __init early_identify_cpu(struct cpuinfo_x86 *c)
Rusty Russelld7cd5612006-12-07 02:14:08 +0100527{
Yinghai Lu6627d242008-09-04 20:09:10 -0700528#ifdef CONFIG_X86_64
529 c->x86_clflush_size = 64;
530#else
Huang, Yingd4387bd2008-01-31 22:05:45 +0100531 c->x86_clflush_size = 32;
Yinghai Lu6627d242008-09-04 20:09:10 -0700532#endif
Yinghai Lu0a488a52008-09-04 21:09:47 +0200533 c->x86_cache_alignment = c->x86_clflush_size;
Rusty Russelld7cd5612006-12-07 02:14:08 +0100534
Yinghai Lu3da99c92008-09-04 21:09:44 +0200535 memset(&c->x86_capability, 0, sizeof c->x86_capability);
Yinghai Lu0a488a52008-09-04 21:09:47 +0200536 c->extended_cpuid_level = 0;
537
Yinghai Luaef93c82008-09-14 02:33:15 -0700538 if (!have_cpuid_p())
539 identify_cpu_without_cpuid(c);
540
541 /* cyrix could have cpuid enabled via c_identify()*/
Rusty Russelld7cd5612006-12-07 02:14:08 +0100542 if (!have_cpuid_p())
543 return;
544
545 cpu_detect(c);
546
Yinghai Lu3da99c92008-09-04 21:09:44 +0200547 get_cpu_vendor(c);
Andi Kleen2b16a232008-01-30 13:32:40 +0100548
Yinghai Lu3da99c92008-09-04 21:09:44 +0200549 get_cpu_cap(c);
Krzysztof Helt12cf1052008-09-04 21:09:43 +0200550
Yinghai Lu10a434f2008-09-04 21:09:45 +0200551 if (this_cpu->c_early_init)
552 this_cpu->c_early_init(c);
Yinghai Lu3da99c92008-09-04 21:09:44 +0200553
554 validate_pat_support(c);
James Bottomleybfcb4c12008-10-30 16:13:37 -0500555
Ingo Molnar1c4acdb2008-10-31 00:43:03 +0100556#ifdef CONFIG_SMP
James Bottomleybfcb4c12008-10-30 16:13:37 -0500557 c->cpu_index = boot_cpu_id;
Ingo Molnar1c4acdb2008-10-31 00:43:03 +0100558#endif
Rusty Russelld7cd5612006-12-07 02:14:08 +0100559}
560
Yinghai Lu9d31d352008-09-04 21:09:44 +0200561void __init early_cpu_init(void)
562{
Yinghai Lu10a434f2008-09-04 21:09:45 +0200563 struct cpu_dev **cdev;
564 int count = 0;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200565
Yinghai Lu10a434f2008-09-04 21:09:45 +0200566 printk("KERNEL supported cpus:\n");
567 for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
568 struct cpu_dev *cpudev = *cdev;
569 unsigned int j;
Yinghai Lu9d31d352008-09-04 21:09:44 +0200570
Yinghai Lu10a434f2008-09-04 21:09:45 +0200571 if (count >= X86_VENDOR_NUM)
572 break;
573 cpu_devs[count] = cpudev;
574 count++;
575
576 for (j = 0; j < 2; j++) {
577 if (!cpudev->c_ident[j])
578 continue;
579 printk(" %s %s\n", cpudev->c_vendor,
580 cpudev->c_ident[j]);
581 }
582 }
583
Yinghai Lu9d31d352008-09-04 21:09:44 +0200584 early_identify_cpu(&boot_cpu_data);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800585}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700587/*
588 * The NOPL instruction is supposed to exist on all CPUs with
H. Peter Anvinba0593b2008-09-16 09:29:40 -0700589 * family >= 6; unfortunately, that's not true in practice because
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700590 * of early VIA chips and (more importantly) broken virtualizers that
H. Peter Anvinba0593b2008-09-16 09:29:40 -0700591 * are not easy to detect. In the latter case it doesn't even *fail*
592 * reliably, so probing for it doesn't even work. Disable it completely
593 * unless we can find a reliable way to detect all the broken cases.
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700594 */
595static void __cpuinit detect_nopl(struct cpuinfo_x86 *c)
596{
H. Peter Anvinb6734c32008-08-18 17:39:32 -0700597 clear_cpu_cap(c, X86_FEATURE_NOPL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100600static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Yinghai Lu3da99c92008-09-04 21:09:44 +0200602 c->extended_cpuid_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Yinghai Luaef93c82008-09-14 02:33:15 -0700604 if (!have_cpuid_p())
605 identify_cpu_without_cpuid(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100606
Yinghai Luaef93c82008-09-14 02:33:15 -0700607 /* cyrix could have cpuid enabled via c_identify()*/
Ingo Molnara9853dd2008-09-14 14:46:58 +0200608 if (!have_cpuid_p())
Yinghai Luaef93c82008-09-14 02:33:15 -0700609 return;
610
Yinghai Lu3da99c92008-09-04 21:09:44 +0200611 cpu_detect(c);
612
613 get_cpu_vendor(c);
614
615 get_cpu_cap(c);
616
617 if (c->cpuid_level >= 0x00000001) {
618 c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
Yinghai Lub89d3b32008-09-04 20:09:12 -0700619#ifdef CONFIG_X86_32
620# ifdef CONFIG_X86_HT
Yinghai Lu3da99c92008-09-04 21:09:44 +0200621 c->apicid = phys_pkg_id(c->initial_apicid, 0);
Yinghai Lub89d3b32008-09-04 20:09:12 -0700622# else
Yinghai Lu3da99c92008-09-04 21:09:44 +0200623 c->apicid = c->initial_apicid;
Yinghai Lub89d3b32008-09-04 20:09:12 -0700624# endif
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800625#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Yinghai Lub89d3b32008-09-04 20:09:12 -0700627#ifdef CONFIG_X86_HT
628 c->phys_proc_id = c->initial_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Yinghai Lu3da99c92008-09-04 21:09:44 +0200631
Yinghai Lu1b05d602008-09-06 01:52:27 -0700632 get_model_name(c); /* Default name */
Yinghai Lu3da99c92008-09-04 21:09:44 +0200633
634 init_scattered_cpuid_features(c);
635 detect_nopl(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/*
639 * This does the hard work of actually picking apart the CPU stuff...
640 */
Yinghai Lu9a250342008-06-21 03:24:00 -0700641static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 int i;
644
645 c->loops_per_jiffy = loops_per_jiffy;
646 c->x86_cache_size = -1;
647 c->x86_vendor = X86_VENDOR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 c->x86_model = c->x86_mask = 0; /* So far unknown... */
649 c->x86_vendor_id[0] = '\0'; /* Unset */
650 c->x86_model_id[0] = '\0'; /* Unset */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100651 c->x86_max_cores = 1;
Yinghai Lu102bbe32008-09-04 20:09:13 -0700652 c->x86_coreid_bits = 0;
Yinghai Lu11fdd252008-09-07 17:58:50 -0700653#ifdef CONFIG_X86_64
Yinghai Lu102bbe32008-09-04 20:09:13 -0700654 c->x86_clflush_size = 64;
655#else
656 c->cpuid_level = -1; /* CPUID not detected */
Andi Kleen770d1322006-12-07 02:14:05 +0100657 c->x86_clflush_size = 32;
Yinghai Lu102bbe32008-09-04 20:09:13 -0700658#endif
659 c->x86_cache_alignment = c->x86_clflush_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 memset(&c->x86_capability, 0, sizeof c->x86_capability);
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 generic_identify(c);
663
Andi Kleen38985342008-01-30 13:32:49 +0100664 if (this_cpu->c_identify)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 this_cpu->c_identify(c);
666
Yinghai Lu102bbe32008-09-04 20:09:13 -0700667#ifdef CONFIG_X86_64
668 c->apicid = phys_pkg_id(0);
669#endif
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /*
672 * Vendor-specific initialization. In this section we
673 * canonicalize the feature flags, meaning if there are
674 * features a certain CPU supports which CPUID doesn't
675 * tell us, CPUID claiming incorrect flags, or other bugs,
676 * we handle them here.
677 *
678 * At the end of this section, c->x86_capability better
679 * indicate the features this CPU genuinely supports!
680 */
681 if (this_cpu->c_init)
682 this_cpu->c_init(c);
683
684 /* Disable the PN if appropriate */
685 squash_the_stupid_serial_number(c);
686
687 /*
688 * The vendor-specific functions might have changed features. Now
689 * we do "generic changes."
690 */
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* If the model name is still unset, do table lookup. */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100693 if (!c->x86_model_id[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 char *p;
695 p = table_lookup_model(c);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100696 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 strcpy(c->x86_model_id, p);
698 else
699 /* Last resort... */
700 sprintf(c->x86_model_id, "%02x/%02x",
Chuck Ebbert54a20f82006-03-23 02:59:36 -0800701 c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703
Yinghai Lu102bbe32008-09-04 20:09:13 -0700704#ifdef CONFIG_X86_64
705 detect_ht(c);
706#endif
707
Alok Kataria88b094f2008-10-27 10:41:46 -0700708 init_hypervisor(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /*
710 * On SMP, boot_cpu_data holds the common feature set between
711 * all CPUs; so make sure that we indicate which features are
712 * common between the CPUs. The first time this routine gets
713 * executed, c == &boot_cpu_data.
714 */
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100715 if (c != &boot_cpu_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* AND the already accumulated flags with these */
Yinghai Lu9d31d352008-09-04 21:09:44 +0200717 for (i = 0; i < NCAPINTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
719 }
720
Andi Kleen7d851c82008-01-30 13:33:20 +0100721 /* Clear all flags overriden by options */
722 for (i = 0; i < NCAPINTS; i++)
Mikael Pettersson12c247a2008-02-24 18:27:03 +0100723 c->x86_capability[i] &= ~cleared_cpu_caps[i];
Andi Kleen7d851c82008-01-30 13:33:20 +0100724
Yinghai Lu102bbe32008-09-04 20:09:13 -0700725#ifdef CONFIG_X86_MCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Init Machine Check Exception if available. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 mcheck_init(c);
Yinghai Lu102bbe32008-09-04 20:09:13 -0700728#endif
Andi Kleen30d432d2008-01-30 13:33:16 +0100729
730 select_idle_routine(c);
Yinghai Lu102bbe32008-09-04 20:09:13 -0700731
732#if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
733 numa_add_cpu(smp_processor_id());
734#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +0200735}
Shaohua Li31ab2692005-11-07 00:58:42 -0800736
Glauber Costae04d6452008-09-22 14:35:08 -0300737#ifdef CONFIG_X86_64
738static void vgetcpu_set_mode(void)
739{
740 if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
741 vgetcpu_mode = VGETCPU_RDTSCP;
742 else
743 vgetcpu_mode = VGETCPU_LSL;
744}
745#endif
746
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +0200747void __init identify_boot_cpu(void)
748{
749 identify_cpu(&boot_cpu_data);
Yinghai Lu102bbe32008-09-04 20:09:13 -0700750#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +0200751 sysenter_setup();
Li Shaohua6fe940d2005-06-25 14:54:53 -0700752 enable_sep_cpu();
Glauber Costae04d6452008-09-22 14:35:08 -0300753#else
754 vgetcpu_set_mode();
Yinghai Lu102bbe32008-09-04 20:09:13 -0700755#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +0200756}
Shaohua Li3b520b22005-07-07 17:56:38 -0700757
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +0200758void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
759{
760 BUG_ON(c == &boot_cpu_data);
761 identify_cpu(c);
Yinghai Lu102bbe32008-09-04 20:09:13 -0700762#ifdef CONFIG_X86_32
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +0200763 enable_sep_cpu();
Yinghai Lu102bbe32008-09-04 20:09:13 -0700764#endif
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +0200765 mtrr_ap_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Yinghai Lua0854a42008-09-04 21:09:46 +0200768struct msr_range {
769 unsigned min;
770 unsigned max;
771};
772
773static struct msr_range msr_range_array[] __cpuinitdata = {
774 { 0x00000000, 0x00000418},
775 { 0xc0000000, 0xc000040b},
776 { 0xc0010000, 0xc0010142},
777 { 0xc0011000, 0xc001103b},
778};
779
780static void __cpuinit print_cpu_msr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Yinghai Lua0854a42008-09-04 21:09:46 +0200782 unsigned index;
783 u64 val;
784 int i;
785 unsigned index_min, index_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Yinghai Lua0854a42008-09-04 21:09:46 +0200787 for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
788 index_min = msr_range_array[i].min;
789 index_max = msr_range_array[i].max;
790 for (index = index_min; index < index_max; index++) {
791 if (rdmsrl_amd_safe(index, &val))
792 continue;
793 printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796}
Yinghai Lua0854a42008-09-04 21:09:46 +0200797
798static int show_msr __cpuinitdata;
799static __init int setup_show_msr(char *arg)
800{
801 int num;
802
803 get_option(&arg, &num);
804
805 if (num > 0)
806 show_msr = num;
807 return 1;
808}
809__setup("show_msr=", setup_show_msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Andi Kleen191679f2008-01-30 13:33:21 +0100811static __init int setup_noclflush(char *arg)
812{
813 setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
814 return 1;
815}
816__setup("noclflush", setup_noclflush);
817
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800818void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 char *vendor = NULL;
821
822 if (c->x86_vendor < X86_VENDOR_NUM)
823 vendor = this_cpu->c_vendor;
824 else if (c->cpuid_level >= 0)
825 vendor = c->x86_vendor_id;
826
Yinghai Lubd32a8c2008-09-19 18:41:16 -0700827 if (vendor && !strstr(c->x86_model_id, vendor))
Yinghai Lu9d31d352008-09-04 21:09:44 +0200828 printk(KERN_CONT "%s ", vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Yinghai Lu9d31d352008-09-04 21:09:44 +0200830 if (c->x86_model_id[0])
831 printk(KERN_CONT "%s", c->x86_model_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 else
Yinghai Lu9d31d352008-09-04 21:09:44 +0200833 printk(KERN_CONT "%d86", c->x86);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +0100835 if (c->x86_mask || c->cpuid_level >= 0)
Yinghai Lu9d31d352008-09-04 21:09:44 +0200836 printk(KERN_CONT " stepping %02x\n", c->x86_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 else
Yinghai Lu9d31d352008-09-04 21:09:44 +0200838 printk(KERN_CONT "\n");
Yinghai Lua0854a42008-09-04 21:09:46 +0200839
840#ifdef CONFIG_SMP
841 if (c->cpu_index < show_msr)
842 print_cpu_msr();
843#else
844 if (show_msr)
845 print_cpu_msr();
846#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Andi Kleenac72e782008-01-30 13:33:21 +0100849static __init int setup_disablecpuid(char *arg)
850{
851 int bit;
852 if (get_option(&arg, &bit) && bit < NCAPINTS*32)
853 setup_clear_cpu_cap(bit);
854 else
855 return 0;
856 return 1;
857}
858__setup("clearcpuid=", setup_disablecpuid);
859
Chuck Ebbert3bc9b762006-03-23 02:59:33 -0800860cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Yinghai Lud5494d42008-09-04 20:09:03 -0700862#ifdef CONFIG_X86_64
863struct x8664_pda **_cpu_pda __read_mostly;
864EXPORT_SYMBOL(_cpu_pda);
865
866struct desc_ptr idt_descr = { 256 * 16 - 1, (unsigned long) idt_table };
867
Jaswinder Singh34945ed2008-12-19 22:33:52 +0530868static char boot_cpu_stack[IRQSTACKSIZE] __page_aligned_bss;
Yinghai Lud5494d42008-09-04 20:09:03 -0700869
Jan Beulich2d9cd6c2008-08-29 13:15:04 +0100870void __cpuinit pda_init(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Yinghai Lud5494d42008-09-04 20:09:03 -0700872 struct x8664_pda *pda = cpu_pda(cpu);
Thomas Petazzoni03ae5762008-02-15 12:00:23 +0100873
Yinghai Lud5494d42008-09-04 20:09:03 -0700874 /* Setup up data that may be needed in __get_free_pages early */
875 loadsegment(fs, 0);
876 loadsegment(gs, 0);
877 /* Memory clobbers used to order PDA accessed */
878 mb();
879 wrmsrl(MSR_GS_BASE, pda);
880 mb();
Thomas Petazzoni03ae5762008-02-15 12:00:23 +0100881
Yinghai Lud5494d42008-09-04 20:09:03 -0700882 pda->cpunumber = cpu;
883 pda->irqcount = -1;
884 pda->kernelstack = (unsigned long)stack_thread_info() -
885 PDA_STACKOFFSET + THREAD_SIZE;
886 pda->active_mm = &init_mm;
887 pda->mmu_state = 0;
888
889 if (cpu == 0) {
890 /* others are initialized in smpboot.c */
891 pda->pcurrent = &init_task;
892 pda->irqstackptr = boot_cpu_stack;
893 pda->irqstackptr += IRQSTACKSIZE - 64;
894 } else {
895 if (!pda->irqstackptr) {
896 pda->irqstackptr = (char *)
897 __get_free_pages(GFP_ATOMIC, IRQSTACK_ORDER);
898 if (!pda->irqstackptr)
899 panic("cannot allocate irqstack for cpu %d",
900 cpu);
901 pda->irqstackptr += IRQSTACKSIZE - 64;
902 }
903
904 if (pda->nodenumber == 0 && cpu_to_node(cpu) != NUMA_NO_NODE)
905 pda->nodenumber = cpu_to_node(cpu);
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100908
Jaswinder Singh34945ed2008-12-19 22:33:52 +0530909static char boot_exception_stacks[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ +
910 DEBUG_STKSZ] __page_aligned_bss;
Yinghai Lud5494d42008-09-04 20:09:03 -0700911
912extern asmlinkage void ignore_sysret(void);
913
914/* May not be marked __init: used by software suspend */
915void syscall_init(void)
916{
917 /*
918 * LSTAR and STAR live in a bit strange symbiosis.
919 * They both write to the same internal register. STAR allows to
920 * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
921 */
922 wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
923 wrmsrl(MSR_LSTAR, system_call);
924 wrmsrl(MSR_CSTAR, ignore_sysret);
925
926#ifdef CONFIG_IA32_EMULATION
927 syscall32_cpu_init();
928#endif
929
930 /* Flags to clear on syscall */
931 wrmsrl(MSR_SYSCALL_MASK,
932 X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
933}
934
Yinghai Lud5494d42008-09-04 20:09:03 -0700935unsigned long kernel_eflags;
936
937/*
938 * Copies of the original ist values from the tss are only accessed during
939 * debugging, no special alignment required.
940 */
941DEFINE_PER_CPU(struct orig_ist, orig_ist);
942
943#else
944
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200945/* Make sure %fs is initialized properly in idle threads */
Adrian Bunk6b2fb3c2008-02-06 01:37:55 -0800946struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100947{
948 memset(regs, 0, sizeof(struct pt_regs));
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100949 regs->fs = __KERNEL_PERCPU;
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100950 return regs;
951}
Yinghai Lud5494d42008-09-04 20:09:03 -0700952#endif
Jeremy Fitzhardingec5413fb2007-05-02 19:27:16 +0200953
Rusty Russelld2cbcc42007-05-02 19:27:10 +0200954/*
955 * cpu_init() initializes state that is per-CPU. Some data is already
956 * initialized (naturally) in the bootstrap process, such as the GDT
957 * and IDT. We reload them nevertheless, this function acts as a
958 * 'CPU state barrier', nothing should get across.
Yinghai Lu1ba76582008-09-04 20:09:04 -0700959 * A lot of state is already set up in PDA init for 64 bit
Rusty Russelld2cbcc42007-05-02 19:27:10 +0200960 */
Yinghai Lu1ba76582008-09-04 20:09:04 -0700961#ifdef CONFIG_X86_64
962void __cpuinit cpu_init(void)
963{
964 int cpu = stack_smp_processor_id();
965 struct tss_struct *t = &per_cpu(init_tss, cpu);
966 struct orig_ist *orig_ist = &per_cpu(orig_ist, cpu);
967 unsigned long v;
968 char *estacks = NULL;
969 struct task_struct *me;
970 int i;
971
972 /* CPU 0 is initialised in head64.c */
973 if (cpu != 0)
974 pda_init(cpu);
975 else
976 estacks = boot_exception_stacks;
977
978 me = current;
979
980 if (cpu_test_and_set(cpu, cpu_initialized))
981 panic("CPU#%d already initialized!\n", cpu);
982
983 printk(KERN_INFO "Initializing CPU#%d\n", cpu);
984
985 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
986
987 /*
988 * Initialize the per-CPU GDT with the boot GDT,
989 * and set up the GDT descriptor:
990 */
991
992 switch_to_new_gdt();
993 load_idt((const struct desc_ptr *)&idt_descr);
994
995 memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
996 syscall_init();
997
998 wrmsrl(MSR_FS_BASE, 0);
999 wrmsrl(MSR_KERNEL_GS_BASE, 0);
1000 barrier();
1001
1002 check_efer();
1003 if (cpu != 0 && x2apic)
1004 enable_x2apic();
1005
1006 /*
1007 * set up and load the per-CPU TSS
1008 */
1009 if (!orig_ist->ist[0]) {
1010 static const unsigned int order[N_EXCEPTION_STACKS] = {
1011 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STACK_ORDER,
1012 [DEBUG_STACK - 1] = DEBUG_STACK_ORDER
1013 };
1014 for (v = 0; v < N_EXCEPTION_STACKS; v++) {
1015 if (cpu) {
1016 estacks = (char *)__get_free_pages(GFP_ATOMIC, order[v]);
1017 if (!estacks)
1018 panic("Cannot allocate exception "
1019 "stack %ld %d\n", v, cpu);
1020 }
1021 estacks += PAGE_SIZE << order[v];
1022 orig_ist->ist[v] = t->x86_tss.ist[v] =
1023 (unsigned long)estacks;
1024 }
1025 }
1026
1027 t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
1028 /*
1029 * <= is required because the CPU will access up to
1030 * 8 bits beyond the end of the IO permission bitmap.
1031 */
1032 for (i = 0; i <= IO_BITMAP_LONGS; i++)
1033 t->io_bitmap[i] = ~0UL;
1034
1035 atomic_inc(&init_mm.mm_count);
1036 me->active_mm = &init_mm;
1037 if (me->mm)
1038 BUG();
1039 enter_lazy_tlb(&init_mm, me);
1040
1041 load_sp0(t, &current->thread);
1042 set_tss_desc(cpu, t);
1043 load_TR_desc();
1044 load_LDT(&init_mm.context);
1045
1046#ifdef CONFIG_KGDB
1047 /*
1048 * If the kgdb is connected no debug regs should be altered. This
1049 * is only applicable when KGDB and a KGDB I/O module are built
1050 * into the kernel and you are using early debugging with
1051 * kgdbwait. KGDB will control the kernel HW breakpoint registers.
1052 */
1053 if (kgdb_connected && arch_kgdb_ops.correct_hw_break)
1054 arch_kgdb_ops.correct_hw_break();
1055 else {
1056#endif
1057 /*
1058 * Clear all 6 debug registers:
1059 */
1060
1061 set_debugreg(0UL, 0);
1062 set_debugreg(0UL, 1);
1063 set_debugreg(0UL, 2);
1064 set_debugreg(0UL, 3);
1065 set_debugreg(0UL, 6);
1066 set_debugreg(0UL, 7);
1067#ifdef CONFIG_KGDB
1068 /* If the kgdb is connected no debug regs should be altered. */
1069 }
1070#endif
1071
1072 fpu_init();
1073
1074 raw_local_save_flags(kernel_eflags);
1075
1076 if (is_uv_system())
1077 uv_cpu_init();
1078}
1079
1080#else
1081
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001082void __cpuinit cpu_init(void)
James Bottomley9ee79a32007-01-22 09:18:31 -06001083{
Rusty Russelld2cbcc42007-05-02 19:27:10 +02001084 int cpu = smp_processor_id();
1085 struct task_struct *curr = current;
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001086 struct tss_struct *t = &per_cpu(init_tss, cpu);
James Bottomley9ee79a32007-01-22 09:18:31 -06001087 struct thread_struct *thread = &curr->thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 if (cpu_test_and_set(cpu, cpu_initialized)) {
1090 printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
1091 for (;;) local_irq_enable();
1092 }
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 printk(KERN_INFO "Initializing CPU#%d\n", cpu);
1095
1096 if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
1097 clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Zachary Amsden4d37e7e2005-09-03 15:56:38 -07001099 load_idt(&idt_descr);
Jeremy Fitzhardingec5413fb2007-05-02 19:27:16 +02001100 switch_to_new_gdt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 * Set up and load the per-CPU TSS and LDT
1104 */
1105 atomic_inc(&init_mm.mm_count);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +01001106 curr->active_mm = &init_mm;
1107 if (curr->mm)
1108 BUG();
1109 enter_lazy_tlb(&init_mm, curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
H. Peter Anvinfaca6222008-01-30 13:31:02 +01001111 load_sp0(t, thread);
Paolo Ciarrocchi34048c92008-02-24 11:58:13 +01001112 set_tss_desc(cpu, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 load_TR_desc();
1114 load_LDT(&init_mm.context);
1115
Matt Mackall22c4e302006-01-08 01:05:24 -08001116#ifdef CONFIG_DOUBLEFAULT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* Set up doublefault TSS pointer in the GDT */
1118 __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
Matt Mackall22c4e302006-01-08 01:05:24 -08001119#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +01001121 /* Clear %gs. */
1122 asm volatile ("mov %0, %%gs" : : "r" (0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /* Clear all 6 debug registers: */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -07001125 set_debugreg(0, 0);
1126 set_debugreg(0, 1);
1127 set_debugreg(0, 2);
1128 set_debugreg(0, 3);
1129 set_debugreg(0, 6);
1130 set_debugreg(0, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 /*
1133 * Force FPU initialization:
1134 */
Suresh Siddhab359e8a2008-07-29 10:29:20 -07001135 if (cpu_has_xsave)
1136 current_thread_info()->status = TS_XSAVE;
1137 else
1138 current_thread_info()->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 clear_used_math();
1140 mxcsr_feature_mask_init();
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07001141
1142 /*
1143 * Boot processor to setup the FP and extended state context info.
1144 */
James Bottomleyb3572e32008-10-30 16:00:59 -05001145 if (smp_processor_id() == boot_cpu_id)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07001146 init_thread_xstate();
1147
1148 xsave_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
Li Shaohuae1367da2005-06-25 14:54:56 -07001150
Yinghai Lu1ba76582008-09-04 20:09:04 -07001151
1152#endif