amd.c 13 KB

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