Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / power / cpupower / debug / i386 / powernow-k8-decode.c
blob638a6b3bfd9711783348b16743b2d301450e7351
1 /*
2 * (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
4 * Licensed under the terms of the GNU GPL License version 2.
6 * Based on code found in
7 * linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c
8 * and originally developed by Paul Devriendt
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdint.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <fcntl.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
21 #define MCPU 32
23 #define MSR_FIDVID_STATUS 0xc0010042
25 #define MSR_S_HI_CURRENT_VID 0x0000001f
26 #define MSR_S_LO_CURRENT_FID 0x0000003f
28 static int get_fidvid(uint32_t cpu, uint32_t *fid, uint32_t *vid)
30 int err = 1;
31 uint64_t msr = 0;
32 int fd;
33 char file[20];
35 if (cpu > MCPU)
36 goto out;
38 sprintf(file, "/dev/cpu/%d/msr", cpu);
40 fd = open(file, O_RDONLY);
41 if (fd < 0)
42 goto out;
43 lseek(fd, MSR_FIDVID_STATUS, SEEK_CUR);
44 if (read(fd, &msr, 8) != 8)
45 goto err1;
47 *fid = ((uint32_t )(msr & 0xffffffffull)) & MSR_S_LO_CURRENT_FID;
48 *vid = ((uint32_t )(msr>>32 & 0xffffffffull)) & MSR_S_HI_CURRENT_VID;
49 err = 0;
50 err1:
51 close(fd);
52 out:
53 return err;
57 /* Return a frequency in MHz, given an input fid */
58 static uint32_t find_freq_from_fid(uint32_t fid)
60 return 800 + (fid * 100);
63 /* Return a voltage in miliVolts, given an input vid */
64 static uint32_t find_millivolts_from_vid(uint32_t vid)
66 return 1550-vid*25;
69 int main (int argc, char *argv[])
71 int err;
72 int cpu;
73 uint32_t fid, vid;
75 if (argc < 2)
76 cpu = 0;
77 else
78 cpu = strtoul(argv[1], NULL, 0);
80 err = get_fidvid(cpu, &fid, &vid);
82 if (err) {
83 printf("can't get fid, vid from MSR\n");
84 printf("Possible trouble: you don't run a powernow-k8 capable cpu\n");
85 printf("or you are not root, or the msr driver is not present\n");
86 exit(1);
90 printf("cpu %d currently at %d MHz and %d mV\n",
91 cpu,
92 find_freq_from_fid(fid),
93 find_millivolts_from_vid(vid));
95 return 0;