dump_psb.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * dump_psb. (c) 2004, Dave Jones, Red Hat Inc.
  3. * Licensed under the GPL v2.
  4. */
  5. #include <fcntl.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #define _GNU_SOURCE
  11. #include <getopt.h>
  12. #include <sys/mman.h>
  13. #define LEN (0x100000 - 0xc0000)
  14. #define OFFSET (0xc0000)
  15. #ifndef __packed
  16. #define __packed __attribute((packed))
  17. #endif
  18. static long relevant;
  19. static const int fid_to_mult[32] = {
  20. 110, 115, 120, 125, 50, 55, 60, 65,
  21. 70, 75, 80, 85, 90, 95, 100, 105,
  22. 30, 190, 40, 200, 130, 135, 140, 210,
  23. 150, 225, 160, 165, 170, 180, -1, -1,
  24. };
  25. static const int vid_to_voltage[32] = {
  26. 2000, 1950, 1900, 1850, 1800, 1750, 1700, 1650,
  27. 1600, 1550, 1500, 1450, 1400, 1350, 1300, 0,
  28. 1275, 1250, 1225, 1200, 1175, 1150, 1125, 1100,
  29. 1075, 1050, 1024, 1000, 975, 950, 925, 0,
  30. };
  31. struct psb_header {
  32. char signature[10];
  33. u_char version;
  34. u_char flags;
  35. u_short settlingtime;
  36. u_char res1;
  37. u_char numpst;
  38. } __packed;
  39. struct pst_header {
  40. u_int32_t cpuid;
  41. u_char fsb;
  42. u_char maxfid;
  43. u_char startvid;
  44. u_char numpstates;
  45. } __packed;
  46. static u_int fsb;
  47. static u_int sgtc;
  48. static int
  49. decode_pst(char *p, int npstates)
  50. {
  51. int i;
  52. int freq, fid, vid;
  53. for (i = 0; i < npstates; ++i) {
  54. fid = *p++;
  55. vid = *p++;
  56. freq = 100 * fid_to_mult[fid] * fsb;
  57. printf(" %2d %8dkHz FID %02x (%2d.%01d) VID %02x (%4dmV)\n",
  58. i,
  59. freq,
  60. fid, fid_to_mult[fid]/10, fid_to_mult[fid]%10,
  61. vid, vid_to_voltage[vid]);
  62. }
  63. return 0;
  64. }
  65. static
  66. void decode_psb(char *p, int numpst)
  67. {
  68. int i;
  69. struct psb_header *psb;
  70. struct pst_header *pst;
  71. psb = (struct psb_header*) p;
  72. if (psb->version != 0x12)
  73. return;
  74. printf("PSB version: %hhx flags: %hhx settling time %hhuus res1 %hhx num pst %hhu\n",
  75. psb->version,
  76. psb->flags,
  77. psb->settlingtime,
  78. psb->res1,
  79. psb->numpst);
  80. sgtc = psb->settlingtime * 100;
  81. if (sgtc < 10000)
  82. sgtc = 10000;
  83. p = ((char *) psb) + sizeof(struct psb_header);
  84. if (numpst < 0)
  85. numpst = psb->numpst;
  86. else
  87. printf("Overriding number of pst :%d\n", numpst);
  88. for (i = 0; i < numpst; i++) {
  89. pst = (struct pst_header*) p;
  90. if (relevant != 0) {
  91. if (relevant!= pst->cpuid)
  92. goto next_one;
  93. }
  94. printf(" PST %d cpuid %.3x fsb %hhu mfid %hhx svid %hhx numberstates %hhu\n",
  95. i+1,
  96. pst->cpuid,
  97. pst->fsb,
  98. pst->maxfid,
  99. pst->startvid,
  100. pst->numpstates);
  101. fsb = pst->fsb;
  102. decode_pst(p + sizeof(struct pst_header), pst->numpstates);
  103. next_one:
  104. p += sizeof(struct pst_header) + 2*pst->numpstates;
  105. }
  106. }
  107. static struct option info_opts[] = {
  108. {"numpst", no_argument, NULL, 'n'},
  109. };
  110. void print_help(void)
  111. {
  112. printf ("Usage: dump_psb [options]\n");
  113. printf ("Options:\n");
  114. printf (" -n, --numpst Set number of PST tables to scan\n");
  115. printf (" -r, --relevant Only display PSTs relevant to cpuid N\n");
  116. }
  117. int
  118. main(int argc, char *argv[])
  119. {
  120. int fd;
  121. int numpst=-1;
  122. int ret=0, cont=1;
  123. char *mem = NULL;
  124. char *p;
  125. do {
  126. ret = getopt_long(argc, argv, "hr:n:", info_opts, NULL);
  127. switch (ret){
  128. case '?':
  129. case 'h':
  130. print_help();
  131. cont = 0;
  132. break;
  133. case 'r':
  134. relevant = strtol(optarg, NULL, 16);
  135. break;
  136. case 'n':
  137. numpst = strtol(optarg, NULL, 10);
  138. break;
  139. case -1:
  140. cont = 0;
  141. break;
  142. }
  143. } while(cont);
  144. fd = open("/dev/mem", O_RDONLY);
  145. if (fd < 0) {
  146. printf ("Couldn't open /dev/mem. Are you root?\n");
  147. exit(1);
  148. }
  149. mem = mmap(mem, 0x100000 - 0xc0000, PROT_READ, MAP_SHARED, fd, 0xc0000);
  150. close(fd);
  151. for (p = mem; p - mem < LEN; p+=16) {
  152. if (memcmp(p, "AMDK7PNOW!", 10) == 0) {
  153. decode_psb(p, numpst);
  154. break;
  155. }
  156. }
  157. munmap(mem, LEN);
  158. return 0;
  159. }