amd.c 27 KB

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