amd.c 19 KB

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