amd.c 30 KB

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