Path: blob/master/tools/power/cpupower/utils/helpers/msr.c
26299 views
// SPDX-License-Identifier: GPL-2.01#if defined(__i386__) || defined(__x86_64__)23#include <fcntl.h>4#include <stdio.h>5#include <unistd.h>6#include <stdint.h>78#include "helpers/helpers.h"910/* Intel specific MSRs */11#define MSR_IA32_PERF_STATUS 0x19812#define MSR_IA32_MISC_ENABLES 0x1a013#define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1ad1415/*16* read_msr17*18* Will return 0 on success and -1 on failure.19* Possible errno values could be:20* EFAULT -If the read/write did not fully complete21* EIO -If the CPU does not support MSRs22* ENXIO -If the CPU does not exist23*/2425int read_msr(int cpu, unsigned int idx, unsigned long long *val)26{27int fd;28char msr_file_name[64];2930sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);31fd = open(msr_file_name, O_RDONLY);32if (fd < 0)33return -1;34if (lseek(fd, idx, SEEK_CUR) == -1)35goto err;36if (read(fd, val, sizeof *val) != sizeof *val)37goto err;38close(fd);39return 0;40err:41close(fd);42return -1;43}4445/*46* write_msr47*48* Will return 0 on success and -1 on failure.49* Possible errno values could be:50* EFAULT -If the read/write did not fully complete51* EIO -If the CPU does not support MSRs52* ENXIO -If the CPU does not exist53*/54int write_msr(int cpu, unsigned int idx, unsigned long long val)55{56int fd;57char msr_file_name[64];5859sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);60fd = open(msr_file_name, O_WRONLY);61if (fd < 0)62return -1;63if (lseek(fd, idx, SEEK_CUR) == -1)64goto err;65if (write(fd, &val, sizeof val) != sizeof val)66goto err;67close(fd);68return 0;69err:70close(fd);71return -1;72}7374unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)75{76unsigned long long val;77int ret;7879if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO))80return -1;8182ret = read_msr(cpu, MSR_NEHALEM_TURBO_RATIO_LIMIT, &val);83if (ret)84return ret;85return val;86}87#endif888990