amd.c 33 KB

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