amd_64.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include <linux/init.h>
  2. #include <linux/bitops.h>
  3. #include <linux/mm.h>
  4. #include <asm/io.h>
  5. #include <asm/processor.h>
  6. #include <asm/apic.h>
  7. #ifdef CONFIG_X86_64
  8. # include <asm/numa_64.h>
  9. # include <asm/mmconfig.h>
  10. # include <asm/cacheflush.h>
  11. #endif
  12. #include <mach_apic.h>
  13. #include "cpu.h"
  14. #ifdef CONFIG_NUMA
  15. static int __cpuinit nearby_node(int apicid)
  16. {
  17. int i, node;
  18. for (i = apicid - 1; i >= 0; i--) {
  19. node = apicid_to_node[i];
  20. if (node != NUMA_NO_NODE && node_online(node))
  21. return node;
  22. }
  23. for (i = apicid + 1; i < MAX_LOCAL_APIC; i++) {
  24. node = apicid_to_node[i];
  25. if (node != NUMA_NO_NODE && node_online(node))
  26. return node;
  27. }
  28. return first_node(node_online_map); /* Shouldn't happen */
  29. }
  30. #endif
  31. /*
  32. * On a AMD dual core setup the lower bits of the APIC id distingush the cores.
  33. * Assumes number of cores is a power of two.
  34. */
  35. static void __cpuinit amd_detect_cmp(struct cpuinfo_x86 *c)
  36. {
  37. #ifdef CONFIG_SMP
  38. unsigned bits;
  39. bits = c->x86_coreid_bits;
  40. /* Low order bits define the core id (index of core in socket) */
  41. c->cpu_core_id = c->initial_apicid & ((1 << bits)-1);
  42. /* Convert the initial APIC ID into the socket ID */
  43. c->phys_proc_id = c->initial_apicid >> bits;
  44. #endif
  45. }
  46. static void __cpuinit srat_detect_node(struct cpuinfo_x86 *c)
  47. {
  48. #ifdef CONFIG_NUMA
  49. int cpu = smp_processor_id();
  50. int node;
  51. unsigned apicid = hard_smp_processor_id();
  52. node = c->phys_proc_id;
  53. if (apicid_to_node[apicid] != NUMA_NO_NODE)
  54. node = apicid_to_node[apicid];
  55. if (!node_online(node)) {
  56. /* Two possibilities here:
  57. - The CPU is missing memory and no node was created.
  58. In that case try picking one from a nearby CPU
  59. - The APIC IDs differ from the HyperTransport node IDs
  60. which the K8 northbridge parsing fills in.
  61. Assume they are all increased by a constant offset,
  62. but in the same order as the HT nodeids.
  63. If that doesn't result in a usable node fall back to the
  64. path for the previous case. */
  65. int ht_nodeid = c->initial_apicid;
  66. if (ht_nodeid >= 0 &&
  67. apicid_to_node[ht_nodeid] != NUMA_NO_NODE)
  68. node = apicid_to_node[ht_nodeid];
  69. /* Pick a nearby node */
  70. if (!node_online(node))
  71. node = nearby_node(apicid);
  72. }
  73. numa_set_node(cpu, node);
  74. printk(KERN_INFO "CPU %d/%x -> Node %d\n", cpu, apicid, node);
  75. #endif
  76. }
  77. static void __cpuinit early_init_amd_mc(struct cpuinfo_x86 *c)
  78. {
  79. #ifdef CONFIG_SMP
  80. unsigned bits, ecx;
  81. /* Multi core CPU? */
  82. if (c->extended_cpuid_level < 0x80000008)
  83. return;
  84. ecx = cpuid_ecx(0x80000008);
  85. c->x86_max_cores = (ecx & 0xff) + 1;
  86. /* CPU telling us the core id bits shift? */
  87. bits = (ecx >> 12) & 0xF;
  88. /* Otherwise recompute */
  89. if (bits == 0) {
  90. while ((1 << bits) < c->x86_max_cores)
  91. bits++;
  92. }
  93. c->x86_coreid_bits = bits;
  94. #endif
  95. }
  96. static void __cpuinit early_init_amd(struct cpuinfo_x86 *c)
  97. {
  98. early_init_amd_mc(c);
  99. /* c->x86_power is 8000_0007 edx. Bit 8 is constant TSC */
  100. if (c->x86_power & (1<<8))
  101. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  102. set_cpu_cap(c, X86_FEATURE_SYSCALL32);
  103. }
  104. static void __cpuinit init_amd(struct cpuinfo_x86 *c)
  105. {
  106. unsigned level;
  107. #ifdef CONFIG_SMP
  108. unsigned long value;
  109. /*
  110. * Disable TLB flush filter by setting HWCR.FFDIS on K8
  111. * bit 6 of msr C001_0015
  112. *
  113. * Errata 63 for SH-B3 steppings
  114. * Errata 122 for all steppings (F+ have it disabled by default)
  115. */
  116. if (c->x86 == 0xf) {
  117. rdmsrl(MSR_K8_HWCR, value);
  118. value |= 1 << 6;
  119. wrmsrl(MSR_K8_HWCR, value);
  120. }
  121. #endif
  122. early_init_amd(c);
  123. /* Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  124. 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway */
  125. clear_cpu_cap(c, 0*32+31);
  126. /* On C+ stepping K8 rep microcode works well for copy/memset */
  127. if (c->x86 == 0xf) {
  128. level = cpuid_eax(1);
  129. if((level >= 0x0f48 && level < 0x0f50) || level >= 0x0f58)
  130. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  131. }
  132. if (c->x86 == 0x10 || c->x86 == 0x11)
  133. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  134. /* Enable workaround for FXSAVE leak */
  135. if (c->x86 >= 6)
  136. set_cpu_cap(c, X86_FEATURE_FXSAVE_LEAK);
  137. if (!c->x86_model_id[0]) {
  138. switch (c->x86) {
  139. case 0xf:
  140. /* Should distinguish Models here, but this is only
  141. a fallback anyways. */
  142. strcpy(c->x86_model_id, "Hammer");
  143. break;
  144. }
  145. }
  146. display_cacheinfo(c);
  147. /* Multi core CPU? */
  148. if (c->extended_cpuid_level >= 0x80000008) {
  149. amd_detect_cmp(c);
  150. srat_detect_node(c);
  151. }
  152. if (c->extended_cpuid_level >= 0x80000006) {
  153. if ((c->x86 >= 0x0f) && (cpuid_edx(0x80000006) & 0xf000))
  154. num_cache_leaves = 4;
  155. else
  156. num_cache_leaves = 3;
  157. }
  158. if (c->x86 >= 0xf && c->x86 <= 0x11)
  159. set_cpu_cap(c, X86_FEATURE_K8);
  160. if (cpu_has_xmm2) {
  161. /* MFENCE stops RDTSC speculation */
  162. set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC);
  163. }
  164. if (c->x86 == 0x10) {
  165. /* do this for boot cpu */
  166. if (c == &boot_cpu_data)
  167. check_enable_amd_mmconf_dmi();
  168. fam10h_check_enable_mmcfg();
  169. }
  170. if (c == &boot_cpu_data && c->x86 >= 0xf && c->x86 <= 0x11) {
  171. unsigned long long tseg;
  172. /*
  173. * Split up direct mapping around the TSEG SMM area.
  174. * Don't do it for gbpages because there seems very little
  175. * benefit in doing so.
  176. */
  177. if (!rdmsrl_safe(MSR_K8_TSEG_ADDR, &tseg)) {
  178. printk(KERN_DEBUG "tseg: %010llx\n", tseg);
  179. if ((tseg>>PMD_SHIFT) <
  180. (max_low_pfn_mapped>>(PMD_SHIFT-PAGE_SHIFT)) ||
  181. ((tseg>>PMD_SHIFT) <
  182. (max_pfn_mapped>>(PMD_SHIFT-PAGE_SHIFT)) &&
  183. (tseg>>PMD_SHIFT) >= (1ULL<<(32 - PMD_SHIFT))))
  184. set_memory_4k((unsigned long)__va(tseg), 1);
  185. }
  186. }
  187. }
  188. static struct cpu_dev amd_cpu_dev __cpuinitdata = {
  189. .c_vendor = "AMD",
  190. .c_ident = { "AuthenticAMD" },
  191. .c_early_init = early_init_amd,
  192. .c_init = init_amd,
  193. .c_x86_vendor = X86_VENDOR_AMD,
  194. };
  195. cpu_dev_register(amd_cpu_dev);