amd.c 23 KB

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