amd.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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/spec-ctrl.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/cacheflush.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. 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_stepping < 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_stepping > 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_stepping >= 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_stepping == 0) ||
  206. (c->x86_stepping == 1)))
  207. return;
  208. /* Duron 670 is valid */
  209. if ((c->x86_model == 7) && (c->x86_stepping == 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_stepping >= 2)) ||
  219. ((c->x86_model == 7) && (c->x86_stepping >= 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. if (c->x86 >= 0x15 && c->x86 <= 0x17) {
  459. unsigned int bit;
  460. switch (c->x86) {
  461. case 0x15: bit = 54; break;
  462. case 0x16: bit = 33; break;
  463. case 0x17: bit = 10; break;
  464. default: return;
  465. }
  466. /*
  467. * Try to cache the base value so further operations can
  468. * avoid RMW. If that faults, do not enable SSBD.
  469. */
  470. if (!rdmsrl_safe(MSR_AMD64_LS_CFG, &x86_amd_ls_cfg_base)) {
  471. setup_force_cpu_cap(X86_FEATURE_LS_CFG_SSBD);
  472. setup_force_cpu_cap(X86_FEATURE_SSBD);
  473. x86_amd_ls_cfg_ssbd_mask = 1ULL << bit;
  474. }
  475. }
  476. }
  477. static void early_init_amd(struct cpuinfo_x86 *c)
  478. {
  479. early_init_amd_mc(c);
  480. /*
  481. * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
  482. * with P/T states and does not stop in deep C-states
  483. */
  484. if (c->x86_power & (1 << 8)) {
  485. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  486. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
  487. if (!check_tsc_unstable())
  488. set_sched_clock_stable();
  489. }
  490. /* Bit 12 of 8000_0007 edx is accumulated power mechanism. */
  491. if (c->x86_power & BIT(12))
  492. set_cpu_cap(c, X86_FEATURE_ACC_POWER);
  493. #ifdef CONFIG_X86_64
  494. set_cpu_cap(c, X86_FEATURE_SYSCALL32);
  495. #else
  496. /* Set MTRR capability flag if appropriate */
  497. if (c->x86 == 5)
  498. if (c->x86_model == 13 || c->x86_model == 9 ||
  499. (c->x86_model == 8 && c->x86_stepping >= 8))
  500. set_cpu_cap(c, X86_FEATURE_K6_MTRR);
  501. #endif
  502. #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_PCI)
  503. /*
  504. * ApicID can always be treated as an 8-bit value for AMD APIC versions
  505. * >= 0x10, but even old K8s came out of reset with version 0x10. So, we
  506. * can safely set X86_FEATURE_EXTD_APICID unconditionally for families
  507. * after 16h.
  508. */
  509. if (boot_cpu_has(X86_FEATURE_APIC)) {
  510. if (c->x86 > 0x16)
  511. set_cpu_cap(c, X86_FEATURE_EXTD_APICID);
  512. else if (c->x86 >= 0xf) {
  513. /* check CPU config space for extended APIC ID */
  514. unsigned int val;
  515. val = read_pci_config(0, 24, 0, 0x68);
  516. if ((val >> 17 & 0x3) == 0x3)
  517. set_cpu_cap(c, X86_FEATURE_EXTD_APICID);
  518. }
  519. }
  520. #endif
  521. /*
  522. * This is only needed to tell the kernel whether to use VMCALL
  523. * and VMMCALL. VMMCALL is never executed except under virt, so
  524. * we can set it unconditionally.
  525. */
  526. set_cpu_cap(c, X86_FEATURE_VMMCALL);
  527. /* F16h erratum 793, CVE-2013-6885 */
  528. if (c->x86 == 0x16 && c->x86_model <= 0xf)
  529. msr_set_bit(MSR_AMD64_LS_CFG, 15);
  530. /*
  531. * Check whether the machine is affected by erratum 400. This is
  532. * used to select the proper idle routine and to enable the check
  533. * whether the machine is affected in arch_post_acpi_init(), which
  534. * sets the X86_BUG_AMD_APIC_C1E bug depending on the MSR check.
  535. */
  536. if (cpu_has_amd_erratum(c, amd_erratum_400))
  537. set_cpu_bug(c, X86_BUG_AMD_E400);
  538. }
  539. static void init_amd_k8(struct cpuinfo_x86 *c)
  540. {
  541. u32 level;
  542. u64 value;
  543. /* On C+ stepping K8 rep microcode works well for copy/memset */
  544. level = cpuid_eax(1);
  545. if ((level >= 0x0f48 && level < 0x0f50) || level >= 0x0f58)
  546. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  547. /*
  548. * Some BIOSes incorrectly force this feature, but only K8 revision D
  549. * (model = 0x14) and later actually support it.
  550. * (AMD Erratum #110, docId: 25759).
  551. */
  552. if (c->x86_model < 0x14 && cpu_has(c, X86_FEATURE_LAHF_LM)) {
  553. clear_cpu_cap(c, X86_FEATURE_LAHF_LM);
  554. if (!rdmsrl_amd_safe(0xc001100d, &value)) {
  555. value &= ~BIT_64(32);
  556. wrmsrl_amd_safe(0xc001100d, value);
  557. }
  558. }
  559. if (!c->x86_model_id[0])
  560. strcpy(c->x86_model_id, "Hammer");
  561. #ifdef CONFIG_SMP
  562. /*
  563. * Disable TLB flush filter by setting HWCR.FFDIS on K8
  564. * bit 6 of msr C001_0015
  565. *
  566. * Errata 63 for SH-B3 steppings
  567. * Errata 122 for all steppings (F+ have it disabled by default)
  568. */
  569. msr_set_bit(MSR_K7_HWCR, 6);
  570. #endif
  571. set_cpu_bug(c, X86_BUG_SWAPGS_FENCE);
  572. }
  573. static void init_amd_gh(struct cpuinfo_x86 *c)
  574. {
  575. #ifdef CONFIG_X86_64
  576. /* do this for boot cpu */
  577. if (c == &boot_cpu_data)
  578. check_enable_amd_mmconf_dmi();
  579. fam10h_check_enable_mmcfg();
  580. #endif
  581. /*
  582. * Disable GART TLB Walk Errors on Fam10h. We do this here because this
  583. * is always needed when GART is enabled, even in a kernel which has no
  584. * MCE support built in. BIOS should disable GartTlbWlk Errors already.
  585. * If it doesn't, we do it here as suggested by the BKDG.
  586. *
  587. * Fixes: https://bugzilla.kernel.org/show_bug.cgi?id=33012
  588. */
  589. msr_set_bit(MSR_AMD64_MCx_MASK(4), 10);
  590. /*
  591. * On family 10h BIOS may not have properly enabled WC+ support, causing
  592. * it to be converted to CD memtype. This may result in performance
  593. * degradation for certain nested-paging guests. Prevent this conversion
  594. * by clearing bit 24 in MSR_AMD64_BU_CFG2.
  595. *
  596. * NOTE: we want to use the _safe accessors so as not to #GP kvm
  597. * guests on older kvm hosts.
  598. */
  599. msr_clear_bit(MSR_AMD64_BU_CFG2, 24);
  600. if (cpu_has_amd_erratum(c, amd_erratum_383))
  601. set_cpu_bug(c, X86_BUG_AMD_TLB_MMATCH);
  602. }
  603. #define MSR_AMD64_DE_CFG 0xC0011029
  604. static void init_amd_ln(struct cpuinfo_x86 *c)
  605. {
  606. /*
  607. * Apply erratum 665 fix unconditionally so machines without a BIOS
  608. * fix work.
  609. */
  610. msr_set_bit(MSR_AMD64_DE_CFG, 31);
  611. }
  612. static void init_amd_bd(struct cpuinfo_x86 *c)
  613. {
  614. u64 value;
  615. /* re-enable TopologyExtensions if switched off by BIOS */
  616. if ((c->x86_model >= 0x10) && (c->x86_model <= 0x6f) &&
  617. !cpu_has(c, X86_FEATURE_TOPOEXT)) {
  618. if (msr_set_bit(0xc0011005, 54) > 0) {
  619. rdmsrl(0xc0011005, value);
  620. if (value & BIT_64(54)) {
  621. set_cpu_cap(c, X86_FEATURE_TOPOEXT);
  622. pr_info_once(FW_INFO "CPU: Re-enabling disabled Topology Extensions Support.\n");
  623. }
  624. }
  625. }
  626. /*
  627. * The way access filter has a performance penalty on some workloads.
  628. * Disable it on the affected CPUs.
  629. */
  630. if ((c->x86_model >= 0x02) && (c->x86_model < 0x20)) {
  631. if (!rdmsrl_safe(MSR_F15H_IC_CFG, &value) && !(value & 0x1E)) {
  632. value |= 0x1E;
  633. wrmsrl_safe(MSR_F15H_IC_CFG, value);
  634. }
  635. }
  636. }
  637. static void init_amd_zn(struct cpuinfo_x86 *c)
  638. {
  639. set_cpu_cap(c, X86_FEATURE_ZEN);
  640. /*
  641. * Fix erratum 1076: CPB feature bit not being set in CPUID. It affects
  642. * all up to and including B1.
  643. */
  644. if (c->x86_model <= 1 && c->x86_stepping <= 1)
  645. set_cpu_cap(c, X86_FEATURE_CPB);
  646. }
  647. static void init_amd(struct cpuinfo_x86 *c)
  648. {
  649. u32 dummy;
  650. early_init_amd(c);
  651. /*
  652. * Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  653. * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway
  654. */
  655. clear_cpu_cap(c, 0*32+31);
  656. if (c->x86 >= 0x10)
  657. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  658. /* get apicid instead of initial apic id from cpuid */
  659. c->apicid = hard_smp_processor_id();
  660. /* K6s reports MCEs but don't actually have all the MSRs */
  661. if (c->x86 < 6)
  662. clear_cpu_cap(c, X86_FEATURE_MCE);
  663. switch (c->x86) {
  664. case 4: init_amd_k5(c); break;
  665. case 5: init_amd_k6(c); break;
  666. case 6: init_amd_k7(c); break;
  667. case 0xf: init_amd_k8(c); break;
  668. case 0x10: init_amd_gh(c); break;
  669. case 0x12: init_amd_ln(c); break;
  670. case 0x15: init_amd_bd(c); break;
  671. case 0x17: init_amd_zn(c); break;
  672. }
  673. /* Enable workaround for FXSAVE leak */
  674. if (c->x86 >= 6)
  675. set_cpu_bug(c, X86_BUG_FXSAVE_LEAK);
  676. cpu_detect_cache_sizes(c);
  677. /* Multi core CPU? */
  678. if (c->extended_cpuid_level >= 0x80000008) {
  679. amd_detect_cmp(c);
  680. srat_detect_node(c);
  681. }
  682. #ifdef CONFIG_X86_32
  683. detect_ht(c);
  684. #endif
  685. init_amd_cacheinfo(c);
  686. if (c->x86 >= 0xf)
  687. set_cpu_cap(c, X86_FEATURE_K8);
  688. if (cpu_has(c, X86_FEATURE_XMM2)) {
  689. unsigned long long val;
  690. int ret;
  691. /*
  692. * A serializing LFENCE has less overhead than MFENCE, so
  693. * use it for execution serialization. On families which
  694. * don't have that MSR, LFENCE is already serializing.
  695. * msr_set_bit() uses the safe accessors, too, even if the MSR
  696. * is not present.
  697. */
  698. msr_set_bit(MSR_F10H_DECFG,
  699. MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT);
  700. /*
  701. * Verify that the MSR write was successful (could be running
  702. * under a hypervisor) and only then assume that LFENCE is
  703. * serializing.
  704. */
  705. ret = rdmsrl_safe(MSR_F10H_DECFG, &val);
  706. if (!ret && (val & MSR_F10H_DECFG_LFENCE_SERIALIZE)) {
  707. /* A serializing LFENCE stops RDTSC speculation */
  708. set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
  709. } else {
  710. /* MFENCE stops RDTSC speculation */
  711. set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC);
  712. }
  713. }
  714. /*
  715. * Family 0x12 and above processors have APIC timer
  716. * running in deep C states.
  717. */
  718. if (c->x86 > 0x11)
  719. set_cpu_cap(c, X86_FEATURE_ARAT);
  720. rdmsr_safe(MSR_AMD64_PATCH_LEVEL, &c->microcode, &dummy);
  721. /* 3DNow or LM implies PREFETCHW */
  722. if (!cpu_has(c, X86_FEATURE_3DNOWPREFETCH))
  723. if (cpu_has(c, X86_FEATURE_3DNOW) || cpu_has(c, X86_FEATURE_LM))
  724. set_cpu_cap(c, X86_FEATURE_3DNOWPREFETCH);
  725. /* AMD CPUs don't reset SS attributes on SYSRET, Xen does. */
  726. if (!cpu_has(c, X86_FEATURE_XENPV))
  727. set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS);
  728. }
  729. #ifdef CONFIG_X86_32
  730. static unsigned int amd_size_cache(struct cpuinfo_x86 *c, unsigned int size)
  731. {
  732. /* AMD errata T13 (order #21922) */
  733. if ((c->x86 == 6)) {
  734. /* Duron Rev A0 */
  735. if (c->x86_model == 3 && c->x86_stepping == 0)
  736. size = 64;
  737. /* Tbird rev A1/A2 */
  738. if (c->x86_model == 4 &&
  739. (c->x86_stepping == 0 || c->x86_stepping == 1))
  740. size = 256;
  741. }
  742. return size;
  743. }
  744. #endif
  745. static void cpu_detect_tlb_amd(struct cpuinfo_x86 *c)
  746. {
  747. u32 ebx, eax, ecx, edx;
  748. u16 mask = 0xfff;
  749. if (c->x86 < 0xf)
  750. return;
  751. if (c->extended_cpuid_level < 0x80000006)
  752. return;
  753. cpuid(0x80000006, &eax, &ebx, &ecx, &edx);
  754. tlb_lld_4k[ENTRIES] = (ebx >> 16) & mask;
  755. tlb_lli_4k[ENTRIES] = ebx & mask;
  756. /*
  757. * K8 doesn't have 2M/4M entries in the L2 TLB so read out the L1 TLB
  758. * characteristics from the CPUID function 0x80000005 instead.
  759. */
  760. if (c->x86 == 0xf) {
  761. cpuid(0x80000005, &eax, &ebx, &ecx, &edx);
  762. mask = 0xff;
  763. }
  764. /* Handle DTLB 2M and 4M sizes, fall back to L1 if L2 is disabled */
  765. if (!((eax >> 16) & mask))
  766. tlb_lld_2m[ENTRIES] = (cpuid_eax(0x80000005) >> 16) & 0xff;
  767. else
  768. tlb_lld_2m[ENTRIES] = (eax >> 16) & mask;
  769. /* a 4M entry uses two 2M entries */
  770. tlb_lld_4m[ENTRIES] = tlb_lld_2m[ENTRIES] >> 1;
  771. /* Handle ITLB 2M and 4M sizes, fall back to L1 if L2 is disabled */
  772. if (!(eax & mask)) {
  773. /* Erratum 658 */
  774. if (c->x86 == 0x15 && c->x86_model <= 0x1f) {
  775. tlb_lli_2m[ENTRIES] = 1024;
  776. } else {
  777. cpuid(0x80000005, &eax, &ebx, &ecx, &edx);
  778. tlb_lli_2m[ENTRIES] = eax & 0xff;
  779. }
  780. } else
  781. tlb_lli_2m[ENTRIES] = eax & mask;
  782. tlb_lli_4m[ENTRIES] = tlb_lli_2m[ENTRIES] >> 1;
  783. }
  784. static const struct cpu_dev amd_cpu_dev = {
  785. .c_vendor = "AMD",
  786. .c_ident = { "AuthenticAMD" },
  787. #ifdef CONFIG_X86_32
  788. .legacy_models = {
  789. { .family = 4, .model_names =
  790. {
  791. [3] = "486 DX/2",
  792. [7] = "486 DX/2-WB",
  793. [8] = "486 DX/4",
  794. [9] = "486 DX/4-WB",
  795. [14] = "Am5x86-WT",
  796. [15] = "Am5x86-WB"
  797. }
  798. },
  799. },
  800. .legacy_cache_size = amd_size_cache,
  801. #endif
  802. .c_early_init = early_init_amd,
  803. .c_detect_tlb = cpu_detect_tlb_amd,
  804. .c_bsp_init = bsp_init_amd,
  805. .c_init = init_amd,
  806. .c_x86_vendor = X86_VENDOR_AMD,
  807. };
  808. cpu_dev_register(amd_cpu_dev);
  809. /*
  810. * AMD errata checking
  811. *
  812. * Errata are defined as arrays of ints using the AMD_LEGACY_ERRATUM() or
  813. * AMD_OSVW_ERRATUM() macros. The latter is intended for newer errata that
  814. * have an OSVW id assigned, which it takes as first argument. Both take a
  815. * variable number of family-specific model-stepping ranges created by
  816. * AMD_MODEL_RANGE().
  817. *
  818. * Example:
  819. *
  820. * const int amd_erratum_319[] =
  821. * AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0x4, 0x2),
  822. * AMD_MODEL_RANGE(0x10, 0x8, 0x0, 0x8, 0x0),
  823. * AMD_MODEL_RANGE(0x10, 0x9, 0x0, 0x9, 0x0));
  824. */
  825. #define AMD_LEGACY_ERRATUM(...) { -1, __VA_ARGS__, 0 }
  826. #define AMD_OSVW_ERRATUM(osvw_id, ...) { osvw_id, __VA_ARGS__, 0 }
  827. #define AMD_MODEL_RANGE(f, m_start, s_start, m_end, s_end) \
  828. ((f << 24) | (m_start << 16) | (s_start << 12) | (m_end << 4) | (s_end))
  829. #define AMD_MODEL_RANGE_FAMILY(range) (((range) >> 24) & 0xff)
  830. #define AMD_MODEL_RANGE_START(range) (((range) >> 12) & 0xfff)
  831. #define AMD_MODEL_RANGE_END(range) ((range) & 0xfff)
  832. static const int amd_erratum_400[] =
  833. AMD_OSVW_ERRATUM(1, AMD_MODEL_RANGE(0xf, 0x41, 0x2, 0xff, 0xf),
  834. AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0xff, 0xf));
  835. static const int amd_erratum_383[] =
  836. AMD_OSVW_ERRATUM(3, AMD_MODEL_RANGE(0x10, 0, 0, 0xff, 0xf));
  837. static bool cpu_has_amd_erratum(struct cpuinfo_x86 *cpu, const int *erratum)
  838. {
  839. int osvw_id = *erratum++;
  840. u32 range;
  841. u32 ms;
  842. if (osvw_id >= 0 && osvw_id < 65536 &&
  843. cpu_has(cpu, X86_FEATURE_OSVW)) {
  844. u64 osvw_len;
  845. rdmsrl(MSR_AMD64_OSVW_ID_LENGTH, osvw_len);
  846. if (osvw_id < osvw_len) {
  847. u64 osvw_bits;
  848. rdmsrl(MSR_AMD64_OSVW_STATUS + (osvw_id >> 6),
  849. osvw_bits);
  850. return osvw_bits & (1ULL << (osvw_id & 0x3f));
  851. }
  852. }
  853. /* OSVW unavailable or ID unknown, match family-model-stepping range */
  854. ms = (cpu->x86_model << 4) | cpu->x86_stepping;
  855. while ((range = *erratum++))
  856. if ((cpu->x86 == AMD_MODEL_RANGE_FAMILY(range)) &&
  857. (ms >= AMD_MODEL_RANGE_START(range)) &&
  858. (ms <= AMD_MODEL_RANGE_END(range)))
  859. return true;
  860. return false;
  861. }
  862. void set_dr_addr_mask(unsigned long mask, int dr)
  863. {
  864. if (!boot_cpu_has(X86_FEATURE_BPEXT))
  865. return;
  866. switch (dr) {
  867. case 0:
  868. wrmsr(MSR_F16H_DR0_ADDR_MASK, mask, 0);
  869. break;
  870. case 1:
  871. case 2:
  872. case 3:
  873. wrmsr(MSR_F16H_DR1_ADDR_MASK - 1 + dr, mask, 0);
  874. break;
  875. default:
  876. break;
  877. }
  878. }