amd.c 24 KB

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