amd.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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>
  6. #include <asm/apic.h>
  7. #include "cpu.h"
  8. /*
  9. * B step AMD K6 before B 9730xxxx have hardware bugs that can cause
  10. * misexecution of code under Linux. Owners of such processors should
  11. * contact AMD for precise details and a CPU swap.
  12. *
  13. * See http://www.multimania.com/poulot/k6bug.html
  14. * http://www.amd.com/K6/k6docs/revgd.html
  15. *
  16. * The following test is erm.. interesting. AMD neglected to up
  17. * the chip setting when fixing the bug but they also tweaked some
  18. * performance at the same time..
  19. */
  20. extern void vide(void);
  21. __asm__(".align 4\nvide: ret");
  22. #ifdef CONFIG_X86_LOCAL_APIC
  23. #define ENABLE_C1E_MASK 0x18000000
  24. #define CPUID_PROCESSOR_SIGNATURE 1
  25. #define CPUID_XFAM 0x0ff00000
  26. #define CPUID_XFAM_K8 0x00000000
  27. #define CPUID_XFAM_10H 0x00100000
  28. #define CPUID_XFAM_11H 0x00200000
  29. #define CPUID_XMOD 0x000f0000
  30. #define CPUID_XMOD_REV_F 0x00040000
  31. /* AMD systems with C1E don't have a working lAPIC timer. Check for that. */
  32. static __cpuinit int amd_apic_timer_broken(void)
  33. {
  34. u32 lo, hi;
  35. u32 eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
  36. switch (eax & CPUID_XFAM) {
  37. case CPUID_XFAM_K8:
  38. if ((eax & CPUID_XMOD) < CPUID_XMOD_REV_F)
  39. break;
  40. case CPUID_XFAM_10H:
  41. case CPUID_XFAM_11H:
  42. rdmsr(MSR_K8_ENABLE_C1E, lo, hi);
  43. if (lo & ENABLE_C1E_MASK)
  44. return 1;
  45. break;
  46. default:
  47. /* err on the side of caution */
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. #endif
  53. int force_mwait __cpuinitdata;
  54. static void __cpuinit init_amd(struct cpuinfo_x86 *c)
  55. {
  56. u32 l, h;
  57. int mbytes = num_physpages >> (20-PAGE_SHIFT);
  58. int r;
  59. #ifdef CONFIG_SMP
  60. unsigned long long value;
  61. /* Disable TLB flush filter by setting HWCR.FFDIS on K8
  62. * bit 6 of msr C001_0015
  63. *
  64. * Errata 63 for SH-B3 steppings
  65. * Errata 122 for all steppings (F+ have it disabled by default)
  66. */
  67. if (c->x86 == 15) {
  68. rdmsrl(MSR_K7_HWCR, value);
  69. value |= 1 << 6;
  70. wrmsrl(MSR_K7_HWCR, value);
  71. }
  72. #endif
  73. /*
  74. * FIXME: We should handle the K5 here. Set up the write
  75. * range and also turn on MSR 83 bits 4 and 31 (write alloc,
  76. * no bus pipeline)
  77. */
  78. /* Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  79. 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway */
  80. clear_bit(0*32+31, c->x86_capability);
  81. r = get_model_name(c);
  82. switch(c->x86)
  83. {
  84. case 4:
  85. /*
  86. * General Systems BIOSen alias the cpu frequency registers
  87. * of the Elan at 0x000df000. Unfortuantly, one of the Linux
  88. * drivers subsequently pokes it, and changes the CPU speed.
  89. * Workaround : Remove the unneeded alias.
  90. */
  91. #define CBAR (0xfffc) /* Configuration Base Address (32-bit) */
  92. #define CBAR_ENB (0x80000000)
  93. #define CBAR_KEY (0X000000CB)
  94. if (c->x86_model==9 || c->x86_model == 10) {
  95. if (inl (CBAR) & CBAR_ENB)
  96. outl (0 | CBAR_KEY, CBAR);
  97. }
  98. break;
  99. case 5:
  100. if( c->x86_model < 6 )
  101. {
  102. /* Based on AMD doc 20734R - June 2000 */
  103. if ( c->x86_model == 0 ) {
  104. clear_bit(X86_FEATURE_APIC, c->x86_capability);
  105. set_bit(X86_FEATURE_PGE, c->x86_capability);
  106. }
  107. break;
  108. }
  109. if ( c->x86_model == 6 && c->x86_mask == 1 ) {
  110. const int K6_BUG_LOOP = 1000000;
  111. int n;
  112. void (*f_vide)(void);
  113. unsigned long d, d2;
  114. printk(KERN_INFO "AMD K6 stepping B detected - ");
  115. /*
  116. * It looks like AMD fixed the 2.6.2 bug and improved indirect
  117. * calls at the same time.
  118. */
  119. n = K6_BUG_LOOP;
  120. f_vide = vide;
  121. rdtscl(d);
  122. while (n--)
  123. f_vide();
  124. rdtscl(d2);
  125. d = d2-d;
  126. if (d > 20*K6_BUG_LOOP)
  127. printk("system stability may be impaired when more than 32 MB are used.\n");
  128. else
  129. printk("probably OK (after B9730xxxx).\n");
  130. printk(KERN_INFO "Please see http://membres.lycos.fr/poulot/k6bug.html\n");
  131. }
  132. /* K6 with old style WHCR */
  133. if (c->x86_model < 8 ||
  134. (c->x86_model== 8 && c->x86_mask < 8)) {
  135. /* We can only write allocate on the low 508Mb */
  136. if(mbytes>508)
  137. mbytes=508;
  138. rdmsr(MSR_K6_WHCR, l, h);
  139. if ((l&0x0000FFFF)==0) {
  140. unsigned long flags;
  141. l=(1<<0)|((mbytes/4)<<1);
  142. local_irq_save(flags);
  143. wbinvd();
  144. wrmsr(MSR_K6_WHCR, l, h);
  145. local_irq_restore(flags);
  146. printk(KERN_INFO "Enabling old style K6 write allocation for %d Mb\n",
  147. mbytes);
  148. }
  149. break;
  150. }
  151. if ((c->x86_model == 8 && c->x86_mask >7) ||
  152. c->x86_model == 9 || c->x86_model == 13) {
  153. /* The more serious chips .. */
  154. if(mbytes>4092)
  155. mbytes=4092;
  156. rdmsr(MSR_K6_WHCR, l, h);
  157. if ((l&0xFFFF0000)==0) {
  158. unsigned long flags;
  159. l=((mbytes>>2)<<22)|(1<<16);
  160. local_irq_save(flags);
  161. wbinvd();
  162. wrmsr(MSR_K6_WHCR, l, h);
  163. local_irq_restore(flags);
  164. printk(KERN_INFO "Enabling new style K6 write allocation for %d Mb\n",
  165. mbytes);
  166. }
  167. /* Set MTRR capability flag if appropriate */
  168. if (c->x86_model == 13 || c->x86_model == 9 ||
  169. (c->x86_model == 8 && c->x86_mask >= 8))
  170. set_bit(X86_FEATURE_K6_MTRR, c->x86_capability);
  171. break;
  172. }
  173. if (c->x86_model == 10) {
  174. /* AMD Geode LX is model 10 */
  175. /* placeholder for any needed mods */
  176. break;
  177. }
  178. break;
  179. case 6: /* An Athlon/Duron */
  180. /* Bit 15 of Athlon specific MSR 15, needs to be 0
  181. * to enable SSE on Palomino/Morgan/Barton CPU's.
  182. * If the BIOS didn't enable it already, enable it here.
  183. */
  184. if (c->x86_model >= 6 && c->x86_model <= 10) {
  185. if (!cpu_has(c, X86_FEATURE_XMM)) {
  186. printk(KERN_INFO "Enabling disabled K7/SSE Support.\n");
  187. rdmsr(MSR_K7_HWCR, l, h);
  188. l &= ~0x00008000;
  189. wrmsr(MSR_K7_HWCR, l, h);
  190. set_bit(X86_FEATURE_XMM, c->x86_capability);
  191. }
  192. }
  193. /* It's been determined by AMD that Athlons since model 8 stepping 1
  194. * are more robust with CLK_CTL set to 200xxxxx instead of 600xxxxx
  195. * As per AMD technical note 27212 0.2
  196. */
  197. if ((c->x86_model == 8 && c->x86_mask>=1) || (c->x86_model > 8)) {
  198. rdmsr(MSR_K7_CLK_CTL, l, h);
  199. if ((l & 0xfff00000) != 0x20000000) {
  200. printk ("CPU: CLK_CTL MSR was %x. Reprogramming to %x\n", l,
  201. ((l & 0x000fffff)|0x20000000));
  202. wrmsr(MSR_K7_CLK_CTL, (l & 0x000fffff)|0x20000000, h);
  203. }
  204. }
  205. break;
  206. }
  207. switch (c->x86) {
  208. case 15:
  209. /* Use K8 tuning for Fam10h and Fam11h */
  210. case 0x10:
  211. case 0x11:
  212. set_bit(X86_FEATURE_K8, c->x86_capability);
  213. break;
  214. case 6:
  215. set_bit(X86_FEATURE_K7, c->x86_capability);
  216. break;
  217. }
  218. if (c->x86 >= 6)
  219. set_bit(X86_FEATURE_FXSAVE_LEAK, c->x86_capability);
  220. display_cacheinfo(c);
  221. if (cpuid_eax(0x80000000) >= 0x80000008) {
  222. c->x86_max_cores = (cpuid_ecx(0x80000008) & 0xff) + 1;
  223. }
  224. if (cpuid_eax(0x80000000) >= 0x80000007) {
  225. c->x86_power = cpuid_edx(0x80000007);
  226. if (c->x86_power & (1<<8))
  227. set_bit(X86_FEATURE_CONSTANT_TSC, c->x86_capability);
  228. }
  229. #ifdef CONFIG_X86_HT
  230. /*
  231. * On a AMD multi core setup the lower bits of the APIC id
  232. * distingush the cores.
  233. */
  234. if (c->x86_max_cores > 1) {
  235. int cpu = smp_processor_id();
  236. unsigned bits = (cpuid_ecx(0x80000008) >> 12) & 0xf;
  237. if (bits == 0) {
  238. while ((1 << bits) < c->x86_max_cores)
  239. bits++;
  240. }
  241. c->cpu_core_id = c->phys_proc_id & ((1<<bits)-1);
  242. c->phys_proc_id >>= bits;
  243. printk(KERN_INFO "CPU %d(%d) -> Core %d\n",
  244. cpu, c->x86_max_cores, c->cpu_core_id);
  245. }
  246. #endif
  247. if (cpuid_eax(0x80000000) >= 0x80000006) {
  248. if ((c->x86 == 0x10) && (cpuid_edx(0x80000006) & 0xf000))
  249. num_cache_leaves = 4;
  250. else
  251. num_cache_leaves = 3;
  252. }
  253. #ifdef CONFIG_X86_LOCAL_APIC
  254. if (amd_apic_timer_broken())
  255. local_apic_timer_disabled = 1;
  256. #endif
  257. if (c->x86 == 0x10 && !force_mwait)
  258. clear_bit(X86_FEATURE_MWAIT, c->x86_capability);
  259. /* K6s reports MCEs but don't actually have all the MSRs */
  260. if (c->x86 < 6)
  261. clear_bit(X86_FEATURE_MCE, c->x86_capability);
  262. }
  263. static unsigned int __cpuinit amd_size_cache(struct cpuinfo_x86 * c, unsigned int size)
  264. {
  265. /* AMD errata T13 (order #21922) */
  266. if ((c->x86 == 6)) {
  267. if (c->x86_model == 3 && c->x86_mask == 0) /* Duron Rev A0 */
  268. size = 64;
  269. if (c->x86_model == 4 &&
  270. (c->x86_mask==0 || c->x86_mask==1)) /* Tbird rev A1/A2 */
  271. size = 256;
  272. }
  273. return size;
  274. }
  275. static struct cpu_dev amd_cpu_dev __cpuinitdata = {
  276. .c_vendor = "AMD",
  277. .c_ident = { "AuthenticAMD" },
  278. .c_models = {
  279. { .vendor = X86_VENDOR_AMD, .family = 4, .model_names =
  280. {
  281. [3] = "486 DX/2",
  282. [7] = "486 DX/2-WB",
  283. [8] = "486 DX/4",
  284. [9] = "486 DX/4-WB",
  285. [14] = "Am5x86-WT",
  286. [15] = "Am5x86-WB"
  287. }
  288. },
  289. },
  290. .c_init = init_amd,
  291. .c_size_cache = amd_size_cache,
  292. };
  293. int __init amd_init_cpu(void)
  294. {
  295. cpu_devs[X86_VENDOR_AMD] = &amd_cpu_dev;
  296. return 0;
  297. }