amd.c 8.7 KB

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