amd.c 27 KB

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