powernow-k8-decode.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
  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/powernow-k8.c
  8. * and originally developed by Paul Devriendt
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include <unistd.h>
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #define MCPU 32
  19. #define MSR_FIDVID_STATUS 0xc0010042
  20. #define MSR_S_HI_CURRENT_VID 0x0000001f
  21. #define MSR_S_LO_CURRENT_FID 0x0000003f
  22. static int get_fidvid(uint32_t cpu, uint32_t *fid, uint32_t *vid)
  23. {
  24. int err = 1;
  25. uint64_t msr = 0;
  26. int fd;
  27. char file[20];
  28. if (cpu > MCPU)
  29. goto out;
  30. sprintf(file, "/dev/cpu/%d/msr", cpu);
  31. fd = open(file, O_RDONLY);
  32. if (fd < 0)
  33. goto out;
  34. lseek(fd, MSR_FIDVID_STATUS, SEEK_CUR);
  35. if (read(fd, &msr, 8) != 8)
  36. goto err1;
  37. *fid = ((uint32_t )(msr & 0xffffffffull)) & MSR_S_LO_CURRENT_FID;
  38. *vid = ((uint32_t )(msr>>32 & 0xffffffffull)) & MSR_S_HI_CURRENT_VID;
  39. err = 0;
  40. err1:
  41. close(fd);
  42. out:
  43. return err;
  44. }
  45. /* Return a frequency in MHz, given an input fid */
  46. static uint32_t find_freq_from_fid(uint32_t fid)
  47. {
  48. return 800 + (fid * 100);
  49. }
  50. /* Return a voltage in miliVolts, given an input vid */
  51. static uint32_t find_millivolts_from_vid(uint32_t vid)
  52. {
  53. return 1550-vid*25;
  54. }
  55. int main (int argc, char *argv[])
  56. {
  57. int err;
  58. int cpu;
  59. uint32_t fid, vid;
  60. if (argc < 2)
  61. cpu = 0;
  62. else
  63. cpu = strtoul(argv[1], NULL, 0);
  64. err = get_fidvid(cpu, &fid, &vid);
  65. if (err) {
  66. printf("can't get fid, vid from MSR\n");
  67. printf("Possible trouble: you don't run a powernow-k8 capable cpu\n");
  68. printf("or you are not root, or the msr driver is not present\n");
  69. exit(1);
  70. }
  71. printf("cpu %d currently at %d MHz and %d mV\n",
  72. cpu,
  73. find_freq_from_fid(fid),
  74. find_millivolts_from_vid(vid));
  75. return 0;
  76. }