Path: blob/master/tools/power/cpupower/debug/i386/centrino-decode.c
26296 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* (C) 2003 - 2004 Dominik Brodowski <[email protected]>3*4* Based on code found in5* linux/arch/i386/kernel/cpu/cpufreq/speedstep-centrino.c6* and originally developed by Jeremy Fitzhardinge.7*8* USAGE: simply run it to decode the current settings on CPU 0,9* or pass the CPU number as argument, or pass the MSR content10* as argument.11*/1213#include <stdio.h>14#include <stdlib.h>15#include <stdint.h>16#include <unistd.h>17#include <errno.h>18#include <fcntl.h>1920#include <sys/types.h>21#include <sys/stat.h>2223#define MCPU 322425#define MSR_IA32_PERF_STATUS 0x1982627static int rdmsr(unsigned int cpu, unsigned int msr,28unsigned int *lo, unsigned int *hi)29{30int fd;31char file[20];32unsigned long long val;33int retval = -1;3435*lo = *hi = 0;3637if (cpu > MCPU)38goto err1;3940sprintf(file, "/dev/cpu/%d/msr", cpu);41fd = open(file, O_RDONLY);4243if (fd < 0)44goto err1;4546if (lseek(fd, msr, SEEK_CUR) == -1)47goto err2;4849if (read(fd, &val, 8) != 8)50goto err2;5152*lo = (uint32_t )(val & 0xffffffffull);53*hi = (uint32_t )(val>>32 & 0xffffffffull);5455retval = 0;56err2:57close(fd);58err1:59return retval;60}6162static void decode (unsigned int msr)63{64unsigned int multiplier;65unsigned int mv;6667multiplier = ((msr >> 8) & 0xFF);6869mv = (((msr & 0xFF) * 16) + 700);7071printf("0x%x means multiplier %d @ %d mV\n", msr, multiplier, mv);72}7374static int decode_live(unsigned int cpu)75{76unsigned int lo, hi;77int err;7879err = rdmsr(cpu, MSR_IA32_PERF_STATUS, &lo, &hi);8081if (err) {82printf("can't get MSR_IA32_PERF_STATUS for cpu %d\n", cpu);83printf("Possible trouble: you don't run an Enhanced SpeedStep capable cpu\n");84printf("or you are not root, or the msr driver is not present\n");85return 1;86}8788decode(lo);8990return 0;91}9293int main (int argc, char **argv)94{95unsigned int cpu, mode = 0;9697if (argc < 2)98cpu = 0;99else {100cpu = strtoul(argv[1], NULL, 0);101if (cpu >= MCPU)102mode = 1;103}104105if (mode)106decode(cpu);107else108decode_live(cpu);109110return 0;111}112113114