amd.c 22 KB

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