1 // SPDX-License-Identifier: GPL-2.0
2 #if defined(__i386__) || defined(__x86_64__)
9 #include "helpers/helpers.h"
11 /* Intel specific MSRs */
12 #define MSR_IA32_PERF_STATUS 0x198
13 #define MSR_IA32_MISC_ENABLES 0x1a0
14 #define MSR_IA32_ENERGY_PERF_BIAS 0x1b0
15 #define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1ad
20 * Will return 0 on success and -1 on failure.
21 * Possible errno values could be:
22 * EFAULT -If the read/write did not fully complete
23 * EIO -If the CPU does not support MSRs
24 * ENXIO -If the CPU does not exist
27 int read_msr(int cpu, unsigned int idx, unsigned long long *val)
30 char msr_file_name[64];
32 sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
33 fd = open(msr_file_name, O_RDONLY);
36 if (lseek(fd, idx, SEEK_CUR) == -1)
38 if (read(fd, val, sizeof *val) != sizeof *val)
50 * Will return 0 on success and -1 on failure.
51 * Possible errno values could be:
52 * EFAULT -If the read/write did not fully complete
53 * EIO -If the CPU does not support MSRs
54 * ENXIO -If the CPU does not exist
56 int write_msr(int cpu, unsigned int idx, unsigned long long val)
59 char msr_file_name[64];
61 sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
62 fd = open(msr_file_name, O_WRONLY);
65 if (lseek(fd, idx, SEEK_CUR) == -1)
67 if (write(fd, &val, sizeof val) != sizeof val)
76 int msr_intel_get_perf_bias(unsigned int cpu)
78 unsigned long long val;
81 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
84 ret = read_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &val);
90 int msr_intel_set_perf_bias(unsigned int cpu, unsigned int val)
94 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
97 ret = write_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, val);
103 unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
105 unsigned long long val;
108 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO))
111 ret = read_msr(cpu, MSR_NEHALEM_TURBO_RATIO_LIMIT, &val);