amd.c 24 KB

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