amd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. #ifdef CONFIG_X86_64
  8. # include <asm/numa_64.h>
  9. # include <asm/mmconfig.h>
  10. # include <asm/cacheflush.h>
  11. #endif
  12. #include "cpu.h"
  13. #ifdef CONFIG_X86_32
  14. /*
  15. * B step AMD K6 before B 9730xxxx have hardware bugs that can cause
  16. * misexecution of code under Linux. Owners of such processors should
  17. * contact AMD for precise details and a CPU swap.
  18. *
  19. * See http://www.multimania.com/poulot/k6bug.html
  20. * http://www.amd.com/K6/k6docs/revgd.html
  21. *
  22. * The following test is erm.. interesting. AMD neglected to up
  23. * the chip setting when fixing the bug but they also tweaked some
  24. * performance at the same time..
  25. */
  26. extern void vide(void);
  27. __asm__(".align 4\nvide: ret");
  28. static void __cpuinit init_amd_k5(struct cpuinfo_x86 *c)
  29. {
  30. /*
  31. * General Systems BIOSen alias the cpu frequency registers
  32. * of the Elan at 0x000df000. Unfortuantly, one of the Linux
  33. * drivers subsequently pokes it, and changes the CPU speed.
  34. * Workaround : Remove the unneeded alias.
  35. */
  36. #define CBAR (0xfffc) /* Configuration Base Address (32-bit) */
  37. #define CBAR_ENB (0x80000000)
  38. #define CBAR_KEY (0X000000CB)
  39. if (c->x86_model == 9 || c->x86_model == 10) {
  40. if (inl (CBAR) & CBAR_ENB)
  41. outl (0 | CBAR_KEY, CBAR);
  42. }
  43. }
  44. static void __cpuinit init_amd_k6(struct cpuinfo_x86 *c)
  45. {
  46. u32 l, h;
  47. int mbytes = num_physpages >> (20-PAGE_SHIFT);
  48. if (c->x86_model < 6) {
  49. /* Based on AMD doc 20734R - June 2000 */
  50. if (c->x86_model == 0) {
  51. clear_cpu_cap(c, X86_FEATURE_APIC);
  52. set_cpu_cap(c, X86_FEATURE_PGE);
  53. }
  54. return;
  55. }
  56. if (c->x86_model == 6 && c->x86_mask == 1) {
  57. const int K6_BUG_LOOP = 1000000;
  58. int n;
  59. void (*f_vide)(void);
  60. unsigned long d, d2;
  61. printk(KERN_INFO "AMD K6 stepping B detected - ");
  62. /*
  63. * It looks like AMD fixed the 2.6.2 bug and improved indirect
  64. * calls at the same time.
  65. */
  66. n = K6_BUG_LOOP;
  67. f_vide = vide;
  68. rdtscl(d);
  69. while (n--)
  70. f_vide();
  71. rdtscl(d2);
  72. d = d2-d;
  73. if (d > 20*K6_BUG_LOOP)
  74. printk("system stability may be impaired when more than 32 MB are used.\n");
  75. else
  76. printk("probably OK (after B9730xxxx).\n");
  77. printk(KERN_INFO "Please see http://membres.lycos.fr/poulot/k6bug.html\n");
  78. }
  79. /* K6 with old style WHCR */
  80. if (c->x86_model < 8 ||
  81. (c->x86_model == 8 && c->x86_mask < 8)) {
  82. /* We can only write allocate on the low 508Mb */
  83. if (mbytes > 508)
  84. mbytes = 508;
  85. rdmsr(MSR_K6_WHCR, l, h);
  86. if ((l&0x0000FFFF) == 0) {
  87. unsigned long flags;
  88. l = (1<<0)|((mbytes/4)<<1);
  89. local_irq_save(flags);
  90. wbinvd();
  91. wrmsr(MSR_K6_WHCR, l, h);
  92. local_irq_restore(flags);
  93. printk(KERN_INFO "Enabling old style K6 write allocation for %d Mb\n",
  94. mbytes);
  95. }
  96. return;
  97. }
  98. if ((c->x86_model == 8 && c->x86_mask > 7) ||
  99. c->x86_model == 9 || c->x86_model == 13) {
  100. /* The more serious chips .. */
  101. if (mbytes > 4092)
  102. mbytes = 4092;
  103. rdmsr(MSR_K6_WHCR, l, h);
  104. if ((l&0xFFFF0000) == 0) {
  105. unsigned long flags;
  106. l = ((mbytes>>2)<<22)|(1<<16);
  107. local_irq_save(flags);
  108. wbinvd();
  109. wrmsr(MSR_K6_WHCR, l, h);
  110. local_irq_restore(flags);
  111. printk(KERN_INFO "Enabling new style K6 write allocation for %d Mb\n",
  112. mbytes);
  113. }
  114. return;
  115. }
  116. if (c->x86_model == 10) {
  117. /* AMD Geode LX is model 10 */
  118. /* placeholder for any needed mods */
  119. return;
  120. }
  121. }
  122. static void __cpuinit init_amd_k7(struct cpuinfo_x86 *c)
  123. {
  124. u32 l, h;
  125. /*
  126. * Bit 15 of Athlon specific MSR 15, needs to be 0
  127. * to enable SSE on Palomino/Morgan/Barton CPU's.
  128. * If the BIOS didn't enable it already, enable it here.
  129. */
  130. if (c->x86_model >= 6 && c->x86_model <= 10) {
  131. if (!cpu_has(c, X86_FEATURE_XMM)) {
  132. printk(KERN_INFO "Enabling disabled K7/SSE Support.\n");
  133. rdmsr(MSR_K7_HWCR, l, h);
  134. l &= ~0x00008000;
  135. wrmsr(MSR_K7_HWCR, l, h);
  136. set_cpu_cap(c, X86_FEATURE_XMM);
  137. }
  138. }
  139. /*
  140. * It's been determined by AMD that Athlons since model 8 stepping 1
  141. * are more robust with CLK_CTL set to 200xxxxx instead of 600xxxxx
  142. * As per AMD technical note 27212 0.2
  143. */
  144. if ((c->x86_model == 8 && c->x86_mask >= 1) || (c->x86_model > 8)) {
  145. rdmsr(MSR_K7_CLK_CTL, l, h);
  146. if ((l & 0xfff00000) != 0x20000000) {
  147. printk ("CPU: CLK_CTL MSR was %x. Reprogramming to %x\n", l,
  148. ((l & 0x000fffff)|0x20000000));
  149. wrmsr(MSR_K7_CLK_CTL, (l & 0x000fffff)|0x20000000, h);
  150. }
  151. }
  152. set_cpu_cap(c, X86_FEATURE_K7);
  153. }
  154. #endif
  155. #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
  156. static int __cpuinit nearby_node(int apicid)
  157. {
  158. int i, node;
  159. for (i = apicid - 1; i >= 0; i--) {
  160. node = apicid_to_node[i];
  161. if (node != NUMA_NO_NODE && node_online(node))
  162. return node;
  163. }
  164. for (i = apicid + 1; i < MAX_LOCAL_APIC; i++) {
  165. node = apicid_to_node[i];
  166. if (node != NUMA_NO_NODE && node_online(node))
  167. return node;
  168. }
  169. return first_node(node_online_map); /* Shouldn't happen */
  170. }
  171. #endif
  172. /*
  173. * On a AMD dual core setup the lower bits of the APIC id distingush the cores.
  174. * Assumes number of cores is a power of two.
  175. */
  176. static void __cpuinit amd_detect_cmp(struct cpuinfo_x86 *c)
  177. {
  178. #ifdef CONFIG_X86_HT
  179. unsigned bits;
  180. bits = c->x86_coreid_bits;
  181. /* Low order bits define the core id (index of core in socket) */
  182. c->cpu_core_id = c->initial_apicid & ((1 << bits)-1);
  183. /* Convert the initial APIC ID into the socket ID */
  184. c->phys_proc_id = c->initial_apicid >> bits;
  185. #endif
  186. }
  187. static void __cpuinit srat_detect_node(struct cpuinfo_x86 *c)
  188. {
  189. #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
  190. int cpu = smp_processor_id();
  191. int node;
  192. unsigned apicid = hard_smp_processor_id();
  193. node = c->phys_proc_id;
  194. if (apicid_to_node[apicid] != NUMA_NO_NODE)
  195. node = apicid_to_node[apicid];
  196. if (!node_online(node)) {
  197. /* Two possibilities here:
  198. - The CPU is missing memory and no node was created.
  199. In that case try picking one from a nearby CPU
  200. - The APIC IDs differ from the HyperTransport node IDs
  201. which the K8 northbridge parsing fills in.
  202. Assume they are all increased by a constant offset,
  203. but in the same order as the HT nodeids.
  204. If that doesn't result in a usable node fall back to the
  205. path for the previous case. */
  206. int ht_nodeid = c->initial_apicid;
  207. if (ht_nodeid >= 0 &&
  208. apicid_to_node[ht_nodeid] != NUMA_NO_NODE)
  209. node = apicid_to_node[ht_nodeid];
  210. /* Pick a nearby node */
  211. if (!node_online(node))
  212. node = nearby_node(apicid);
  213. }
  214. numa_set_node(cpu, node);
  215. printk(KERN_INFO "CPU %d/0x%x -> Node %d\n", cpu, apicid, node);
  216. #endif
  217. }
  218. static void __cpuinit early_init_amd_mc(struct cpuinfo_x86 *c)
  219. {
  220. #ifdef CONFIG_X86_HT
  221. unsigned bits, ecx;
  222. /* Multi core CPU? */
  223. if (c->extended_cpuid_level < 0x80000008)
  224. return;
  225. ecx = cpuid_ecx(0x80000008);
  226. c->x86_max_cores = (ecx & 0xff) + 1;
  227. /* CPU telling us the core id bits shift? */
  228. bits = (ecx >> 12) & 0xF;
  229. /* Otherwise recompute */
  230. if (bits == 0) {
  231. while ((1 << bits) < c->x86_max_cores)
  232. bits++;
  233. }
  234. c->x86_coreid_bits = bits;
  235. #endif
  236. }
  237. static void __cpuinit early_init_amd(struct cpuinfo_x86 *c)
  238. {
  239. early_init_amd_mc(c);
  240. /*
  241. * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
  242. * with P/T states and does not stop in deep C-states
  243. */
  244. if (c->x86_power & (1 << 8)) {
  245. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  246. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
  247. }
  248. #ifdef CONFIG_X86_64
  249. set_cpu_cap(c, X86_FEATURE_SYSCALL32);
  250. #else
  251. /* Set MTRR capability flag if appropriate */
  252. if (c->x86 == 5)
  253. if (c->x86_model == 13 || c->x86_model == 9 ||
  254. (c->x86_model == 8 && c->x86_mask >= 8))
  255. set_cpu_cap(c, X86_FEATURE_K6_MTRR);
  256. #endif
  257. }
  258. static void __cpuinit init_amd(struct cpuinfo_x86 *c)
  259. {
  260. #ifdef CONFIG_SMP
  261. unsigned long long value;
  262. /*
  263. * Disable TLB flush filter by setting HWCR.FFDIS on K8
  264. * bit 6 of msr C001_0015
  265. *
  266. * Errata 63 for SH-B3 steppings
  267. * Errata 122 for all steppings (F+ have it disabled by default)
  268. */
  269. if (c->x86 == 0xf) {
  270. rdmsrl(MSR_K7_HWCR, value);
  271. value |= 1 << 6;
  272. wrmsrl(MSR_K7_HWCR, value);
  273. }
  274. #endif
  275. early_init_amd(c);
  276. /*
  277. * Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  278. * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway
  279. */
  280. clear_cpu_cap(c, 0*32+31);
  281. #ifdef CONFIG_X86_64
  282. /* On C+ stepping K8 rep microcode works well for copy/memset */
  283. if (c->x86 == 0xf) {
  284. u32 level;
  285. level = cpuid_eax(1);
  286. if((level >= 0x0f48 && level < 0x0f50) || level >= 0x0f58)
  287. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  288. }
  289. if (c->x86 == 0x10 || c->x86 == 0x11)
  290. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  291. #else
  292. /*
  293. * FIXME: We should handle the K5 here. Set up the write
  294. * range and also turn on MSR 83 bits 4 and 31 (write alloc,
  295. * no bus pipeline)
  296. */
  297. switch (c->x86) {
  298. case 4:
  299. init_amd_k5(c);
  300. break;
  301. case 5:
  302. init_amd_k6(c);
  303. break;
  304. case 6: /* An Athlon/Duron */
  305. init_amd_k7(c);
  306. break;
  307. }
  308. /* K6s reports MCEs but don't actually have all the MSRs */
  309. if (c->x86 < 6)
  310. clear_cpu_cap(c, X86_FEATURE_MCE);
  311. #endif
  312. /* Enable workaround for FXSAVE leak */
  313. if (c->x86 >= 6)
  314. set_cpu_cap(c, X86_FEATURE_FXSAVE_LEAK);
  315. if (!c->x86_model_id[0]) {
  316. switch (c->x86) {
  317. case 0xf:
  318. /* Should distinguish Models here, but this is only
  319. a fallback anyways. */
  320. strcpy(c->x86_model_id, "Hammer");
  321. break;
  322. }
  323. }
  324. display_cacheinfo(c);
  325. /* Multi core CPU? */
  326. if (c->extended_cpuid_level >= 0x80000008) {
  327. amd_detect_cmp(c);
  328. srat_detect_node(c);
  329. }
  330. #ifdef CONFIG_X86_32
  331. detect_ht(c);
  332. #endif
  333. if (c->extended_cpuid_level >= 0x80000006) {
  334. if ((c->x86 >= 0x0f) && (cpuid_edx(0x80000006) & 0xf000))
  335. num_cache_leaves = 4;
  336. else
  337. num_cache_leaves = 3;
  338. }
  339. if (c->x86 >= 0xf && c->x86 <= 0x11)
  340. set_cpu_cap(c, X86_FEATURE_K8);
  341. if (cpu_has_xmm2) {
  342. /* MFENCE stops RDTSC speculation */
  343. set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC);
  344. }
  345. #ifdef CONFIG_X86_64
  346. if (c->x86 == 0x10) {
  347. /* do this for boot cpu */
  348. if (c == &boot_cpu_data)
  349. check_enable_amd_mmconf_dmi();
  350. fam10h_check_enable_mmcfg();
  351. }
  352. if (c == &boot_cpu_data && c->x86 >= 0xf && c->x86 <= 0x11) {
  353. unsigned long long tseg;
  354. /*
  355. * Split up direct mapping around the TSEG SMM area.
  356. * Don't do it for gbpages because there seems very little
  357. * benefit in doing so.
  358. */
  359. if (!rdmsrl_safe(MSR_K8_TSEG_ADDR, &tseg)) {
  360. printk(KERN_DEBUG "tseg: %010llx\n", tseg);
  361. if ((tseg>>PMD_SHIFT) <
  362. (max_low_pfn_mapped>>(PMD_SHIFT-PAGE_SHIFT)) ||
  363. ((tseg>>PMD_SHIFT) <
  364. (max_pfn_mapped>>(PMD_SHIFT-PAGE_SHIFT)) &&
  365. (tseg>>PMD_SHIFT) >= (1ULL<<(32 - PMD_SHIFT))))
  366. set_memory_4k((unsigned long)__va(tseg), 1);
  367. }
  368. }
  369. #endif
  370. }
  371. #ifdef CONFIG_X86_32
  372. static unsigned int __cpuinit amd_size_cache(struct cpuinfo_x86 *c, unsigned int size)
  373. {
  374. /* AMD errata T13 (order #21922) */
  375. if ((c->x86 == 6)) {
  376. if (c->x86_model == 3 && c->x86_mask == 0) /* Duron Rev A0 */
  377. size = 64;
  378. if (c->x86_model == 4 &&
  379. (c->x86_mask == 0 || c->x86_mask == 1)) /* Tbird rev A1/A2 */
  380. size = 256;
  381. }
  382. return size;
  383. }
  384. #endif
  385. static struct cpu_dev amd_cpu_dev __cpuinitdata = {
  386. .c_vendor = "AMD",
  387. .c_ident = { "AuthenticAMD" },
  388. #ifdef CONFIG_X86_32
  389. .c_models = {
  390. { .vendor = X86_VENDOR_AMD, .family = 4, .model_names =
  391. {
  392. [3] = "486 DX/2",
  393. [7] = "486 DX/2-WB",
  394. [8] = "486 DX/4",
  395. [9] = "486 DX/4-WB",
  396. [14] = "Am5x86-WT",
  397. [15] = "Am5x86-WB"
  398. }
  399. },
  400. },
  401. .c_size_cache = amd_size_cache,
  402. #endif
  403. .c_early_init = early_init_amd,
  404. .c_init = init_amd,
  405. .c_x86_vendor = X86_VENDOR_AMD,
  406. };
  407. cpu_dev_register(amd_cpu_dev);