powernow-k8-decode.c (1849B)
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 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 17#include <sys/types.h> 18#include <sys/stat.h> 19 20#define MCPU 32 21 22#define MSR_FIDVID_STATUS 0xc0010042 23 24#define MSR_S_HI_CURRENT_VID 0x0000001f 25#define MSR_S_LO_CURRENT_FID 0x0000003f 26 27static int get_fidvid(uint32_t cpu, uint32_t *fid, uint32_t *vid) 28{ 29 int err = 1; 30 uint64_t msr = 0; 31 int fd; 32 char file[20]; 33 34 if (cpu > MCPU) 35 goto out; 36 37 sprintf(file, "/dev/cpu/%d/msr", cpu); 38 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; 45 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; 49err1: 50 close(fd); 51out: 52 return err; 53} 54 55 56/* Return a frequency in MHz, given an input fid */ 57static uint32_t find_freq_from_fid(uint32_t fid) 58{ 59 return 800 + (fid * 100); 60} 61 62/* Return a voltage in miliVolts, given an input vid */ 63static uint32_t find_millivolts_from_vid(uint32_t vid) 64{ 65 return 1550-vid*25; 66} 67 68int main (int argc, char *argv[]) 69{ 70 int err; 71 int cpu; 72 uint32_t fid, vid; 73 74 if (argc < 2) 75 cpu = 0; 76 else 77 cpu = strtoul(argv[1], NULL, 0); 78 79 err = get_fidvid(cpu, &fid, &vid); 80 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); 86 } 87 88 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)); 93 94 return 0; 95}