Path: blob/master/tools/power/cpupower/debug/i386/powernow-k8-decode.c
26296 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* (C) 2004 Bruno Ducrot <[email protected]>3*4* Based on code found in5* linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c6* and originally developed by Paul Devriendt7*/89#include <stdio.h>10#include <stdlib.h>11#include <stdint.h>12#include <unistd.h>13#include <errno.h>14#include <fcntl.h>1516#include <sys/types.h>17#include <sys/stat.h>1819#define MCPU 322021#define MSR_FIDVID_STATUS 0xc00100422223#define MSR_S_HI_CURRENT_VID 0x0000001f24#define MSR_S_LO_CURRENT_FID 0x0000003f2526static int get_fidvid(uint32_t cpu, uint32_t *fid, uint32_t *vid)27{28int err = 1;29uint64_t msr = 0;30int fd;31char file[20];3233if (cpu > MCPU)34goto out;3536sprintf(file, "/dev/cpu/%d/msr", cpu);3738fd = open(file, O_RDONLY);39if (fd < 0)40goto out;41lseek(fd, MSR_FIDVID_STATUS, SEEK_CUR);42if (read(fd, &msr, 8) != 8)43goto err1;4445*fid = ((uint32_t )(msr & 0xffffffffull)) & MSR_S_LO_CURRENT_FID;46*vid = ((uint32_t )(msr>>32 & 0xffffffffull)) & MSR_S_HI_CURRENT_VID;47err = 0;48err1:49close(fd);50out:51return err;52}535455/* Return a frequency in MHz, given an input fid */56static uint32_t find_freq_from_fid(uint32_t fid)57{58return 800 + (fid * 100);59}6061/* Return a voltage in miliVolts, given an input vid */62static uint32_t find_millivolts_from_vid(uint32_t vid)63{64return 1550-vid*25;65}6667int main (int argc, char *argv[])68{69int err;70int cpu;71uint32_t fid, vid;7273if (argc < 2)74cpu = 0;75else76cpu = strtoul(argv[1], NULL, 0);7778err = get_fidvid(cpu, &fid, &vid);7980if (err) {81printf("can't get fid, vid from MSR\n");82printf("Possible trouble: you don't run a powernow-k8 capable cpu\n");83printf("or you are not root, or the msr driver is not present\n");84exit(1);85}868788printf("cpu %d currently at %d MHz and %d mV\n",89cpu,90find_freq_from_fid(fid),91find_millivolts_from_vid(vid));9293return 0;94}959697