addon_cpuid_features.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Routines to indentify additional cpu features that are scattered in
  3. * cpuid space.
  4. */
  5. #include <linux/cpu.h>
  6. #include <asm/pat.h>
  7. #include <asm/processor.h>
  8. #include <asm/apic.h>
  9. struct cpuid_bit {
  10. u16 feature;
  11. u8 reg;
  12. u8 bit;
  13. u32 level;
  14. u32 sub_leaf;
  15. };
  16. enum cpuid_regs {
  17. CR_EAX = 0,
  18. CR_ECX,
  19. CR_EDX,
  20. CR_EBX
  21. };
  22. void __cpuinit init_scattered_cpuid_features(struct cpuinfo_x86 *c)
  23. {
  24. u32 max_level;
  25. u32 regs[4];
  26. const struct cpuid_bit *cb;
  27. static const struct cpuid_bit __cpuinitconst cpuid_bits[] = {
  28. { X86_FEATURE_IDA, CR_EAX, 1, 0x00000006, 0 },
  29. { X86_FEATURE_ARAT, CR_EAX, 2, 0x00000006, 0 },
  30. { X86_FEATURE_APERFMPERF, CR_ECX, 0, 0x00000006, 0 },
  31. { X86_FEATURE_EPB, CR_ECX, 3, 0x00000006, 0 },
  32. { X86_FEATURE_XSAVEOPT, CR_EAX, 0, 0x0000000d, 1 },
  33. { X86_FEATURE_CPB, CR_EDX, 9, 0x80000007, 0 },
  34. { X86_FEATURE_NPT, CR_EDX, 0, 0x8000000a, 0 },
  35. { X86_FEATURE_LBRV, CR_EDX, 1, 0x8000000a, 0 },
  36. { X86_FEATURE_SVML, CR_EDX, 2, 0x8000000a, 0 },
  37. { X86_FEATURE_NRIPS, CR_EDX, 3, 0x8000000a, 0 },
  38. { 0, 0, 0, 0, 0 }
  39. };
  40. for (cb = cpuid_bits; cb->feature; cb++) {
  41. /* Verify that the level is valid */
  42. max_level = cpuid_eax(cb->level & 0xffff0000);
  43. if (max_level < cb->level ||
  44. max_level > (cb->level | 0xffff))
  45. continue;
  46. cpuid_count(cb->level, cb->sub_leaf, &regs[CR_EAX],
  47. &regs[CR_EBX], &regs[CR_ECX], &regs[CR_EDX]);
  48. if (regs[cb->reg] & (1 << cb->bit))
  49. set_cpu_cap(c, cb->feature);
  50. }
  51. }
  52. /* leaf 0xb SMT level */
  53. #define SMT_LEVEL 0
  54. /* leaf 0xb sub-leaf types */
  55. #define INVALID_TYPE 0
  56. #define SMT_TYPE 1
  57. #define CORE_TYPE 2
  58. #define LEAFB_SUBTYPE(ecx) (((ecx) >> 8) & 0xff)
  59. #define BITS_SHIFT_NEXT_LEVEL(eax) ((eax) & 0x1f)
  60. #define LEVEL_MAX_SIBLINGS(ebx) ((ebx) & 0xffff)
  61. /*
  62. * Check for extended topology enumeration cpuid leaf 0xb and if it
  63. * exists, use it for populating initial_apicid and cpu topology
  64. * detection.
  65. */
  66. void __cpuinit detect_extended_topology(struct cpuinfo_x86 *c)
  67. {
  68. #ifdef CONFIG_SMP
  69. unsigned int eax, ebx, ecx, edx, sub_index;
  70. unsigned int ht_mask_width, core_plus_mask_width;
  71. unsigned int core_select_mask, core_level_siblings;
  72. static bool printed;
  73. if (c->cpuid_level < 0xb)
  74. return;
  75. cpuid_count(0xb, SMT_LEVEL, &eax, &ebx, &ecx, &edx);
  76. /*
  77. * check if the cpuid leaf 0xb is actually implemented.
  78. */
  79. if (ebx == 0 || (LEAFB_SUBTYPE(ecx) != SMT_TYPE))
  80. return;
  81. set_cpu_cap(c, X86_FEATURE_XTOPOLOGY);
  82. /*
  83. * initial apic id, which also represents 32-bit extended x2apic id.
  84. */
  85. c->initial_apicid = edx;
  86. /*
  87. * Populate HT related information from sub-leaf level 0.
  88. */
  89. core_level_siblings = smp_num_siblings = LEVEL_MAX_SIBLINGS(ebx);
  90. core_plus_mask_width = ht_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
  91. sub_index = 1;
  92. do {
  93. cpuid_count(0xb, sub_index, &eax, &ebx, &ecx, &edx);
  94. /*
  95. * Check for the Core type in the implemented sub leaves.
  96. */
  97. if (LEAFB_SUBTYPE(ecx) == CORE_TYPE) {
  98. core_level_siblings = LEVEL_MAX_SIBLINGS(ebx);
  99. core_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
  100. break;
  101. }
  102. sub_index++;
  103. } while (LEAFB_SUBTYPE(ecx) != INVALID_TYPE);
  104. core_select_mask = (~(-1 << core_plus_mask_width)) >> ht_mask_width;
  105. c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, ht_mask_width)
  106. & core_select_mask;
  107. c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, core_plus_mask_width);
  108. /*
  109. * Reinit the apicid, now that we have extended initial_apicid.
  110. */
  111. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  112. c->x86_max_cores = (core_level_siblings / smp_num_siblings);
  113. if (!printed) {
  114. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  115. c->phys_proc_id);
  116. if (c->x86_max_cores > 1)
  117. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  118. c->cpu_core_id);
  119. printed = 1;
  120. }
  121. return;
  122. #endif
  123. }