amd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. #include <linux/export.h>
  2. #include <linux/init.h>
  3. #include <linux/bitops.h>
  4. #include <linux/elf.h>
  5. #include <linux/mm.h>
  6. #include <linux/io.h>
  7. #include <linux/sched.h>
  8. #include <asm/processor.h>
  9. #include <asm/apic.h>
  10. #include <asm/cpu.h>
  11. #include <asm/pci-direct.h>
  12. #ifdef CONFIG_X86_64
  13. # include <asm/mmconfig.h>
  14. # include <asm/cacheflush.h>
  15. #endif
  16. #include "cpu.h"
  17. static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
  18. {
  19. u32 gprs[8] = { 0 };
  20. int err;
  21. WARN_ONCE((boot_cpu_data.x86 != 0xf),
  22. "%s should only be used on K8!\n", __func__);
  23. gprs[1] = msr;
  24. gprs[7] = 0x9c5a203a;
  25. err = rdmsr_safe_regs(gprs);
  26. *p = gprs[0] | ((u64)gprs[2] << 32);
  27. return err;
  28. }
  29. static inline int wrmsrl_amd_safe(unsigned msr, unsigned long long val)
  30. {
  31. u32 gprs[8] = { 0 };
  32. WARN_ONCE((boot_cpu_data.x86 != 0xf),
  33. "%s should only be used on K8!\n", __func__);
  34. gprs[0] = (u32)val;
  35. gprs[1] = msr;
  36. gprs[2] = val >> 32;
  37. gprs[7] = 0x9c5a203a;
  38. return wrmsr_safe_regs(gprs);
  39. }
  40. #ifdef CONFIG_X86_32
  41. /*
  42. * B step AMD K6 before B 9730xxxx have hardware bugs that can cause
  43. * misexecution of code under Linux. Owners of such processors should
  44. * contact AMD for precise details and a CPU swap.
  45. *
  46. * See http://www.multimania.com/poulot/k6bug.html
  47. * and section 2.6.2 of "AMD-K6 Processor Revision Guide - Model 6"
  48. * (Publication # 21266 Issue Date: August 1998)
  49. *
  50. * The following test is erm.. interesting. AMD neglected to up
  51. * the chip setting when fixing the bug but they also tweaked some
  52. * performance at the same time..
  53. */
  54. extern void vide(void);
  55. __asm__(".align 4\nvide: ret");
  56. static void __cpuinit init_amd_k5(struct cpuinfo_x86 *c)
  57. {
  58. /*
  59. * General Systems BIOSen alias the cpu frequency registers
  60. * of the Elan at 0x000df000. Unfortuantly, one of the Linux
  61. * drivers subsequently pokes it, and changes the CPU speed.
  62. * Workaround : Remove the unneeded alias.
  63. */
  64. #define CBAR (0xfffc) /* Configuration Base Address (32-bit) */
  65. #define CBAR_ENB (0x80000000)
  66. #define CBAR_KEY (0X000000CB)
  67. if (c->x86_model == 9 || c->x86_model == 10) {
  68. if (inl(CBAR) & CBAR_ENB)
  69. outl(0 | CBAR_KEY, CBAR);
  70. }
  71. }
  72. static void __cpuinit init_amd_k6(struct cpuinfo_x86 *c)
  73. {
  74. u32 l, h;
  75. int mbytes = num_physpages >> (20-PAGE_SHIFT);
  76. if (c->x86_model < 6) {
  77. /* Based on AMD doc 20734R - June 2000 */
  78. if (c->x86_model == 0) {
  79. clear_cpu_cap(c, X86_FEATURE_APIC);
  80. set_cpu_cap(c, X86_FEATURE_PGE);
  81. }
  82. return;
  83. }
  84. if (c->x86_model == 6 && c->x86_mask == 1) {
  85. const int K6_BUG_LOOP = 1000000;
  86. int n;
  87. void (*f_vide)(void);
  88. unsigned long d, d2;
  89. printk(KERN_INFO "AMD K6 stepping B detected - ");
  90. /*
  91. * It looks like AMD fixed the 2.6.2 bug and improved indirect
  92. * calls at the same time.
  93. */
  94. n = K6_BUG_LOOP;
  95. f_vide = vide;
  96. rdtscl(d);
  97. while (n--)
  98. f_vide();
  99. rdtscl(d2);
  100. d = d2-d;
  101. if (d > 20*K6_BUG_LOOP)
  102. printk(KERN_CONT
  103. "system stability may be impaired when more than 32 MB are used.\n");
  104. else
  105. printk(KERN_CONT "probably OK (after B9730xxxx).\n");
  106. }
  107. /* K6 with old style WHCR */
  108. if (c->x86_model < 8 ||
  109. (c->x86_model == 8 && c->x86_mask < 8)) {
  110. /* We can only write allocate on the low 508Mb */
  111. if (mbytes > 508)
  112. mbytes = 508;
  113. rdmsr(MSR_K6_WHCR, l, h);
  114. if ((l&0x0000FFFF) == 0) {
  115. unsigned long flags;
  116. l = (1<<0)|((mbytes/4)<<1);
  117. local_irq_save(flags);
  118. wbinvd();
  119. wrmsr(MSR_K6_WHCR, l, h);
  120. local_irq_restore(flags);
  121. printk(KERN_INFO "Enabling old style K6 write allocation for %d Mb\n",
  122. mbytes);
  123. }
  124. return;
  125. }
  126. if ((c->x86_model == 8 && c->x86_mask > 7) ||
  127. c->x86_model == 9 || c->x86_model == 13) {
  128. /* The more serious chips .. */
  129. if (mbytes > 4092)
  130. mbytes = 4092;
  131. rdmsr(MSR_K6_WHCR, l, h);
  132. if ((l&0xFFFF0000) == 0) {
  133. unsigned long flags;
  134. l = ((mbytes>>2)<<22)|(1<<16);
  135. local_irq_save(flags);
  136. wbinvd();
  137. wrmsr(MSR_K6_WHCR, l, h);
  138. local_irq_restore(flags);
  139. printk(KERN_INFO "Enabling new style K6 write allocation for %d Mb\n",
  140. mbytes);
  141. }
  142. return;
  143. }
  144. if (c->x86_model == 10) {
  145. /* AMD Geode LX is model 10 */
  146. /* placeholder for any needed mods */
  147. return;
  148. }
  149. }
  150. static void __cpuinit amd_k7_smp_check(struct cpuinfo_x86 *c)
  151. {
  152. /* calling is from identify_secondary_cpu() ? */
  153. if (!c->cpu_index)
  154. return;
  155. /*
  156. * Certain Athlons might work (for various values of 'work') in SMP
  157. * but they are not certified as MP capable.
  158. */
  159. /* Athlon 660/661 is valid. */
  160. if ((c->x86_model == 6) && ((c->x86_mask == 0) ||
  161. (c->x86_mask == 1)))
  162. return;
  163. /* Duron 670 is valid */
  164. if ((c->x86_model == 7) && (c->x86_mask == 0))
  165. return;
  166. /*
  167. * Athlon 662, Duron 671, and Athlon >model 7 have capability
  168. * bit. It's worth noting that the A5 stepping (662) of some
  169. * Athlon XP's have the MP bit set.
  170. * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for
  171. * more.
  172. */
  173. if (((c->x86_model == 6) && (c->x86_mask >= 2)) ||
  174. ((c->x86_model == 7) && (c->x86_mask >= 1)) ||
  175. (c->x86_model > 7))
  176. if (cpu_has_mp)
  177. return;
  178. /* If we get here, not a certified SMP capable AMD system. */
  179. /*
  180. * Don't taint if we are running SMP kernel on a single non-MP
  181. * approved Athlon
  182. */
  183. WARN_ONCE(1, "WARNING: This combination of AMD"
  184. " processors is not suitable for SMP.\n");
  185. add_taint(TAINT_UNSAFE_SMP, LOCKDEP_NOW_UNRELIABLE);
  186. }
  187. static void __cpuinit init_amd_k7(struct cpuinfo_x86 *c)
  188. {
  189. u32 l, h;
  190. /*
  191. * Bit 15 of Athlon specific MSR 15, needs to be 0
  192. * to enable SSE on Palomino/Morgan/Barton CPU's.
  193. * If the BIOS didn't enable it already, enable it here.
  194. */
  195. if (c->x86_model >= 6 && c->x86_model <= 10) {
  196. if (!cpu_has(c, X86_FEATURE_XMM)) {
  197. printk(KERN_INFO "Enabling disabled K7/SSE Support.\n");
  198. rdmsr(MSR_K7_HWCR, l, h);
  199. l &= ~0x00008000;
  200. wrmsr(MSR_K7_HWCR, l, h);
  201. set_cpu_cap(c, X86_FEATURE_XMM);
  202. }
  203. }
  204. /*
  205. * It's been determined by AMD that Athlons since model 8 stepping 1
  206. * are more robust with CLK_CTL set to 200xxxxx instead of 600xxxxx
  207. * As per AMD technical note 27212 0.2
  208. */
  209. if ((c->x86_model == 8 && c->x86_mask >= 1) || (c->x86_model > 8)) {
  210. rdmsr(MSR_K7_CLK_CTL, l, h);
  211. if ((l & 0xfff00000) != 0x20000000) {
  212. printk(KERN_INFO
  213. "CPU: CLK_CTL MSR was %x. Reprogramming to %x\n",
  214. l, ((l & 0x000fffff)|0x20000000));
  215. wrmsr(MSR_K7_CLK_CTL, (l & 0x000fffff)|0x20000000, h);
  216. }
  217. }
  218. set_cpu_cap(c, X86_FEATURE_K7);
  219. amd_k7_smp_check(c);
  220. }
  221. #endif
  222. #ifdef CONFIG_NUMA
  223. /*
  224. * To workaround broken NUMA config. Read the comment in
  225. * srat_detect_node().
  226. */
  227. static int __cpuinit nearby_node(int apicid)
  228. {
  229. int i, node;
  230. for (i = apicid - 1; i >= 0; i--) {
  231. node = __apicid_to_node[i];
  232. if (node != NUMA_NO_NODE && node_online(node))
  233. return node;
  234. }
  235. for (i = apicid + 1; i < MAX_LOCAL_APIC; i++) {
  236. node = __apicid_to_node[i];
  237. if (node != NUMA_NO_NODE && node_online(node))
  238. return node;
  239. }
  240. return first_node(node_online_map); /* Shouldn't happen */
  241. }
  242. #endif
  243. /*
  244. * Fixup core topology information for
  245. * (1) AMD multi-node processors
  246. * Assumption: Number of cores in each internal node is the same.
  247. * (2) AMD processors supporting compute units
  248. */
  249. #ifdef CONFIG_X86_HT
  250. static void __cpuinit amd_get_topology(struct cpuinfo_x86 *c)
  251. {
  252. u32 nodes, cores_per_cu = 1;
  253. u8 node_id;
  254. int cpu = smp_processor_id();
  255. /* get information required for multi-node processors */
  256. if (cpu_has_topoext) {
  257. u32 eax, ebx, ecx, edx;
  258. cpuid(0x8000001e, &eax, &ebx, &ecx, &edx);
  259. nodes = ((ecx >> 8) & 7) + 1;
  260. node_id = ecx & 7;
  261. /* get compute unit information */
  262. smp_num_siblings = ((ebx >> 8) & 3) + 1;
  263. c->compute_unit_id = ebx & 0xff;
  264. cores_per_cu += ((ebx >> 8) & 3);
  265. } else if (cpu_has(c, X86_FEATURE_NODEID_MSR)) {
  266. u64 value;
  267. rdmsrl(MSR_FAM10H_NODE_ID, value);
  268. nodes = ((value >> 3) & 7) + 1;
  269. node_id = value & 7;
  270. } else
  271. return;
  272. /* fixup multi-node processor information */
  273. if (nodes > 1) {
  274. u32 cores_per_node;
  275. u32 cus_per_node;
  276. set_cpu_cap(c, X86_FEATURE_AMD_DCM);
  277. cores_per_node = c->x86_max_cores / nodes;
  278. cus_per_node = cores_per_node / cores_per_cu;
  279. /* store NodeID, use llc_shared_map to store sibling info */
  280. per_cpu(cpu_llc_id, cpu) = node_id;
  281. /* core id has to be in the [0 .. cores_per_node - 1] range */
  282. c->cpu_core_id %= cores_per_node;
  283. c->compute_unit_id %= cus_per_node;
  284. }
  285. }
  286. #endif
  287. /*
  288. * On a AMD dual core setup the lower bits of the APIC id distingush the cores.
  289. * Assumes number of cores is a power of two.
  290. */
  291. static void __cpuinit amd_detect_cmp(struct cpuinfo_x86 *c)
  292. {
  293. #ifdef CONFIG_X86_HT
  294. unsigned bits;
  295. int cpu = smp_processor_id();
  296. bits = c->x86_coreid_bits;
  297. /* Low order bits define the core id (index of core in socket) */
  298. c->cpu_core_id = c->initial_apicid & ((1 << bits)-1);
  299. /* Convert the initial APIC ID into the socket ID */
  300. c->phys_proc_id = c->initial_apicid >> bits;
  301. /* use socket ID also for last level cache */
  302. per_cpu(cpu_llc_id, cpu) = c->phys_proc_id;
  303. amd_get_topology(c);
  304. #endif
  305. }
  306. u16 amd_get_nb_id(int cpu)
  307. {
  308. u16 id = 0;
  309. #ifdef CONFIG_SMP
  310. id = per_cpu(cpu_llc_id, cpu);
  311. #endif
  312. return id;
  313. }
  314. EXPORT_SYMBOL_GPL(amd_get_nb_id);
  315. static void __cpuinit srat_detect_node(struct cpuinfo_x86 *c)
  316. {
  317. #ifdef CONFIG_NUMA
  318. int cpu = smp_processor_id();
  319. int node;
  320. unsigned apicid = c->apicid;
  321. node = numa_cpu_node(cpu);
  322. if (node == NUMA_NO_NODE)
  323. node = per_cpu(cpu_llc_id, cpu);
  324. /*
  325. * On multi-fabric platform (e.g. Numascale NumaChip) a
  326. * platform-specific handler needs to be called to fixup some
  327. * IDs of the CPU.
  328. */
  329. if (x86_cpuinit.fixup_cpu_id)
  330. x86_cpuinit.fixup_cpu_id(c, node);
  331. if (!node_online(node)) {
  332. /*
  333. * Two possibilities here:
  334. *
  335. * - The CPU is missing memory and no node was created. In
  336. * that case try picking one from a nearby CPU.
  337. *
  338. * - The APIC IDs differ from the HyperTransport node IDs
  339. * which the K8 northbridge parsing fills in. Assume
  340. * they are all increased by a constant offset, but in
  341. * the same order as the HT nodeids. If that doesn't
  342. * result in a usable node fall back to the path for the
  343. * previous case.
  344. *
  345. * This workaround operates directly on the mapping between
  346. * APIC ID and NUMA node, assuming certain relationship
  347. * between APIC ID, HT node ID and NUMA topology. As going
  348. * through CPU mapping may alter the outcome, directly
  349. * access __apicid_to_node[].
  350. */
  351. int ht_nodeid = c->initial_apicid;
  352. if (ht_nodeid >= 0 &&
  353. __apicid_to_node[ht_nodeid] != NUMA_NO_NODE)
  354. node = __apicid_to_node[ht_nodeid];
  355. /* Pick a nearby node */
  356. if (!node_online(node))
  357. node = nearby_node(apicid);
  358. }
  359. numa_set_node(cpu, node);
  360. #endif
  361. }
  362. static void __cpuinit early_init_amd_mc(struct cpuinfo_x86 *c)
  363. {
  364. #ifdef CONFIG_X86_HT
  365. unsigned bits, ecx;
  366. /* Multi core CPU? */
  367. if (c->extended_cpuid_level < 0x80000008)
  368. return;
  369. ecx = cpuid_ecx(0x80000008);
  370. c->x86_max_cores = (ecx & 0xff) + 1;
  371. /* CPU telling us the core id bits shift? */
  372. bits = (ecx >> 12) & 0xF;
  373. /* Otherwise recompute */
  374. if (bits == 0) {
  375. while ((1 << bits) < c->x86_max_cores)
  376. bits++;
  377. }
  378. c->x86_coreid_bits = bits;
  379. #endif
  380. }
  381. static void __cpuinit bsp_init_amd(struct cpuinfo_x86 *c)
  382. {
  383. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
  384. if (c->x86 > 0x10 ||
  385. (c->x86 == 0x10 && c->x86_model >= 0x2)) {
  386. u64 val;
  387. rdmsrl(MSR_K7_HWCR, val);
  388. if (!(val & BIT(24)))
  389. printk(KERN_WARNING FW_BUG "TSC doesn't count "
  390. "with P0 frequency!\n");
  391. }
  392. }
  393. if (c->x86 == 0x15) {
  394. unsigned long upperbit;
  395. u32 cpuid, assoc;
  396. cpuid = cpuid_edx(0x80000005);
  397. assoc = cpuid >> 16 & 0xff;
  398. upperbit = ((cpuid >> 24) << 10) / assoc;
  399. va_align.mask = (upperbit - 1) & PAGE_MASK;
  400. va_align.flags = ALIGN_VA_32 | ALIGN_VA_64;
  401. }
  402. }
  403. static void __cpuinit early_init_amd(struct cpuinfo_x86 *c)
  404. {
  405. early_init_amd_mc(c);
  406. /*
  407. * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
  408. * with P/T states and does not stop in deep C-states
  409. */
  410. if (c->x86_power & (1 << 8)) {
  411. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  412. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
  413. if (!check_tsc_unstable())
  414. sched_clock_stable = 1;
  415. }
  416. #ifdef CONFIG_X86_64
  417. set_cpu_cap(c, X86_FEATURE_SYSCALL32);
  418. #else
  419. /* Set MTRR capability flag if appropriate */
  420. if (c->x86 == 5)
  421. if (c->x86_model == 13 || c->x86_model == 9 ||
  422. (c->x86_model == 8 && c->x86_mask >= 8))
  423. set_cpu_cap(c, X86_FEATURE_K6_MTRR);
  424. #endif
  425. #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_PCI)
  426. /* check CPU config space for extended APIC ID */
  427. if (cpu_has_apic && c->x86 >= 0xf) {
  428. unsigned int val;
  429. val = read_pci_config(0, 24, 0, 0x68);
  430. if ((val & ((1 << 17) | (1 << 18))) == ((1 << 17) | (1 << 18)))
  431. set_cpu_cap(c, X86_FEATURE_EXTD_APICID);
  432. }
  433. #endif
  434. }
  435. static const int amd_erratum_383[];
  436. static const int amd_erratum_400[];
  437. static bool cpu_has_amd_erratum(const int *erratum);
  438. static void __cpuinit init_amd(struct cpuinfo_x86 *c)
  439. {
  440. u32 dummy;
  441. unsigned long long value;
  442. #ifdef CONFIG_SMP
  443. /*
  444. * Disable TLB flush filter by setting HWCR.FFDIS on K8
  445. * bit 6 of msr C001_0015
  446. *
  447. * Errata 63 for SH-B3 steppings
  448. * Errata 122 for all steppings (F+ have it disabled by default)
  449. */
  450. if (c->x86 == 0xf) {
  451. rdmsrl(MSR_K7_HWCR, value);
  452. value |= 1 << 6;
  453. wrmsrl(MSR_K7_HWCR, value);
  454. }
  455. #endif
  456. early_init_amd(c);
  457. /*
  458. * Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  459. * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway
  460. */
  461. clear_cpu_cap(c, 0*32+31);
  462. #ifdef CONFIG_X86_64
  463. /* On C+ stepping K8 rep microcode works well for copy/memset */
  464. if (c->x86 == 0xf) {
  465. u32 level;
  466. level = cpuid_eax(1);
  467. if ((level >= 0x0f48 && level < 0x0f50) || level >= 0x0f58)
  468. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  469. /*
  470. * Some BIOSes incorrectly force this feature, but only K8
  471. * revision D (model = 0x14) and later actually support it.
  472. * (AMD Erratum #110, docId: 25759).
  473. */
  474. if (c->x86_model < 0x14 && cpu_has(c, X86_FEATURE_LAHF_LM)) {
  475. clear_cpu_cap(c, X86_FEATURE_LAHF_LM);
  476. if (!rdmsrl_amd_safe(0xc001100d, &value)) {
  477. value &= ~(1ULL << 32);
  478. wrmsrl_amd_safe(0xc001100d, value);
  479. }
  480. }
  481. }
  482. if (c->x86 >= 0x10)
  483. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  484. /* get apicid instead of initial apic id from cpuid */
  485. c->apicid = hard_smp_processor_id();
  486. #else
  487. /*
  488. * FIXME: We should handle the K5 here. Set up the write
  489. * range and also turn on MSR 83 bits 4 and 31 (write alloc,
  490. * no bus pipeline)
  491. */
  492. switch (c->x86) {
  493. case 4:
  494. init_amd_k5(c);
  495. break;
  496. case 5:
  497. init_amd_k6(c);
  498. break;
  499. case 6: /* An Athlon/Duron */
  500. init_amd_k7(c);
  501. break;
  502. }
  503. /* K6s reports MCEs but don't actually have all the MSRs */
  504. if (c->x86 < 6)
  505. clear_cpu_cap(c, X86_FEATURE_MCE);
  506. #endif
  507. /* Enable workaround for FXSAVE leak */
  508. if (c->x86 >= 6)
  509. set_cpu_cap(c, X86_FEATURE_FXSAVE_LEAK);
  510. if (!c->x86_model_id[0]) {
  511. switch (c->x86) {
  512. case 0xf:
  513. /* Should distinguish Models here, but this is only
  514. a fallback anyways. */
  515. strcpy(c->x86_model_id, "Hammer");
  516. break;
  517. }
  518. }
  519. /* re-enable TopologyExtensions if switched off by BIOS */
  520. if ((c->x86 == 0x15) &&
  521. (c->x86_model >= 0x10) && (c->x86_model <= 0x1f) &&
  522. !cpu_has(c, X86_FEATURE_TOPOEXT)) {
  523. if (!rdmsrl_safe(0xc0011005, &value)) {
  524. value |= 1ULL << 54;
  525. wrmsrl_safe(0xc0011005, value);
  526. rdmsrl(0xc0011005, value);
  527. if (value & (1ULL << 54)) {
  528. set_cpu_cap(c, X86_FEATURE_TOPOEXT);
  529. printk(KERN_INFO FW_INFO "CPU: Re-enabling "
  530. "disabled Topology Extensions Support\n");
  531. }
  532. }
  533. }
  534. /*
  535. * The way access filter has a performance penalty on some workloads.
  536. * Disable it on the affected CPUs.
  537. */
  538. if ((c->x86 == 0x15) &&
  539. (c->x86_model >= 0x02) && (c->x86_model < 0x20)) {
  540. if (!rdmsrl_safe(0xc0011021, &value) && !(value & 0x1E)) {
  541. value |= 0x1E;
  542. wrmsrl_safe(0xc0011021, value);
  543. }
  544. }
  545. cpu_detect_cache_sizes(c);
  546. /* Multi core CPU? */
  547. if (c->extended_cpuid_level >= 0x80000008) {
  548. amd_detect_cmp(c);
  549. srat_detect_node(c);
  550. }
  551. #ifdef CONFIG_X86_32
  552. detect_ht(c);
  553. #endif
  554. init_amd_cacheinfo(c);
  555. if (c->x86 >= 0xf)
  556. set_cpu_cap(c, X86_FEATURE_K8);
  557. if (cpu_has_xmm2) {
  558. /* MFENCE stops RDTSC speculation */
  559. set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC);
  560. }
  561. #ifdef CONFIG_X86_64
  562. if (c->x86 == 0x10) {
  563. /* do this for boot cpu */
  564. if (c == &boot_cpu_data)
  565. check_enable_amd_mmconf_dmi();
  566. fam10h_check_enable_mmcfg();
  567. }
  568. if (c == &boot_cpu_data && c->x86 >= 0xf) {
  569. unsigned long long tseg;
  570. /*
  571. * Split up direct mapping around the TSEG SMM area.
  572. * Don't do it for gbpages because there seems very little
  573. * benefit in doing so.
  574. */
  575. if (!rdmsrl_safe(MSR_K8_TSEG_ADDR, &tseg)) {
  576. unsigned long pfn = tseg >> PAGE_SHIFT;
  577. printk(KERN_DEBUG "tseg: %010llx\n", tseg);
  578. if (pfn_range_is_mapped(pfn, pfn + 1))
  579. set_memory_4k((unsigned long)__va(tseg), 1);
  580. }
  581. }
  582. #endif
  583. /*
  584. * Family 0x12 and above processors have APIC timer
  585. * running in deep C states.
  586. */
  587. if (c->x86 > 0x11)
  588. set_cpu_cap(c, X86_FEATURE_ARAT);
  589. if (c->x86 == 0x10) {
  590. /*
  591. * Disable GART TLB Walk Errors on Fam10h. We do this here
  592. * because this is always needed when GART is enabled, even in a
  593. * kernel which has no MCE support built in.
  594. * BIOS should disable GartTlbWlk Errors themself. If
  595. * it doesn't do it here as suggested by the BKDG.
  596. *
  597. * Fixes: https://bugzilla.kernel.org/show_bug.cgi?id=33012
  598. */
  599. u64 mask;
  600. int err;
  601. err = rdmsrl_safe(MSR_AMD64_MCx_MASK(4), &mask);
  602. if (err == 0) {
  603. mask |= (1 << 10);
  604. wrmsrl_safe(MSR_AMD64_MCx_MASK(4), mask);
  605. }
  606. /*
  607. * On family 10h BIOS may not have properly enabled WC+ support,
  608. * causing it to be converted to CD memtype. This may result in
  609. * performance degradation for certain nested-paging guests.
  610. * Prevent this conversion by clearing bit 24 in
  611. * MSR_AMD64_BU_CFG2.
  612. *
  613. * NOTE: we want to use the _safe accessors so as not to #GP kvm
  614. * guests on older kvm hosts.
  615. */
  616. rdmsrl_safe(MSR_AMD64_BU_CFG2, &value);
  617. value &= ~(1ULL << 24);
  618. wrmsrl_safe(MSR_AMD64_BU_CFG2, value);
  619. if (cpu_has_amd_erratum(amd_erratum_383))
  620. set_cpu_bug(c, X86_BUG_AMD_TLB_MMATCH);
  621. }
  622. if (cpu_has_amd_erratum(amd_erratum_400))
  623. set_cpu_bug(c, X86_BUG_AMD_APIC_C1E);
  624. rdmsr_safe(MSR_AMD64_PATCH_LEVEL, &c->microcode, &dummy);
  625. }
  626. #ifdef CONFIG_X86_32
  627. static unsigned int __cpuinit amd_size_cache(struct cpuinfo_x86 *c,
  628. unsigned int size)
  629. {
  630. /* AMD errata T13 (order #21922) */
  631. if ((c->x86 == 6)) {
  632. /* Duron Rev A0 */
  633. if (c->x86_model == 3 && c->x86_mask == 0)
  634. size = 64;
  635. /* Tbird rev A1/A2 */
  636. if (c->x86_model == 4 &&
  637. (c->x86_mask == 0 || c->x86_mask == 1))
  638. size = 256;
  639. }
  640. return size;
  641. }
  642. #endif
  643. static void __cpuinit cpu_set_tlb_flushall_shift(struct cpuinfo_x86 *c)
  644. {
  645. tlb_flushall_shift = 5;
  646. if (c->x86 <= 0x11)
  647. tlb_flushall_shift = 4;
  648. }
  649. static void __cpuinit cpu_detect_tlb_amd(struct cpuinfo_x86 *c)
  650. {
  651. u32 ebx, eax, ecx, edx;
  652. u16 mask = 0xfff;
  653. if (c->x86 < 0xf)
  654. return;
  655. if (c->extended_cpuid_level < 0x80000006)
  656. return;
  657. cpuid(0x80000006, &eax, &ebx, &ecx, &edx);
  658. tlb_lld_4k[ENTRIES] = (ebx >> 16) & mask;
  659. tlb_lli_4k[ENTRIES] = ebx & mask;
  660. /*
  661. * K8 doesn't have 2M/4M entries in the L2 TLB so read out the L1 TLB
  662. * characteristics from the CPUID function 0x80000005 instead.
  663. */
  664. if (c->x86 == 0xf) {
  665. cpuid(0x80000005, &eax, &ebx, &ecx, &edx);
  666. mask = 0xff;
  667. }
  668. /* Handle DTLB 2M and 4M sizes, fall back to L1 if L2 is disabled */
  669. if (!((eax >> 16) & mask)) {
  670. u32 a, b, c, d;
  671. cpuid(0x80000005, &a, &b, &c, &d);
  672. tlb_lld_2m[ENTRIES] = (a >> 16) & 0xff;
  673. } else {
  674. tlb_lld_2m[ENTRIES] = (eax >> 16) & mask;
  675. }
  676. /* a 4M entry uses two 2M entries */
  677. tlb_lld_4m[ENTRIES] = tlb_lld_2m[ENTRIES] >> 1;
  678. /* Handle ITLB 2M and 4M sizes, fall back to L1 if L2 is disabled */
  679. if (!(eax & mask)) {
  680. /* Erratum 658 */
  681. if (c->x86 == 0x15 && c->x86_model <= 0x1f) {
  682. tlb_lli_2m[ENTRIES] = 1024;
  683. } else {
  684. cpuid(0x80000005, &eax, &ebx, &ecx, &edx);
  685. tlb_lli_2m[ENTRIES] = eax & 0xff;
  686. }
  687. } else
  688. tlb_lli_2m[ENTRIES] = eax & mask;
  689. tlb_lli_4m[ENTRIES] = tlb_lli_2m[ENTRIES] >> 1;
  690. cpu_set_tlb_flushall_shift(c);
  691. }
  692. static const struct cpu_dev __cpuinitconst amd_cpu_dev = {
  693. .c_vendor = "AMD",
  694. .c_ident = { "AuthenticAMD" },
  695. #ifdef CONFIG_X86_32
  696. .c_models = {
  697. { .vendor = X86_VENDOR_AMD, .family = 4, .model_names =
  698. {
  699. [3] = "486 DX/2",
  700. [7] = "486 DX/2-WB",
  701. [8] = "486 DX/4",
  702. [9] = "486 DX/4-WB",
  703. [14] = "Am5x86-WT",
  704. [15] = "Am5x86-WB"
  705. }
  706. },
  707. },
  708. .c_size_cache = amd_size_cache,
  709. #endif
  710. .c_early_init = early_init_amd,
  711. .c_detect_tlb = cpu_detect_tlb_amd,
  712. .c_bsp_init = bsp_init_amd,
  713. .c_init = init_amd,
  714. .c_x86_vendor = X86_VENDOR_AMD,
  715. };
  716. cpu_dev_register(amd_cpu_dev);
  717. /*
  718. * AMD errata checking
  719. *
  720. * Errata are defined as arrays of ints using the AMD_LEGACY_ERRATUM() or
  721. * AMD_OSVW_ERRATUM() macros. The latter is intended for newer errata that
  722. * have an OSVW id assigned, which it takes as first argument. Both take a
  723. * variable number of family-specific model-stepping ranges created by
  724. * AMD_MODEL_RANGE().
  725. *
  726. * Example:
  727. *
  728. * const int amd_erratum_319[] =
  729. * AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0x4, 0x2),
  730. * AMD_MODEL_RANGE(0x10, 0x8, 0x0, 0x8, 0x0),
  731. * AMD_MODEL_RANGE(0x10, 0x9, 0x0, 0x9, 0x0));
  732. */
  733. #define AMD_LEGACY_ERRATUM(...) { -1, __VA_ARGS__, 0 }
  734. #define AMD_OSVW_ERRATUM(osvw_id, ...) { osvw_id, __VA_ARGS__, 0 }
  735. #define AMD_MODEL_RANGE(f, m_start, s_start, m_end, s_end) \
  736. ((f << 24) | (m_start << 16) | (s_start << 12) | (m_end << 4) | (s_end))
  737. #define AMD_MODEL_RANGE_FAMILY(range) (((range) >> 24) & 0xff)
  738. #define AMD_MODEL_RANGE_START(range) (((range) >> 12) & 0xfff)
  739. #define AMD_MODEL_RANGE_END(range) ((range) & 0xfff)
  740. static const int amd_erratum_400[] =
  741. AMD_OSVW_ERRATUM(1, AMD_MODEL_RANGE(0xf, 0x41, 0x2, 0xff, 0xf),
  742. AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0xff, 0xf));
  743. static const int amd_erratum_383[] =
  744. AMD_OSVW_ERRATUM(3, AMD_MODEL_RANGE(0x10, 0, 0, 0xff, 0xf));
  745. static bool cpu_has_amd_erratum(const int *erratum)
  746. {
  747. struct cpuinfo_x86 *cpu = __this_cpu_ptr(&cpu_info);
  748. int osvw_id = *erratum++;
  749. u32 range;
  750. u32 ms;
  751. /*
  752. * If called early enough that current_cpu_data hasn't been initialized
  753. * yet, fall back to boot_cpu_data.
  754. */
  755. if (cpu->x86 == 0)
  756. cpu = &boot_cpu_data;
  757. if (cpu->x86_vendor != X86_VENDOR_AMD)
  758. return false;
  759. if (osvw_id >= 0 && osvw_id < 65536 &&
  760. cpu_has(cpu, X86_FEATURE_OSVW)) {
  761. u64 osvw_len;
  762. rdmsrl(MSR_AMD64_OSVW_ID_LENGTH, osvw_len);
  763. if (osvw_id < osvw_len) {
  764. u64 osvw_bits;
  765. rdmsrl(MSR_AMD64_OSVW_STATUS + (osvw_id >> 6),
  766. osvw_bits);
  767. return osvw_bits & (1ULL << (osvw_id & 0x3f));
  768. }
  769. }
  770. /* OSVW unavailable or ID unknown, match family-model-stepping range */
  771. ms = (cpu->x86_model << 4) | cpu->x86_mask;
  772. while ((range = *erratum++))
  773. if ((cpu->x86 == AMD_MODEL_RANGE_FAMILY(range)) &&
  774. (ms >= AMD_MODEL_RANGE_START(range)) &&
  775. (ms <= AMD_MODEL_RANGE_END(range)))
  776. return true;
  777. return false;
  778. }