Merge tag 'ntb-5.11' of git://github.com/jonmason/ntb
[linux/fpc-iii.git] / tools / power / cpupower / debug / i386 / powernow-k8-decode.c
blob735dca1e25bcdafaa6cd57c615a98d78ea09a917
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
5 * Based on code found in
6 * linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c
7 * and originally developed by Paul Devriendt
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <fcntl.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
20 #define MCPU 32
22 #define MSR_FIDVID_STATUS 0xc0010042
24 #define MSR_S_HI_CURRENT_VID 0x0000001f
25 #define MSR_S_LO_CURRENT_FID 0x0000003f
27 static int get_fidvid(uint32_t cpu, uint32_t *fid, uint32_t *vid)
29 int err = 1;
30 uint64_t msr = 0;
31 int fd;
32 char file[20];
34 if (cpu > MCPU)
35 goto out;
37 sprintf(file, "/dev/cpu/%d/msr", cpu);
39 fd = open(file, O_RDONLY);
40 if (fd < 0)
41 goto out;
42 lseek(fd, MSR_FIDVID_STATUS, SEEK_CUR);
43 if (read(fd, &msr, 8) != 8)
44 goto err1;
46 *fid = ((uint32_t )(msr & 0xffffffffull)) & MSR_S_LO_CURRENT_FID;
47 *vid = ((uint32_t )(msr>>32 & 0xffffffffull)) & MSR_S_HI_CURRENT_VID;
48 err = 0;
49 err1:
50 close(fd);
51 out:
52 return err;
56 /* Return a frequency in MHz, given an input fid */
57 static uint32_t find_freq_from_fid(uint32_t fid)
59 return 800 + (fid * 100);
62 /* Return a voltage in miliVolts, given an input vid */
63 static uint32_t find_millivolts_from_vid(uint32_t vid)
65 return 1550-vid*25;
68 int main (int argc, char *argv[])
70 int err;
71 int cpu;
72 uint32_t fid, vid;
74 if (argc < 2)
75 cpu = 0;
76 else
77 cpu = strtoul(argv[1], NULL, 0);
79 err = get_fidvid(cpu, &fid, &vid);
81 if (err) {
82 printf("can't get fid, vid from MSR\n");
83 printf("Possible trouble: you don't run a powernow-k8 capable cpu\n");
84 printf("or you are not root, or the msr driver is not present\n");
85 exit(1);
89 printf("cpu %d currently at %d MHz and %d mV\n",
90 cpu,
91 find_freq_from_fid(fid),
92 find_millivolts_from_vid(vid));
94 return 0;