cpuid.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include "helpers/helpers.h"
  7. static const char *cpu_vendor_table[X86_VENDOR_MAX] = {
  8. "Unknown", "GenuineIntel", "AuthenticAMD",
  9. };
  10. #if defined(__i386__) || defined(__x86_64__)
  11. /* from gcc */
  12. #include <cpuid.h>
  13. /*
  14. * CPUID functions returning a single datum
  15. *
  16. * Define unsigned int cpuid_e[abcd]x(unsigned int op)
  17. */
  18. #define cpuid_func(reg) \
  19. unsigned int cpuid_##reg(unsigned int op) \
  20. { \
  21. unsigned int eax, ebx, ecx, edx; \
  22. __cpuid(op, eax, ebx, ecx, edx); \
  23. return reg; \
  24. }
  25. cpuid_func(eax);
  26. cpuid_func(ebx);
  27. cpuid_func(ecx);
  28. cpuid_func(edx);
  29. #endif /* defined(__i386__) || defined(__x86_64__) */
  30. /* get_cpu_info
  31. *
  32. * Extract CPU vendor, family, model, stepping info from /proc/cpuinfo
  33. *
  34. * Returns 0 on success or a negativ error code
  35. *
  36. * TBD: Should there be a cpuid alternative for this if /proc is not mounted?
  37. */
  38. int get_cpu_info(unsigned int cpu, struct cpupower_cpu_info *cpu_info)
  39. {
  40. FILE *fp;
  41. char value[64];
  42. unsigned int proc, x;
  43. unsigned int unknown = 0xffffff;
  44. unsigned int cpuid_level, ext_cpuid_level;
  45. int ret = -EINVAL;
  46. cpu_info->vendor = X86_VENDOR_UNKNOWN;
  47. cpu_info->family = unknown;
  48. cpu_info->model = unknown;
  49. cpu_info->stepping = unknown;
  50. cpu_info->caps = 0;
  51. fp = fopen("/proc/cpuinfo", "r");
  52. if (!fp)
  53. return -EIO;
  54. while (!feof(fp)) {
  55. if (!fgets(value, 64, fp))
  56. continue;
  57. value[63 - 1] = '\0';
  58. if (!strncmp(value, "processor\t: ", 12))
  59. sscanf(value, "processor\t: %u", &proc);
  60. if (proc != cpu)
  61. continue;
  62. /* Get CPU vendor */
  63. if (!strncmp(value, "vendor_id", 9)) {
  64. for (x = 1; x < X86_VENDOR_MAX; x++) {
  65. if (strstr(value, cpu_vendor_table[x]))
  66. cpu_info->vendor = x;
  67. }
  68. /* Get CPU family, etc. */
  69. } else if (!strncmp(value, "cpu family\t: ", 13)) {
  70. sscanf(value, "cpu family\t: %u",
  71. &cpu_info->family);
  72. } else if (!strncmp(value, "model\t\t: ", 9)) {
  73. sscanf(value, "model\t\t: %u",
  74. &cpu_info->model);
  75. } else if (!strncmp(value, "stepping\t: ", 10)) {
  76. sscanf(value, "stepping\t: %u",
  77. &cpu_info->stepping);
  78. /* Exit -> all values must have been set */
  79. if (cpu_info->vendor == X86_VENDOR_UNKNOWN ||
  80. cpu_info->family == unknown ||
  81. cpu_info->model == unknown ||
  82. cpu_info->stepping == unknown) {
  83. ret = -EINVAL;
  84. goto out;
  85. }
  86. ret = 0;
  87. goto out;
  88. }
  89. }
  90. ret = -ENODEV;
  91. out:
  92. fclose(fp);
  93. /* Get some useful CPU capabilities from cpuid */
  94. if (cpu_info->vendor != X86_VENDOR_AMD &&
  95. cpu_info->vendor != X86_VENDOR_INTEL)
  96. return ret;
  97. cpuid_level = cpuid_eax(0);
  98. ext_cpuid_level = cpuid_eax(0x80000000);
  99. /* Invariant TSC */
  100. if (ext_cpuid_level >= 0x80000007 &&
  101. (cpuid_edx(0x80000007) & (1 << 8)))
  102. cpu_info->caps |= CPUPOWER_CAP_INV_TSC;
  103. /* Aperf/Mperf registers support */
  104. if (cpuid_level >= 6 && (cpuid_ecx(6) & 0x1))
  105. cpu_info->caps |= CPUPOWER_CAP_APERF;
  106. /* AMD Boost state enable/disable register */
  107. if (cpu_info->vendor == X86_VENDOR_AMD) {
  108. if (ext_cpuid_level >= 0x80000007 &&
  109. (cpuid_edx(0x80000007) & (1 << 9)))
  110. cpu_info->caps |= CPUPOWER_CAP_AMD_CBP;
  111. }
  112. if (cpu_info->vendor == X86_VENDOR_INTEL) {
  113. if (cpuid_level >= 6 &&
  114. (cpuid_eax(6) & (1 << 1)))
  115. cpu_info->caps |= CPUPOWER_CAP_INTEL_IDA;
  116. }
  117. if (cpu_info->vendor == X86_VENDOR_INTEL) {
  118. /* Intel's perf-bias MSR support */
  119. if (cpuid_level >= 6 && (cpuid_ecx(6) & (1 << 3)))
  120. cpu_info->caps |= CPUPOWER_CAP_PERF_BIAS;
  121. /* Intel's Turbo Ratio Limit support */
  122. if (cpu_info->family == 6) {
  123. switch (cpu_info->model) {
  124. case 0x1A: /* Core i7, Xeon 5500 series
  125. * Bloomfield, Gainstown NHM-EP
  126. */
  127. case 0x1E: /* Core i7 and i5 Processor
  128. * Clarksfield, Lynnfield, Jasper Forest
  129. */
  130. case 0x1F: /* Core i7 and i5 Processor - Nehalem */
  131. case 0x25: /* Westmere Client
  132. * Clarkdale, Arrandale
  133. */
  134. case 0x2C: /* Westmere EP - Gulftown */
  135. cpu_info->caps |= CPUPOWER_CAP_HAS_TURBO_RATIO;
  136. case 0x2A: /* SNB */
  137. case 0x2D: /* SNB Xeon */
  138. cpu_info->caps |= CPUPOWER_CAP_HAS_TURBO_RATIO;
  139. cpu_info->caps |= CPUPOWER_CAP_IS_SNB;
  140. break;
  141. case 0x2E: /* Nehalem-EX Xeon - Beckton */
  142. case 0x2F: /* Westmere-EX Xeon - Eagleton */
  143. default:
  144. break;
  145. }
  146. }
  147. }
  148. /* printf("ID: %u - Extid: 0x%x - Caps: 0x%llx\n",
  149. cpuid_level, ext_cpuid_level, cpu_info->caps);
  150. */
  151. return ret;
  152. }