amd.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/export.h>
  3. #include <linux/bitops.h>
  4. #include <linux/elf.h>
  5. #include <linux/mm.h>
  6. #include <linux/io.h>
  7. #include <linux/sched.h>
  8. #include <linux/sched/clock.h>
  9. #include <linux/random.h>
  10. #include <linux/topology.h>
  11. #include <asm/processor.h>
  12. #include <asm/apic.h>
  13. #include <asm/cacheinfo.h>
  14. #include <asm/cpu.h>
  15. #include <asm/cpu_device_id.h>
  16. #include <asm/spec-ctrl.h>
  17. #include <asm/smp.h>
  18. #include <asm/numa.h>
  19. #include <asm/pci-direct.h>
  20. #include <asm/delay.h>
  21. #include <asm/debugreg.h>
  22. #include <asm/resctrl.h>
  23. #include <asm/sev.h>
  24. #ifdef CONFIG_X86_64
  25. # include <asm/mmconfig.h>
  26. #endif
  27. #include "cpu.h"
  28. static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
  29. {
  30. u32 gprs[8] = { 0 };
  31. int err;
  32. WARN_ONCE((boot_cpu_data.x86 != 0xf),
  33. "%s should only be used on K8!\n", __func__);
  34. gprs[1] = msr;
  35. gprs[7] = 0x9c5a203a;
  36. err = rdmsr_safe_regs(gprs);
  37. *p = gprs[0] | ((u64)gprs[2] << 32);
  38. return err;
  39. }
  40. static inline int wrmsrl_amd_safe(unsigned msr, unsigned long long val)
  41. {
  42. u32 gprs[8] = { 0 };
  43. WARN_ONCE((boot_cpu_data.x86 != 0xf),
  44. "%s should only be used on K8!\n", __func__);
  45. gprs[0] = (u32)val;
  46. gprs[1] = msr;
  47. gprs[2] = val >> 32;
  48. gprs[7] = 0x9c5a203a;
  49. return wrmsr_safe_regs(gprs);
  50. }
  51. /*
  52. * B step AMD K6 before B 9730xxxx have hardware bugs that can cause
  53. * misexecution of code under Linux. Owners of such processors should
  54. * contact AMD for precise details and a CPU swap.
  55. *
  56. * See http://www.multimania.com/poulot/k6bug.html
  57. * and section 2.6.2 of "AMD-K6 Processor Revision Guide - Model 6"
  58. * (Publication # 21266 Issue Date: August 1998)
  59. *
  60. * The following test is erm.. interesting. AMD neglected to up
  61. * the chip setting when fixing the bug but they also tweaked some
  62. * performance at the same time..
  63. */
  64. #ifdef CONFIG_X86_32
  65. extern __visible void vide(void);
  66. __asm__(".text\n"
  67. ".globl vide\n"
  68. ".type vide, @function\n"
  69. ".align 4\n"
  70. "vide: ret\n");
  71. #endif
  72. static void init_amd_k5(struct cpuinfo_x86 *c)
  73. {
  74. #ifdef CONFIG_X86_32
  75. /*
  76. * General Systems BIOSen alias the cpu frequency registers
  77. * of the Elan at 0x000df000. Unfortunately, one of the Linux
  78. * drivers subsequently pokes it, and changes the CPU speed.
  79. * Workaround : Remove the unneeded alias.
  80. */
  81. #define CBAR (0xfffc) /* Configuration Base Address (32-bit) */
  82. #define CBAR_ENB (0x80000000)
  83. #define CBAR_KEY (0X000000CB)
  84. if (c->x86_model == 9 || c->x86_model == 10) {
  85. if (inl(CBAR) & CBAR_ENB)
  86. outl(0 | CBAR_KEY, CBAR);
  87. }
  88. #endif
  89. }
  90. static void init_amd_k6(struct cpuinfo_x86 *c)
  91. {
  92. #ifdef CONFIG_X86_32
  93. u32 l, h;
  94. int mbytes = get_num_physpages() >> (20-PAGE_SHIFT);
  95. if (c->x86_model < 6) {
  96. /* Based on AMD doc 20734R - June 2000 */
  97. if (c->x86_model == 0) {
  98. clear_cpu_cap(c, X86_FEATURE_APIC);
  99. set_cpu_cap(c, X86_FEATURE_PGE);
  100. }
  101. return;
  102. }
  103. if (c->x86_model == 6 && c->x86_stepping == 1) {
  104. const int K6_BUG_LOOP = 1000000;
  105. int n;
  106. void (*f_vide)(void);
  107. u64 d, d2;
  108. pr_info("AMD K6 stepping B detected - ");
  109. /*
  110. * It looks like AMD fixed the 2.6.2 bug and improved indirect
  111. * calls at the same time.
  112. */
  113. n = K6_BUG_LOOP;
  114. f_vide = vide;
  115. OPTIMIZER_HIDE_VAR(f_vide);
  116. d = rdtsc();
  117. while (n--)
  118. f_vide();
  119. d2 = rdtsc();
  120. d = d2-d;
  121. if (d > 20*K6_BUG_LOOP)
  122. pr_cont("system stability may be impaired when more than 32 MB are used.\n");
  123. else
  124. pr_cont("probably OK (after B9730xxxx).\n");
  125. }
  126. /* K6 with old style WHCR */
  127. if (c->x86_model < 8 ||
  128. (c->x86_model == 8 && c->x86_stepping < 8)) {
  129. /* We can only write allocate on the low 508Mb */
  130. if (mbytes > 508)
  131. mbytes = 508;
  132. rdmsr(MSR_K6_WHCR, l, h);
  133. if ((l&0x0000FFFF) == 0) {
  134. unsigned long flags;
  135. l = (1<<0)|((mbytes/4)<<1);
  136. local_irq_save(flags);
  137. wbinvd();
  138. wrmsr(MSR_K6_WHCR, l, h);
  139. local_irq_restore(flags);
  140. pr_info("Enabling old style K6 write allocation for %d Mb\n",
  141. mbytes);
  142. }
  143. return;
  144. }
  145. if ((c->x86_model == 8 && c->x86_stepping > 7) ||
  146. c->x86_model == 9 || c->x86_model == 13) {
  147. /* The more serious chips .. */
  148. if (mbytes > 4092)
  149. mbytes = 4092;
  150. rdmsr(MSR_K6_WHCR, l, h);
  151. if ((l&0xFFFF0000) == 0) {
  152. unsigned long flags;
  153. l = ((mbytes>>2)<<22)|(1<<16);
  154. local_irq_save(flags);
  155. wbinvd();
  156. wrmsr(MSR_K6_WHCR, l, h);
  157. local_irq_restore(flags);
  158. pr_info("Enabling new style K6 write allocation for %d Mb\n",
  159. mbytes);
  160. }
  161. return;
  162. }
  163. if (c->x86_model == 10) {
  164. /* AMD Geode LX is model 10 */
  165. /* placeholder for any needed mods */
  166. return;
  167. }
  168. #endif
  169. }
  170. static void init_amd_k7(struct cpuinfo_x86 *c)
  171. {
  172. #ifdef CONFIG_X86_32
  173. u32 l, h;
  174. /*
  175. * Bit 15 of Athlon specific MSR 15, needs to be 0
  176. * to enable SSE on Palomino/Morgan/Barton CPU's.
  177. * If the BIOS didn't enable it already, enable it here.
  178. */
  179. if (c->x86_model >= 6 && c->x86_model <= 10) {
  180. if (!cpu_has(c, X86_FEATURE_XMM)) {
  181. pr_info("Enabling disabled K7/SSE Support.\n");
  182. msr_clear_bit(MSR_K7_HWCR, 15);
  183. set_cpu_cap(c, X86_FEATURE_XMM);
  184. }
  185. }
  186. /*
  187. * It's been determined by AMD that Athlons since model 8 stepping 1
  188. * are more robust with CLK_CTL set to 200xxxxx instead of 600xxxxx
  189. * As per AMD technical note 27212 0.2
  190. */
  191. if ((c->x86_model == 8 && c->x86_stepping >= 1) || (c->x86_model > 8)) {
  192. rdmsr(MSR_K7_CLK_CTL, l, h);
  193. if ((l & 0xfff00000) != 0x20000000) {
  194. pr_info("CPU: CLK_CTL MSR was %x. Reprogramming to %x\n",
  195. l, ((l & 0x000fffff)|0x20000000));
  196. wrmsr(MSR_K7_CLK_CTL, (l & 0x000fffff)|0x20000000, h);
  197. }
  198. }
  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. static void srat_detect_node(struct cpuinfo_x86 *c)
  257. {
  258. #ifdef CONFIG_NUMA
  259. int cpu = smp_processor_id();
  260. int node;
  261. unsigned apicid = c->topo.apicid;
  262. node = numa_cpu_node(cpu);
  263. if (node == NUMA_NO_NODE)
  264. node = per_cpu_llc_id(cpu);
  265. /*
  266. * On multi-fabric platform (e.g. Numascale NumaChip) a
  267. * platform-specific handler needs to be called to fixup some
  268. * IDs of the CPU.
  269. */
  270. if (x86_cpuinit.fixup_cpu_id)
  271. x86_cpuinit.fixup_cpu_id(c, node);
  272. if (!node_online(node)) {
  273. /*
  274. * Two possibilities here:
  275. *
  276. * - The CPU is missing memory and no node was created. In
  277. * that case try picking one from a nearby CPU.
  278. *
  279. * - The APIC IDs differ from the HyperTransport node IDs
  280. * which the K8 northbridge parsing fills in. Assume
  281. * they are all increased by a constant offset, but in
  282. * the same order as the HT nodeids. If that doesn't
  283. * result in a usable node fall back to the path for the
  284. * previous case.
  285. *
  286. * This workaround operates directly on the mapping between
  287. * APIC ID and NUMA node, assuming certain relationship
  288. * between APIC ID, HT node ID and NUMA topology. As going
  289. * through CPU mapping may alter the outcome, directly
  290. * access __apicid_to_node[].
  291. */
  292. int ht_nodeid = c->topo.initial_apicid;
  293. if (__apicid_to_node[ht_nodeid] != NUMA_NO_NODE)
  294. node = __apicid_to_node[ht_nodeid];
  295. /* Pick a nearby node */
  296. if (!node_online(node))
  297. node = nearby_node(apicid);
  298. }
  299. numa_set_node(cpu, node);
  300. #endif
  301. }
  302. static void bsp_determine_snp(struct cpuinfo_x86 *c)
  303. {
  304. #ifdef CONFIG_ARCH_HAS_CC_PLATFORM
  305. cc_vendor = CC_VENDOR_AMD;
  306. if (cpu_has(c, X86_FEATURE_SEV_SNP)) {
  307. /*
  308. * RMP table entry format is not architectural and is defined by the
  309. * per-processor PPR. Restrict SNP support on the known CPU models
  310. * for which the RMP table entry format is currently defined for.
  311. */
  312. if (!cpu_has(c, X86_FEATURE_HYPERVISOR) &&
  313. c->x86 >= 0x19 && snp_probe_rmptable_info()) {
  314. cc_platform_set(CC_ATTR_HOST_SEV_SNP);
  315. } else {
  316. setup_clear_cpu_cap(X86_FEATURE_SEV_SNP);
  317. cc_platform_clear(CC_ATTR_HOST_SEV_SNP);
  318. }
  319. }
  320. #endif
  321. }
  322. static bool amd_check_tsa_microcode(void)
  323. {
  324. struct cpuinfo_x86 *c = &boot_cpu_data;
  325. union zen_patch_rev p;
  326. u32 min_rev = 0;
  327. p.ext_fam = c->x86 - 0xf;
  328. p.model = c->x86_model;
  329. p.ext_model = c->x86_model >> 4;
  330. p.stepping = c->x86_stepping;
  331. /* reserved bits are expected to be 0 in test below */
  332. p.__reserved = 0;
  333. if (cpu_has(c, X86_FEATURE_ZEN3) ||
  334. cpu_has(c, X86_FEATURE_ZEN4)) {
  335. switch (p.ucode_rev >> 8) {
  336. case 0xa0011: min_rev = 0x0a0011d7; break;
  337. case 0xa0012: min_rev = 0x0a00123b; break;
  338. case 0xa0082: min_rev = 0x0a00820d; break;
  339. case 0xa1011: min_rev = 0x0a10114c; break;
  340. case 0xa1012: min_rev = 0x0a10124c; break;
  341. case 0xa1081: min_rev = 0x0a108109; break;
  342. case 0xa2010: min_rev = 0x0a20102e; break;
  343. case 0xa2012: min_rev = 0x0a201211; break;
  344. case 0xa4041: min_rev = 0x0a404108; break;
  345. case 0xa5000: min_rev = 0x0a500012; break;
  346. case 0xa6012: min_rev = 0x0a60120a; break;
  347. case 0xa7041: min_rev = 0x0a704108; break;
  348. case 0xa7052: min_rev = 0x0a705208; break;
  349. case 0xa7080: min_rev = 0x0a708008; break;
  350. case 0xa70c0: min_rev = 0x0a70c008; break;
  351. case 0xaa002: min_rev = 0x0aa00216; break;
  352. default:
  353. pr_debug("%s: ucode_rev: 0x%x, current revision: 0x%x\n",
  354. __func__, p.ucode_rev, c->microcode);
  355. return false;
  356. }
  357. }
  358. if (!min_rev)
  359. return false;
  360. return c->microcode >= min_rev;
  361. }
  362. static void tsa_init(struct cpuinfo_x86 *c)
  363. {
  364. if (cpu_has(c, X86_FEATURE_HYPERVISOR))
  365. return;
  366. if (cpu_has(c, X86_FEATURE_ZEN3) ||
  367. cpu_has(c, X86_FEATURE_ZEN4)) {
  368. if (amd_check_tsa_microcode())
  369. setup_force_cpu_cap(X86_FEATURE_VERW_CLEAR);
  370. } else {
  371. setup_force_cpu_cap(X86_FEATURE_TSA_SQ_NO);
  372. setup_force_cpu_cap(X86_FEATURE_TSA_L1_NO);
  373. }
  374. }
  375. static void bsp_init_amd(struct cpuinfo_x86 *c)
  376. {
  377. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
  378. if (c->x86 > 0x10 ||
  379. (c->x86 == 0x10 && c->x86_model >= 0x2)) {
  380. u64 val;
  381. rdmsrl(MSR_K7_HWCR, val);
  382. if (!(val & BIT(24)))
  383. pr_warn(FW_BUG "TSC doesn't count with P0 frequency!\n");
  384. }
  385. }
  386. if (c->x86 == 0x15) {
  387. unsigned long upperbit;
  388. u32 cpuid, assoc;
  389. cpuid = cpuid_edx(0x80000005);
  390. assoc = cpuid >> 16 & 0xff;
  391. upperbit = ((cpuid >> 24) << 10) / assoc;
  392. va_align.mask = (upperbit - 1) & PAGE_MASK;
  393. va_align.flags = ALIGN_VA_32 | ALIGN_VA_64;
  394. /* A random value per boot for bit slice [12:upper_bit) */
  395. va_align.bits = get_random_u32() & va_align.mask;
  396. }
  397. if (cpu_has(c, X86_FEATURE_MWAITX))
  398. use_mwaitx_delay();
  399. if (!boot_cpu_has(X86_FEATURE_AMD_SSBD) &&
  400. !boot_cpu_has(X86_FEATURE_VIRT_SSBD) &&
  401. c->x86 >= 0x15 && c->x86 <= 0x17) {
  402. unsigned int bit;
  403. switch (c->x86) {
  404. case 0x15: bit = 54; break;
  405. case 0x16: bit = 33; break;
  406. case 0x17: bit = 10; break;
  407. default: return;
  408. }
  409. /*
  410. * Try to cache the base value so further operations can
  411. * avoid RMW. If that faults, do not enable SSBD.
  412. */
  413. if (!rdmsrl_safe(MSR_AMD64_LS_CFG, &x86_amd_ls_cfg_base)) {
  414. setup_force_cpu_cap(X86_FEATURE_LS_CFG_SSBD);
  415. setup_force_cpu_cap(X86_FEATURE_SSBD);
  416. x86_amd_ls_cfg_ssbd_mask = 1ULL << bit;
  417. }
  418. }
  419. resctrl_cpu_detect(c);
  420. /* Figure out Zen generations: */
  421. switch (c->x86) {
  422. case 0x17:
  423. switch (c->x86_model) {
  424. case 0x00 ... 0x2f:
  425. case 0x50 ... 0x5f:
  426. setup_force_cpu_cap(X86_FEATURE_ZEN1);
  427. break;
  428. case 0x30 ... 0x4f:
  429. case 0x60 ... 0x7f:
  430. case 0x90 ... 0x91:
  431. case 0xa0 ... 0xaf:
  432. setup_force_cpu_cap(X86_FEATURE_ZEN2);
  433. break;
  434. default:
  435. goto warn;
  436. }
  437. break;
  438. case 0x19:
  439. switch (c->x86_model) {
  440. case 0x00 ... 0x0f:
  441. case 0x20 ... 0x5f:
  442. setup_force_cpu_cap(X86_FEATURE_ZEN3);
  443. break;
  444. case 0x10 ... 0x1f:
  445. case 0x60 ... 0xaf:
  446. setup_force_cpu_cap(X86_FEATURE_ZEN4);
  447. break;
  448. default:
  449. goto warn;
  450. }
  451. break;
  452. case 0x1a:
  453. switch (c->x86_model) {
  454. case 0x00 ... 0x2f:
  455. case 0x40 ... 0x4f:
  456. case 0x60 ... 0x7f:
  457. setup_force_cpu_cap(X86_FEATURE_ZEN5);
  458. break;
  459. default:
  460. goto warn;
  461. }
  462. break;
  463. default:
  464. break;
  465. }
  466. bsp_determine_snp(c);
  467. tsa_init(c);
  468. return;
  469. warn:
  470. WARN_ONCE(1, "Family 0x%x, model: 0x%x??\n", c->x86, c->x86_model);
  471. }
  472. static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)
  473. {
  474. u64 msr;
  475. /*
  476. * BIOS support is required for SME and SEV.
  477. * For SME: If BIOS has enabled SME then adjust x86_phys_bits by
  478. * the SME physical address space reduction value.
  479. * If BIOS has not enabled SME then don't advertise the
  480. * SME feature (set in scattered.c).
  481. * If the kernel has not enabled SME via any means then
  482. * don't advertise the SME feature.
  483. * For SEV: If BIOS has not enabled SEV then don't advertise SEV and
  484. * any additional functionality based on it.
  485. *
  486. * In all cases, since support for SME and SEV requires long mode,
  487. * don't advertise the feature under CONFIG_X86_32.
  488. */
  489. if (cpu_has(c, X86_FEATURE_SME) || cpu_has(c, X86_FEATURE_SEV)) {
  490. /* Check if memory encryption is enabled */
  491. rdmsrl(MSR_AMD64_SYSCFG, msr);
  492. if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
  493. goto clear_all;
  494. /*
  495. * Always adjust physical address bits. Even though this
  496. * will be a value above 32-bits this is still done for
  497. * CONFIG_X86_32 so that accurate values are reported.
  498. */
  499. c->x86_phys_bits -= (cpuid_ebx(0x8000001f) >> 6) & 0x3f;
  500. if (IS_ENABLED(CONFIG_X86_32))
  501. goto clear_all;
  502. if (!sme_me_mask)
  503. setup_clear_cpu_cap(X86_FEATURE_SME);
  504. rdmsrl(MSR_K7_HWCR, msr);
  505. if (!(msr & MSR_K7_HWCR_SMMLOCK))
  506. goto clear_sev;
  507. return;
  508. clear_all:
  509. setup_clear_cpu_cap(X86_FEATURE_SME);
  510. clear_sev:
  511. setup_clear_cpu_cap(X86_FEATURE_SEV);
  512. setup_clear_cpu_cap(X86_FEATURE_SEV_ES);
  513. setup_clear_cpu_cap(X86_FEATURE_SEV_SNP);
  514. }
  515. }
  516. static void early_init_amd(struct cpuinfo_x86 *c)
  517. {
  518. u32 dummy;
  519. if (c->x86 >= 0xf)
  520. set_cpu_cap(c, X86_FEATURE_K8);
  521. rdmsr_safe(MSR_AMD64_PATCH_LEVEL, &c->microcode, &dummy);
  522. /*
  523. * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
  524. * with P/T states and does not stop in deep C-states
  525. */
  526. if (c->x86_power & (1 << 8)) {
  527. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  528. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
  529. }
  530. /* Bit 12 of 8000_0007 edx is accumulated power mechanism. */
  531. if (c->x86_power & BIT(12))
  532. set_cpu_cap(c, X86_FEATURE_ACC_POWER);
  533. /* Bit 14 indicates the Runtime Average Power Limit interface. */
  534. if (c->x86_power & BIT(14))
  535. set_cpu_cap(c, X86_FEATURE_RAPL);
  536. #ifdef CONFIG_X86_64
  537. set_cpu_cap(c, X86_FEATURE_SYSCALL32);
  538. #else
  539. /* Set MTRR capability flag if appropriate */
  540. if (c->x86 == 5)
  541. if (c->x86_model == 13 || c->x86_model == 9 ||
  542. (c->x86_model == 8 && c->x86_stepping >= 8))
  543. set_cpu_cap(c, X86_FEATURE_K6_MTRR);
  544. #endif
  545. #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_PCI)
  546. /*
  547. * ApicID can always be treated as an 8-bit value for AMD APIC versions
  548. * >= 0x10, but even old K8s came out of reset with version 0x10. So, we
  549. * can safely set X86_FEATURE_EXTD_APICID unconditionally for families
  550. * after 16h.
  551. */
  552. if (boot_cpu_has(X86_FEATURE_APIC)) {
  553. if (c->x86 > 0x16)
  554. set_cpu_cap(c, X86_FEATURE_EXTD_APICID);
  555. else if (c->x86 >= 0xf) {
  556. /* check CPU config space for extended APIC ID */
  557. unsigned int val;
  558. val = read_pci_config(0, 24, 0, 0x68);
  559. if ((val >> 17 & 0x3) == 0x3)
  560. set_cpu_cap(c, X86_FEATURE_EXTD_APICID);
  561. }
  562. }
  563. #endif
  564. /*
  565. * This is only needed to tell the kernel whether to use VMCALL
  566. * and VMMCALL. VMMCALL is never executed except under virt, so
  567. * we can set it unconditionally.
  568. */
  569. set_cpu_cap(c, X86_FEATURE_VMMCALL);
  570. /* F16h erratum 793, CVE-2013-6885 */
  571. if (c->x86 == 0x16 && c->x86_model <= 0xf)
  572. msr_set_bit(MSR_AMD64_LS_CFG, 15);
  573. early_detect_mem_encrypt(c);
  574. if (!cpu_has(c, X86_FEATURE_HYPERVISOR) && !cpu_has(c, X86_FEATURE_IBPB_BRTYPE)) {
  575. if (c->x86 == 0x17 && boot_cpu_has(X86_FEATURE_AMD_IBPB))
  576. setup_force_cpu_cap(X86_FEATURE_IBPB_BRTYPE);
  577. else if (c->x86 >= 0x19 && !wrmsrl_safe(MSR_IA32_PRED_CMD, PRED_CMD_SBPB)) {
  578. setup_force_cpu_cap(X86_FEATURE_IBPB_BRTYPE);
  579. setup_force_cpu_cap(X86_FEATURE_SBPB);
  580. }
  581. }
  582. }
  583. static void init_amd_k8(struct cpuinfo_x86 *c)
  584. {
  585. u32 level;
  586. u64 value;
  587. /* On C+ stepping K8 rep microcode works well for copy/memset */
  588. level = cpuid_eax(1);
  589. if ((level >= 0x0f48 && level < 0x0f50) || level >= 0x0f58)
  590. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  591. /*
  592. * Some BIOSes incorrectly force this feature, but only K8 revision D
  593. * (model = 0x14) and later actually support it.
  594. * (AMD Erratum #110, docId: 25759).
  595. */
  596. if (c->x86_model < 0x14 && cpu_has(c, X86_FEATURE_LAHF_LM) && !cpu_has(c, X86_FEATURE_HYPERVISOR)) {
  597. clear_cpu_cap(c, X86_FEATURE_LAHF_LM);
  598. if (!rdmsrl_amd_safe(0xc001100d, &value)) {
  599. value &= ~BIT_64(32);
  600. wrmsrl_amd_safe(0xc001100d, value);
  601. }
  602. }
  603. if (!c->x86_model_id[0])
  604. strcpy(c->x86_model_id, "Hammer");
  605. #ifdef CONFIG_SMP
  606. /*
  607. * Disable TLB flush filter by setting HWCR.FFDIS on K8
  608. * bit 6 of msr C001_0015
  609. *
  610. * Errata 63 for SH-B3 steppings
  611. * Errata 122 for all steppings (F+ have it disabled by default)
  612. */
  613. msr_set_bit(MSR_K7_HWCR, 6);
  614. #endif
  615. set_cpu_bug(c, X86_BUG_SWAPGS_FENCE);
  616. /*
  617. * Check models and steppings affected by erratum 400. This is
  618. * used to select the proper idle routine and to enable the
  619. * check whether the machine is affected in arch_post_acpi_subsys_init()
  620. * which sets the X86_BUG_AMD_APIC_C1E bug depending on the MSR check.
  621. */
  622. if (c->x86_model > 0x41 ||
  623. (c->x86_model == 0x41 && c->x86_stepping >= 0x2))
  624. setup_force_cpu_bug(X86_BUG_AMD_E400);
  625. }
  626. static void init_amd_gh(struct cpuinfo_x86 *c)
  627. {
  628. #ifdef CONFIG_MMCONF_FAM10H
  629. /* do this for boot cpu */
  630. if (c == &boot_cpu_data)
  631. check_enable_amd_mmconf_dmi();
  632. fam10h_check_enable_mmcfg();
  633. #endif
  634. /*
  635. * Disable GART TLB Walk Errors on Fam10h. We do this here because this
  636. * is always needed when GART is enabled, even in a kernel which has no
  637. * MCE support built in. BIOS should disable GartTlbWlk Errors already.
  638. * If it doesn't, we do it here as suggested by the BKDG.
  639. *
  640. * Fixes: https://bugzilla.kernel.org/show_bug.cgi?id=33012
  641. */
  642. msr_set_bit(MSR_AMD64_MCx_MASK(4), 10);
  643. /*
  644. * On family 10h BIOS may not have properly enabled WC+ support, causing
  645. * it to be converted to CD memtype. This may result in performance
  646. * degradation for certain nested-paging guests. Prevent this conversion
  647. * by clearing bit 24 in MSR_AMD64_BU_CFG2.
  648. *
  649. * NOTE: we want to use the _safe accessors so as not to #GP kvm
  650. * guests on older kvm hosts.
  651. */
  652. msr_clear_bit(MSR_AMD64_BU_CFG2, 24);
  653. set_cpu_bug(c, X86_BUG_AMD_TLB_MMATCH);
  654. /*
  655. * Check models and steppings affected by erratum 400. This is
  656. * used to select the proper idle routine and to enable the
  657. * check whether the machine is affected in arch_post_acpi_subsys_init()
  658. * which sets the X86_BUG_AMD_APIC_C1E bug depending on the MSR check.
  659. */
  660. if (c->x86_model > 0x2 ||
  661. (c->x86_model == 0x2 && c->x86_stepping >= 0x1))
  662. setup_force_cpu_bug(X86_BUG_AMD_E400);
  663. }
  664. static void init_amd_ln(struct cpuinfo_x86 *c)
  665. {
  666. /*
  667. * Apply erratum 665 fix unconditionally so machines without a BIOS
  668. * fix work.
  669. */
  670. msr_set_bit(MSR_AMD64_DE_CFG, 31);
  671. }
  672. static bool rdrand_force;
  673. static int __init rdrand_cmdline(char *str)
  674. {
  675. if (!str)
  676. return -EINVAL;
  677. if (!strcmp(str, "force"))
  678. rdrand_force = true;
  679. else
  680. return -EINVAL;
  681. return 0;
  682. }
  683. early_param("rdrand", rdrand_cmdline);
  684. static void clear_rdrand_cpuid_bit(struct cpuinfo_x86 *c)
  685. {
  686. /*
  687. * Saving of the MSR used to hide the RDRAND support during
  688. * suspend/resume is done by arch/x86/power/cpu.c, which is
  689. * dependent on CONFIG_PM_SLEEP.
  690. */
  691. if (!IS_ENABLED(CONFIG_PM_SLEEP))
  692. return;
  693. /*
  694. * The self-test can clear X86_FEATURE_RDRAND, so check for
  695. * RDRAND support using the CPUID function directly.
  696. */
  697. if (!(cpuid_ecx(1) & BIT(30)) || rdrand_force)
  698. return;
  699. msr_clear_bit(MSR_AMD64_CPUID_FN_1, 62);
  700. /*
  701. * Verify that the CPUID change has occurred in case the kernel is
  702. * running virtualized and the hypervisor doesn't support the MSR.
  703. */
  704. if (cpuid_ecx(1) & BIT(30)) {
  705. pr_info_once("BIOS may not properly restore RDRAND after suspend, but hypervisor does not support hiding RDRAND via CPUID.\n");
  706. return;
  707. }
  708. clear_cpu_cap(c, X86_FEATURE_RDRAND);
  709. pr_info_once("BIOS may not properly restore RDRAND after suspend, hiding RDRAND via CPUID. Use rdrand=force to reenable.\n");
  710. }
  711. static void init_amd_jg(struct cpuinfo_x86 *c)
  712. {
  713. /*
  714. * Some BIOS implementations do not restore proper RDRAND support
  715. * across suspend and resume. Check on whether to hide the RDRAND
  716. * instruction support via CPUID.
  717. */
  718. clear_rdrand_cpuid_bit(c);
  719. }
  720. static void init_amd_bd(struct cpuinfo_x86 *c)
  721. {
  722. u64 value;
  723. /*
  724. * The way access filter has a performance penalty on some workloads.
  725. * Disable it on the affected CPUs.
  726. */
  727. if ((c->x86_model >= 0x02) && (c->x86_model < 0x20)) {
  728. if (!rdmsrl_safe(MSR_F15H_IC_CFG, &value) && !(value & 0x1E)) {
  729. value |= 0x1E;
  730. wrmsrl_safe(MSR_F15H_IC_CFG, value);
  731. }
  732. }
  733. /*
  734. * Some BIOS implementations do not restore proper RDRAND support
  735. * across suspend and resume. Check on whether to hide the RDRAND
  736. * instruction support via CPUID.
  737. */
  738. clear_rdrand_cpuid_bit(c);
  739. }
  740. static const struct x86_cpu_desc erratum_1386_microcode[] = {
  741. AMD_CPU_DESC(0x17, 0x1, 0x2, 0x0800126e),
  742. AMD_CPU_DESC(0x17, 0x31, 0x0, 0x08301052),
  743. {},
  744. };
  745. static void fix_erratum_1386(struct cpuinfo_x86 *c)
  746. {
  747. /*
  748. * Work around Erratum 1386. The XSAVES instruction malfunctions in
  749. * certain circumstances on Zen1/2 uarch, and not all parts have had
  750. * updated microcode at the time of writing (March 2023).
  751. *
  752. * Affected parts all have no supervisor XSAVE states, meaning that
  753. * the XSAVEC instruction (which works fine) is equivalent.
  754. *
  755. * Clear the feature flag only on microcode revisions which
  756. * don't have the fix.
  757. */
  758. if (x86_cpu_has_min_microcode_rev(erratum_1386_microcode))
  759. return;
  760. clear_cpu_cap(c, X86_FEATURE_XSAVES);
  761. }
  762. void init_spectral_chicken(struct cpuinfo_x86 *c)
  763. {
  764. #ifdef CONFIG_MITIGATION_UNRET_ENTRY
  765. u64 value;
  766. /*
  767. * On Zen2 we offer this chicken (bit) on the altar of Speculation.
  768. *
  769. * This suppresses speculation from the middle of a basic block, i.e. it
  770. * suppresses non-branch predictions.
  771. */
  772. if (!cpu_has(c, X86_FEATURE_HYPERVISOR)) {
  773. if (!rdmsrl_safe(MSR_ZEN2_SPECTRAL_CHICKEN, &value)) {
  774. value |= MSR_ZEN2_SPECTRAL_CHICKEN_BIT;
  775. wrmsrl_safe(MSR_ZEN2_SPECTRAL_CHICKEN, value);
  776. }
  777. }
  778. #endif
  779. }
  780. static void init_amd_zen_common(void)
  781. {
  782. setup_force_cpu_cap(X86_FEATURE_ZEN);
  783. #ifdef CONFIG_NUMA
  784. node_reclaim_distance = 32;
  785. #endif
  786. }
  787. static void init_amd_zen1(struct cpuinfo_x86 *c)
  788. {
  789. fix_erratum_1386(c);
  790. /* Fix up CPUID bits, but only if not virtualised. */
  791. if (!cpu_has(c, X86_FEATURE_HYPERVISOR)) {
  792. /* Erratum 1076: CPB feature bit not being set in CPUID. */
  793. if (!cpu_has(c, X86_FEATURE_CPB))
  794. set_cpu_cap(c, X86_FEATURE_CPB);
  795. }
  796. pr_notice_once("AMD Zen1 DIV0 bug detected. Disable SMT for full protection.\n");
  797. setup_force_cpu_bug(X86_BUG_DIV0);
  798. /*
  799. * Turn off the Instructions Retired free counter on machines that are
  800. * susceptible to erratum #1054 "Instructions Retired Performance
  801. * Counter May Be Inaccurate".
  802. */
  803. if (c->x86_model < 0x30) {
  804. msr_clear_bit(MSR_K7_HWCR, MSR_K7_HWCR_IRPERF_EN_BIT);
  805. clear_cpu_cap(c, X86_FEATURE_IRPERF);
  806. }
  807. }
  808. static bool cpu_has_zenbleed_microcode(void)
  809. {
  810. u32 good_rev = 0;
  811. switch (boot_cpu_data.x86_model) {
  812. case 0x30 ... 0x3f: good_rev = 0x0830107b; break;
  813. case 0x60 ... 0x67: good_rev = 0x0860010c; break;
  814. case 0x68 ... 0x6f: good_rev = 0x08608107; break;
  815. case 0x70 ... 0x7f: good_rev = 0x08701033; break;
  816. case 0xa0 ... 0xaf: good_rev = 0x08a00009; break;
  817. default:
  818. return false;
  819. }
  820. if (boot_cpu_data.microcode < good_rev)
  821. return false;
  822. return true;
  823. }
  824. static void zen2_zenbleed_check(struct cpuinfo_x86 *c)
  825. {
  826. if (cpu_has(c, X86_FEATURE_HYPERVISOR))
  827. return;
  828. if (!cpu_has(c, X86_FEATURE_AVX))
  829. return;
  830. if (!cpu_has_zenbleed_microcode()) {
  831. pr_notice_once("Zenbleed: please update your microcode for the most optimal fix\n");
  832. msr_set_bit(MSR_AMD64_DE_CFG, MSR_AMD64_DE_CFG_ZEN2_FP_BACKUP_FIX_BIT);
  833. } else {
  834. msr_clear_bit(MSR_AMD64_DE_CFG, MSR_AMD64_DE_CFG_ZEN2_FP_BACKUP_FIX_BIT);
  835. }
  836. }
  837. static void init_amd_zen2(struct cpuinfo_x86 *c)
  838. {
  839. init_spectral_chicken(c);
  840. fix_erratum_1386(c);
  841. zen2_zenbleed_check(c);
  842. /* Disable RDSEED on AMD Cyan Skillfish because of an error. */
  843. if (c->x86_model == 0x47 && c->x86_stepping == 0x0) {
  844. clear_cpu_cap(c, X86_FEATURE_RDSEED);
  845. msr_clear_bit(MSR_AMD64_CPUID_FN_7, 18);
  846. pr_emerg("RDSEED is not reliable on this platform; disabling.\n");
  847. }
  848. }
  849. static void init_amd_zen3(struct cpuinfo_x86 *c)
  850. {
  851. if (!cpu_has(c, X86_FEATURE_HYPERVISOR)) {
  852. /*
  853. * Zen3 (Fam19 model < 0x10) parts are not susceptible to
  854. * Branch Type Confusion, but predate the allocation of the
  855. * BTC_NO bit.
  856. */
  857. if (!cpu_has(c, X86_FEATURE_BTC_NO))
  858. set_cpu_cap(c, X86_FEATURE_BTC_NO);
  859. }
  860. }
  861. static void init_amd_zen4(struct cpuinfo_x86 *c)
  862. {
  863. if (!cpu_has(c, X86_FEATURE_HYPERVISOR))
  864. msr_set_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_SHARED_BTB_FIX_BIT);
  865. /*
  866. * These Zen4 SoCs advertise support for virtualized VMLOAD/VMSAVE
  867. * in some BIOS versions but they can lead to random host reboots.
  868. */
  869. switch (c->x86_model) {
  870. case 0x18 ... 0x1f:
  871. case 0x60 ... 0x7f:
  872. clear_cpu_cap(c, X86_FEATURE_V_VMSAVE_VMLOAD);
  873. break;
  874. }
  875. }
  876. static void init_amd_zen5(struct cpuinfo_x86 *c)
  877. {
  878. }
  879. static void init_amd(struct cpuinfo_x86 *c)
  880. {
  881. u64 vm_cr;
  882. early_init_amd(c);
  883. /*
  884. * Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  885. * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway
  886. */
  887. clear_cpu_cap(c, 0*32+31);
  888. if (c->x86 >= 0x10)
  889. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  890. /* AMD FSRM also implies FSRS */
  891. if (cpu_has(c, X86_FEATURE_FSRM))
  892. set_cpu_cap(c, X86_FEATURE_FSRS);
  893. /* K6s reports MCEs but don't actually have all the MSRs */
  894. if (c->x86 < 6)
  895. clear_cpu_cap(c, X86_FEATURE_MCE);
  896. switch (c->x86) {
  897. case 4: init_amd_k5(c); break;
  898. case 5: init_amd_k6(c); break;
  899. case 6: init_amd_k7(c); break;
  900. case 0xf: init_amd_k8(c); break;
  901. case 0x10: init_amd_gh(c); break;
  902. case 0x12: init_amd_ln(c); break;
  903. case 0x15: init_amd_bd(c); break;
  904. case 0x16: init_amd_jg(c); break;
  905. }
  906. /*
  907. * Save up on some future enablement work and do common Zen
  908. * settings.
  909. */
  910. if (c->x86 >= 0x17)
  911. init_amd_zen_common();
  912. if (boot_cpu_has(X86_FEATURE_ZEN1))
  913. init_amd_zen1(c);
  914. else if (boot_cpu_has(X86_FEATURE_ZEN2))
  915. init_amd_zen2(c);
  916. else if (boot_cpu_has(X86_FEATURE_ZEN3))
  917. init_amd_zen3(c);
  918. else if (boot_cpu_has(X86_FEATURE_ZEN4))
  919. init_amd_zen4(c);
  920. else if (boot_cpu_has(X86_FEATURE_ZEN5))
  921. init_amd_zen5(c);
  922. /*
  923. * Enable workaround for FXSAVE leak on CPUs
  924. * without a XSaveErPtr feature
  925. */
  926. if ((c->x86 >= 6) && (!cpu_has(c, X86_FEATURE_XSAVEERPTR)))
  927. set_cpu_bug(c, X86_BUG_FXSAVE_LEAK);
  928. cpu_detect_cache_sizes(c);
  929. srat_detect_node(c);
  930. init_amd_cacheinfo(c);
  931. if (cpu_has(c, X86_FEATURE_SVM)) {
  932. rdmsrl(MSR_VM_CR, vm_cr);
  933. if (vm_cr & SVM_VM_CR_SVM_DIS_MASK) {
  934. pr_notice_once("SVM disabled (by BIOS) in MSR_VM_CR\n");
  935. clear_cpu_cap(c, X86_FEATURE_SVM);
  936. }
  937. }
  938. if (!cpu_has(c, X86_FEATURE_LFENCE_RDTSC) && cpu_has(c, X86_FEATURE_XMM2)) {
  939. /*
  940. * Use LFENCE for execution serialization. On families which
  941. * don't have that MSR, LFENCE is already serializing.
  942. * msr_set_bit() uses the safe accessors, too, even if the MSR
  943. * is not present.
  944. */
  945. msr_set_bit(MSR_AMD64_DE_CFG,
  946. MSR_AMD64_DE_CFG_LFENCE_SERIALIZE_BIT);
  947. /* A serializing LFENCE stops RDTSC speculation */
  948. set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
  949. }
  950. /*
  951. * Family 0x12 and above processors have APIC timer
  952. * running in deep C states.
  953. */
  954. if (c->x86 > 0x11)
  955. set_cpu_cap(c, X86_FEATURE_ARAT);
  956. /* 3DNow or LM implies PREFETCHW */
  957. if (!cpu_has(c, X86_FEATURE_3DNOWPREFETCH))
  958. if (cpu_has(c, X86_FEATURE_3DNOW) || cpu_has(c, X86_FEATURE_LM))
  959. set_cpu_cap(c, X86_FEATURE_3DNOWPREFETCH);
  960. /* AMD CPUs don't reset SS attributes on SYSRET, Xen does. */
  961. if (!cpu_feature_enabled(X86_FEATURE_XENPV))
  962. set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS);
  963. /* Enable the Instructions Retired free counter */
  964. if (cpu_has(c, X86_FEATURE_IRPERF))
  965. msr_set_bit(MSR_K7_HWCR, MSR_K7_HWCR_IRPERF_EN_BIT);
  966. check_null_seg_clears_base(c);
  967. /*
  968. * Make sure EFER[AIBRSE - Automatic IBRS Enable] is set. The APs are brought up
  969. * using the trampoline code and as part of it, MSR_EFER gets prepared there in
  970. * order to be replicated onto them. Regardless, set it here again, if not set,
  971. * to protect against any future refactoring/code reorganization which might
  972. * miss setting this important bit.
  973. */
  974. if (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
  975. cpu_has(c, X86_FEATURE_AUTOIBRS))
  976. WARN_ON_ONCE(msr_set_bit(MSR_EFER, _EFER_AUTOIBRS) < 0);
  977. /* AMD CPUs don't need fencing after x2APIC/TSC_DEADLINE MSR writes. */
  978. clear_cpu_cap(c, X86_FEATURE_APIC_MSRS_FENCE);
  979. }
  980. #ifdef CONFIG_X86_32
  981. static unsigned int amd_size_cache(struct cpuinfo_x86 *c, unsigned int size)
  982. {
  983. /* AMD errata T13 (order #21922) */
  984. if (c->x86 == 6) {
  985. /* Duron Rev A0 */
  986. if (c->x86_model == 3 && c->x86_stepping == 0)
  987. size = 64;
  988. /* Tbird rev A1/A2 */
  989. if (c->x86_model == 4 &&
  990. (c->x86_stepping == 0 || c->x86_stepping == 1))
  991. size = 256;
  992. }
  993. return size;
  994. }
  995. #endif
  996. static void cpu_detect_tlb_amd(struct cpuinfo_x86 *c)
  997. {
  998. u32 ebx, eax, ecx, edx;
  999. u16 mask = 0xfff;
  1000. if (c->x86 < 0xf)
  1001. return;
  1002. if (c->extended_cpuid_level < 0x80000006)
  1003. return;
  1004. cpuid(0x80000006, &eax, &ebx, &ecx, &edx);
  1005. tlb_lld_4k[ENTRIES] = (ebx >> 16) & mask;
  1006. tlb_lli_4k[ENTRIES] = ebx & mask;
  1007. /*
  1008. * K8 doesn't have 2M/4M entries in the L2 TLB so read out the L1 TLB
  1009. * characteristics from the CPUID function 0x80000005 instead.
  1010. */
  1011. if (c->x86 == 0xf) {
  1012. cpuid(0x80000005, &eax, &ebx, &ecx, &edx);
  1013. mask = 0xff;
  1014. }
  1015. /* Handle DTLB 2M and 4M sizes, fall back to L1 if L2 is disabled */
  1016. if (!((eax >> 16) & mask))
  1017. tlb_lld_2m[ENTRIES] = (cpuid_eax(0x80000005) >> 16) & 0xff;
  1018. else
  1019. tlb_lld_2m[ENTRIES] = (eax >> 16) & mask;
  1020. /* a 4M entry uses two 2M entries */
  1021. tlb_lld_4m[ENTRIES] = tlb_lld_2m[ENTRIES] >> 1;
  1022. /* Handle ITLB 2M and 4M sizes, fall back to L1 if L2 is disabled */
  1023. if (!(eax & mask)) {
  1024. /* Erratum 658 */
  1025. if (c->x86 == 0x15 && c->x86_model <= 0x1f) {
  1026. tlb_lli_2m[ENTRIES] = 1024;
  1027. } else {
  1028. cpuid(0x80000005, &eax, &ebx, &ecx, &edx);
  1029. tlb_lli_2m[ENTRIES] = eax & 0xff;
  1030. }
  1031. } else
  1032. tlb_lli_2m[ENTRIES] = eax & mask;
  1033. tlb_lli_4m[ENTRIES] = tlb_lli_2m[ENTRIES] >> 1;
  1034. }
  1035. static const struct cpu_dev amd_cpu_dev = {
  1036. .c_vendor = "AMD",
  1037. .c_ident = { "AuthenticAMD" },
  1038. #ifdef CONFIG_X86_32
  1039. .legacy_models = {
  1040. { .family = 4, .model_names =
  1041. {
  1042. [3] = "486 DX/2",
  1043. [7] = "486 DX/2-WB",
  1044. [8] = "486 DX/4",
  1045. [9] = "486 DX/4-WB",
  1046. [14] = "Am5x86-WT",
  1047. [15] = "Am5x86-WB"
  1048. }
  1049. },
  1050. },
  1051. .legacy_cache_size = amd_size_cache,
  1052. #endif
  1053. .c_early_init = early_init_amd,
  1054. .c_detect_tlb = cpu_detect_tlb_amd,
  1055. .c_bsp_init = bsp_init_amd,
  1056. .c_init = init_amd,
  1057. .c_x86_vendor = X86_VENDOR_AMD,
  1058. };
  1059. cpu_dev_register(amd_cpu_dev);
  1060. static DEFINE_PER_CPU_READ_MOSTLY(unsigned long[4], amd_dr_addr_mask);
  1061. static unsigned int amd_msr_dr_addr_masks[] = {
  1062. MSR_F16H_DR0_ADDR_MASK,
  1063. MSR_F16H_DR1_ADDR_MASK,
  1064. MSR_F16H_DR1_ADDR_MASK + 1,
  1065. MSR_F16H_DR1_ADDR_MASK + 2
  1066. };
  1067. void amd_set_dr_addr_mask(unsigned long mask, unsigned int dr)
  1068. {
  1069. int cpu = smp_processor_id();
  1070. if (!cpu_feature_enabled(X86_FEATURE_BPEXT))
  1071. return;
  1072. if (WARN_ON_ONCE(dr >= ARRAY_SIZE(amd_msr_dr_addr_masks)))
  1073. return;
  1074. if (per_cpu(amd_dr_addr_mask, cpu)[dr] == mask)
  1075. return;
  1076. wrmsr(amd_msr_dr_addr_masks[dr], mask, 0);
  1077. per_cpu(amd_dr_addr_mask, cpu)[dr] = mask;
  1078. }
  1079. unsigned long amd_get_dr_addr_mask(unsigned int dr)
  1080. {
  1081. if (!cpu_feature_enabled(X86_FEATURE_BPEXT))
  1082. return 0;
  1083. if (WARN_ON_ONCE(dr >= ARRAY_SIZE(amd_msr_dr_addr_masks)))
  1084. return 0;
  1085. return per_cpu(amd_dr_addr_mask[dr], smp_processor_id());
  1086. }
  1087. EXPORT_SYMBOL_GPL(amd_get_dr_addr_mask);
  1088. static void zenbleed_check_cpu(void *unused)
  1089. {
  1090. struct cpuinfo_x86 *c = &cpu_data(smp_processor_id());
  1091. zen2_zenbleed_check(c);
  1092. }
  1093. void amd_check_microcode(void)
  1094. {
  1095. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  1096. return;
  1097. if (cpu_feature_enabled(X86_FEATURE_ZEN2))
  1098. on_each_cpu(zenbleed_check_cpu, NULL, 1);
  1099. }