centrino-decode.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * (C) 2003 - 2004 Dominik Brodowski <linux@dominikbrodowski.de>
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2.
  5. *
  6. * Based on code found in
  7. * linux/arch/i386/kernel/cpu/cpufreq/speedstep-centrino.c
  8. * and originally developed by Jeremy Fitzhardinge.
  9. *
  10. * USAGE: simply run it to decode the current settings on CPU 0,
  11. * or pass the CPU number as argument, or pass the MSR content
  12. * as argument.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #define MCPU 32
  23. #define MSR_IA32_PERF_STATUS 0x198
  24. static int rdmsr(unsigned int cpu, unsigned int msr,
  25. unsigned int *lo, unsigned int *hi)
  26. {
  27. int fd;
  28. char file[20];
  29. unsigned long long val;
  30. int retval = -1;
  31. *lo = *hi = 0;
  32. if (cpu > MCPU)
  33. goto err1;
  34. sprintf(file, "/dev/cpu/%d/msr", cpu);
  35. fd = open(file, O_RDONLY);
  36. if (fd < 0)
  37. goto err1;
  38. if (lseek(fd, msr, SEEK_CUR) == -1)
  39. goto err2;
  40. if (read(fd, &val, 8) != 8)
  41. goto err2;
  42. *lo = (uint32_t )(val & 0xffffffffull);
  43. *hi = (uint32_t )(val>>32 & 0xffffffffull);
  44. retval = 0;
  45. err2:
  46. close(fd);
  47. err1:
  48. return retval;
  49. }
  50. static void decode (unsigned int msr)
  51. {
  52. unsigned int multiplier;
  53. unsigned int mv;
  54. multiplier = ((msr >> 8) & 0xFF);
  55. mv = (((msr & 0xFF) * 16) + 700);
  56. printf("0x%x means multiplier %d @ %d mV\n", msr, multiplier, mv);
  57. }
  58. static int decode_live(unsigned int cpu)
  59. {
  60. unsigned int lo, hi;
  61. int err;
  62. err = rdmsr(cpu, MSR_IA32_PERF_STATUS, &lo, &hi);
  63. if (err) {
  64. printf("can't get MSR_IA32_PERF_STATUS for cpu %d\n", cpu);
  65. printf("Possible trouble: you don't run an Enhanced SpeedStep capable cpu\n");
  66. printf("or you are not root, or the msr driver is not present\n");
  67. return 1;
  68. }
  69. decode(lo);
  70. return 0;
  71. }
  72. int main (int argc, char **argv)
  73. {
  74. unsigned int cpu, mode = 0;
  75. if (argc < 2)
  76. cpu = 0;
  77. else {
  78. cpu = strtoul(argv[1], NULL, 0);
  79. if (cpu >= MCPU)
  80. mode = 1;
  81. }
  82. if (mode)
  83. decode(cpu);
  84. else
  85. decode_live(cpu);
  86. return 0;
  87. }