amd.c 27 KB

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