msr.c 2.3 KB

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