amd.c 23 KB

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