amd.c 22 KB

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