Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/init.h> |
| 2 | #include <linux/bitops.h> |
| 3 | #include <linux/mm.h> |
| 4 | #include <asm/io.h> |
| 5 | #include <asm/processor.h> |
Andi Kleen | d3f7eae | 2007-08-10 22:31:07 +0200 | [diff] [blame] | 6 | #include <asm/apic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
Glauber Costa | dd46e3c | 2008-03-25 18:10:46 -0300 | [diff] [blame] | 8 | #include <mach_apic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include "cpu.h" |
| 10 | |
| 11 | /* |
| 12 | * B step AMD K6 before B 9730xxxx have hardware bugs that can cause |
| 13 | * misexecution of code under Linux. Owners of such processors should |
| 14 | * contact AMD for precise details and a CPU swap. |
| 15 | * |
| 16 | * See http://www.multimania.com/poulot/k6bug.html |
| 17 | * http://www.amd.com/K6/k6docs/revgd.html |
| 18 | * |
| 19 | * The following test is erm.. interesting. AMD neglected to up |
| 20 | * the chip setting when fixing the bug but they also tweaked some |
| 21 | * performance at the same time.. |
| 22 | */ |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | extern void vide(void); |
| 25 | __asm__(".align 4\nvide: ret"); |
| 26 | |
Yinghai Lu | 11fdd25 | 2008-09-07 17:58:50 -0700 | [diff] [blame^] | 27 | static void __cpuinit init_amd_k5(struct cpuinfo_x86 *c) |
| 28 | { |
| 29 | /* |
| 30 | * General Systems BIOSen alias the cpu frequency registers |
| 31 | * of the Elan at 0x000df000. Unfortuantly, one of the Linux |
| 32 | * drivers subsequently pokes it, and changes the CPU speed. |
| 33 | * Workaround : Remove the unneeded alias. |
| 34 | */ |
| 35 | #define CBAR (0xfffc) /* Configuration Base Address (32-bit) */ |
| 36 | #define CBAR_ENB (0x80000000) |
| 37 | #define CBAR_KEY (0X000000CB) |
| 38 | if (c->x86_model == 9 || c->x86_model == 10) { |
| 39 | if (inl (CBAR) & CBAR_ENB) |
| 40 | outl (0 | CBAR_KEY, CBAR); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | static void __cpuinit init_amd_k6(struct cpuinfo_x86 *c) |
| 46 | { |
| 47 | u32 l, h; |
| 48 | int mbytes = num_physpages >> (20-PAGE_SHIFT); |
| 49 | |
| 50 | if (c->x86_model < 6) { |
| 51 | /* Based on AMD doc 20734R - June 2000 */ |
| 52 | if (c->x86_model == 0) { |
| 53 | clear_cpu_cap(c, X86_FEATURE_APIC); |
| 54 | set_cpu_cap(c, X86_FEATURE_PGE); |
| 55 | } |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | if (c->x86_model == 6 && c->x86_mask == 1) { |
| 60 | const int K6_BUG_LOOP = 1000000; |
| 61 | int n; |
| 62 | void (*f_vide)(void); |
| 63 | unsigned long d, d2; |
| 64 | |
| 65 | printk(KERN_INFO "AMD K6 stepping B detected - "); |
| 66 | |
| 67 | /* |
| 68 | * It looks like AMD fixed the 2.6.2 bug and improved indirect |
| 69 | * calls at the same time. |
| 70 | */ |
| 71 | |
| 72 | n = K6_BUG_LOOP; |
| 73 | f_vide = vide; |
| 74 | rdtscl(d); |
| 75 | while (n--) |
| 76 | f_vide(); |
| 77 | rdtscl(d2); |
| 78 | d = d2-d; |
| 79 | |
| 80 | if (d > 20*K6_BUG_LOOP) |
| 81 | printk("system stability may be impaired when more than 32 MB are used.\n"); |
| 82 | else |
| 83 | printk("probably OK (after B9730xxxx).\n"); |
| 84 | printk(KERN_INFO "Please see http://membres.lycos.fr/poulot/k6bug.html\n"); |
| 85 | } |
| 86 | |
| 87 | /* K6 with old style WHCR */ |
| 88 | if (c->x86_model < 8 || |
| 89 | (c->x86_model == 8 && c->x86_mask < 8)) { |
| 90 | /* We can only write allocate on the low 508Mb */ |
| 91 | if (mbytes > 508) |
| 92 | mbytes = 508; |
| 93 | |
| 94 | rdmsr(MSR_K6_WHCR, l, h); |
| 95 | if ((l&0x0000FFFF) == 0) { |
| 96 | unsigned long flags; |
| 97 | l = (1<<0)|((mbytes/4)<<1); |
| 98 | local_irq_save(flags); |
| 99 | wbinvd(); |
| 100 | wrmsr(MSR_K6_WHCR, l, h); |
| 101 | local_irq_restore(flags); |
| 102 | printk(KERN_INFO "Enabling old style K6 write allocation for %d Mb\n", |
| 103 | mbytes); |
| 104 | } |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | if ((c->x86_model == 8 && c->x86_mask > 7) || |
| 109 | c->x86_model == 9 || c->x86_model == 13) { |
| 110 | /* The more serious chips .. */ |
| 111 | |
| 112 | if (mbytes > 4092) |
| 113 | mbytes = 4092; |
| 114 | |
| 115 | rdmsr(MSR_K6_WHCR, l, h); |
| 116 | if ((l&0xFFFF0000) == 0) { |
| 117 | unsigned long flags; |
| 118 | l = ((mbytes>>2)<<22)|(1<<16); |
| 119 | local_irq_save(flags); |
| 120 | wbinvd(); |
| 121 | wrmsr(MSR_K6_WHCR, l, h); |
| 122 | local_irq_restore(flags); |
| 123 | printk(KERN_INFO "Enabling new style K6 write allocation for %d Mb\n", |
| 124 | mbytes); |
| 125 | } |
| 126 | |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | if (c->x86_model == 10) { |
| 131 | /* AMD Geode LX is model 10 */ |
| 132 | /* placeholder for any needed mods */ |
| 133 | return; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void __cpuinit init_amd_k7(struct cpuinfo_x86 *c) |
| 138 | { |
| 139 | u32 l, h; |
| 140 | |
| 141 | /* |
| 142 | * Bit 15 of Athlon specific MSR 15, needs to be 0 |
| 143 | * to enable SSE on Palomino/Morgan/Barton CPU's. |
| 144 | * If the BIOS didn't enable it already, enable it here. |
| 145 | */ |
| 146 | if (c->x86_model >= 6 && c->x86_model <= 10) { |
| 147 | if (!cpu_has(c, X86_FEATURE_XMM)) { |
| 148 | printk(KERN_INFO "Enabling disabled K7/SSE Support.\n"); |
| 149 | rdmsr(MSR_K7_HWCR, l, h); |
| 150 | l &= ~0x00008000; |
| 151 | wrmsr(MSR_K7_HWCR, l, h); |
| 152 | set_cpu_cap(c, X86_FEATURE_XMM); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * It's been determined by AMD that Athlons since model 8 stepping 1 |
| 158 | * are more robust with CLK_CTL set to 200xxxxx instead of 600xxxxx |
| 159 | * As per AMD technical note 27212 0.2 |
| 160 | */ |
| 161 | if ((c->x86_model == 8 && c->x86_mask >= 1) || (c->x86_model > 8)) { |
| 162 | rdmsr(MSR_K7_CLK_CTL, l, h); |
| 163 | if ((l & 0xfff00000) != 0x20000000) { |
| 164 | printk ("CPU: CLK_CTL MSR was %x. Reprogramming to %x\n", l, |
| 165 | ((l & 0x000fffff)|0x20000000)); |
| 166 | wrmsr(MSR_K7_CLK_CTL, (l & 0x000fffff)|0x20000000, h); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | set_cpu_cap(c, X86_FEATURE_K7); |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * On a AMD dual core setup the lower bits of the APIC id distingush the cores. |
| 175 | * Assumes number of cores is a power of two. |
| 176 | */ |
| 177 | static void __cpuinit amd_detect_cmp(struct cpuinfo_x86 *c) |
| 178 | { |
| 179 | #ifdef CONFIG_X86_HT |
| 180 | unsigned bits; |
| 181 | |
| 182 | bits = c->x86_coreid_bits; |
| 183 | |
| 184 | /* Low order bits define the core id (index of core in socket) */ |
| 185 | c->cpu_core_id = c->initial_apicid & ((1 << bits)-1); |
| 186 | /* Convert the initial APIC ID into the socket ID */ |
| 187 | c->phys_proc_id = c->initial_apicid >> bits; |
| 188 | #endif |
| 189 | } |
| 190 | |
| 191 | static void __cpuinit early_init_amd_mc(struct cpuinfo_x86 *c) |
| 192 | { |
| 193 | #ifdef CONFIG_X86_HT |
| 194 | unsigned bits, ecx; |
| 195 | |
| 196 | /* Multi core CPU? */ |
| 197 | if (c->extended_cpuid_level < 0x80000008) |
| 198 | return; |
| 199 | |
| 200 | ecx = cpuid_ecx(0x80000008); |
| 201 | |
| 202 | c->x86_max_cores = (ecx & 0xff) + 1; |
| 203 | |
| 204 | /* CPU telling us the core id bits shift? */ |
| 205 | bits = (ecx >> 12) & 0xF; |
| 206 | |
| 207 | /* Otherwise recompute */ |
| 208 | if (bits == 0) { |
| 209 | while ((1 << bits) < c->x86_max_cores) |
| 210 | bits++; |
| 211 | } |
| 212 | |
| 213 | c->x86_coreid_bits = bits; |
| 214 | #endif |
| 215 | } |
| 216 | |
Thomas Petazzoni | 03ae576 | 2008-02-15 12:00:23 +0100 | [diff] [blame] | 217 | static void __cpuinit early_init_amd(struct cpuinfo_x86 *c) |
Andi Kleen | 2b16a23 | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 218 | { |
Yinghai Lu | 11fdd25 | 2008-09-07 17:58:50 -0700 | [diff] [blame^] | 219 | early_init_amd_mc(c); |
| 220 | |
Yinghai Lu | e322423 | 2008-09-06 01:52:28 -0700 | [diff] [blame] | 221 | if (c->x86_power & (1<<8)) |
| 222 | set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC); |
Yinghai Lu | 5fef55f | 2008-09-04 21:09:43 +0200 | [diff] [blame] | 223 | |
| 224 | /* Set MTRR capability flag if appropriate */ |
| 225 | if (c->x86_model == 13 || c->x86_model == 9 || |
| 226 | (c->x86_model == 8 && c->x86_mask >= 8)) |
| 227 | set_cpu_cap(c, X86_FEATURE_K6_MTRR); |
Andi Kleen | 2b16a23 | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Magnus Damm | b4af3f7 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 230 | static void __cpuinit init_amd(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
Andi Kleen | 7d318d7 | 2005-09-29 22:05:55 +0200 | [diff] [blame] | 232 | #ifdef CONFIG_SMP |
Andi Kleen | 3c92c2b | 2005-10-11 01:28:33 +0200 | [diff] [blame] | 233 | unsigned long long value; |
Andi Kleen | 7d318d7 | 2005-09-29 22:05:55 +0200 | [diff] [blame] | 234 | |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 235 | /* |
| 236 | * Disable TLB flush filter by setting HWCR.FFDIS on K8 |
Andi Kleen | 7d318d7 | 2005-09-29 22:05:55 +0200 | [diff] [blame] | 237 | * bit 6 of msr C001_0015 |
| 238 | * |
| 239 | * Errata 63 for SH-B3 steppings |
| 240 | * Errata 122 for all steppings (F+ have it disabled by default) |
| 241 | */ |
Yinghai Lu | 11fdd25 | 2008-09-07 17:58:50 -0700 | [diff] [blame^] | 242 | if (c->x86 == 0xf) { |
Andi Kleen | 7d318d7 | 2005-09-29 22:05:55 +0200 | [diff] [blame] | 243 | rdmsrl(MSR_K7_HWCR, value); |
| 244 | value |= 1 << 6; |
| 245 | wrmsrl(MSR_K7_HWCR, value); |
| 246 | } |
| 247 | #endif |
| 248 | |
Andi Kleen | 2b16a23 | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 249 | early_init_amd(c); |
| 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | /* |
| 252 | * FIXME: We should handle the K5 here. Set up the write |
| 253 | * range and also turn on MSR 83 bits 4 and 31 (write alloc, |
| 254 | * no bus pipeline) |
| 255 | */ |
| 256 | |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 257 | /* |
| 258 | * Bit 31 in normal CPUID used for nonstandard 3DNow ID; |
Ingo Molnar | 16282a8 | 2008-02-26 08:49:57 +0100 | [diff] [blame] | 259 | * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 260 | */ |
Ingo Molnar | 16282a8 | 2008-02-26 08:49:57 +0100 | [diff] [blame] | 261 | clear_cpu_cap(c, 0*32+31); |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 262 | |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 263 | switch (c->x86) { |
| 264 | case 4: |
Yinghai Lu | 11fdd25 | 2008-09-07 17:58:50 -0700 | [diff] [blame^] | 265 | init_amd_k5(c); |
| 266 | break; |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 267 | case 5: |
Yinghai Lu | 11fdd25 | 2008-09-07 17:58:50 -0700 | [diff] [blame^] | 268 | init_amd_k6(c); |
| 269 | break; |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 270 | case 6: /* An Athlon/Duron */ |
Yinghai Lu | 11fdd25 | 2008-09-07 17:58:50 -0700 | [diff] [blame^] | 271 | init_amd_k7(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | break; |
Andi Kleen | 67cddd9 | 2007-07-21 17:10:03 +0200 | [diff] [blame] | 273 | } |
Andi Kleen | 3556ddf | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 274 | |
Andi Kleen | c12ceb7 | 2007-05-21 14:31:47 +0200 | [diff] [blame] | 275 | /* K6s reports MCEs but don't actually have all the MSRs */ |
| 276 | if (c->x86 < 6) |
Ingo Molnar | 16282a8 | 2008-02-26 08:49:57 +0100 | [diff] [blame] | 277 | clear_cpu_cap(c, X86_FEATURE_MCE); |
Andi Kleen | de42186 | 2008-01-30 13:32:37 +0100 | [diff] [blame] | 278 | |
Yinghai Lu | 11fdd25 | 2008-09-07 17:58:50 -0700 | [diff] [blame^] | 279 | if (c->x86 >= 6) |
| 280 | set_cpu_cap(c, X86_FEATURE_FXSAVE_LEAK); |
| 281 | |
| 282 | if (!c->x86_model_id[0]) { |
| 283 | switch (c->x86) { |
| 284 | case 0xf: |
| 285 | /* Should distinguish Models here, but this is only |
| 286 | a fallback anyways. */ |
| 287 | strcpy(c->x86_model_id, "Hammer"); |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | display_cacheinfo(c); |
| 293 | |
| 294 | /* Multi core CPU? */ |
| 295 | if (c->extended_cpuid_level >= 0x80000008) |
| 296 | amd_detect_cmp(c); |
| 297 | |
| 298 | detect_ht(c); |
| 299 | |
| 300 | if (c->extended_cpuid_level >= 0x80000006) { |
| 301 | if ((c->x86 >= 0x0f) && (cpuid_edx(0x80000006) & 0xf000)) |
| 302 | num_cache_leaves = 4; |
| 303 | else |
| 304 | num_cache_leaves = 3; |
| 305 | } |
| 306 | |
| 307 | if (c->x86 >= 0xf && c->x86 <= 0x11) |
| 308 | set_cpu_cap(c, X86_FEATURE_K8); |
| 309 | |
| 310 | if (cpu_has_xmm2) { |
| 311 | /* MFENCE stops RDTSC speculation */ |
Ingo Molnar | 16282a8 | 2008-02-26 08:49:57 +0100 | [diff] [blame] | 312 | set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC); |
Yinghai Lu | 11fdd25 | 2008-09-07 17:58:50 -0700 | [diff] [blame^] | 313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 316 | static unsigned int __cpuinit amd_size_cache(struct cpuinfo_x86 *c, unsigned int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
| 318 | /* AMD errata T13 (order #21922) */ |
| 319 | if ((c->x86 == 6)) { |
| 320 | if (c->x86_model == 3 && c->x86_mask == 0) /* Duron Rev A0 */ |
| 321 | size = 64; |
| 322 | if (c->x86_model == 4 && |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 323 | (c->x86_mask == 0 || c->x86_mask == 1)) /* Tbird rev A1/A2 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | size = 256; |
| 325 | } |
| 326 | return size; |
| 327 | } |
| 328 | |
Magnus Damm | 9541493 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 329 | static struct cpu_dev amd_cpu_dev __cpuinitdata = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | .c_vendor = "AMD", |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 331 | .c_ident = { "AuthenticAMD" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | .c_models = { |
| 333 | { .vendor = X86_VENDOR_AMD, .family = 4, .model_names = |
| 334 | { |
| 335 | [3] = "486 DX/2", |
| 336 | [7] = "486 DX/2-WB", |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 337 | [8] = "486 DX/4", |
| 338 | [9] = "486 DX/4-WB", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | [14] = "Am5x86-WT", |
Paolo Ciarrocchi | fb87a29 | 2008-02-22 23:10:33 +0100 | [diff] [blame] | 340 | [15] = "Am5x86-WB" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | }, |
| 343 | }, |
Thomas Petazzoni | 03ae576 | 2008-02-15 12:00:23 +0100 | [diff] [blame] | 344 | .c_early_init = early_init_amd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | .c_init = init_amd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | .c_size_cache = amd_size_cache, |
Yinghai Lu | 10a434f | 2008-09-04 21:09:45 +0200 | [diff] [blame] | 347 | .c_x86_vendor = X86_VENDOR_AMD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | }; |
| 349 | |
Yinghai Lu | 10a434f | 2008-09-04 21:09:45 +0200 | [diff] [blame] | 350 | cpu_dev_register(amd_cpu_dev); |