amd.c 27 KB

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