common_64.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include <linux/init.h>
  2. #include <linux/string.h>
  3. #include <linux/delay.h>
  4. #include <linux/smp.h>
  5. #include <linux/module.h>
  6. #include <linux/percpu.h>
  7. #include <linux/bootmem.h>
  8. #include <asm/processor.h>
  9. #include <asm/i387.h>
  10. #include <asm/msr.h>
  11. #include <asm/io.h>
  12. #include <asm/mmu_context.h>
  13. #include <asm/mtrr.h>
  14. #include <asm/mce.h>
  15. #include <asm/pat.h>
  16. #include <asm/numa.h>
  17. #ifdef CONFIG_X86_LOCAL_APIC
  18. #include <asm/mpspec.h>
  19. #include <asm/apic.h>
  20. #include <mach_apic.h>
  21. #endif
  22. #include "cpu.h"
  23. /* We need valid kernel segments for data and code in long mode too
  24. * IRET will check the segment types kkeil 2000/10/28
  25. * Also sysret mandates a special GDT layout
  26. */
  27. /* The TLS descriptors are currently at a different place compared to i386.
  28. Hopefully nobody expects them at a fixed place (Wine?) */
  29. DEFINE_PER_CPU(struct gdt_page, gdt_page) = { .gdt = {
  30. [GDT_ENTRY_KERNEL32_CS] = { { { 0x0000ffff, 0x00cf9b00 } } },
  31. [GDT_ENTRY_KERNEL_CS] = { { { 0x0000ffff, 0x00af9b00 } } },
  32. [GDT_ENTRY_KERNEL_DS] = { { { 0x0000ffff, 0x00cf9300 } } },
  33. [GDT_ENTRY_DEFAULT_USER32_CS] = { { { 0x0000ffff, 0x00cffb00 } } },
  34. [GDT_ENTRY_DEFAULT_USER_DS] = { { { 0x0000ffff, 0x00cff300 } } },
  35. [GDT_ENTRY_DEFAULT_USER_CS] = { { { 0x0000ffff, 0x00affb00 } } },
  36. } };
  37. EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
  38. __u32 cleared_cpu_caps[NCAPINTS] __cpuinitdata;
  39. /* Current gdt points %fs at the "master" per-cpu area: after this,
  40. * it's on the real one. */
  41. void switch_to_new_gdt(void)
  42. {
  43. struct desc_ptr gdt_descr;
  44. gdt_descr.address = (long)get_cpu_gdt_table(smp_processor_id());
  45. gdt_descr.size = GDT_SIZE - 1;
  46. load_gdt(&gdt_descr);
  47. }
  48. struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  49. static void __cpuinit default_init(struct cpuinfo_x86 *c)
  50. {
  51. display_cacheinfo(c);
  52. }
  53. static struct cpu_dev __cpuinitdata default_cpu = {
  54. .c_init = default_init,
  55. .c_vendor = "Unknown",
  56. };
  57. static struct cpu_dev *this_cpu __cpuinitdata = &default_cpu;
  58. int __cpuinit get_model_name(struct cpuinfo_x86 *c)
  59. {
  60. unsigned int *v;
  61. if (c->extended_cpuid_level < 0x80000004)
  62. return 0;
  63. v = (unsigned int *) c->x86_model_id;
  64. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  65. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  66. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  67. c->x86_model_id[48] = 0;
  68. return 1;
  69. }
  70. void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
  71. {
  72. unsigned int n, dummy, eax, ebx, ecx, edx;
  73. n = c->extended_cpuid_level;
  74. if (n >= 0x80000005) {
  75. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  76. printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), "
  77. "D cache %dK (%d bytes/line)\n",
  78. edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
  79. c->x86_cache_size = (ecx>>24) + (edx>>24);
  80. /* On K8 L1 TLB is inclusive, so don't count it */
  81. c->x86_tlbsize = 0;
  82. }
  83. if (n >= 0x80000006) {
  84. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  85. ecx = cpuid_ecx(0x80000006);
  86. c->x86_cache_size = ecx >> 16;
  87. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  88. printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
  89. c->x86_cache_size, ecx & 0xFF);
  90. }
  91. if (n >= 0x80000008) {
  92. cpuid(0x80000008, &eax, &dummy, &dummy, &dummy);
  93. c->x86_virt_bits = (eax >> 8) & 0xff;
  94. c->x86_phys_bits = eax & 0xff;
  95. }
  96. }
  97. void __cpuinit detect_ht(struct cpuinfo_x86 *c)
  98. {
  99. #ifdef CONFIG_SMP
  100. u32 eax, ebx, ecx, edx;
  101. int index_msb, core_bits;
  102. cpuid(1, &eax, &ebx, &ecx, &edx);
  103. if (!cpu_has(c, X86_FEATURE_HT))
  104. return;
  105. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  106. goto out;
  107. smp_num_siblings = (ebx & 0xff0000) >> 16;
  108. if (smp_num_siblings == 1) {
  109. printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
  110. } else if (smp_num_siblings > 1) {
  111. if (smp_num_siblings > NR_CPUS) {
  112. printk(KERN_WARNING "CPU: Unsupported number of "
  113. "siblings %d", smp_num_siblings);
  114. smp_num_siblings = 1;
  115. return;
  116. }
  117. index_msb = get_count_order(smp_num_siblings);
  118. c->phys_proc_id = phys_pkg_id(index_msb);
  119. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  120. index_msb = get_count_order(smp_num_siblings);
  121. core_bits = get_count_order(c->x86_max_cores);
  122. c->cpu_core_id = phys_pkg_id(index_msb) &
  123. ((1 << core_bits) - 1);
  124. }
  125. out:
  126. if ((c->x86_max_cores * smp_num_siblings) > 1) {
  127. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  128. c->phys_proc_id);
  129. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  130. c->cpu_core_id);
  131. }
  132. #endif
  133. }
  134. static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
  135. {
  136. char *v = c->x86_vendor_id;
  137. int i;
  138. static int printed;
  139. for (i = 0; i < X86_VENDOR_NUM; i++) {
  140. if (cpu_devs[i]) {
  141. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  142. (cpu_devs[i]->c_ident[1] &&
  143. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  144. c->x86_vendor = i;
  145. this_cpu = cpu_devs[i];
  146. return;
  147. }
  148. }
  149. }
  150. if (!printed) {
  151. printed++;
  152. printk(KERN_ERR "CPU: Vendor unknown, using generic init.\n");
  153. printk(KERN_ERR "CPU: Your system may be unstable.\n");
  154. }
  155. c->x86_vendor = X86_VENDOR_UNKNOWN;
  156. }
  157. static void __init early_cpu_support_print(void)
  158. {
  159. int i,j;
  160. struct cpu_dev *cpu_devx;
  161. printk("KERNEL supported cpus:\n");
  162. for (i = 0; i < X86_VENDOR_NUM; i++) {
  163. cpu_devx = cpu_devs[i];
  164. if (!cpu_devx)
  165. continue;
  166. for (j = 0; j < 2; j++) {
  167. if (!cpu_devx->c_ident[j])
  168. continue;
  169. printk(" %s %s\n", cpu_devx->c_vendor,
  170. cpu_devx->c_ident[j]);
  171. }
  172. }
  173. }
  174. static void __cpuinit early_identify_cpu(struct cpuinfo_x86 *c);
  175. void __init early_cpu_init(void)
  176. {
  177. struct cpu_vendor_dev *cvdev;
  178. for (cvdev = __x86cpuvendor_start ;
  179. cvdev < __x86cpuvendor_end ;
  180. cvdev++)
  181. cpu_devs[cvdev->vendor] = cvdev->cpu_dev;
  182. early_cpu_support_print();
  183. early_identify_cpu(&boot_cpu_data);
  184. }
  185. /* Do some early cpuid on the boot CPU to get some parameter that are
  186. needed before check_bugs. Everything advanced is in identify_cpu
  187. below. */
  188. static void __cpuinit early_identify_cpu(struct cpuinfo_x86 *c)
  189. {
  190. u32 tfms, xlvl;
  191. c->loops_per_jiffy = loops_per_jiffy;
  192. c->x86_cache_size = -1;
  193. c->x86_vendor = X86_VENDOR_UNKNOWN;
  194. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  195. c->x86_vendor_id[0] = '\0'; /* Unset */
  196. c->x86_model_id[0] = '\0'; /* Unset */
  197. c->x86_clflush_size = 64;
  198. c->x86_cache_alignment = c->x86_clflush_size;
  199. c->x86_max_cores = 1;
  200. c->x86_coreid_bits = 0;
  201. c->extended_cpuid_level = 0;
  202. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  203. /* Get vendor name */
  204. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  205. (unsigned int *)&c->x86_vendor_id[0],
  206. (unsigned int *)&c->x86_vendor_id[8],
  207. (unsigned int *)&c->x86_vendor_id[4]);
  208. get_cpu_vendor(c);
  209. /* Initialize the standard set of capabilities */
  210. /* Note that the vendor-specific code below might override */
  211. /* Intel-defined flags: level 0x00000001 */
  212. if (c->cpuid_level >= 0x00000001) {
  213. __u32 misc;
  214. cpuid(0x00000001, &tfms, &misc, &c->x86_capability[4],
  215. &c->x86_capability[0]);
  216. c->x86 = (tfms >> 8) & 0xf;
  217. c->x86_model = (tfms >> 4) & 0xf;
  218. c->x86_mask = tfms & 0xf;
  219. if (c->x86 == 0xf)
  220. c->x86 += (tfms >> 20) & 0xff;
  221. if (c->x86 >= 0x6)
  222. c->x86_model += ((tfms >> 16) & 0xF) << 4;
  223. if (test_cpu_cap(c, X86_FEATURE_CLFLSH))
  224. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  225. } else {
  226. /* Have CPUID level 0 only - unheard of */
  227. c->x86 = 4;
  228. }
  229. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xff;
  230. #ifdef CONFIG_SMP
  231. c->phys_proc_id = c->initial_apicid;
  232. #endif
  233. /* AMD-defined flags: level 0x80000001 */
  234. xlvl = cpuid_eax(0x80000000);
  235. c->extended_cpuid_level = xlvl;
  236. if ((xlvl & 0xffff0000) == 0x80000000) {
  237. if (xlvl >= 0x80000001) {
  238. c->x86_capability[1] = cpuid_edx(0x80000001);
  239. c->x86_capability[6] = cpuid_ecx(0x80000001);
  240. }
  241. if (xlvl >= 0x80000004)
  242. get_model_name(c); /* Default name */
  243. }
  244. /* Transmeta-defined flags: level 0x80860001 */
  245. xlvl = cpuid_eax(0x80860000);
  246. if ((xlvl & 0xffff0000) == 0x80860000) {
  247. /* Don't set x86_cpuid_level here for now to not confuse. */
  248. if (xlvl >= 0x80860001)
  249. c->x86_capability[2] = cpuid_edx(0x80860001);
  250. }
  251. c->extended_cpuid_level = cpuid_eax(0x80000000);
  252. if (c->extended_cpuid_level >= 0x80000007)
  253. c->x86_power = cpuid_edx(0x80000007);
  254. if (c->x86_vendor != X86_VENDOR_UNKNOWN &&
  255. cpu_devs[c->x86_vendor]->c_early_init)
  256. cpu_devs[c->x86_vendor]->c_early_init(c);
  257. validate_pat_support(c);
  258. /* early_param could clear that, but recall get it set again */
  259. if (disable_apic)
  260. clear_cpu_cap(c, X86_FEATURE_APIC);
  261. }
  262. /*
  263. * This does the hard work of actually picking apart the CPU stuff...
  264. */
  265. static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
  266. {
  267. int i;
  268. early_identify_cpu(c);
  269. init_scattered_cpuid_features(c);
  270. c->apicid = phys_pkg_id(0);
  271. /*
  272. * Vendor-specific initialization. In this section we
  273. * canonicalize the feature flags, meaning if there are
  274. * features a certain CPU supports which CPUID doesn't
  275. * tell us, CPUID claiming incorrect flags, or other bugs,
  276. * we handle them here.
  277. *
  278. * At the end of this section, c->x86_capability better
  279. * indicate the features this CPU genuinely supports!
  280. */
  281. if (this_cpu->c_init)
  282. this_cpu->c_init(c);
  283. detect_ht(c);
  284. /*
  285. * On SMP, boot_cpu_data holds the common feature set between
  286. * all CPUs; so make sure that we indicate which features are
  287. * common between the CPUs. The first time this routine gets
  288. * executed, c == &boot_cpu_data.
  289. */
  290. if (c != &boot_cpu_data) {
  291. /* AND the already accumulated flags with these */
  292. for (i = 0; i < NCAPINTS; i++)
  293. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  294. }
  295. /* Clear all flags overriden by options */
  296. for (i = 0; i < NCAPINTS; i++)
  297. c->x86_capability[i] &= ~cleared_cpu_caps[i];
  298. #ifdef CONFIG_X86_MCE
  299. mcheck_init(c);
  300. #endif
  301. select_idle_routine(c);
  302. #ifdef CONFIG_NUMA
  303. numa_add_cpu(smp_processor_id());
  304. #endif
  305. }
  306. void __cpuinit identify_boot_cpu(void)
  307. {
  308. identify_cpu(&boot_cpu_data);
  309. }
  310. void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
  311. {
  312. BUG_ON(c == &boot_cpu_data);
  313. identify_cpu(c);
  314. mtrr_ap_init();
  315. }
  316. static __init int setup_noclflush(char *arg)
  317. {
  318. setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
  319. return 1;
  320. }
  321. __setup("noclflush", setup_noclflush);
  322. void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
  323. {
  324. if (c->x86_model_id[0])
  325. printk(KERN_CONT "%s", c->x86_model_id);
  326. if (c->x86_mask || c->cpuid_level >= 0)
  327. printk(KERN_CONT " stepping %02x\n", c->x86_mask);
  328. else
  329. printk(KERN_CONT "\n");
  330. }
  331. static __init int setup_disablecpuid(char *arg)
  332. {
  333. int bit;
  334. if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  335. setup_clear_cpu_cap(bit);
  336. else
  337. return 0;
  338. return 1;
  339. }
  340. __setup("clearcpuid=", setup_disablecpuid);