amd.c 22 KB

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