amd.c 15 KB

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