blob: 25423a5b80ed28a7058bd01cf3f5f0358fb167b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/init.h>
2#include <linux/bitops.h>
3#include <linux/mm.h>
Yinghai Lu8d71a2e2008-09-07 17:58:53 -07004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <asm/io.h>
6#include <asm/processor.h>
Andi Kleend3f7eae2007-08-10 22:31:07 +02007#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Yinghai Lu8d71a2e2008-09-07 17:58:53 -07009#ifdef CONFIG_X86_64
10# include <asm/numa_64.h>
11# include <asm/mmconfig.h>
12# include <asm/cacheflush.h>
13#endif
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "cpu.h"
16
Yinghai Lu6c62aa42008-09-07 17:58:54 -070017#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/*
19 * B step AMD K6 before B 9730xxxx have hardware bugs that can cause
20 * misexecution of code under Linux. Owners of such processors should
21 * contact AMD for precise details and a CPU swap.
22 *
23 * See http://www.multimania.com/poulot/k6bug.html
24 * http://www.amd.com/K6/k6docs/revgd.html
25 *
26 * The following test is erm.. interesting. AMD neglected to up
27 * the chip setting when fixing the bug but they also tweaked some
28 * performance at the same time..
29 */
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +010030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031extern void vide(void);
32__asm__(".align 4\nvide: ret");
33
Yinghai Lu11fdd252008-09-07 17:58:50 -070034static void __cpuinit init_amd_k5(struct cpuinfo_x86 *c)
35{
36/*
37 * General Systems BIOSen alias the cpu frequency registers
38 * of the Elan at 0x000df000. Unfortuantly, one of the Linux
39 * drivers subsequently pokes it, and changes the CPU speed.
40 * Workaround : Remove the unneeded alias.
41 */
42#define CBAR (0xfffc) /* Configuration Base Address (32-bit) */
43#define CBAR_ENB (0x80000000)
44#define CBAR_KEY (0X000000CB)
45 if (c->x86_model == 9 || c->x86_model == 10) {
46 if (inl (CBAR) & CBAR_ENB)
47 outl (0 | CBAR_KEY, CBAR);
48 }
49}
50
51
52static void __cpuinit init_amd_k6(struct cpuinfo_x86 *c)
53{
54 u32 l, h;
55 int mbytes = num_physpages >> (20-PAGE_SHIFT);
56
57 if (c->x86_model < 6) {
58 /* Based on AMD doc 20734R - June 2000 */
59 if (c->x86_model == 0) {
60 clear_cpu_cap(c, X86_FEATURE_APIC);
61 set_cpu_cap(c, X86_FEATURE_PGE);
62 }
63 return;
64 }
65
66 if (c->x86_model == 6 && c->x86_mask == 1) {
67 const int K6_BUG_LOOP = 1000000;
68 int n;
69 void (*f_vide)(void);
70 unsigned long d, d2;
71
72 printk(KERN_INFO "AMD K6 stepping B detected - ");
73
74 /*
75 * It looks like AMD fixed the 2.6.2 bug and improved indirect
76 * calls at the same time.
77 */
78
79 n = K6_BUG_LOOP;
80 f_vide = vide;
81 rdtscl(d);
82 while (n--)
83 f_vide();
84 rdtscl(d2);
85 d = d2-d;
86
87 if (d > 20*K6_BUG_LOOP)
88 printk("system stability may be impaired when more than 32 MB are used.\n");
89 else
90 printk("probably OK (after B9730xxxx).\n");
91 printk(KERN_INFO "Please see http://membres.lycos.fr/poulot/k6bug.html\n");
92 }
93
94 /* K6 with old style WHCR */
95 if (c->x86_model < 8 ||
96 (c->x86_model == 8 && c->x86_mask < 8)) {
97 /* We can only write allocate on the low 508Mb */
98 if (mbytes > 508)
99 mbytes = 508;
100
101 rdmsr(MSR_K6_WHCR, l, h);
102 if ((l&0x0000FFFF) == 0) {
103 unsigned long flags;
104 l = (1<<0)|((mbytes/4)<<1);
105 local_irq_save(flags);
106 wbinvd();
107 wrmsr(MSR_K6_WHCR, l, h);
108 local_irq_restore(flags);
109 printk(KERN_INFO "Enabling old style K6 write allocation for %d Mb\n",
110 mbytes);
111 }
112 return;
113 }
114
115 if ((c->x86_model == 8 && c->x86_mask > 7) ||
116 c->x86_model == 9 || c->x86_model == 13) {
117 /* The more serious chips .. */
118
119 if (mbytes > 4092)
120 mbytes = 4092;
121
122 rdmsr(MSR_K6_WHCR, l, h);
123 if ((l&0xFFFF0000) == 0) {
124 unsigned long flags;
125 l = ((mbytes>>2)<<22)|(1<<16);
126 local_irq_save(flags);
127 wbinvd();
128 wrmsr(MSR_K6_WHCR, l, h);
129 local_irq_restore(flags);
130 printk(KERN_INFO "Enabling new style K6 write allocation for %d Mb\n",
131 mbytes);
132 }
133
134 return;
135 }
136
137 if (c->x86_model == 10) {
138 /* AMD Geode LX is model 10 */
139 /* placeholder for any needed mods */
140 return;
141 }
142}
143
144static void __cpuinit init_amd_k7(struct cpuinfo_x86 *c)
145{
146 u32 l, h;
147
148 /*
149 * Bit 15 of Athlon specific MSR 15, needs to be 0
150 * to enable SSE on Palomino/Morgan/Barton CPU's.
151 * If the BIOS didn't enable it already, enable it here.
152 */
153 if (c->x86_model >= 6 && c->x86_model <= 10) {
154 if (!cpu_has(c, X86_FEATURE_XMM)) {
155 printk(KERN_INFO "Enabling disabled K7/SSE Support.\n");
156 rdmsr(MSR_K7_HWCR, l, h);
157 l &= ~0x00008000;
158 wrmsr(MSR_K7_HWCR, l, h);
159 set_cpu_cap(c, X86_FEATURE_XMM);
160 }
161 }
162
163 /*
164 * It's been determined by AMD that Athlons since model 8 stepping 1
165 * are more robust with CLK_CTL set to 200xxxxx instead of 600xxxxx
166 * As per AMD technical note 27212 0.2
167 */
168 if ((c->x86_model == 8 && c->x86_mask >= 1) || (c->x86_model > 8)) {
169 rdmsr(MSR_K7_CLK_CTL, l, h);
170 if ((l & 0xfff00000) != 0x20000000) {
171 printk ("CPU: CLK_CTL MSR was %x. Reprogramming to %x\n", l,
172 ((l & 0x000fffff)|0x20000000));
173 wrmsr(MSR_K7_CLK_CTL, (l & 0x000fffff)|0x20000000, h);
174 }
175 }
176
177 set_cpu_cap(c, X86_FEATURE_K7);
178}
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700179#endif
180
181#if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
182static int __cpuinit nearby_node(int apicid)
183{
184 int i, node;
185
186 for (i = apicid - 1; i >= 0; i--) {
187 node = apicid_to_node[i];
188 if (node != NUMA_NO_NODE && node_online(node))
189 return node;
190 }
191 for (i = apicid + 1; i < MAX_LOCAL_APIC; i++) {
192 node = apicid_to_node[i];
193 if (node != NUMA_NO_NODE && node_online(node))
194 return node;
195 }
196 return first_node(node_online_map); /* Shouldn't happen */
197}
198#endif
Yinghai Lu11fdd252008-09-07 17:58:50 -0700199
200/*
201 * On a AMD dual core setup the lower bits of the APIC id distingush the cores.
202 * Assumes number of cores is a power of two.
203 */
204static void __cpuinit amd_detect_cmp(struct cpuinfo_x86 *c)
205{
206#ifdef CONFIG_X86_HT
207 unsigned bits;
208
209 bits = c->x86_coreid_bits;
210
211 /* Low order bits define the core id (index of core in socket) */
212 c->cpu_core_id = c->initial_apicid & ((1 << bits)-1);
213 /* Convert the initial APIC ID into the socket ID */
214 c->phys_proc_id = c->initial_apicid >> bits;
215#endif
216}
217
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700218static void __cpuinit srat_detect_node(struct cpuinfo_x86 *c)
219{
220#if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
221 int cpu = smp_processor_id();
222 int node;
223 unsigned apicid = hard_smp_processor_id();
224
225 node = c->phys_proc_id;
226 if (apicid_to_node[apicid] != NUMA_NO_NODE)
227 node = apicid_to_node[apicid];
228 if (!node_online(node)) {
229 /* Two possibilities here:
230 - The CPU is missing memory and no node was created.
231 In that case try picking one from a nearby CPU
232 - The APIC IDs differ from the HyperTransport node IDs
233 which the K8 northbridge parsing fills in.
234 Assume they are all increased by a constant offset,
235 but in the same order as the HT nodeids.
236 If that doesn't result in a usable node fall back to the
237 path for the previous case. */
238
239 int ht_nodeid = c->initial_apicid;
240
241 if (ht_nodeid >= 0 &&
242 apicid_to_node[ht_nodeid] != NUMA_NO_NODE)
243 node = apicid_to_node[ht_nodeid];
244 /* Pick a nearby node */
245 if (!node_online(node))
246 node = nearby_node(apicid);
247 }
248 numa_set_node(cpu, node);
249
Yinghai Lu823b2592008-09-10 21:56:46 -0700250 printk(KERN_INFO "CPU %d/0x%x -> Node %d\n", cpu, apicid, node);
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700251#endif
252}
253
Yinghai Lu11fdd252008-09-07 17:58:50 -0700254static void __cpuinit early_init_amd_mc(struct cpuinfo_x86 *c)
255{
256#ifdef CONFIG_X86_HT
257 unsigned bits, ecx;
258
259 /* Multi core CPU? */
260 if (c->extended_cpuid_level < 0x80000008)
261 return;
262
263 ecx = cpuid_ecx(0x80000008);
264
265 c->x86_max_cores = (ecx & 0xff) + 1;
266
267 /* CPU telling us the core id bits shift? */
268 bits = (ecx >> 12) & 0xF;
269
270 /* Otherwise recompute */
271 if (bits == 0) {
272 while ((1 << bits) < c->x86_max_cores)
273 bits++;
274 }
275
276 c->x86_coreid_bits = bits;
277#endif
278}
279
Thomas Petazzoni03ae5762008-02-15 12:00:23 +0100280static void __cpuinit early_init_amd(struct cpuinfo_x86 *c)
Andi Kleen2b16a232008-01-30 13:32:40 +0100281{
Yinghai Lu11fdd252008-09-07 17:58:50 -0700282 early_init_amd_mc(c);
283
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800284 /*
285 * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
286 * with P/T states and does not stop in deep C-states
287 */
288 if (c->x86_power & (1 << 8)) {
Yinghai Lue3224232008-09-06 01:52:28 -0700289 set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800290 set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
291 }
Yinghai Lu5fef55f2008-09-04 21:09:43 +0200292
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700293#ifdef CONFIG_X86_64
294 set_cpu_cap(c, X86_FEATURE_SYSCALL32);
295#else
Yinghai Lu5fef55f2008-09-04 21:09:43 +0200296 /* Set MTRR capability flag if appropriate */
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700297 if (c->x86 == 5)
298 if (c->x86_model == 13 || c->x86_model == 9 ||
299 (c->x86_model == 8 && c->x86_mask >= 8))
300 set_cpu_cap(c, X86_FEATURE_K6_MTRR);
301#endif
Andi Kleen2b16a232008-01-30 13:32:40 +0100302}
303
Magnus Dammb4af3f72006-09-26 10:52:36 +0200304static void __cpuinit init_amd(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Andi Kleen7d318d72005-09-29 22:05:55 +0200306#ifdef CONFIG_SMP
Andi Kleen3c92c2b2005-10-11 01:28:33 +0200307 unsigned long long value;
Andi Kleen7d318d72005-09-29 22:05:55 +0200308
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100309 /*
310 * Disable TLB flush filter by setting HWCR.FFDIS on K8
Andi Kleen7d318d72005-09-29 22:05:55 +0200311 * bit 6 of msr C001_0015
312 *
313 * Errata 63 for SH-B3 steppings
314 * Errata 122 for all steppings (F+ have it disabled by default)
315 */
Yinghai Lu11fdd252008-09-07 17:58:50 -0700316 if (c->x86 == 0xf) {
Andi Kleen7d318d72005-09-29 22:05:55 +0200317 rdmsrl(MSR_K7_HWCR, value);
318 value |= 1 << 6;
319 wrmsrl(MSR_K7_HWCR, value);
320 }
321#endif
322
Andi Kleen2b16a232008-01-30 13:32:40 +0100323 early_init_amd(c);
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /*
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100326 * Bit 31 in normal CPUID used for nonstandard 3DNow ID;
Ingo Molnar16282a82008-02-26 08:49:57 +0100327 * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100328 */
Ingo Molnar16282a82008-02-26 08:49:57 +0100329 clear_cpu_cap(c, 0*32+31);
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100330
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700331#ifdef CONFIG_X86_64
332 /* On C+ stepping K8 rep microcode works well for copy/memset */
333 if (c->x86 == 0xf) {
334 u32 level;
335
336 level = cpuid_eax(1);
337 if((level >= 0x0f48 && level < 0x0f50) || level >= 0x0f58)
338 set_cpu_cap(c, X86_FEATURE_REP_GOOD);
339 }
340 if (c->x86 == 0x10 || c->x86 == 0x11)
341 set_cpu_cap(c, X86_FEATURE_REP_GOOD);
342#else
343
344 /*
345 * FIXME: We should handle the K5 here. Set up the write
346 * range and also turn on MSR 83 bits 4 and 31 (write alloc,
347 * no bus pipeline)
348 */
349
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100350 switch (c->x86) {
351 case 4:
Yinghai Lu11fdd252008-09-07 17:58:50 -0700352 init_amd_k5(c);
353 break;
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100354 case 5:
Yinghai Lu11fdd252008-09-07 17:58:50 -0700355 init_amd_k6(c);
356 break;
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100357 case 6: /* An Athlon/Duron */
Yinghai Lu11fdd252008-09-07 17:58:50 -0700358 init_amd_k7(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 break;
Andi Kleen67cddd92007-07-21 17:10:03 +0200360 }
Andi Kleen3556ddf2007-04-02 12:14:12 +0200361
Andi Kleenc12ceb72007-05-21 14:31:47 +0200362 /* K6s reports MCEs but don't actually have all the MSRs */
363 if (c->x86 < 6)
Ingo Molnar16282a82008-02-26 08:49:57 +0100364 clear_cpu_cap(c, X86_FEATURE_MCE);
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700365#endif
Andi Kleende421862008-01-30 13:32:37 +0100366
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700367 /* Enable workaround for FXSAVE leak */
Yinghai Lu11fdd252008-09-07 17:58:50 -0700368 if (c->x86 >= 6)
369 set_cpu_cap(c, X86_FEATURE_FXSAVE_LEAK);
370
371 if (!c->x86_model_id[0]) {
372 switch (c->x86) {
373 case 0xf:
374 /* Should distinguish Models here, but this is only
375 a fallback anyways. */
376 strcpy(c->x86_model_id, "Hammer");
377 break;
378 }
379 }
380
381 display_cacheinfo(c);
382
383 /* Multi core CPU? */
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700384 if (c->extended_cpuid_level >= 0x80000008) {
Yinghai Lu11fdd252008-09-07 17:58:50 -0700385 amd_detect_cmp(c);
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700386 srat_detect_node(c);
387 }
Yinghai Lu11fdd252008-09-07 17:58:50 -0700388
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700389#ifdef CONFIG_X86_32
Yinghai Lu11fdd252008-09-07 17:58:50 -0700390 detect_ht(c);
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700391#endif
Yinghai Lu11fdd252008-09-07 17:58:50 -0700392
393 if (c->extended_cpuid_level >= 0x80000006) {
394 if ((c->x86 >= 0x0f) && (cpuid_edx(0x80000006) & 0xf000))
395 num_cache_leaves = 4;
396 else
397 num_cache_leaves = 3;
398 }
399
400 if (c->x86 >= 0xf && c->x86 <= 0x11)
401 set_cpu_cap(c, X86_FEATURE_K8);
402
403 if (cpu_has_xmm2) {
404 /* MFENCE stops RDTSC speculation */
Ingo Molnar16282a82008-02-26 08:49:57 +0100405 set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC);
Yinghai Lu11fdd252008-09-07 17:58:50 -0700406 }
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700407
408#ifdef CONFIG_X86_64
409 if (c->x86 == 0x10) {
410 /* do this for boot cpu */
411 if (c == &boot_cpu_data)
412 check_enable_amd_mmconf_dmi();
413
414 fam10h_check_enable_mmcfg();
415 }
416
417 if (c == &boot_cpu_data && c->x86 >= 0xf && c->x86 <= 0x11) {
418 unsigned long long tseg;
419
420 /*
421 * Split up direct mapping around the TSEG SMM area.
422 * Don't do it for gbpages because there seems very little
423 * benefit in doing so.
424 */
425 if (!rdmsrl_safe(MSR_K8_TSEG_ADDR, &tseg)) {
426 printk(KERN_DEBUG "tseg: %010llx\n", tseg);
427 if ((tseg>>PMD_SHIFT) <
428 (max_low_pfn_mapped>>(PMD_SHIFT-PAGE_SHIFT)) ||
429 ((tseg>>PMD_SHIFT) <
430 (max_pfn_mapped>>(PMD_SHIFT-PAGE_SHIFT)) &&
431 (tseg>>PMD_SHIFT) >= (1ULL<<(32 - PMD_SHIFT))))
432 set_memory_4k((unsigned long)__va(tseg), 1);
433 }
434 }
435#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700438#ifdef CONFIG_X86_32
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100439static unsigned int __cpuinit amd_size_cache(struct cpuinfo_x86 *c, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 /* AMD errata T13 (order #21922) */
442 if ((c->x86 == 6)) {
443 if (c->x86_model == 3 && c->x86_mask == 0) /* Duron Rev A0 */
444 size = 64;
445 if (c->x86_model == 4 &&
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100446 (c->x86_mask == 0 || c->x86_mask == 1)) /* Tbird rev A1/A2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 size = 256;
448 }
449 return size;
450}
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700451#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Magnus Damm95414932006-09-26 10:52:36 +0200453static struct cpu_dev amd_cpu_dev __cpuinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 .c_vendor = "AMD",
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100455 .c_ident = { "AuthenticAMD" },
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700456#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 .c_models = {
458 { .vendor = X86_VENDOR_AMD, .family = 4, .model_names =
459 {
460 [3] = "486 DX/2",
461 [7] = "486 DX/2-WB",
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100462 [8] = "486 DX/4",
463 [9] = "486 DX/4-WB",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 [14] = "Am5x86-WT",
Paolo Ciarrocchifb87a292008-02-22 23:10:33 +0100465 [15] = "Am5x86-WB"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467 },
468 },
Yinghai Lu6c62aa42008-09-07 17:58:54 -0700469 .c_size_cache = amd_size_cache,
470#endif
Thomas Petazzoni03ae5762008-02-15 12:00:23 +0100471 .c_early_init = early_init_amd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 .c_init = init_amd,
Yinghai Lu10a434f2008-09-04 21:09:45 +0200473 .c_x86_vendor = X86_VENDOR_AMD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474};
475
Yinghai Lu10a434f2008-09-04 21:09:45 +0200476cpu_dev_register(amd_cpu_dev);