powernow-k8-decode.c 1.8 KB

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