msr.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #if defined(__i386__) || defined(__x86_64__)
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <stdint.h>
  6. #include "helpers/helpers.h"
  7. /* Intel specific MSRs */
  8. #define MSR_IA32_PERF_STATUS 0x198
  9. #define MSR_IA32_MISC_ENABLES 0x1a0
  10. #define MSR_IA32_ENERGY_PERF_BIAS 0x1b0
  11. #define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1ad
  12. /*
  13. * read_msr
  14. *
  15. * Will return 0 on success and -1 on failure.
  16. * Possible errno values could be:
  17. * EFAULT -If the read/write did not fully complete
  18. * EIO -If the CPU does not support MSRs
  19. * ENXIO -If the CPU does not exist
  20. */
  21. int read_msr(int cpu, unsigned int idx, unsigned long long *val)
  22. {
  23. int fd;
  24. char msr_file_name[64];
  25. sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
  26. fd = open(msr_file_name, O_RDONLY);
  27. if (fd < 0)
  28. return -1;
  29. if (lseek(fd, idx, SEEK_CUR) == -1)
  30. goto err;
  31. if (read(fd, val, sizeof *val) != sizeof *val)
  32. goto err;
  33. close(fd);
  34. return 0;
  35. err:
  36. close(fd);
  37. return -1;
  38. }
  39. /*
  40. * write_msr
  41. *
  42. * Will return 0 on success and -1 on failure.
  43. * Possible errno values could be:
  44. * EFAULT -If the read/write did not fully complete
  45. * EIO -If the CPU does not support MSRs
  46. * ENXIO -If the CPU does not exist
  47. */
  48. int write_msr(int cpu, unsigned int idx, unsigned long long val)
  49. {
  50. int fd;
  51. char msr_file_name[64];
  52. sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
  53. fd = open(msr_file_name, O_WRONLY);
  54. if (fd < 0)
  55. return -1;
  56. if (lseek(fd, idx, SEEK_CUR) == -1)
  57. goto err;
  58. if (write(fd, &val, sizeof val) != sizeof val)
  59. goto err;
  60. close(fd);
  61. return 0;
  62. err:
  63. close(fd);
  64. return -1;
  65. }
  66. int msr_intel_get_perf_bias(unsigned int cpu)
  67. {
  68. unsigned long long val;
  69. int ret;
  70. if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
  71. return -1;
  72. ret = read_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &val);
  73. if (ret)
  74. return ret;
  75. return val;
  76. }
  77. int msr_intel_set_perf_bias(unsigned int cpu, unsigned int val)
  78. {
  79. int ret;
  80. if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
  81. return -1;
  82. ret = write_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, val);
  83. if (ret)
  84. return ret;
  85. return 0;
  86. }
  87. unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
  88. {
  89. unsigned long long val;
  90. int ret;
  91. if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO))
  92. return -1;
  93. ret = read_msr(cpu, MSR_NEHALEM_TURBO_RATIO_LIMIT, &val);
  94. if (ret)
  95. return ret;
  96. return val;
  97. }
  98. #endif