amd.c 25 KB

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